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 |
43 | 45 |
44 // Event router for systemInfo API. It is a singleton instance shared by | 46 // Event router for systemInfo API. It is a singleton instance shared by |
45 // multiple profiles. | 47 // multiple profiles. |
46 class SystemInfoEventRouter : public gfx::DisplayObserver, | 48 class SystemInfoEventRouter : public gfx::DisplayObserver, |
47 public RemovableStorageObserver { | 49 public RemovableStorageObserver { |
48 public: | 50 public: |
49 static SystemInfoEventRouter* GetInstance(); | 51 static SystemInfoEventRouter* GetInstance(); |
50 | 52 |
51 SystemInfoEventRouter(); | 53 SystemInfoEventRouter(); |
52 virtual ~SystemInfoEventRouter(); | 54 virtual ~SystemInfoEventRouter(); |
53 | 55 |
54 // Add/remove event listener for the |event_name| event. | 56 // Add/remove event listener for the |event_name| event. |
55 void AddEventListener(const std::string& event_name); | 57 void AddEventListener(const std::string& event_name); |
56 void RemoveEventListener(const std::string& event_name); | 58 void RemoveEventListener(const std::string& event_name); |
57 | 59 |
58 private: | 60 private: |
59 // gfx::DisplayObserver: | 61 // gfx::DisplayObserver: |
60 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; | 62 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; |
61 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; | 63 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; |
62 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; | 64 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; |
63 | 65 |
64 // RemovableStorageObserver implementation. | 66 // RemovableStorageObserver implementation. |
65 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE; | 67 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE; |
66 virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE; | 68 virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE; |
67 | 69 |
68 // Called from any thread to dispatch the systemInfo event to all extension | 70 // Called from any thread to dispatch the systemInfo event to all extension |
69 // processes cross multiple profiles. | 71 // processes cross multiple profiles. |
70 void DispatchEvent(const std::string& event_name, | 72 void DispatchEvent(const std::string& event_name, |
71 scoped_ptr<base::ListValue> args); | 73 scoped_ptr<base::ListValue> args); |
72 | 74 |
73 // Called to dispatch the systemInfo.display.onDisplayChanged event. | 75 // Called to dispatch the systemInfo.display.onDisplayChanged event. |
74 void OnDisplayChanged(); | 76 void OnDisplayChanged(); |
75 | 77 |
76 // Used to record the event names being watched. | 78 // Used to record the event names being watched. |
77 std::multiset<std::string> watching_event_set_; | 79 std::multiset<std::string> watching_event_set_; |
78 | 80 |
79 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); | 81 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); |
80 }; | 82 }; |
81 | 83 |
82 static base::LazyInstance<SystemInfoEventRouter>::Leaky | 84 static base::LazyInstance<SystemInfoEventRouter>::Leaky |
83 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; | 85 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; |
84 | 86 |
85 // static | 87 // static |
86 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | 88 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
87 return g_system_info_event_router.Pointer(); | 89 return g_system_info_event_router.Pointer(); |
88 } | 90 } |
89 | 91 |
90 SystemInfoEventRouter::SystemInfoEventRouter() { | 92 SystemInfoEventRouter::SystemInfoEventRouter() { |
91 StorageMonitor::GetInstance()->AddObserver(this); | 93 StorageMonitor::GetInstance()->AddObserver(this); |
92 StorageMonitor::GetInstance()->EnsureInitialized(base::Closure()); | 94 StorageMonitor::GetInstance()->EnsureInitialized(base::Closure()); |
93 } | 95 } |
94 | 96 |
95 SystemInfoEventRouter::~SystemInfoEventRouter() { | 97 SystemInfoEventRouter::~SystemInfoEventRouter() { |
96 if (StorageMonitor* storage_monitor = StorageMonitor::GetInstance()) | 98 if (StorageMonitor* storage_monitor = StorageMonitor::GetInstance()) |
97 storage_monitor->RemoveObserver(this); | 99 storage_monitor->RemoveObserver(this); |
98 } | 100 } |
99 | 101 |
100 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 102 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
102 | 104 |
103 watching_event_set_.insert(event_name); | 105 watching_event_set_.insert(event_name); |
104 if (watching_event_set_.count(event_name) > 1) | 106 if (watching_event_set_.count(event_name) > 1) |
105 return; | 107 return; |
106 | 108 |
107 // For systemInfo.display event. | 109 // For systemInfo.display event. |
108 if (IsDisplayChangedEvent(event_name)) { | |
109 #if defined(USE_ASH) | 110 #if defined(USE_ASH) |
| 111 if (IsDisplayChangedEvent(event_name)) |
110 ash::Shell::GetScreen()->AddObserver(this); | 112 ash::Shell::GetScreen()->AddObserver(this); |
111 #endif | 113 #endif |
112 } | |
113 } | 114 } |
114 | 115 |
115 void SystemInfoEventRouter::RemoveEventListener( | 116 void SystemInfoEventRouter::RemoveEventListener( |
116 const std::string& event_name) { | 117 const std::string& event_name) { |
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
118 | 119 |
119 std::multiset<std::string>::iterator it = | 120 std::multiset<std::string>::iterator it = |
120 watching_event_set_.find(event_name); | 121 watching_event_set_.find(event_name); |
121 if (it != watching_event_set_.end()) | 122 if (it != watching_event_set_.end()) { |
122 watching_event_set_.erase(it); | 123 watching_event_set_.erase(it); |
| 124 if (watching_event_set_.count(event_name) > 0) |
| 125 return; |
| 126 } |
123 | 127 |
124 if (watching_event_set_.count(event_name) > 0) | |
125 return; | |
126 | |
127 if (IsDisplayChangedEvent(event_name)) { | |
128 #if defined(USE_ASH) | 128 #if defined(USE_ASH) |
| 129 if (IsDisplayChangedEvent(event_name)) |
129 ash::Shell::GetScreen()->RemoveObserver(this); | 130 ash::Shell::GetScreen()->RemoveObserver(this); |
130 #endif | 131 #endif |
131 } | |
132 } | 132 } |
133 | 133 |
134 void SystemInfoEventRouter::OnRemovableStorageAttached( | 134 void SystemInfoEventRouter::OnRemovableStorageAttached( |
135 const StorageInfo& info) { | 135 const StorageInfo& info) { |
136 StorageUnitInfo unit; | 136 StorageUnitInfo unit; |
137 systeminfo::BuildStorageUnitInfo(info, &unit); | 137 systeminfo::BuildStorageUnitInfo(info, &unit); |
138 scoped_ptr<base::ListValue> args(new base::ListValue); | 138 scoped_ptr<base::ListValue> args(new base::ListValue); |
139 args->Append(unit.ToValue().release()); | 139 args->Append(unit.ToValue().release()); |
140 DispatchEvent(system_storage::OnAttached::kEventName, args.Pass()); | 140 DispatchEvent(system_storage::OnAttached::kEventName, args.Pass()); |
141 } | 141 } |
142 | 142 |
143 void SystemInfoEventRouter::OnRemovableStorageDetached( | 143 void SystemInfoEventRouter::OnRemovableStorageDetached( |
144 const StorageInfo& info) { | 144 const StorageInfo& info) { |
145 scoped_ptr<base::ListValue> args(new base::ListValue); | 145 scoped_ptr<base::ListValue> args(new base::ListValue); |
146 args->Append(new base::StringValue(StorageMonitor::GetInstance()-> | 146 std::string transient_id = |
147 GetTransientIdForDeviceId(info.device_id()))); | 147 StorageMonitor::GetInstance()->GetTransientIdForDeviceId( |
| 148 info.device_id()); |
| 149 args->AppendString(transient_id); |
148 | 150 |
149 DispatchEvent(system_storage::OnDetached::kEventName, args.Pass()); | 151 DispatchEvent(system_storage::OnDetached::kEventName, args.Pass()); |
150 } | 152 } |
151 | 153 |
152 void SystemInfoEventRouter::OnDisplayBoundsChanged( | 154 void SystemInfoEventRouter::OnDisplayBoundsChanged( |
153 const gfx::Display& display) { | 155 const gfx::Display& display) { |
154 OnDisplayChanged(); | 156 OnDisplayChanged(); |
155 } | 157 } |
156 | 158 |
157 void SystemInfoEventRouter::OnDisplayAdded(const gfx::Display& new_display) { | 159 void SystemInfoEventRouter::OnDisplayAdded(const gfx::Display& new_display) { |
(...skipping 15 matching lines...) Expand all Loading... |
173 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); | 175 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); |
174 } | 176 } |
175 | 177 |
176 } // namespace | 178 } // namespace |
177 | 179 |
178 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > | 180 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > |
179 g_factory = LAZY_INSTANCE_INITIALIZER; | 181 g_factory = LAZY_INSTANCE_INITIALIZER; |
180 | 182 |
181 // static | 183 // static |
182 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() { | 184 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() { |
183 return &g_factory.Get(); | 185 return g_factory.Pointer(); |
184 } | 186 } |
185 | 187 |
186 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) { | 188 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) { |
187 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 189 EventRouter* router = ExtensionSystem::Get(profile_)->event_router(); |
188 this, system_storage::OnAttached::kEventName); | 190 router->RegisterObserver(this, system_storage::OnAttached::kEventName); |
189 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 191 router->RegisterObserver(this, system_storage::OnDetached::kEventName); |
190 this, system_storage::OnDetached::kEventName); | 192 router->RegisterObserver(this, system_display::OnDisplayChanged::kEventName); |
191 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
192 this, system_display::OnDisplayChanged::kEventName); | |
193 } | 193 } |
194 | 194 |
195 SystemInfoAPI::~SystemInfoAPI() { | 195 SystemInfoAPI::~SystemInfoAPI() { |
196 } | 196 } |
197 | 197 |
198 void SystemInfoAPI::Shutdown() { | 198 void SystemInfoAPI::Shutdown() { |
199 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 199 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
200 } | 200 } |
201 | 201 |
202 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { | 202 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { |
203 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); | 203 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); |
204 } | 204 } |
205 | 205 |
206 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { | 206 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { |
207 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); | 207 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); |
208 } | 208 } |
209 | 209 |
210 } // namespace extensions | 210 } // namespace extensions |
OLD | NEW |