OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_WATCHER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_WATCHER_H_ |
| 7 |
| 8 #include <set> |
| 9 |
| 10 #include "base/lazy_instance.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/observer_list.h" |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 // The SystemInfoWatcher class is an abstract interface for watching system |
| 17 // information change, e.g. storage free space changes. It is used by |
| 18 // SystemInfoEventRouter to monitor device status for systemInfo.* API. |
| 19 class SystemInfoWatcher { |
| 20 public: |
| 21 // Observer for system information changes. |
| 22 class Observer { |
| 23 public: |
| 24 virtual ~Observer() {} |
| 25 |
| 26 // Called when the storage available capacity gets changed. |
| 27 // |id|: identifies the storage unit on which the change happened. |
| 28 // |available_capacity|: the available capacity on the storage unit. |
| 29 virtual void OnStorageDeviceAvailableCapacityChanged(const std::string& id, |
| 30 double available_capacity) {} |
| 31 |
| 32 // Called when a new storage device is attached to the system, e.g. |
| 33 // a USB disk is plugged in. |
| 34 // |id|: The unique identifier for the storage device attached already. |
| 35 // |type|: The string of storage type. See storage_info_provider.h comment. |
| 36 // |capacity|: The total capacity of the storage device can hold. |
| 37 // |available_capacity|: The available capacity left on the storage device. |
| 38 virtual void OnStorageDeviceAdded(const std::string& id, |
| 39 const std::string& type, |
| 40 double capacity, |
| 41 double available_capacity) {} |
| 42 |
| 43 // Called when a storage device is removed from the system, e.g. |
| 44 // a USB disk is unplugged. |
| 45 // |id|: the identifier of the storage device that has been removed. |
| 46 virtual void OnStorageDeviceRemoved(const std::string& id) {} |
| 47 }; |
| 48 |
| 49 // Return the single shared instance. |
| 50 static SystemInfoWatcher* Get(); |
| 51 |
| 52 // Initialize for testing |
| 53 static void InitializeForTesting(SystemInfoWatcher* watcher); |
| 54 |
| 55 virtual ~SystemInfoWatcher(); |
| 56 |
| 57 // Add and remove the observer for device information change event. |
| 58 void AddObserver(Observer* observer); |
| 59 void RemoveObserver(Observer* observer); |
| 60 |
| 61 // Start watching the |event_name| event. Return true if the watching |
| 62 // succeeds to start. Otherwise, false is returned. |
| 63 virtual bool StartWatching(const std::string& event_name); |
| 64 |
| 65 // Stop watching the |event_name| event. Return true if the watching |
| 66 // succeeds to stop. Otherwise, false is returned. |
| 67 virtual bool StopWatching(const std::string& event_name); |
| 68 |
| 69 // Return true if the |event_name| event is being watched. |
| 70 virtual bool IsWatching(const std::string& event_name); |
| 71 |
| 72 protected: |
| 73 SystemInfoWatcher(); |
| 74 |
| 75 // Platform specific implementation for start/stop watching. |
| 76 virtual bool PlatformStartWatching(const std::string& event_name) = 0; |
| 77 virtual bool PlatformStopWatching(const std::string& event_name) = 0; |
| 78 |
| 79 // Template function for creating the shared instance. The parameter I is |
| 80 // the type of SystemInfoWatcher implementation. |
| 81 template<class I> |
| 82 static SystemInfoWatcher* GetInstance() { |
| 83 if (!single_shared_watcher_.Get().get()) { |
| 84 I* impl = new I(); |
| 85 single_shared_watcher_.Get().reset(impl); |
| 86 } |
| 87 return single_shared_watcher_.Get().get(); |
| 88 } |
| 89 |
| 90 ObserverList<SystemInfoWatcher::Observer> observers_; |
| 91 std::set<std::string> watching_event_names_; |
| 92 |
| 93 private: |
| 94 // The single shared SystemInfoWatcher instance. It is constructed only when |
| 95 // needed, and kept until the program exits. |
| 96 static base::LazyInstance<scoped_ptr<SystemInfoWatcher> > |
| 97 single_shared_watcher_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(SystemInfoWatcher); |
| 100 }; |
| 101 |
| 102 } // namespace extensions |
| 103 #endif // CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_WATCHER_H_ |
OLD | NEW |