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

Unified Diff: chrome/browser/extensions/system_info_event_router.cc

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: use event router forwarder instead 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_event_router.cc
diff --git a/chrome/browser/extensions/system_info_event_router.cc b/chrome/browser/extensions/system_info_event_router.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8b8fbda6eae7e2e0328cf150587f36b5b0ceb12e
--- /dev/null
+++ b/chrome/browser/extensions/system_info_event_router.cc
@@ -0,0 +1,114 @@
+// 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.
+
+#include "chrome/browser/extensions/system_info_event_router.h"
+
+#include "base/bind.h"
+#include "base/memory/scoped_ptr.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/extensions/event_router_forwarder.h"
+#include "chrome/browser/extensions/event_names.h"
+#include "chrome/browser/extensions/api/system_info_storage/storage_info_provider.h"
+#include "chrome/common/extensions/api/experimental_system_info_storage.h"
+
+namespace extensions {
+
+using api::experimental_system_info_storage::StorageInfo;
+using api::experimental_system_info_storage::StorageUnitInfo;
+using api::experimental_system_info_storage::StorageChangeInfo;
+using content::BrowserThread;
+
+const char kSystemInfoEventPrefix[] = "experimental.systemInfo";
+const char kStorageEventPrefix[] = "experimental.systemInfo.storage";
+
+// static
+SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() {
+ return Singleton<SystemInfoEventRouter>::get();
+}
+
+SystemInfoEventRouter::SystemInfoEventRouter() {
+}
+
+SystemInfoEventRouter::~SystemInfoEventRouter() {
+}
+
+void SystemInfoEventRouter::AddEventListener(const std::string& event_name) {
+ // Start watching the |event_name| event if the first event listener arrives.
+ if (event_watching_map_.find(event_name) == event_watching_map_.end()) {
+ std::string prefix(kStorageEventPrefix);
+ if (event_name.compare(0, prefix.size(), kStorageEventPrefix) == 0) {
+ // TODO (hongbo): Start watching storage device information via calling
+ // SystemMonitor::StartWatchingStorage.
+ }
+ event_watching_map_[event_name] = 1;
+ return;
+ }
+
+ // Increase the watching count.
+ event_watching_map_[event_name]++;
+}
+
+void SystemInfoEventRouter::RemoveEventListener(
+ const std::string& event_name) {
+ if (event_watching_map_.find(event_name) == event_watching_map_.end()) {
+ NOTREACHED() << "The event is NOT watched yet: " << event_name;
+ return;
+ }
+
+ if (--event_watching_map_[event_name] > 0)
+ return;
+
+ // The watching count is down to 0, it means the last event listener is
+ // removed, we need to stop watching it.
+ std::string prefix(kStorageEventPrefix);
+ if (event_name.compare(
+ 0, prefix.size(), kStorageEventPrefix) == 0) {
+ // TODO(hongbo): Stop watching storage device information via calling
+ // SystemMonitor::StopWatchingStorage.
+ return;
+ }
+}
+
+// static
+bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) {
+ std::string prefix(kSystemInfoEventPrefix);
+ return event_name.compare(0, prefix.size(), prefix) == 0;
+}
+
+void SystemInfoEventRouter::OnStorageAvailableCapacityChanged(
+ const std::string& id, int64 available_capacity) {
+ if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(&SystemInfoEventRouter::OnStorageAvailableCapacityChanged,
+ base::Unretained(this), id, available_capacity));
+ return;
+ }
+
+ // We are on the UI thread now.
+ StorageChangeInfo info;
+ info.id = id;
+ info.available_capacity = static_cast<double>(available_capacity);
+
+ scoped_ptr<base::ListValue> args(new base::ListValue());
+ args->Append(info.ToValue().release());
+
+ DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass());
+}
+
+void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id,
+ const string16& name, const FilePath::StringType& location) {
+ // TODO(hongbo): Handle storage device arrival/removal event.
+}
+
+void SystemInfoEventRouter::OnRemovableStorageDetached(const std::string& id) {
+ // TODO(hongbo): Same as above.
+}
+
+void SystemInfoEventRouter::DispatchEvent(const std::string& event_name,
+ scoped_ptr<base::ListValue> args) {
+ g_browser_process->extension_event_router_forwarder()->
+ BroadcastEventToRenderers(event_name, args.Pass(), GURL());
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698