Chromium Code Reviews| Index: chrome/browser/extensions/api/system_info/system_info_api.cc |
| =================================================================== |
| --- chrome/browser/extensions/api/system_info/system_info_api.cc (revision 245000) |
| +++ chrome/browser/extensions/api/system_info/system_info_api.cc (working copy) |
| @@ -37,10 +37,17 @@ |
| namespace { |
| +#if defined(USE_ASH) |
| bool IsDisplayChangedEvent(const std::string& event_name) { |
| return event_name == system_display::OnDisplayChanged::kEventName; |
| } |
| +#endif |
| +bool IsSystemStorageEvent(const std::string& event_name) { |
| + return (event_name == system_storage::OnAttached::kEventName || |
| + event_name == system_storage::OnDetached::kEventName); |
| +} |
| + |
| // Event router for systemInfo API. It is a singleton instance shared by |
| // multiple profiles. |
| class SystemInfoEventRouter : public gfx::DisplayObserver, |
| @@ -68,7 +75,7 @@ |
| // Called from any thread to dispatch the systemInfo event to all extension |
| // processes cross multiple profiles. |
| void DispatchEvent(const std::string& event_name, |
| - scoped_ptr<base::ListValue> args); |
| + 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
|
| // Called to dispatch the systemInfo.display.onDisplayChanged event. |
| void OnDisplayChanged(); |
| @@ -76,6 +83,8 @@ |
| // Used to record the event names being watched. |
| std::multiset<std::string> watching_event_set_; |
| + bool has_storage_monitor_observer_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); |
| }; |
| @@ -87,14 +96,16 @@ |
| return g_system_info_event_router.Pointer(); |
| } |
| -SystemInfoEventRouter::SystemInfoEventRouter() { |
| - StorageMonitor::GetInstance()->AddObserver(this); |
| - StorageMonitor::GetInstance()->EnsureInitialized(base::Closure()); |
| +SystemInfoEventRouter::SystemInfoEventRouter() |
| + : has_storage_monitor_observer_(false) { |
| } |
| SystemInfoEventRouter::~SystemInfoEventRouter() { |
| - if (StorageMonitor* storage_monitor = StorageMonitor::GetInstance()) |
| - storage_monitor->RemoveObserver(this); |
| + if (has_storage_monitor_observer_) { |
| + StorageMonitor* storage_monitor = StorageMonitor::GetInstance(); |
| + if (storage_monitor) |
| + storage_monitor->RemoveObserver(this); |
| + } |
| } |
| void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
| @@ -104,16 +115,22 @@ |
| if (watching_event_set_.count(event_name) > 1) |
| return; |
| +#if defined(USE_ASH) |
| // For systemInfo.display event. |
| - if (IsDisplayChangedEvent(event_name)) { |
| -#if defined(USE_ASH) |
| + if (IsDisplayChangedEvent(event_name)) |
| ash::Shell::GetScreen()->AddObserver(this); |
| #endif |
| + |
| + if (IsSystemStorageEvent(event_name)) { |
| + if (!has_storage_monitor_observer_) { |
| + has_storage_monitor_observer_ = true; |
| + DCHECK(StorageMonitor::GetInstance()->IsInitialized()); |
| + StorageMonitor::GetInstance()->AddObserver(this); |
| + } |
| } |
| } |
| -void SystemInfoEventRouter::RemoveEventListener( |
| - const std::string& event_name) { |
| +void SystemInfoEventRouter::RemoveEventListener(const std::string& event_name) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| std::multiset<std::string>::iterator it = |
| @@ -124,10 +141,20 @@ |
| if (watching_event_set_.count(event_name) > 0) |
| return; |
| - if (IsDisplayChangedEvent(event_name)) { |
| #if defined(USE_ASH) |
| + if (IsDisplayChangedEvent(event_name)) |
| ash::Shell::GetScreen()->RemoveObserver(this); |
| #endif |
| + |
| + if (IsSystemStorageEvent(event_name)) { |
| + const std::string& other_event_name = |
| + (event_name == system_storage::OnDetached::kEventName) ? |
| + system_storage::OnAttached::kEventName : |
| + system_storage::OnDetached::kEventName; |
| + if (watching_event_set_.count(other_event_name) == 0) { |
| + StorageMonitor::GetInstance()->RemoveObserver(this); |
| + has_storage_monitor_observer_ = false; |
| + } |
| } |
| } |
| @@ -173,6 +200,14 @@ |
| BroadcastEventToRenderers(event_name, args.Pass(), GURL()); |
| } |
| +void AddEventListener(const std::string& event_name) { |
| + SystemInfoEventRouter::GetInstance()->AddEventListener(event_name); |
| +} |
| + |
| +void RemoveEventListener(const std::string& event_name) { |
| + SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name); |
| +} |
| + |
| } // namespace |
| static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > |
| @@ -200,11 +235,21 @@ |
| } |
| void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { |
| - SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); |
| + if (IsSystemStorageEvent(details.event_name)) { |
| + StorageMonitor::GetInstance()->EnsureInitialized( |
| + base::Bind(&AddEventListener, details.event_name)); |
| + } else { |
| + AddEventListener(details.event_name); |
| + } |
| } |
| void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| - SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); |
| + if (IsSystemStorageEvent(details.event_name)) { |
| + StorageMonitor::GetInstance()->EnsureInitialized( |
| + base::Bind(&RemoveEventListener, details.event_name)); |
| + } else { |
| + RemoveEventListener(details.event_name); |
| + } |
| } |
| } // namespace extensions |