Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/system_info/system_info_api.h" | 5 #include "chrome/browser/extensions/api/system_info/system_info_api.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 namespace extensions { | 30 namespace extensions { |
| 31 | 31 |
| 32 using api::system_storage::StorageUnitInfo; | 32 using api::system_storage::StorageUnitInfo; |
| 33 using content::BrowserThread; | 33 using content::BrowserThread; |
| 34 | 34 |
| 35 namespace system_display = api::system_display; | 35 namespace system_display = api::system_display; |
| 36 namespace system_storage = api::system_storage; | 36 namespace system_storage = api::system_storage; |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 #if defined(USE_ASH) | |
| 40 bool IsDisplayChangedEvent(const std::string& event_name) { | 41 bool IsDisplayChangedEvent(const std::string& event_name) { |
| 41 return event_name == system_display::OnDisplayChanged::kEventName; | 42 return event_name == system_display::OnDisplayChanged::kEventName; |
| 42 } | 43 } |
| 44 #endif | |
| 45 | |
| 46 bool IsSystemStorageEvent(const std::string& event_name) { | |
| 47 return (event_name == system_storage::OnAttached::kEventName || | |
| 48 event_name == system_storage::OnDetached::kEventName); | |
| 49 } | |
| 43 | 50 |
| 44 // Event router for systemInfo API. It is a singleton instance shared by | 51 // Event router for systemInfo API. It is a singleton instance shared by |
| 45 // multiple profiles. | 52 // multiple profiles. |
| 46 class SystemInfoEventRouter : public gfx::DisplayObserver, | 53 class SystemInfoEventRouter : public gfx::DisplayObserver, |
| 47 public RemovableStorageObserver { | 54 public RemovableStorageObserver { |
| 48 public: | 55 public: |
| 49 static SystemInfoEventRouter* GetInstance(); | 56 static SystemInfoEventRouter* GetInstance(); |
| 50 | 57 |
| 51 SystemInfoEventRouter(); | 58 SystemInfoEventRouter(); |
| 52 virtual ~SystemInfoEventRouter(); | 59 virtual ~SystemInfoEventRouter(); |
| 53 | 60 |
| 54 // Add/remove event listener for the |event_name| event. | 61 // Add/remove event listener for the |event_name| event. |
| 55 void AddEventListener(const std::string& event_name); | 62 void AddEventListener(const std::string& event_name); |
| 56 void RemoveEventListener(const std::string& event_name); | 63 void RemoveEventListener(const std::string& event_name); |
| 57 | 64 |
| 58 private: | 65 private: |
| 59 // gfx::DisplayObserver: | 66 // gfx::DisplayObserver: |
| 60 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; | 67 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; |
| 61 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; | 68 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; |
| 62 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; | 69 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; |
| 63 | 70 |
| 64 // RemovableStorageObserver implementation. | 71 // RemovableStorageObserver implementation. |
| 65 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE; | 72 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE; |
| 66 virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE; | 73 virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE; |
| 67 | 74 |
| 68 // Called from any thread to dispatch the systemInfo event to all extension | 75 // Called from any thread to dispatch the systemInfo event to all extension |
| 69 // processes cross multiple profiles. | 76 // processes cross multiple profiles. |
| 70 void DispatchEvent(const std::string& event_name, | 77 void DispatchEvent(const std::string& event_name, |
| 71 scoped_ptr<base::ListValue> args); | 78 scoped_ptr<base::ListValue> args); |
|
Haojian Wu
2014/01/17 00:54:43
nits: code indent
Lei Zhang
2014/01/17 00:57:29
*blink* *blink* look ok to me. It's lined up with
| |
| 72 | 79 |
| 73 // Called to dispatch the systemInfo.display.onDisplayChanged event. | 80 // Called to dispatch the systemInfo.display.onDisplayChanged event. |
| 74 void OnDisplayChanged(); | 81 void OnDisplayChanged(); |
| 75 | 82 |
| 76 // Used to record the event names being watched. | 83 // Used to record the event names being watched. |
| 77 std::multiset<std::string> watching_event_set_; | 84 std::multiset<std::string> watching_event_set_; |
| 78 | 85 |
| 86 bool has_storage_monitor_observer_; | |
| 87 | |
| 79 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); | 88 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); |
| 80 }; | 89 }; |
| 81 | 90 |
| 82 static base::LazyInstance<SystemInfoEventRouter>::Leaky | 91 static base::LazyInstance<SystemInfoEventRouter>::Leaky |
| 83 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; | 92 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; |
| 84 | 93 |
| 85 // static | 94 // static |
| 86 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | 95 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| 87 return g_system_info_event_router.Pointer(); | 96 return g_system_info_event_router.Pointer(); |
| 88 } | 97 } |
| 89 | 98 |
| 90 SystemInfoEventRouter::SystemInfoEventRouter() { | 99 SystemInfoEventRouter::SystemInfoEventRouter() |
| 91 StorageMonitor::GetInstance()->AddObserver(this); | 100 : has_storage_monitor_observer_(false) { |
| 92 StorageMonitor::GetInstance()->EnsureInitialized(base::Closure()); | |
| 93 } | 101 } |
| 94 | 102 |
| 95 SystemInfoEventRouter::~SystemInfoEventRouter() { | 103 SystemInfoEventRouter::~SystemInfoEventRouter() { |
| 96 if (StorageMonitor* storage_monitor = StorageMonitor::GetInstance()) | 104 if (has_storage_monitor_observer_) { |
| 97 storage_monitor->RemoveObserver(this); | 105 StorageMonitor* storage_monitor = StorageMonitor::GetInstance(); |
| 106 if (storage_monitor) | |
| 107 storage_monitor->RemoveObserver(this); | |
| 108 } | |
| 98 } | 109 } |
| 99 | 110 |
| 100 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 111 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
| 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 102 | 113 |
| 103 watching_event_set_.insert(event_name); | 114 watching_event_set_.insert(event_name); |
| 104 if (watching_event_set_.count(event_name) > 1) | 115 if (watching_event_set_.count(event_name) > 1) |
| 105 return; | 116 return; |
| 106 | 117 |
| 118 #if defined(USE_ASH) | |
| 107 // For systemInfo.display event. | 119 // For systemInfo.display event. |
| 108 if (IsDisplayChangedEvent(event_name)) { | 120 if (IsDisplayChangedEvent(event_name)) |
| 109 #if defined(USE_ASH) | |
| 110 ash::Shell::GetScreen()->AddObserver(this); | 121 ash::Shell::GetScreen()->AddObserver(this); |
| 111 #endif | 122 #endif |
| 123 | |
| 124 if (IsSystemStorageEvent(event_name)) { | |
| 125 if (!has_storage_monitor_observer_) { | |
| 126 has_storage_monitor_observer_ = true; | |
| 127 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); | |
| 128 StorageMonitor::GetInstance()->AddObserver(this); | |
| 129 } | |
| 112 } | 130 } |
| 113 } | 131 } |
| 114 | 132 |
| 115 void SystemInfoEventRouter::RemoveEventListener( | 133 void SystemInfoEventRouter::RemoveEventListener(const std::string& event_name) { |
| 116 const std::string& event_name) { | |
| 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 118 | 135 |
| 119 std::multiset<std::string>::iterator it = | 136 std::multiset<std::string>::iterator it = |
| 120 watching_event_set_.find(event_name); | 137 watching_event_set_.find(event_name); |
| 121 if (it != watching_event_set_.end()) | 138 if (it != watching_event_set_.end()) |
| 122 watching_event_set_.erase(it); | 139 watching_event_set_.erase(it); |
| 123 | 140 |
| 124 if (watching_event_set_.count(event_name) > 0) | 141 if (watching_event_set_.count(event_name) > 0) |
| 125 return; | 142 return; |
| 126 | 143 |
| 127 if (IsDisplayChangedEvent(event_name)) { | |
| 128 #if defined(USE_ASH) | 144 #if defined(USE_ASH) |
| 145 if (IsDisplayChangedEvent(event_name)) | |
| 129 ash::Shell::GetScreen()->RemoveObserver(this); | 146 ash::Shell::GetScreen()->RemoveObserver(this); |
| 130 #endif | 147 #endif |
| 148 | |
| 149 if (IsSystemStorageEvent(event_name)) { | |
| 150 const std::string& other_event_name = | |
| 151 (event_name == system_storage::OnDetached::kEventName) ? | |
| 152 system_storage::OnAttached::kEventName : | |
| 153 system_storage::OnDetached::kEventName; | |
| 154 if (watching_event_set_.count(other_event_name) == 0) { | |
| 155 StorageMonitor::GetInstance()->RemoveObserver(this); | |
| 156 has_storage_monitor_observer_ = false; | |
| 157 } | |
| 131 } | 158 } |
| 132 } | 159 } |
| 133 | 160 |
| 134 void SystemInfoEventRouter::OnRemovableStorageAttached( | 161 void SystemInfoEventRouter::OnRemovableStorageAttached( |
| 135 const StorageInfo& info) { | 162 const StorageInfo& info) { |
| 136 StorageUnitInfo unit; | 163 StorageUnitInfo unit; |
| 137 systeminfo::BuildStorageUnitInfo(info, &unit); | 164 systeminfo::BuildStorageUnitInfo(info, &unit); |
| 138 scoped_ptr<base::ListValue> args(new base::ListValue); | 165 scoped_ptr<base::ListValue> args(new base::ListValue); |
| 139 args->Append(unit.ToValue().release()); | 166 args->Append(unit.ToValue().release()); |
| 140 DispatchEvent(system_storage::OnAttached::kEventName, args.Pass()); | 167 DispatchEvent(system_storage::OnAttached::kEventName, args.Pass()); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 166 scoped_ptr<base::ListValue> args(new base::ListValue()); | 193 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 167 DispatchEvent(system_display::OnDisplayChanged::kEventName, args.Pass()); | 194 DispatchEvent(system_display::OnDisplayChanged::kEventName, args.Pass()); |
| 168 } | 195 } |
| 169 | 196 |
| 170 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, | 197 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, |
| 171 scoped_ptr<base::ListValue> args) { | 198 scoped_ptr<base::ListValue> args) { |
| 172 g_browser_process->extension_event_router_forwarder()-> | 199 g_browser_process->extension_event_router_forwarder()-> |
| 173 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); | 200 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); |
| 174 } | 201 } |
| 175 | 202 |
| 203 void AddEventListener(const std::string& event_name) { | |
| 204 SystemInfoEventRouter::GetInstance()->AddEventListener(event_name); | |
| 205 } | |
| 206 | |
| 207 void RemoveEventListener(const std::string& event_name) { | |
| 208 SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name); | |
| 209 } | |
| 210 | |
| 176 } // namespace | 211 } // namespace |
| 177 | 212 |
| 178 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > | 213 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > |
| 179 g_factory = LAZY_INSTANCE_INITIALIZER; | 214 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 180 | 215 |
| 181 // static | 216 // static |
| 182 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() { | 217 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() { |
| 183 return &g_factory.Get(); | 218 return &g_factory.Get(); |
| 184 } | 219 } |
| 185 | 220 |
| 186 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) { | 221 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) { |
| 187 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 222 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 188 this, system_storage::OnAttached::kEventName); | 223 this, system_storage::OnAttached::kEventName); |
| 189 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 224 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 190 this, system_storage::OnDetached::kEventName); | 225 this, system_storage::OnDetached::kEventName); |
| 191 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 226 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 192 this, system_display::OnDisplayChanged::kEventName); | 227 this, system_display::OnDisplayChanged::kEventName); |
| 193 } | 228 } |
| 194 | 229 |
| 195 SystemInfoAPI::~SystemInfoAPI() { | 230 SystemInfoAPI::~SystemInfoAPI() { |
| 196 } | 231 } |
| 197 | 232 |
| 198 void SystemInfoAPI::Shutdown() { | 233 void SystemInfoAPI::Shutdown() { |
| 199 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 234 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 200 } | 235 } |
| 201 | 236 |
| 202 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { | 237 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 203 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); | 238 if (IsSystemStorageEvent(details.event_name)) { |
| 239 StorageMonitor::GetInstance()->EnsureInitialized( | |
| 240 base::Bind(&AddEventListener, details.event_name)); | |
| 241 } else { | |
| 242 AddEventListener(details.event_name); | |
| 243 } | |
| 204 } | 244 } |
| 205 | 245 |
| 206 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { | 246 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 207 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); | 247 if (IsSystemStorageEvent(details.event_name)) { |
| 248 StorageMonitor::GetInstance()->EnsureInitialized( | |
| 249 base::Bind(&RemoveEventListener, details.event_name)); | |
| 250 } else { | |
| 251 RemoveEventListener(details.event_name); | |
| 252 } | |
| 208 } | 253 } |
| 209 | 254 |
| 210 } // namespace extensions | 255 } // namespace extensions |
| OLD | NEW |