OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/system_info_event_router.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/extensions/event_router.h" |
| 12 #include "chrome/browser/extensions/event_names.h" |
| 13 #include "chrome/browser/extensions/system_info_watcher.h" |
| 14 #include "chrome/common/extensions/api/experimental_system_info_storage.h" |
| 15 |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 using api::experimental_system_info_storage::StorageInfo; |
| 20 using api::experimental_system_info_storage::StorageUnitInfo; |
| 21 using api::experimental_system_info_storage::StorageUnitChangeInfo; |
| 22 |
| 23 // |
| 24 // SystemInfoEventRouter |
| 25 // |
| 26 |
| 27 // static |
| 28 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| 29 return Singleton<SystemInfoEventRouter>::get(); |
| 30 } |
| 31 |
| 32 SystemInfoEventRouter::SystemInfoEventRouter() |
| 33 : system_info_watcher_(SystemInfoWatcher::Get()) { |
| 34 system_info_watcher_->AddObserver(this); |
| 35 } |
| 36 |
| 37 SystemInfoEventRouter::~SystemInfoEventRouter() { |
| 38 system_info_watcher_->RemoveObserver(this); |
| 39 } |
| 40 |
| 41 void SystemInfoEventRouter::AddEventListener(Profile* profile, |
| 42 const std::string& event_name) { |
| 43 // Start watching the |event_name| event if the first event listener |
| 44 // arrives. |
| 45 if (!HasEventListener(event_name) && |
| 46 system_info_watcher_->StartWatching(event_name)) { |
| 47 event_listener_map_[profile].insert(event_name); |
| 48 } |
| 49 } |
| 50 |
| 51 void SystemInfoEventRouter::RemoveEventListener(Profile* profile, |
| 52 const std::string& event_name) { |
| 53 event_listener_map_[profile].erase(event_name); |
| 54 |
| 55 // Stop watching the |event_name| event if the last event listener has been |
| 56 // removed. |
| 57 if (!HasEventListener(event_name)) { |
| 58 system_info_watcher_->StopWatching(event_name); |
| 59 } |
| 60 } |
| 61 |
| 62 bool SystemInfoEventRouter::HasEventListener(const std::string& event_name) { |
| 63 for (EventListenerMap::iterator iter = event_listener_map_.begin(); |
| 64 iter != event_listener_map_.end(); |
| 65 ++iter) { |
| 66 if (iter->second.count(event_name) > 0) |
| 67 return true; |
| 68 } |
| 69 return false; |
| 70 } |
| 71 |
| 72 void SystemInfoEventRouter::OnStorageDeviceAvailableCapacityChanged( |
| 73 const std::string& id, double available_capacity) { |
| 74 StorageUnitChangeInfo info; |
| 75 info.id = id; |
| 76 info.available_capacity = available_capacity; |
| 77 |
| 78 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 79 args->Append(info.ToValue().release()); |
| 80 |
| 81 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); |
| 82 } |
| 83 |
| 84 void SystemInfoEventRouter::OnStorageDeviceAdded(const std::string& id, |
| 85 const std::string& type, |
| 86 double capacity, |
| 87 double available_capacity) { |
| 88 StorageUnitInfo info; |
| 89 info.id = id; |
| 90 info.type = type; |
| 91 info.capacity = capacity; |
| 92 info.available_capacity = available_capacity; |
| 93 |
| 94 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 95 args->Append(info.ToValue().release()); |
| 96 |
| 97 DispatchEvent(event_names::kOnStorageAdded, args.Pass()); |
| 98 } |
| 99 |
| 100 void SystemInfoEventRouter::OnStorageDeviceRemoved(const std::string& id) { |
| 101 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 102 args->Append(base::Value::CreateStringValue(id)); |
| 103 |
| 104 DispatchEvent(event_names::kOnStorageRemoved, args.Pass()); |
| 105 } |
| 106 |
| 107 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, |
| 108 scoped_ptr<base::ListValue> args) { |
| 109 for (EventListenerMap::iterator iter = event_listener_map_.begin(); |
| 110 iter != event_listener_map_.end(); |
| 111 ++iter) { |
| 112 // Ignore the Profile without listening the |event_name| event. |
| 113 if (iter->second.count(event_name) == 0) |
| 114 continue; |
| 115 |
| 116 Profile* profile = iter->first; |
| 117 if (profile->GetExtensionEventRouter()) { |
| 118 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 119 event_name, args.Pass(), NULL, GURL(), EventFilteringInfo()); |
| 120 } |
| 121 } |
| 122 } |
| 123 |
| 124 } // namespace extensions |
OLD | NEW |