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" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/extensions/api/system_display/display_info_provider.h" |
16 #include "chrome/browser/extensions/api/system_storage/storage_info_provider.h" | 17 #include "chrome/browser/extensions/api/system_storage/storage_info_provider.h" |
17 #include "chrome/browser/extensions/event_router_forwarder.h" | 18 #include "chrome/browser/extensions/event_router_forwarder.h" |
18 #include "chrome/common/extensions/api/system_display.h" | 19 #include "chrome/common/extensions/api/system_display.h" |
19 #include "chrome/common/extensions/api/system_storage.h" | 20 #include "chrome/common/extensions/api/system_storage.h" |
20 #include "components/storage_monitor/removable_storage_observer.h" | 21 #include "components/storage_monitor/removable_storage_observer.h" |
21 #include "components/storage_monitor/storage_info.h" | 22 #include "components/storage_monitor/storage_info.h" |
22 #include "components/storage_monitor/storage_monitor.h" | 23 #include "components/storage_monitor/storage_monitor.h" |
23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
24 #include "ui/gfx/display_observer.h" | 25 #include "ui/gfx/display_observer.h" |
25 | |
26 #if defined(OS_CHROMEOS) | |
27 #include "ash/shell.h" | |
28 #include "ui/gfx/screen.h" | 26 #include "ui/gfx/screen.h" |
29 #endif | |
30 | 27 |
31 namespace extensions { | 28 namespace extensions { |
32 | 29 |
33 using api::system_storage::StorageUnitInfo; | 30 using api::system_storage::StorageUnitInfo; |
34 using content::BrowserThread; | 31 using content::BrowserThread; |
35 using storage_monitor::StorageMonitor; | 32 using storage_monitor::StorageMonitor; |
36 | 33 |
37 namespace system_display = api::system_display; | 34 namespace system_display = api::system_display; |
38 namespace system_storage = api::system_storage; | 35 namespace system_storage = api::system_storage; |
39 | 36 |
40 namespace { | 37 namespace { |
41 | 38 |
42 #if defined(OS_CHROMEOS) | |
43 bool IsDisplayChangedEvent(const std::string& event_name) { | 39 bool IsDisplayChangedEvent(const std::string& event_name) { |
44 return event_name == system_display::OnDisplayChanged::kEventName; | 40 return event_name == system_display::OnDisplayChanged::kEventName; |
45 } | 41 } |
46 #endif | |
47 | 42 |
48 bool IsSystemStorageEvent(const std::string& event_name) { | 43 bool IsSystemStorageEvent(const std::string& event_name) { |
49 return (event_name == system_storage::OnAttached::kEventName || | 44 return (event_name == system_storage::OnAttached::kEventName || |
50 event_name == system_storage::OnDetached::kEventName); | 45 event_name == system_storage::OnDetached::kEventName); |
51 } | 46 } |
52 | 47 |
53 // Event router for systemInfo API. It is a singleton instance shared by | 48 // Event router for systemInfo API. It is a singleton instance shared by |
54 // multiple profiles. | 49 // multiple profiles. |
55 class SystemInfoEventRouter : public gfx::DisplayObserver, | 50 class SystemInfoEventRouter : public gfx::DisplayObserver, |
56 public storage_monitor::RemovableStorageObserver { | 51 public storage_monitor::RemovableStorageObserver { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 } | 108 } |
114 } | 109 } |
115 | 110 |
116 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 111 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
117 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 112 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
118 | 113 |
119 watching_event_set_.insert(event_name); | 114 watching_event_set_.insert(event_name); |
120 if (watching_event_set_.count(event_name) > 1) | 115 if (watching_event_set_.count(event_name) > 1) |
121 return; | 116 return; |
122 | 117 |
123 // For system.display event. | 118 if (IsDisplayChangedEvent(event_name)) { |
124 #if defined(OS_CHROMEOS) | 119 gfx::Screen* screen = DisplayInfoProvider::Get()->GetActiveScreen(); |
125 if (IsDisplayChangedEvent(event_name)) | 120 if (screen) |
126 ash::Shell::GetScreen()->AddObserver(this); | 121 screen->AddObserver(this); |
127 #endif | 122 } |
128 | 123 |
129 if (IsSystemStorageEvent(event_name)) { | 124 if (IsSystemStorageEvent(event_name)) { |
130 if (!has_storage_monitor_observer_) { | 125 if (!has_storage_monitor_observer_) { |
131 has_storage_monitor_observer_ = true; | 126 has_storage_monitor_observer_ = true; |
132 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); | 127 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); |
133 StorageMonitor::GetInstance()->AddObserver(this); | 128 StorageMonitor::GetInstance()->AddObserver(this); |
134 } | 129 } |
135 } | 130 } |
136 } | 131 } |
137 | 132 |
138 void SystemInfoEventRouter::RemoveEventListener(const std::string& event_name) { | 133 void SystemInfoEventRouter::RemoveEventListener(const std::string& event_name) { |
139 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
140 | 135 |
141 std::multiset<std::string>::iterator it = | 136 std::multiset<std::string>::iterator it = |
142 watching_event_set_.find(event_name); | 137 watching_event_set_.find(event_name); |
143 if (it != watching_event_set_.end()) { | 138 if (it != watching_event_set_.end()) { |
144 watching_event_set_.erase(it); | 139 watching_event_set_.erase(it); |
145 if (watching_event_set_.count(event_name) > 0) | 140 if (watching_event_set_.count(event_name) > 0) |
146 return; | 141 return; |
147 } | 142 } |
148 | 143 |
149 #if defined(OS_CHROMEOS) | 144 if (IsDisplayChangedEvent(event_name)) { |
150 if (IsDisplayChangedEvent(event_name)) | 145 gfx::Screen* screen = DisplayInfoProvider::Get()->GetActiveScreen(); |
151 ash::Shell::GetScreen()->RemoveObserver(this); | 146 if (screen) |
152 #endif | 147 screen->RemoveObserver(this); |
| 148 } |
153 | 149 |
154 if (IsSystemStorageEvent(event_name)) { | 150 if (IsSystemStorageEvent(event_name)) { |
155 const std::string& other_event_name = | 151 const std::string& other_event_name = |
156 (event_name == system_storage::OnDetached::kEventName) ? | 152 (event_name == system_storage::OnDetached::kEventName) ? |
157 system_storage::OnAttached::kEventName : | 153 system_storage::OnAttached::kEventName : |
158 system_storage::OnDetached::kEventName; | 154 system_storage::OnDetached::kEventName; |
159 if (watching_event_set_.count(other_event_name) == 0) { | 155 if (watching_event_set_.count(other_event_name) == 0) { |
160 StorageMonitor::GetInstance()->RemoveObserver(this); | 156 StorageMonitor::GetInstance()->RemoveObserver(this); |
161 has_storage_monitor_observer_ = false; | 157 has_storage_monitor_observer_ = false; |
162 } | 158 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { | 249 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { |
254 if (IsSystemStorageEvent(details.event_name)) { | 250 if (IsSystemStorageEvent(details.event_name)) { |
255 StorageMonitor::GetInstance()->EnsureInitialized( | 251 StorageMonitor::GetInstance()->EnsureInitialized( |
256 base::Bind(&RemoveEventListener, details.event_name)); | 252 base::Bind(&RemoveEventListener, details.event_name)); |
257 } else { | 253 } else { |
258 RemoveEventListener(details.event_name); | 254 RemoveEventListener(details.event_name); |
259 } | 255 } |
260 } | 256 } |
261 | 257 |
262 } // namespace extensions | 258 } // namespace extensions |
OLD | NEW |