Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: chrome/browser/extensions/api/system_info/system_info_api.cc

Issue 15896007: [SystemInfo API] Rewrite storage info provider using storage monitor impl (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fix comments from thestig Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h" 13 #include "base/memory/singleton.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "base/sys_info.h"
16 #include "base/task_runner_util.h"
15 #include "base/values.h" 17 #include "base/values.h"
16 #include "chrome/browser/browser_process.h" 18 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" 19 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h"
18 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide r.h" 20 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide r.h"
19 #include "chrome/browser/extensions/event_names.h" 21 #include "chrome/browser/extensions/event_names.h"
20 #include "chrome/browser/extensions/event_router_forwarder.h" 22 #include "chrome/browser/extensions/event_router_forwarder.h"
23 #include "chrome/browser/storage_monitor/removable_storage_observer.h"
24 #include "chrome/browser/storage_monitor/storage_info.h"
25 #include "chrome/browser/storage_monitor/storage_monitor.h"
21 #include "chrome/common/extensions/api/experimental_system_info_cpu.h" 26 #include "chrome/common/extensions/api/experimental_system_info_cpu.h"
22 #include "chrome/common/extensions/api/experimental_system_info_storage.h" 27 #include "chrome/common/extensions/api/experimental_system_info_storage.h"
23 #include "ui/gfx/display_observer.h" 28 #include "ui/gfx/display_observer.h"
24 29
25 #if defined(USE_ASH) 30 #if defined(USE_ASH)
26 #include "ash/screen_ash.h" 31 #include "ash/screen_ash.h"
27 #include "ash/shell.h" 32 #include "ash/shell.h"
28 #endif 33 #endif
29 34
30 namespace extensions { 35 namespace extensions {
(...skipping 18 matching lines...) Expand all
49 bool IsAvailableCapacityChangedEvent(const std::string& event_name) { 54 bool IsAvailableCapacityChangedEvent(const std::string& event_name) {
50 return event_name == event_names::kOnStorageAvailableCapacityChanged; 55 return event_name == event_names::kOnStorageAvailableCapacityChanged;
51 } 56 }
52 57
53 bool IsCpuUpdatedEvent(const std::string& event_name) { 58 bool IsCpuUpdatedEvent(const std::string& event_name) {
54 return event_name == event_names::kOnCpuUpdated; 59 return event_name == event_names::kOnCpuUpdated;
55 } 60 }
56 61
57 // Event router for systemInfo API. It is a singleton instance shared by 62 // Event router for systemInfo API. It is a singleton instance shared by
58 // multiple profiles. 63 // multiple profiles.
59 class SystemInfoEventRouter 64 class SystemInfoEventRouter : public gfx::DisplayObserver,
60 : public gfx::DisplayObserver, public StorageInfoObserver { 65 public chrome::RemovableStorageObserver,
66 public StorageFreeSpaceObserver {
61 public: 67 public:
62 static SystemInfoEventRouter* GetInstance(); 68 static SystemInfoEventRouter* GetInstance();
63 69
64 SystemInfoEventRouter(); 70 SystemInfoEventRouter();
65 virtual ~SystemInfoEventRouter(); 71 virtual ~SystemInfoEventRouter();
66 72
67 // Add/remove event listener for the |event_name| event. 73 // Add/remove event listener for the |event_name| event.
68 void AddEventListener(const std::string& event_name); 74 void AddEventListener(const std::string& event_name);
69 void RemoveEventListener(const std::string& event_name); 75 void RemoveEventListener(const std::string& event_name);
70 76
71 // Return true if the |event_name| is an event from systemInfo namespace. 77 // Return true if the |event_name| is an event from systemInfo namespace.
72 static bool IsSystemInfoEvent(const std::string& event_name); 78 static bool IsSystemInfoEvent(const std::string& event_name);
73 79
74 private: 80 private:
75 // StorageInfoObserver:
76 virtual void OnStorageFreeSpaceChanged(const std::string& id,
77 double new_value,
78 double old_value) OVERRIDE;
79 virtual void OnStorageAttached(
80 const std::string& id,
81 api::experimental_system_info_storage::StorageUnitType type,
82 double capacity,
83 double available_capacity) OVERRIDE;
84 virtual void OnStorageDetached(const std::string& id) OVERRIDE;
85
86 // gfx::DisplayObserver: 81 // gfx::DisplayObserver:
87 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; 82 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE;
88 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; 83 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE;
89 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; 84 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE;
90 85
86 // chrome::RemovableStorageObserver implementation.
87 virtual void OnRemovableStorageAttached(
88 const chrome::StorageInfo& info) OVERRIDE;
89 virtual void OnRemovableStorageDetached(
90 const chrome::StorageInfo& info) OVERRIDE;
91
92 // StorageFreeSpaceObserver implementation.
vandebo (ex-Chrome) 2013/05/28 20:00:29 What was the conclusion of the discussion about ha
Hongbo Min 2013/05/29 01:05:38 It is still pending now.
93 virtual void OnFreeSpaceChanged(const std::string& id,
94 double new_value,
95 double old_value) OVERRIDE;
96
97 void DispatchStorageAttachedEvent(const chrome::StorageInfo& info,
98 int64 avail_bytes);
99
91 // Called from any thread to dispatch the systemInfo event to all extension 100 // Called from any thread to dispatch the systemInfo event to all extension
92 // processes cross multiple profiles. 101 // processes cross multiple profiles.
93 void DispatchEvent(const std::string& event_name, 102 void DispatchEvent(const std::string& event_name,
94 scoped_ptr<base::ListValue> args); 103 scoped_ptr<base::ListValue> args);
95 104
96 // The callbacks of querying storage info to start and stop watching the 105 // The callbacks of querying storage info to start and stop watching the
97 // storages. Called from UI thread. 106 // storages. Called from UI thread.
98 void StartWatchingStorages(const StorageInfo& info, bool success); 107 void StartWatchingStorages(const StorageUnitList& info, bool success);
99 void StopWatchingStorages(const StorageInfo& info, bool success); 108 void StopWatchingStorages(const StorageUnitList& info, bool success);
100 109
101 // The callback for CPU sampling cycle. Called from FILE thread. 110 // The callback for CPU sampling cycle. Called from FILE thread.
102 void OnNextCpuSampling( 111 void OnNextCpuSampling(
103 scoped_ptr<api::experimental_system_info_cpu::CpuUpdateInfo> info); 112 scoped_ptr<api::experimental_system_info_cpu::CpuUpdateInfo> info);
104 113
105 // Called to dispatch the systemInfo.display.onDisplayChanged event. 114 // Called to dispatch the systemInfo.display.onDisplayChanged event.
106 void OnDisplayChanged(); 115 void OnDisplayChanged();
107 116
108 // Used to record the event names being watched. 117 // Used to record the event names being watched.
109 std::multiset<std::string> watching_event_set_; 118 std::multiset<std::string> watching_event_set_;
110 119
111 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); 120 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter);
112 }; 121 };
113 122
114 static base::LazyInstance<SystemInfoEventRouter>::Leaky 123 static base::LazyInstance<SystemInfoEventRouter>::Leaky
115 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; 124 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER;
116 125
117 // static 126 // static
118 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { 127 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() {
119 return g_system_info_event_router.Pointer(); 128 return g_system_info_event_router.Pointer();
120 } 129 }
121 130
122 SystemInfoEventRouter::SystemInfoEventRouter() { 131 SystemInfoEventRouter::SystemInfoEventRouter() {
132 DCHECK(chrome::StorageMonitor::GetInstance());
vandebo (ex-Chrome) 2013/05/28 20:00:29 When is SystemInfoEventRouter created? We are chan
Hongbo Min 2013/05/29 01:05:38 The SystemInfoEventRouter instance is declared as
133 chrome::StorageMonitor::GetInstance()->AddObserver(this);
123 StorageInfoProvider::Get()->AddObserver(this); 134 StorageInfoProvider::Get()->AddObserver(this);
124 } 135 }
125 136
126 SystemInfoEventRouter::~SystemInfoEventRouter() { 137 SystemInfoEventRouter::~SystemInfoEventRouter() {
127 StorageInfoProvider::Get()->RemoveObserver(this); 138 StorageInfoProvider::Get()->RemoveObserver(this);
139 if (chrome::StorageMonitor::GetInstance())
140 chrome::StorageMonitor::GetInstance()->RemoveObserver(this);
128 } 141 }
129 142
130 void SystemInfoEventRouter::StartWatchingStorages( 143 void SystemInfoEventRouter::StartWatchingStorages(
131 const StorageInfo& info, bool success) { 144 const StorageUnitList& info, bool success) {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
133 if (!success) 146 if (!success)
134 return; 147 return;
135 148
136 for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { 149 for (StorageUnitList::const_iterator it = info.begin();
150 it != info.end(); ++it) {
137 StorageInfoProvider::Get()->StartWatching((*it)->id); 151 StorageInfoProvider::Get()->StartWatching((*it)->id);
138 } 152 }
139 } 153 }
140 154
141 void SystemInfoEventRouter::StopWatchingStorages( 155 void SystemInfoEventRouter::StopWatchingStorages(
142 const StorageInfo& info, bool success) { 156 const StorageUnitList& info, bool success) {
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
144 if (!success) 158 if (!success)
145 return; 159 return;
146 160
147 for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { 161 for (StorageUnitList::const_iterator it = info.begin();
162 it != info.end(); ++it) {
148 StorageInfoProvider::Get()->StopWatching((*it)->id); 163 StorageInfoProvider::Get()->StopWatching((*it)->id);
149 } 164 }
150 } 165 }
151 166
152 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { 167 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
154 169
155 watching_event_set_.insert(event_name); 170 watching_event_set_.insert(event_name);
156 if (watching_event_set_.count(event_name) > 1) 171 if (watching_event_set_.count(event_name) > 1)
157 return; 172 return;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 229
215 // static 230 // static
216 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { 231 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) {
217 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental. 232 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental.
218 return (StartsWithASCII(event_name, kSystemInfoEventPrefix, true) || 233 return (StartsWithASCII(event_name, kSystemInfoEventPrefix, true) ||
219 StartsWithASCII(event_name, kExperimentalSystemInfoEventPrefix, 234 StartsWithASCII(event_name, kExperimentalSystemInfoEventPrefix,
220 true)); 235 true));
221 } 236 }
222 237
223 // Called on UI thread since the observer is added from UI thread. 238 // Called on UI thread since the observer is added from UI thread.
224 void SystemInfoEventRouter::OnStorageFreeSpaceChanged( 239 void SystemInfoEventRouter::OnFreeSpaceChanged(
225 const std::string& id, double new_value, double old_value) { 240 const std::string& id, double old_value, double new_value) {
241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
242
226 StorageChangeInfo info; 243 StorageChangeInfo info;
227 info.id = id; 244 info.id = id;
228 info.available_capacity = static_cast<double>(new_value); 245 info.old_value = old_value;
246 info.new_value = new_value;
229 247
230 scoped_ptr<base::ListValue> args(new base::ListValue()); 248 scoped_ptr<base::ListValue> args(new base::ListValue());
231 args->Append(info.ToValue().release()); 249 args->Append(info.ToValue().release());
232 250
233 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); 251 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass());
234 } 252 }
235 253
236 void SystemInfoEventRouter::OnStorageAttached(const std::string& id, 254 void SystemInfoEventRouter::OnRemovableStorageAttached(
237 StorageUnitType type, 255 const chrome::StorageInfo& info) {
238 double capacity, 256 base::FilePath mount_path(info.location());
239 double available_capacity) { 257 base::PostTaskAndReplyWithResult(
240 StorageUnitInfo info; 258 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
241 info.id = id; 259 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN),
242 info.type = type; 260 FROM_HERE,
243 info.capacity = capacity; 261 base::Bind(&StorageInfoProvider::GetStorageFreeSpace,
244 info.available_capacity = available_capacity; 262 StorageInfoProvider::Get(), mount_path),
263 base::Bind(&SystemInfoEventRouter::DispatchStorageAttachedEvent,
264 base::Unretained(this), info));
265 }
266
267 void SystemInfoEventRouter::DispatchStorageAttachedEvent(
268 const chrome::StorageInfo& info, int64 avail_bytes) {
269 StorageUnitInfo unit;
270 systeminfo::BuildStorageUnitInfo(info, &unit);
271
272 unit.available_capacity =
273 avail_bytes > 0 ? static_cast<double>(avail_bytes) : 0;
245 274
246 scoped_ptr<base::ListValue> args(new base::ListValue); 275 scoped_ptr<base::ListValue> args(new base::ListValue);
247 args->Append(info.ToValue().release()); 276 args->Append(unit.ToValue().release());
248 277
249 DispatchEvent(event_names::kOnStorageAttached, args.Pass()); 278 DispatchEvent(event_names::kOnStorageAttached, args.Pass());
250 } 279 }
251 280
252 void SystemInfoEventRouter::OnStorageDetached(const std::string& id) { 281 void SystemInfoEventRouter::OnRemovableStorageDetached(
282 const chrome::StorageInfo& info) {
253 scoped_ptr<base::ListValue> args(new base::ListValue); 283 scoped_ptr<base::ListValue> args(new base::ListValue);
254 args->Append(new base::StringValue(id)); 284 args->Append(new base::StringValue(info.device_id()));
255 285
256 DispatchEvent(event_names::kOnStorageDetached, args.Pass()); 286 DispatchEvent(event_names::kOnStorageDetached, args.Pass());
257 } 287 }
258 288
259 void SystemInfoEventRouter::OnDisplayBoundsChanged( 289 void SystemInfoEventRouter::OnDisplayBoundsChanged(
260 const gfx::Display& display) { 290 const gfx::Display& display) {
261 OnDisplayChanged(); 291 OnDisplayChanged();
262 } 292 }
263 293
264 void SystemInfoEventRouter::OnDisplayAdded(const gfx::Display& new_display) { 294 void SystemInfoEventRouter::OnDisplayAdded(const gfx::Display& new_display) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 347
318 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { 348 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) {
319 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); 349 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name);
320 } 350 }
321 351
322 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { 352 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) {
323 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); 353 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name);
324 } 354 }
325 355
326 } // namespace extensions 356 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698