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 26 matching lines...) Expand all Loading... |
37 | 37 |
38 namespace { | 38 namespace { |
39 | 39 |
40 bool IsDisplayChangedEvent(const std::string& event_name) { | 40 bool IsDisplayChangedEvent(const std::string& event_name) { |
41 return event_name == system_display::OnDisplayChanged::kEventName; | 41 return event_name == system_display::OnDisplayChanged::kEventName; |
42 } | 42 } |
43 | 43 |
44 // Event router for systemInfo API. It is a singleton instance shared by | 44 // Event router for systemInfo API. It is a singleton instance shared by |
45 // multiple profiles. | 45 // multiple profiles. |
46 class SystemInfoEventRouter : public gfx::DisplayObserver, | 46 class SystemInfoEventRouter : public gfx::DisplayObserver, |
47 public chrome::RemovableStorageObserver { | 47 public RemovableStorageObserver { |
48 public: | 48 public: |
49 static SystemInfoEventRouter* GetInstance(); | 49 static SystemInfoEventRouter* GetInstance(); |
50 | 50 |
51 SystemInfoEventRouter(); | 51 SystemInfoEventRouter(); |
52 virtual ~SystemInfoEventRouter(); | 52 virtual ~SystemInfoEventRouter(); |
53 | 53 |
54 // Add/remove event listener for the |event_name| event. | 54 // Add/remove event listener for the |event_name| event. |
55 void AddEventListener(const std::string& event_name); | 55 void AddEventListener(const std::string& event_name); |
56 void RemoveEventListener(const std::string& event_name); | 56 void RemoveEventListener(const std::string& event_name); |
57 | 57 |
58 private: | 58 private: |
59 // gfx::DisplayObserver: | 59 // gfx::DisplayObserver: |
60 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; | 60 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; |
61 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; | 61 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; |
62 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; | 62 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; |
63 | 63 |
64 // chrome::RemovableStorageObserver implementation. | 64 // RemovableStorageObserver implementation. |
65 virtual void OnRemovableStorageAttached( | 65 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE; |
66 const chrome::StorageInfo& info) OVERRIDE; | 66 virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE; |
67 virtual void OnRemovableStorageDetached( | |
68 const chrome::StorageInfo& info) OVERRIDE; | |
69 | 67 |
70 // Called from any thread to dispatch the systemInfo event to all extension | 68 // Called from any thread to dispatch the systemInfo event to all extension |
71 // processes cross multiple profiles. | 69 // processes cross multiple profiles. |
72 void DispatchEvent(const std::string& event_name, | 70 void DispatchEvent(const std::string& event_name, |
73 scoped_ptr<base::ListValue> args); | 71 scoped_ptr<base::ListValue> args); |
74 | 72 |
75 // Called to dispatch the systemInfo.display.onDisplayChanged event. | 73 // Called to dispatch the systemInfo.display.onDisplayChanged event. |
76 void OnDisplayChanged(); | 74 void OnDisplayChanged(); |
77 | 75 |
78 // Used to record the event names being watched. | 76 // Used to record the event names being watched. |
79 std::multiset<std::string> watching_event_set_; | 77 std::multiset<std::string> watching_event_set_; |
80 | 78 |
81 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); | 79 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); |
82 }; | 80 }; |
83 | 81 |
84 static base::LazyInstance<SystemInfoEventRouter>::Leaky | 82 static base::LazyInstance<SystemInfoEventRouter>::Leaky |
85 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; | 83 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; |
86 | 84 |
87 // static | 85 // static |
88 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | 86 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
89 return g_system_info_event_router.Pointer(); | 87 return g_system_info_event_router.Pointer(); |
90 } | 88 } |
91 | 89 |
92 SystemInfoEventRouter::SystemInfoEventRouter() { | 90 SystemInfoEventRouter::SystemInfoEventRouter() { |
93 chrome::StorageMonitor::GetInstance()->AddObserver(this); | 91 StorageMonitor::GetInstance()->AddObserver(this); |
94 } | 92 } |
95 | 93 |
96 SystemInfoEventRouter::~SystemInfoEventRouter() { | 94 SystemInfoEventRouter::~SystemInfoEventRouter() { |
97 if (chrome::StorageMonitor* storage_monitor = | 95 if (StorageMonitor* storage_monitor = StorageMonitor::GetInstance()) |
98 chrome::StorageMonitor::GetInstance()) | |
99 storage_monitor->RemoveObserver(this); | 96 storage_monitor->RemoveObserver(this); |
100 } | 97 } |
101 | 98 |
102 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 99 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
104 | 101 |
105 watching_event_set_.insert(event_name); | 102 watching_event_set_.insert(event_name); |
106 if (watching_event_set_.count(event_name) > 1) | 103 if (watching_event_set_.count(event_name) > 1) |
107 return; | 104 return; |
108 | 105 |
(...skipping 18 matching lines...) Expand all Loading... |
127 return; | 124 return; |
128 | 125 |
129 if (IsDisplayChangedEvent(event_name)) { | 126 if (IsDisplayChangedEvent(event_name)) { |
130 #if defined(USE_ASH) | 127 #if defined(USE_ASH) |
131 ash::Shell::GetScreen()->RemoveObserver(this); | 128 ash::Shell::GetScreen()->RemoveObserver(this); |
132 #endif | 129 #endif |
133 } | 130 } |
134 } | 131 } |
135 | 132 |
136 void SystemInfoEventRouter::OnRemovableStorageAttached( | 133 void SystemInfoEventRouter::OnRemovableStorageAttached( |
137 const chrome::StorageInfo& info) { | 134 const StorageInfo& info) { |
138 StorageUnitInfo unit; | 135 StorageUnitInfo unit; |
139 systeminfo::BuildStorageUnitInfo(info, &unit); | 136 systeminfo::BuildStorageUnitInfo(info, &unit); |
140 scoped_ptr<base::ListValue> args(new base::ListValue); | 137 scoped_ptr<base::ListValue> args(new base::ListValue); |
141 args->Append(unit.ToValue().release()); | 138 args->Append(unit.ToValue().release()); |
142 DispatchEvent(system_storage::OnAttached::kEventName, args.Pass()); | 139 DispatchEvent(system_storage::OnAttached::kEventName, args.Pass()); |
143 } | 140 } |
144 | 141 |
145 void SystemInfoEventRouter::OnRemovableStorageDetached( | 142 void SystemInfoEventRouter::OnRemovableStorageDetached( |
146 const chrome::StorageInfo& info) { | 143 const StorageInfo& info) { |
147 scoped_ptr<base::ListValue> args(new base::ListValue); | 144 scoped_ptr<base::ListValue> args(new base::ListValue); |
148 args->Append(new base::StringValue(chrome::StorageMonitor::GetInstance()-> | 145 args->Append(new base::StringValue(StorageMonitor::GetInstance()-> |
149 GetTransientIdForDeviceId(info.device_id()))); | 146 GetTransientIdForDeviceId(info.device_id()))); |
150 | 147 |
151 DispatchEvent(system_storage::OnDetached::kEventName, args.Pass()); | 148 DispatchEvent(system_storage::OnDetached::kEventName, args.Pass()); |
152 } | 149 } |
153 | 150 |
154 void SystemInfoEventRouter::OnDisplayBoundsChanged( | 151 void SystemInfoEventRouter::OnDisplayBoundsChanged( |
155 const gfx::Display& display) { | 152 const gfx::Display& display) { |
156 OnDisplayChanged(); | 153 OnDisplayChanged(); |
157 } | 154 } |
158 | 155 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 | 200 |
204 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { | 201 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { |
205 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); | 202 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); |
206 } | 203 } |
207 | 204 |
208 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { | 205 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { |
209 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); | 206 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); |
210 } | 207 } |
211 | 208 |
212 } // namespace extensions | 209 } // namespace extensions |
OLD | NEW |