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

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: 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_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..696277b5943f813dfdf8c8f18b7b7f1202c47ce4
--- /dev/null
+++ b/chrome/browser/extensions/system_info_event_router.cc
@@ -0,0 +1,124 @@
+// 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/json/json_writer.h"
+#include "base/values.h"
+#include "chrome/browser/extensions/extension_system.h"
+#include "chrome/browser/extensions/event_router.h"
+#include "chrome/browser/extensions/event_names.h"
+#include "chrome/browser/extensions/system_info_watcher.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::StorageUnitChangeInfo;
+
+//
+// SystemInfoEventRouter
+//
+
+// static
+SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() {
+ return Singleton<SystemInfoEventRouter>::get();
+}
+
+SystemInfoEventRouter::SystemInfoEventRouter()
+ : system_info_watcher_(SystemInfoWatcher::Get()) {
+ system_info_watcher_->AddObserver(this);
+}
+
+SystemInfoEventRouter::~SystemInfoEventRouter() {
+ system_info_watcher_->RemoveObserver(this);
+}
+
+void SystemInfoEventRouter::AddEventListener(Profile* profile,
+ const std::string& event_name) {
+ // Start watching the |event_name| event if the first event listener
+ // arrives.
+ if (!HasEventListener(event_name) &&
+ system_info_watcher_->StartWatching(event_name)) {
+ event_listener_map_[profile].insert(event_name);
+ }
+}
+
+void SystemInfoEventRouter::RemoveEventListener(Profile* profile,
+ const std::string& event_name) {
+ event_listener_map_[profile].erase(event_name);
+
+ // Stop watching the |event_name| event if the last event listener has been
+ // removed.
+ if (!HasEventListener(event_name)) {
+ system_info_watcher_->StopWatching(event_name);
+ }
+}
+
+bool SystemInfoEventRouter::HasEventListener(const std::string& event_name) {
+ for (EventListenerMap::iterator iter = event_listener_map_.begin();
+ iter != event_listener_map_.end();
+ ++iter) {
+ if (iter->second.count(event_name) > 0)
+ return true;
+ }
+ return false;
+}
+
+void SystemInfoEventRouter::OnStorageDeviceAvailableCapacityChanged(
+ const std::string& id, double available_capacity) {
+ StorageUnitChangeInfo info;
+ info.id = id;
+ info.available_capacity = available_capacity;
+
+ scoped_ptr<base::ListValue> args(new base::ListValue());
+ args->Append(info.ToValue().release());
+
+ DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass());
+}
+
+void SystemInfoEventRouter::OnStorageDeviceAdded(const std::string& id,
+ const std::string& type,
+ double capacity,
+ double available_capacity) {
+ StorageUnitInfo info;
+ info.id = id;
+ info.type = type;
+ info.capacity = capacity;
+ info.available_capacity = available_capacity;
+
+ scoped_ptr<base::ListValue> args(new base::ListValue());
+ args->Append(info.ToValue().release());
+
+ DispatchEvent(event_names::kOnStorageAdded, args.Pass());
+}
+
+void SystemInfoEventRouter::OnStorageDeviceRemoved(const std::string& id) {
+ scoped_ptr<base::ListValue> args(new base::ListValue());
+ args->Append(base::Value::CreateStringValue(id));
+
+ DispatchEvent(event_names::kOnStorageRemoved, args.Pass());
+}
+
+void SystemInfoEventRouter::DispatchEvent(const std::string& event_name,
+ scoped_ptr<base::ListValue> args) {
+ for (EventListenerMap::iterator iter = event_listener_map_.begin();
+ iter != event_listener_map_.end();
+ ++iter) {
+ // Ignore the Profile without listening the |event_name| event.
+ if (iter->second.count(event_name) == 0)
+ continue;
+
+ Profile* profile = iter->first;
+ if (profile->GetExtensionEventRouter()) {
+ profile->GetExtensionEventRouter()->DispatchEventToRenderers(
+ event_name, args.Pass(), NULL, GURL(), EventFilteringInfo());
+ }
+ }
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698