| 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/common/extensions/api/experimental_system_info_storage.h" |
| 14 |
| 15 |
| 16 namespace extensions { |
| 17 |
| 18 using api::experimental_system_info_storage::StorageInfo; |
| 19 using api::experimental_system_info_storage::StorageUnitInfo; |
| 20 using api::experimental_system_info_storage::StorageChangeInfo; |
| 21 using content::BrowserThread; |
| 22 |
| 23 const char kSystemInfoEventPrefix[] = "experimental.systemInfo"; |
| 24 const char kStorageEventPrefix[] = "experimental.systemInfo.storage"; |
| 25 |
| 26 // static |
| 27 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| 28 return Singleton<SystemInfoEventRouter>::get(); |
| 29 } |
| 30 |
| 31 SystemInfoEventRouter::SystemInfoEventRouter() { |
| 32 } |
| 33 |
| 34 SystemInfoEventRouter::~SystemInfoEventRouter() { |
| 35 } |
| 36 |
| 37 void SystemInfoEventRouter::AddEventListener(Profile* profile, |
| 38 const std::string& event_name) { |
| 39 // Start watching the |event_name| event if the first event listener |
| 40 // arrives. |
| 41 if (!HasEventListener(event_name)) { |
| 42 event_listener_map_[profile].insert(event_name); |
| 43 |
| 44 if (event_name.compare( |
| 45 0, strlen(kStorageEventPrefix), kStorageEventPrefix) == 0) { |
| 46 StorageInfoProvider::Get()->StartWatching(this); |
| 47 return; |
| 48 } |
| 49 } |
| 50 } |
| 51 |
| 52 void SystemInfoEventRouter::RemoveEventListener(Profile* profile, |
| 53 const std::string& event_name) { |
| 54 event_listener_map_[profile].erase(event_name); |
| 55 |
| 56 // Stop watching the |event_name| event if the last event listener |
| 57 // has been removed. |
| 58 if (!HasEventListener(event_name)) { |
| 59 if (event_name.compare( |
| 60 0, strlen(kStorageEventPrefix), kStorageEventPrefix) == 0) { |
| 61 StorageInfoProvider::Get()->StopWatching(); |
| 62 return; |
| 63 } |
| 64 } |
| 65 } |
| 66 |
| 67 bool SystemInfoEventRouter::HasEventListener(const std::string& event_name) { |
| 68 for (EventListenerMap::iterator iter = event_listener_map_.begin(); |
| 69 iter != event_listener_map_.end(); |
| 70 ++iter) { |
| 71 if (iter->second.count(event_name) > 0) |
| 72 return true; |
| 73 } |
| 74 return false; |
| 75 } |
| 76 |
| 77 // static |
| 78 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
| 79 static std::string prefix(kSystemInfoEventPrefix); |
| 80 return event_name.compare(0, prefix.size(), prefix) == 0; |
| 81 } |
| 82 |
| 83 void SystemInfoEventRouter::OnStorageDeviceAvailableCapacityChanged( |
| 84 const std::string& id, double available_capacity) { |
| 85 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
| 86 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 87 base::Bind( |
| 88 &SystemInfoEventRouter::DispatchAvailableCapacityChangedEvent, |
| 89 base::Unretained(this), id, available_capacity)); |
| 90 else |
| 91 DispatchAvailableCapacityChangedEvent(id, available_capacity); |
| 92 } |
| 93 |
| 94 void SystemInfoEventRouter::OnStorageDeviceAdded(const std::string& id, |
| 95 const std::string& type, |
| 96 double capacity, |
| 97 double available_capacity) { |
| 98 // TODO(hongbo): Handle storage device arrival/removal event. |
| 99 } |
| 100 |
| 101 void SystemInfoEventRouter::OnStorageDeviceRemoved(const std::string& id) { |
| 102 // TODO(hongbo): Same as above. |
| 103 } |
| 104 |
| 105 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, |
| 106 const base::ListValue& args) { |
| 107 for (EventListenerMap::iterator iter = event_listener_map_.begin(); |
| 108 iter != event_listener_map_.end(); |
| 109 ++iter) { |
| 110 // Ignore the Profile without listening the |event_name| event. |
| 111 if (iter->second.count(event_name) == 0) |
| 112 continue; |
| 113 |
| 114 Profile* profile = iter->first; |
| 115 if (profile->GetExtensionEventRouter()) { |
| 116 scoped_ptr<base::ListValue> scoped_args(args.DeepCopy()); |
| 117 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 118 event_name, scoped_args.Pass(), NULL, GURL(), EventFilteringInfo()); |
| 119 } |
| 120 } |
| 121 } |
| 122 |
| 123 void SystemInfoEventRouter::DispatchAvailableCapacityChangedEvent( |
| 124 const std::string& id, double available_capacity) { |
| 125 StorageChangeInfo info; |
| 126 info.id = id; |
| 127 info.available_capacity = available_capacity; |
| 128 |
| 129 base::ListValue args; |
| 130 args.Append(info.ToValue().release()); |
| 131 |
| 132 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args); |
| 133 } |
| 134 |
| 135 } // namespace extensions |
| OLD | NEW |