| 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..4a890760bc70a7c9c726402f685a09d574d28659
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/system_info_event_router.cc
|
| @@ -0,0 +1,135 @@
|
| +// 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/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);
|
| +
|
| + if (event_name.compare(
|
| + 0, strlen(kStorageEventPrefix), kStorageEventPrefix) == 0) {
|
| + StorageInfoProvider::Get()->StartWatching(this);
|
| + 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)) {
|
| + if (event_name.compare(
|
| + 0, strlen(kStorageEventPrefix), kStorageEventPrefix) == 0) {
|
| + StorageInfoProvider::Get()->StopWatching();
|
| + 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) {
|
| + static std::string prefix(kSystemInfoEventPrefix);
|
| + return event_name.compare(0, prefix.size(), prefix) == 0;
|
| +}
|
| +
|
| +void SystemInfoEventRouter::OnStorageDeviceAvailableCapacityChanged(
|
| + const std::string& id, double available_capacity) {
|
| + if (!BrowserThread::CurrentlyOn(BrowserThread::UI))
|
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
| + base::Bind(
|
| + &SystemInfoEventRouter::DispatchAvailableCapacityChangedEvent,
|
| + base::Unretained(this), id, available_capacity));
|
| + else
|
| + DispatchAvailableCapacityChangedEvent(id, available_capacity);
|
| +}
|
| +
|
| +void SystemInfoEventRouter::OnStorageDeviceAdded(const std::string& id,
|
| + const std::string& type,
|
| + double capacity,
|
| + double available_capacity) {
|
| + // TODO(hongbo): Handle storage device arrival/removal event.
|
| +}
|
| +
|
| +void SystemInfoEventRouter::OnStorageDeviceRemoved(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());
|
| + profile->GetExtensionEventRouter()->DispatchEventToRenderers(
|
| + event_name, scoped_args.Pass(), NULL, GURL(), EventFilteringInfo());
|
| + }
|
| + }
|
| +}
|
| +
|
| +void SystemInfoEventRouter::DispatchAvailableCapacityChangedEvent(
|
| + const std::string& id, double available_capacity) {
|
| + StorageChangeInfo info;
|
| + info.id = id;
|
| + info.available_capacity = available_capacity;
|
| +
|
| + base::ListValue args;
|
| + args.Append(info.ToValue().release());
|
| +
|
| + DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args);
|
| +}
|
| +
|
| +} // namespace extensions
|
|
|