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 25 matching lines...) Expand all Loading... |
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 #if defined(USE_ASH) |
41 bool IsDisplayChangedEvent(const std::string& event_name) { | 41 bool IsDisplayChangedEvent(const std::string& event_name) { |
42 return event_name == system_display::OnDisplayChanged::kEventName; | 42 return event_name == system_display::OnDisplayChanged::kEventName; |
43 } | 43 } |
44 #endif | 44 #endif |
45 | 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 } |
| 50 |
46 // 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 |
47 // multiple profiles. | 52 // multiple profiles. |
48 class SystemInfoEventRouter : public gfx::DisplayObserver, | 53 class SystemInfoEventRouter : public gfx::DisplayObserver, |
49 public RemovableStorageObserver { | 54 public RemovableStorageObserver { |
50 public: | 55 public: |
51 static SystemInfoEventRouter* GetInstance(); | 56 static SystemInfoEventRouter* GetInstance(); |
52 | 57 |
53 SystemInfoEventRouter(); | 58 SystemInfoEventRouter(); |
54 virtual ~SystemInfoEventRouter(); | 59 virtual ~SystemInfoEventRouter(); |
55 | 60 |
(...skipping 15 matching lines...) Expand all Loading... |
71 // processes cross multiple profiles. | 76 // processes cross multiple profiles. |
72 void DispatchEvent(const std::string& event_name, | 77 void DispatchEvent(const std::string& event_name, |
73 scoped_ptr<base::ListValue> args); | 78 scoped_ptr<base::ListValue> args); |
74 | 79 |
75 // Called to dispatch the systemInfo.display.onDisplayChanged event. | 80 // Called to dispatch the systemInfo.display.onDisplayChanged event. |
76 void OnDisplayChanged(); | 81 void OnDisplayChanged(); |
77 | 82 |
78 // Used to record the event names being watched. | 83 // Used to record the event names being watched. |
79 std::multiset<std::string> watching_event_set_; | 84 std::multiset<std::string> watching_event_set_; |
80 | 85 |
| 86 bool has_storage_monitor_observer_; |
| 87 |
81 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); | 88 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); |
82 }; | 89 }; |
83 | 90 |
84 static base::LazyInstance<SystemInfoEventRouter>::Leaky | 91 static base::LazyInstance<SystemInfoEventRouter>::Leaky |
85 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; | 92 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; |
86 | 93 |
87 // static | 94 // static |
88 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | 95 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
89 return g_system_info_event_router.Pointer(); | 96 return g_system_info_event_router.Pointer(); |
90 } | 97 } |
91 | 98 |
92 SystemInfoEventRouter::SystemInfoEventRouter() { | 99 SystemInfoEventRouter::SystemInfoEventRouter() |
93 StorageMonitor::GetInstance()->AddObserver(this); | 100 : has_storage_monitor_observer_(false) { |
94 StorageMonitor::GetInstance()->EnsureInitialized(base::Closure()); | |
95 } | 101 } |
96 | 102 |
97 SystemInfoEventRouter::~SystemInfoEventRouter() { | 103 SystemInfoEventRouter::~SystemInfoEventRouter() { |
98 if (StorageMonitor* storage_monitor = StorageMonitor::GetInstance()) | 104 if (has_storage_monitor_observer_) { |
99 storage_monitor->RemoveObserver(this); | 105 StorageMonitor* storage_monitor = StorageMonitor::GetInstance(); |
| 106 if (storage_monitor) |
| 107 storage_monitor->RemoveObserver(this); |
| 108 } |
100 } | 109 } |
101 | 110 |
102 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 111 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
104 | 113 |
105 watching_event_set_.insert(event_name); | 114 watching_event_set_.insert(event_name); |
106 if (watching_event_set_.count(event_name) > 1) | 115 if (watching_event_set_.count(event_name) > 1) |
107 return; | 116 return; |
108 | 117 |
109 // For systemInfo.display event. | 118 // For systemInfo.display event. |
110 #if defined(USE_ASH) | 119 #if defined(USE_ASH) |
111 if (IsDisplayChangedEvent(event_name)) | 120 if (IsDisplayChangedEvent(event_name)) |
112 ash::Shell::GetScreen()->AddObserver(this); | 121 ash::Shell::GetScreen()->AddObserver(this); |
113 #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 } |
| 130 } |
114 } | 131 } |
115 | 132 |
116 void SystemInfoEventRouter::RemoveEventListener( | 133 void SystemInfoEventRouter::RemoveEventListener(const std::string& event_name) { |
117 const std::string& event_name) { | |
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
119 | 135 |
120 std::multiset<std::string>::iterator it = | 136 std::multiset<std::string>::iterator it = |
121 watching_event_set_.find(event_name); | 137 watching_event_set_.find(event_name); |
122 if (it != watching_event_set_.end()) { | 138 if (it != watching_event_set_.end()) { |
123 watching_event_set_.erase(it); | 139 watching_event_set_.erase(it); |
124 if (watching_event_set_.count(event_name) > 0) | 140 if (watching_event_set_.count(event_name) > 0) |
125 return; | 141 return; |
126 } | 142 } |
127 | 143 |
128 #if defined(USE_ASH) | 144 #if defined(USE_ASH) |
129 if (IsDisplayChangedEvent(event_name)) | 145 if (IsDisplayChangedEvent(event_name)) |
130 ash::Shell::GetScreen()->RemoveObserver(this); | 146 ash::Shell::GetScreen()->RemoveObserver(this); |
131 #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 } |
| 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()); |
141 } | 168 } |
(...skipping 26 matching lines...) Expand all Loading... |
168 scoped_ptr<base::ListValue> args(new base::ListValue()); | 195 scoped_ptr<base::ListValue> args(new base::ListValue()); |
169 DispatchEvent(system_display::OnDisplayChanged::kEventName, args.Pass()); | 196 DispatchEvent(system_display::OnDisplayChanged::kEventName, args.Pass()); |
170 } | 197 } |
171 | 198 |
172 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, | 199 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, |
173 scoped_ptr<base::ListValue> args) { | 200 scoped_ptr<base::ListValue> args) { |
174 g_browser_process->extension_event_router_forwarder()-> | 201 g_browser_process->extension_event_router_forwarder()-> |
175 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); | 202 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); |
176 } | 203 } |
177 | 204 |
| 205 void AddEventListener(const std::string& event_name) { |
| 206 SystemInfoEventRouter::GetInstance()->AddEventListener(event_name); |
| 207 } |
| 208 |
| 209 void RemoveEventListener(const std::string& event_name) { |
| 210 SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name); |
| 211 } |
| 212 |
178 } // namespace | 213 } // namespace |
179 | 214 |
180 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > | 215 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > |
181 g_factory = LAZY_INSTANCE_INITIALIZER; | 216 g_factory = LAZY_INSTANCE_INITIALIZER; |
182 | 217 |
183 // static | 218 // static |
184 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() { | 219 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() { |
185 return g_factory.Pointer(); | 220 return g_factory.Pointer(); |
186 } | 221 } |
187 | 222 |
188 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) { | 223 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) { |
189 EventRouter* router = ExtensionSystem::Get(profile_)->event_router(); | 224 EventRouter* router = ExtensionSystem::Get(profile_)->event_router(); |
190 router->RegisterObserver(this, system_storage::OnAttached::kEventName); | 225 router->RegisterObserver(this, system_storage::OnAttached::kEventName); |
191 router->RegisterObserver(this, system_storage::OnDetached::kEventName); | 226 router->RegisterObserver(this, system_storage::OnDetached::kEventName); |
192 router->RegisterObserver(this, system_display::OnDisplayChanged::kEventName); | 227 router->RegisterObserver(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 |