OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/system_info_event_router.h" | 5 #include "chrome/browser/extensions/system_info_event_router.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | 56 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
57 return Singleton<SystemInfoEventRouter>::get(); | 57 return Singleton<SystemInfoEventRouter>::get(); |
58 } | 58 } |
59 | 59 |
60 SystemInfoEventRouter::SystemInfoEventRouter() { | 60 SystemInfoEventRouter::SystemInfoEventRouter() { |
61 } | 61 } |
62 | 62 |
63 SystemInfoEventRouter::~SystemInfoEventRouter() { | 63 SystemInfoEventRouter::~SystemInfoEventRouter() { |
64 } | 64 } |
65 | 65 |
66 void SystemInfoEventRouter::StartWatchingStorages( | |
67 const StorageInfo& info, bool success) { | |
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
69 if (!success) return; | |
benwells
2013/02/04 04:21:41
nit: return on next line.
Hongbo Min
2013/02/04 04:53:28
Done.
| |
70 | |
71 for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { | |
benwells
2013/02/04 04:21:41
Is the list of StorageInfos constant? i.e. can new
Hongbo Min
2013/02/04 04:53:28
Yes, no new storage info will appear dynamically.
| |
72 StorageInfoProvider::Get()->StartWatching((*it)->id); | |
benwells
2013/02/04 04:21:41
nit: indenting << 2
Hongbo Min
2013/02/04 04:53:28
Done.
| |
73 } | |
74 } | |
75 | |
76 void SystemInfoEventRouter::StopWatchingStorages( | |
77 const StorageInfo& info, bool success) { | |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
79 if (!success) return; | |
benwells
2013/02/04 04:21:41
Nit: return on next line.
Hongbo Min
2013/02/04 04:53:28
Done.
| |
80 | |
81 for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { | |
82 StorageInfoProvider::Get()->StopWatching((*it)->id); | |
benwells
2013/02/04 04:21:41
nit: indenting << 2
Hongbo Min
2013/02/04 04:53:28
Done.
| |
83 } | |
84 } | |
85 | |
66 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 86 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
88 | |
67 watching_event_set_.insert(event_name); | 89 watching_event_set_.insert(event_name); |
68 if (watching_event_set_.count(event_name) > 1) | 90 if (watching_event_set_.count(event_name) > 1) |
69 return; | 91 return; |
70 // Start watching the |event_name| event if the first event listener arrives. | 92 // Start watching the |event_name| event if the first event listener arrives. |
71 // For systemInfo.storage event. | 93 // For systemInfo.storage event. |
72 if (IsStorageEvent(event_name)) { | 94 if (IsStorageEvent(event_name)) { |
73 // TODO (hongbo): Start watching storage device information via calling | 95 StorageInfoProvider::Get()->AddObserver(this); |
74 // SystemMonitor::StartWatchingStorage. | 96 StorageInfoProvider::Get()->StartQueryInfo( |
97 base::Bind(&SystemInfoEventRouter::StartWatchingStorages, | |
98 base::Unretained(this))); | |
75 return; | 99 return; |
76 } | 100 } |
77 | 101 |
78 // For systemInfo.cpu event. | 102 // For systemInfo.cpu event. |
79 if (IsCpuEvent(event_name)) { | 103 if (IsCpuEvent(event_name)) { |
80 CpuInfoProvider::Get()->StartSampling( | 104 CpuInfoProvider::Get()->StartSampling( |
81 base::Bind(&SystemInfoEventRouter::OnNextCpuSampling, | 105 base::Bind(&SystemInfoEventRouter::OnNextCpuSampling, |
82 base::Unretained(this))); | 106 base::Unretained(this))); |
83 return; | 107 return; |
84 } | 108 } |
85 | 109 |
86 // For systemInfo.display event. | 110 // For systemInfo.display event. |
87 if (IsDisplayEvent(event_name)) { | 111 if (IsDisplayEvent(event_name)) { |
88 #if defined(USE_ASH) | 112 #if defined(USE_ASH) |
89 ash::Shell::GetScreen()->AddObserver(this); | 113 ash::Shell::GetScreen()->AddObserver(this); |
90 #endif | 114 #endif |
91 } | 115 } |
92 } | 116 } |
93 | 117 |
94 void SystemInfoEventRouter::RemoveEventListener( | 118 void SystemInfoEventRouter::RemoveEventListener( |
95 const std::string& event_name) { | 119 const std::string& event_name) { |
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
121 | |
96 watching_event_set_.erase(event_name); | 122 watching_event_set_.erase(event_name); |
97 if (watching_event_set_.count(event_name) > 0) | 123 if (watching_event_set_.count(event_name) > 0) |
98 return; | 124 return; |
99 | 125 |
100 // In case of the last event listener is removed, we need to stop watching | 126 // In case of the last event listener is removed, we need to stop watching |
101 // it to avoid unnecessary overhead. | 127 // it to avoid unnecessary overhead. |
102 if (IsStorageEvent(event_name)) { | 128 if (IsStorageEvent(event_name)) { |
103 // TODO(hongbo): Stop watching storage device information via calling | 129 StorageInfoProvider::Get()->StartQueryInfo( |
104 // SystemMonitor::StopWatchingStorage. | 130 base::Bind(&SystemInfoEventRouter::StopWatchingStorages, |
105 return; | 131 base::Unretained(this))); |
132 StorageInfoProvider::Get()->RemoveObserver(this); | |
106 } | 133 } |
134 | |
107 if (IsCpuEvent(event_name)) { | 135 if (IsCpuEvent(event_name)) { |
108 CpuInfoProvider::Get()->StopSampling(); | 136 CpuInfoProvider::Get()->StopSampling(); |
109 } | 137 } |
110 | 138 |
111 if (IsDisplayEvent(event_name)) { | 139 if (IsDisplayEvent(event_name)) { |
112 #if defined(USE_ASH) | 140 #if defined(USE_ASH) |
113 ash::Shell::GetScreen()->RemoveObserver(this); | 141 ash::Shell::GetScreen()->RemoveObserver(this); |
114 #endif | 142 #endif |
115 } | 143 } |
116 } | 144 } |
117 | 145 |
118 // static | 146 // static |
119 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { | 147 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
120 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental. | 148 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental. |
121 return (StartsWithASCII(event_name, kSystemInfoEventPrefix, true) || | 149 return (StartsWithASCII(event_name, kSystemInfoEventPrefix, true) || |
122 StartsWithASCII(event_name, kExperimentalSystemInfoEventPrefix, | 150 StartsWithASCII(event_name, kExperimentalSystemInfoEventPrefix, |
123 true)); | 151 true)); |
124 } | 152 } |
125 | 153 |
126 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( | 154 // Called on UI thread since the observer is added from UI thread. |
127 const std::string& id, int64 available_capacity) { | 155 void SystemInfoEventRouter::OnStorageFreeSpaceChanged( |
128 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 156 const std::string& id, double new_value, double old_value) { |
129 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
130 base::Bind(&SystemInfoEventRouter::OnStorageAvailableCapacityChanged, | |
131 base::Unretained(this), id, available_capacity)); | |
132 return; | |
133 } | |
134 | |
135 // We are on the UI thread now. | |
136 StorageChangeInfo info; | 157 StorageChangeInfo info; |
137 info.id = id; | 158 info.id = id; |
138 info.available_capacity = static_cast<double>(available_capacity); | 159 info.available_capacity = static_cast<double>(new_value); |
139 | 160 |
140 scoped_ptr<base::ListValue> args(new base::ListValue()); | 161 scoped_ptr<base::ListValue> args(new base::ListValue()); |
141 args->Append(info.ToValue().release()); | 162 args->Append(info.ToValue().release()); |
142 | 163 |
143 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); | 164 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); |
144 } | 165 } |
145 | 166 |
146 void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id, | 167 void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id, |
147 const string16& name, const FilePath::StringType& location) { | 168 const string16& name, const FilePath::StringType& location) { |
148 // TODO(hongbo): Handle storage device arrival/removal event. | 169 // TODO(hongbo): Handle storage device arrival/removal event. |
(...skipping 28 matching lines...) Expand all Loading... | |
177 } | 198 } |
178 | 199 |
179 void SystemInfoEventRouter::OnNextCpuSampling(scoped_ptr<CpuUpdateInfo> info) { | 200 void SystemInfoEventRouter::OnNextCpuSampling(scoped_ptr<CpuUpdateInfo> info) { |
180 scoped_ptr<base::ListValue> args(new base::ListValue()); | 201 scoped_ptr<base::ListValue> args(new base::ListValue()); |
181 args->Append(info->ToValue().release()); | 202 args->Append(info->ToValue().release()); |
182 | 203 |
183 DispatchEvent(event_names::kOnCpuUpdated, args.Pass()); | 204 DispatchEvent(event_names::kOnCpuUpdated, args.Pass()); |
184 } | 205 } |
185 | 206 |
186 } // namespace extensions | 207 } // namespace extensions |
OLD | NEW |