blob: 2dc96f5714ee6cd76e4536a21d28cf6c38fbfe34 [file] [log] [blame]
Ben Murdoch558790d2013-07-30 15:19:42 +01001// Copyright 2013 The Chromium Authors. All rights reserved.
Torne (Richard Coles)58218062012-11-14 11:43:16 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +01004
Ben Murdoch558790d2013-07-30 15:19:42 +01005#ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_STORAGE_STORAGE_INFO_PROVIDER_H_
6#define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_STORAGE_STORAGE_INFO_PROVIDER_H_
Torne (Richard Coles)58218062012-11-14 11:43:16 +00007
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00008#include <set>
9
Ben Murdochbb1529c2013-08-08 10:24:53 +010010#include "base/lazy_instance.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000011#include "base/memory/ref_counted.h"
12#include "base/observer_list_threadsafe.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010013#include "base/timer/timer.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000014#include "chrome/browser/extensions/api/system_info/system_info_provider.h"
Ben Murdoch558790d2013-07-30 15:19:42 +010015#include "chrome/browser/extensions/api/system_storage/storage_free_space_observer.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000016#include "chrome/browser/storage_monitor/removable_storage_observer.h"
17#include "chrome/browser/storage_monitor/storage_info.h"
Ben Murdoch558790d2013-07-30 15:19:42 +010018#include "chrome/common/extensions/api/system_storage.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000019#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000021
22namespace extensions {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010023
Torne (Richard Coles)58218062012-11-14 11:43:16 +000024namespace systeminfo {
25
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010026// Build StorageUnitInfo struct from chrome::StorageInfo instance. The |unit|
27// parameter is the output value.
28void BuildStorageUnitInfo(const chrome::StorageInfo& info,
Ben Murdoch558790d2013-07-30 15:19:42 +010029 api::system_storage::StorageUnitInfo* unit);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010030
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031} // namespace systeminfo
32
33typedef std::vector<linked_ptr<
Ben Murdoch558790d2013-07-30 15:19:42 +010034 api::system_storage::StorageUnitInfo> >
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010035 StorageUnitInfoList;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000036
Ben Murdochbb1529c2013-08-08 10:24:53 +010037class StorageInfoProvider : public SystemInfoProvider {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000038 public:
Torne (Richard Coles)58218062012-11-14 11:43:16 +000039 // Get the single shared instance of StorageInfoProvider.
40 static StorageInfoProvider* Get();
41
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000042 // Add and remove observer, both can be called from any thread.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010043 void AddObserver(StorageFreeSpaceObserver* obs);
44 void RemoveObserver(StorageFreeSpaceObserver* obs);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000045
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010046 // Start and stop watching the given storage |transient_id|.
47 void StartWatching(const std::string& transient_id);
48 void StopWatching(const std::string& transient_id);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000049
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010050 // Start and stop watching all available storages.
51 void StartWatchingAllStorages();
52 void StopWatchingAllStorages();
53
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010054 // SystemInfoProvider implementations
55 virtual void PrepareQueryOnUIThread() OVERRIDE;
56 virtual void InitializeProvider(const base::Closure& do_query_info_callback)
57 OVERRIDE;
58
59 // Get the amount of storage free space from |transient_id|, or -1 on failure.
60 virtual int64 GetStorageFreeSpaceFromTransientId(
61 const std::string& transient_id);
62
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010063 const StorageUnitInfoList& storage_unit_info_list() const;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000064
Ben Murdochbb1529c2013-08-08 10:24:53 +010065 static void InitializeForTesting(scoped_refptr<StorageInfoProvider> provider);
66
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000067 protected:
Ben Murdochbb1529c2013-08-08 10:24:53 +010068 StorageInfoProvider();
69 explicit StorageInfoProvider(size_t watching_interval);
70
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000071 virtual ~StorageInfoProvider();
72
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010073 // Put all available storages' information into |info_|.
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010074 void GetAllStoragesIntoInfoList();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010075
Ben Murdochbb1529c2013-08-08 10:24:53 +010076 // The last information filled up by QueryInfo and is accessed on multiple
77 // threads, but the whole class is being guarded by SystemInfoProvider base
78 // class.
79 //
80 // |info_| is accessed on the UI thread while |is_waiting_for_completion_| is
81 // false and on the sequenced worker pool while |is_waiting_for_completion_|
82 // is true.
83 StorageUnitInfoList info_;
84
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000085 private:
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010086 typedef std::map<std::string, double> StorageTransientIdToSizeMap;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000087
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010088 // SystemInfoProvider implementations.
89 // Override to query the available capacity of all known storage devices on
90 // the blocking pool, including fixed and removable devices.
91 virtual bool QueryInfo() OVERRIDE;
Ben Murdoch558790d2013-07-30 15:19:42 +010092
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000093 // Query the new attached removable storage info on the blocking pool.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010094 void QueryAttachedStorageInfoOnBlockingPool(const std::string& transient_id);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000095
96 // Posts a task to check for free space changes on the blocking pool.
97 // Should be called on the UI thread.
98 void CheckWatchedStorages();
99
100 // Check if the free space changes for the watched storages by iterating over
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100101 // the |storage_transient_id_to_size_map_|. It is called on blocking pool.
102 void CheckWatchedStorageOnBlockingPool(const std::string& transient_id);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000103
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100104 // Add the storage identified by |transient_id| into watching list.
105 void AddWatchedStorageOnBlockingPool(const std::string& transient_id);
106 // Remove the storage identified by |transient_id| from watching list.
107 void RemoveWatchedStorageOnBlockingPool(const std::string& transient_id);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000108
109 void StartWatchingTimerOnUIThread();
110 // Force to stop the watching timer or there is no any one storage to be
111 // watched. It is called on UI thread.
112 void StopWatchingTimerOnUIThread();
113
114 // Mapping of the storage being watched and the recent free space value. It
115 // is maintained on the blocking pool.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100116 StorageTransientIdToSizeMap storage_transient_id_to_size_map_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000117
118 // The timer used for watching the storage free space changes periodically.
119 base::RepeatingTimer<StorageInfoProvider> watching_timer_;
120
121 // The thread-safe observer list that observe the changes happening on the
122 // storages.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100123 scoped_refptr<ObserverListThreadSafe<StorageFreeSpaceObserver> > observers_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000124
125 // The time interval for watching the free space change, in milliseconds.
126 // Only changed for testing purposes.
127 size_t watching_interval_;
Ben Murdochbb1529c2013-08-08 10:24:53 +0100128
129 static base::LazyInstance<scoped_refptr<StorageInfoProvider> > provider_;
130
131 DISALLOW_COPY_AND_ASSIGN(StorageInfoProvider);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000132};
133
134} // namespace extensions
135
Ben Murdoch558790d2013-07-30 15:19:42 +0100136#endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_STORAGE_STORAGE_INFO_PROVIDER_H_