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..93967e8790618ba524e13bce86dfaef465ab6e32 |
--- /dev/null |
+++ b/chrome/browser/extensions/system_info_event_router.cc |
@@ -0,0 +1,132 @@ |
+// 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/extensions/extension_system.h" |
+#include "chrome/browser/extensions/event_router.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(Profile* profile, |
+ const std::string& event_name) { |
+ // Start watching the |event_name| event if the first event listener |
+ // arrives. |
+ if (!HasEventListener(event_name)) { |
+ event_listener_map_[profile].insert(event_name); |
+ std::string prefix(kStorageEventPrefix); |
+ if (event_name.compare(0, prefix.size(), kStorageEventPrefix) == 0) { |
+ // TODO (hongbo): Start watching storage device information via calling |
+ // SystemMonitor::StartWatchingStorage. |
+ return; |
+ } |
+ } |
+} |
+ |
+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)) { |
+ 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; |
+ } |
+ } |
+} |
+ |
+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; |
+} |
+ |
+// 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)); |
Mihai Parparita -not on Chrome
2012/08/31 01:01:00
Add an early return here, so that the rest of the
Hongbo Min
2012/08/31 15:29:53
Done.
|
+ } else { |
+ // We are on the UI thread now. |
+ StorageChangeInfo info; |
+ info.id = id; |
+ info.available_capacity = static_cast<double>(available_capacity); |
+ |
+ base::ListValue args; |
+ args.Append(info.ToValue().release()); |
+ |
+ DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args); |
+ } |
+} |
+ |
+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, |
+ const 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()) { |
+ scoped_ptr<base::ListValue> scoped_args(args.DeepCopy()); |
+ ExtensionSystem::Get(profile)->event_router()->DispatchEventToRenderers( |
+ event_name, scoped_args.Pass(), NULL, GURL(), EventFilteringInfo()); |
+ } |
+ } |
+} |
+ |
+} // namespace extensions |