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 |