Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5265)

Unified Diff: chrome/browser/extensions/system_info_watcher.h

Issue 10836341: Add the basic code skeleton for system info event router (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add mock implementation for onAdded and onRemoved events Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/system_info_watcher.h
diff --git a/chrome/browser/extensions/system_info_watcher.h b/chrome/browser/extensions/system_info_watcher.h
new file mode 100644
index 0000000000000000000000000000000000000000..b53acdc3f9199b9935a66df8729ee39661151129
--- /dev/null
+++ b/chrome/browser/extensions/system_info_watcher.h
@@ -0,0 +1,103 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_WATCHER_H_
+#define CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_WATCHER_H_
+
+#include <set>
+
+#include "base/lazy_instance.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/observer_list.h"
+
+namespace extensions {
+
+// The SystemInfoWatcher class is an abstract interface for watching system
+// information change, e.g. storage free space changes. It is used by
+// SystemInfoEventRouter to monitor device status for systemInfo.* API.
+class SystemInfoWatcher {
+ public:
+ // Observer for system information changes.
+ class Observer {
+ public:
+ virtual ~Observer() {}
+
+ // Called when the storage available capacity gets changed.
+ // |id|: identifies the storage unit on which the change happened.
+ // |available_capacity|: the available capacity on the storage unit.
+ virtual void OnStorageDeviceAvailableCapacityChanged(const std::string& id,
+ double available_capacity) {}
+
+ // Called when a new storage device is attached to the system, e.g.
+ // a USB disk is plugged in.
+ // |id|: The unique identifier for the storage device attached already.
+ // |type|: The string of storage type. See storage_info_provider.h comment.
+ // |capacity|: The total capacity of the storage device can hold.
+ // |available_capacity|: The available capacity left on the storage device.
+ virtual void OnStorageDeviceAdded(const std::string& id,
+ const std::string& type,
+ double capacity,
+ double available_capacity) {}
+
+ // Called when a storage device is removed from the system, e.g.
+ // a USB disk is unplugged.
+ // |id|: the identifier of the storage device that has been removed.
+ virtual void OnStorageDeviceRemoved(const std::string& id) {}
+ };
+
+ // Return the single shared instance.
+ static SystemInfoWatcher* Get();
+
+ // Initialize for testing
+ static void InitializeForTesting(SystemInfoWatcher* watcher);
+
+ virtual ~SystemInfoWatcher();
+
+ // Add and remove the observer for device information change event.
+ void AddObserver(Observer* observer);
+ void RemoveObserver(Observer* observer);
+
+ // Start watching the |event_name| event. Return true if the watching
+ // succeeds to start. Otherwise, false is returned.
+ virtual bool StartWatching(const std::string& event_name);
+
+ // Stop watching the |event_name| event. Return true if the watching
+ // succeeds to stop. Otherwise, false is returned.
+ virtual bool StopWatching(const std::string& event_name);
+
+ // Return true if the |event_name| event is being watched.
+ virtual bool IsWatching(const std::string& event_name);
+
+ protected:
+ SystemInfoWatcher();
+
+ // Platform specific implementation for start/stop watching.
+ virtual bool PlatformStartWatching(const std::string& event_name) = 0;
+ virtual bool PlatformStopWatching(const std::string& event_name) = 0;
+
+ // Template function for creating the shared instance. The parameter I is
+ // the type of SystemInfoWatcher implementation.
+ template<class I>
+ static SystemInfoWatcher* GetInstance() {
+ if (!single_shared_watcher_.Get().get()) {
+ I* impl = new I();
+ single_shared_watcher_.Get().reset(impl);
+ }
+ return single_shared_watcher_.Get().get();
+ }
+
+ ObserverList<SystemInfoWatcher::Observer> observers_;
+ std::set<std::string> watching_event_names_;
+
+ private:
+ // The single shared SystemInfoWatcher instance. It is constructed only when
+ // needed, and kept until the program exits.
+ static base::LazyInstance<scoped_ptr<SystemInfoWatcher> >
+ single_shared_watcher_;
+
+ DISALLOW_COPY_AND_ASSIGN(SystemInfoWatcher);
+};
+
+} // namespace extensions
+#endif // CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_WATCHER_H_

Powered by Google App Engine
This is Rietveld 408576698