Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 4 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 5 | #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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 7 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 8 | #include <set> |
| 9 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 10 | #include "base/lazy_instance.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
| 12 | #include "base/observer_list_threadsafe.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 13 | #include "base/timer/timer.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 14 | #include "chrome/browser/extensions/api/system_info/system_info_provider.h" |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 15 | #include "chrome/browser/extensions/api/system_storage/storage_free_space_observer.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 16 | #include "chrome/browser/storage_monitor/removable_storage_observer.h" |
| 17 | #include "chrome/browser/storage_monitor/storage_info.h" |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 18 | #include "chrome/common/extensions/api/system_storage.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 19 | #include "content/public/browser/notification_observer.h" |
| 20 | #include "content/public/browser/notification_registrar.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 21 | |
| 22 | namespace extensions { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 23 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 24 | namespace systeminfo { |
| 25 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 26 | // Build StorageUnitInfo struct from chrome::StorageInfo instance. The |unit| |
| 27 | // parameter is the output value. |
| 28 | void BuildStorageUnitInfo(const chrome::StorageInfo& info, |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 29 | api::system_storage::StorageUnitInfo* unit); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 30 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | } // namespace systeminfo |
| 32 | |
| 33 | typedef std::vector<linked_ptr< |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 34 | api::system_storage::StorageUnitInfo> > |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 35 | StorageUnitInfoList; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 36 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 37 | class StorageInfoProvider : public SystemInfoProvider { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 38 | public: |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 39 | // Get the single shared instance of StorageInfoProvider. |
| 40 | static StorageInfoProvider* Get(); |
| 41 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 42 | // Add and remove observer, both can be called from any thread. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 43 | void AddObserver(StorageFreeSpaceObserver* obs); |
| 44 | void RemoveObserver(StorageFreeSpaceObserver* obs); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 45 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 46 | // 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 49 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 50 | // Start and stop watching all available storages. |
| 51 | void StartWatchingAllStorages(); |
| 52 | void StopWatchingAllStorages(); |
| 53 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 54 | // 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 Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 63 | const StorageUnitInfoList& storage_unit_info_list() const; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 64 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 65 | static void InitializeForTesting(scoped_refptr<StorageInfoProvider> provider); |
| 66 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 67 | protected: |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 68 | StorageInfoProvider(); |
| 69 | explicit StorageInfoProvider(size_t watching_interval); |
| 70 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 71 | virtual ~StorageInfoProvider(); |
| 72 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 73 | // Put all available storages' information into |info_|. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 74 | void GetAllStoragesIntoInfoList(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 75 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 76 | // 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 85 | private: |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 86 | typedef std::map<std::string, double> StorageTransientIdToSizeMap; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 87 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 88 | // 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 Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 92 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 93 | // Query the new attached removable storage info on the blocking pool. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 94 | void QueryAttachedStorageInfoOnBlockingPool(const std::string& transient_id); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 95 | |
| 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 Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 101 | // the |storage_transient_id_to_size_map_|. It is called on blocking pool. |
| 102 | void CheckWatchedStorageOnBlockingPool(const std::string& transient_id); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 103 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 104 | // 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 108 | |
| 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 Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 116 | StorageTransientIdToSizeMap storage_transient_id_to_size_map_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 117 | |
| 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 Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 123 | scoped_refptr<ObserverListThreadSafe<StorageFreeSpaceObserver> > observers_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 124 | |
| 125 | // The time interval for watching the free space change, in milliseconds. |
| 126 | // Only changed for testing purposes. |
| 127 | size_t watching_interval_; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 128 | |
| 129 | static base::LazyInstance<scoped_refptr<StorageInfoProvider> > provider_; |
| 130 | |
| 131 | DISALLOW_COPY_AND_ASSIGN(StorageInfoProvider); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace extensions |
| 135 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 136 | #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_STORAGE_STORAGE_INFO_PROVIDER_H_ |