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 "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/extensions/event_router_forwarder.h" | 10 #include "chrome/browser/extensions/event_router_forwarder.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 return Singleton<SystemInfoEventRouter>::get(); | 43 return Singleton<SystemInfoEventRouter>::get(); |
44 } | 44 } |
45 | 45 |
46 SystemInfoEventRouter::SystemInfoEventRouter() { | 46 SystemInfoEventRouter::SystemInfoEventRouter() { |
47 } | 47 } |
48 | 48 |
49 SystemInfoEventRouter::~SystemInfoEventRouter() { | 49 SystemInfoEventRouter::~SystemInfoEventRouter() { |
50 } | 50 } |
51 | 51 |
52 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 52 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
53 watching_event_set_.insert(event_name); | 53 watching_event_set_.insert(event_name); |
benwells
2013/01/31 04:04:57
Nit: add DCHECK that are on the UI thread.
Hongbo Min
2013/02/01 07:06:50
Done for both AddEventListener and RemoveEventList
| |
54 if (watching_event_set_.count(event_name) > 1) | 54 if (watching_event_set_.count(event_name) > 1) |
55 return; | 55 return; |
56 // Start watching the |event_name| event if the first event listener arrives. | 56 // Start watching the |event_name| event if the first event listener arrives. |
57 // For systemInfo.storage event. | 57 // For systemInfo.storage event. |
58 if (IsStorageEvent(event_name)) { | 58 if (IsStorageEvent(event_name)) { |
59 // TODO (hongbo): Start watching storage device information via calling | 59 StorageInfoProvider::Get()->AddObserver(this); |
60 // SystemMonitor::StartWatchingStorage. | 60 StorageInfo info; |
61 if (StorageInfoProvider::Get()->QueryInfo(&info)) { | |
62 for (StorageInfo::iterator it = info.begin(); it != info.end(); ++it) | |
63 StorageInfoProvider::Get()->StartWatching((*it)->id); | |
64 } | |
61 return; | 65 return; |
62 } | 66 } |
63 | 67 |
64 // For systemInfo.cpu event. | 68 // For systemInfo.cpu event. |
65 if (IsCpuEvent(event_name)) { | 69 if (IsCpuEvent(event_name)) { |
66 CpuInfoProvider::Get()->StartSampling( | 70 CpuInfoProvider::Get()->StartSampling( |
67 base::Bind(&SystemInfoEventRouter::OnNextCpuSampling, | 71 base::Bind(&SystemInfoEventRouter::OnNextCpuSampling, |
68 base::Unretained(this))); | 72 base::Unretained(this))); |
69 return; | 73 return; |
70 } | 74 } |
71 } | 75 } |
72 | 76 |
73 void SystemInfoEventRouter::RemoveEventListener( | 77 void SystemInfoEventRouter::RemoveEventListener( |
74 const std::string& event_name) { | 78 const std::string& event_name) { |
75 watching_event_set_.erase(event_name); | 79 watching_event_set_.erase(event_name); |
76 if (watching_event_set_.count(event_name) > 0) | 80 if (watching_event_set_.count(event_name) > 0) |
77 return; | 81 return; |
78 | 82 |
79 // In case of the last event listener is removed, we need to stop watching | 83 // In case of the last event listener is removed, we need to stop watching |
80 // it to avoid unnecessary overhead. | 84 // it to avoid unnecessary overhead. |
81 if (IsStorageEvent(event_name)) { | 85 if (IsStorageEvent(event_name)) { |
82 // TODO(hongbo): Stop watching storage device information via calling | 86 StorageInfo info; |
83 // SystemMonitor::StopWatchingStorage. | 87 if (StorageInfoProvider::Get()->QueryInfo(&info)) { |
88 for (StorageInfo::iterator it = info.begin(); it != info.end(); ++it) | |
89 StorageInfoProvider::Get()->StopWatching((*it)->id); | |
90 } | |
91 StorageInfoProvider::Get()->RemoveObserver(this); | |
84 return; | 92 return; |
85 } | 93 } |
86 if (IsCpuEvent(event_name)) { | 94 if (IsCpuEvent(event_name)) { |
87 CpuInfoProvider::Get()->StopSampling(); | 95 CpuInfoProvider::Get()->StopSampling(); |
88 } | 96 } |
89 } | 97 } |
90 | 98 |
91 // static | 99 // static |
92 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { | 100 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
93 std::string prefix(kSystemInfoEventPrefix); | 101 std::string prefix(kSystemInfoEventPrefix); |
94 return event_name.compare(0, prefix.size(), prefix) == 0; | 102 return event_name.compare(0, prefix.size(), prefix) == 0; |
95 } | 103 } |
96 | 104 |
97 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( | 105 // Called on UI thread since the observer is added from UI thread. |
98 const std::string& id, int64 available_capacity) { | 106 void SystemInfoEventRouter::OnStorageFreeSpaceChanged( |
99 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 107 const std::string& id, double new_value, double old_value) { |
benwells
2013/01/31 04:04:57
I don't understand why the threading stuff changed
Hongbo Min
2013/02/01 07:06:50
Since the observers of StorageInfoProvider are hol
benwells
2013/02/04 04:21:41
Oh wow, I didn't realize that class was so smart.
| |
100 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
101 base::Bind(&SystemInfoEventRouter::OnStorageAvailableCapacityChanged, | |
102 base::Unretained(this), id, available_capacity)); | |
103 return; | |
104 } | |
105 | |
106 // We are on the UI thread now. | |
107 StorageChangeInfo info; | 108 StorageChangeInfo info; |
108 info.id = id; | 109 info.id = id; |
109 info.available_capacity = static_cast<double>(available_capacity); | 110 info.available_capacity = static_cast<double>(new_value); |
110 | 111 |
111 scoped_ptr<base::ListValue> args(new base::ListValue()); | 112 scoped_ptr<base::ListValue> args(new base::ListValue()); |
112 args->Append(info.ToValue().release()); | 113 args->Append(info.ToValue().release()); |
113 | 114 |
114 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); | 115 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); |
115 } | 116 } |
116 | 117 |
117 void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id, | 118 void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id, |
118 const string16& name, const FilePath::StringType& location) { | 119 const string16& name, const FilePath::StringType& location) { |
119 // TODO(hongbo): Handle storage device arrival/removal event. | 120 // TODO(hongbo): Handle storage device arrival/removal event. |
(...skipping 10 matching lines...) Expand all Loading... | |
130 } | 131 } |
131 | 132 |
132 void SystemInfoEventRouter::OnNextCpuSampling(scoped_ptr<CpuUpdateInfo> info) { | 133 void SystemInfoEventRouter::OnNextCpuSampling(scoped_ptr<CpuUpdateInfo> info) { |
133 scoped_ptr<base::ListValue> args(new base::ListValue()); | 134 scoped_ptr<base::ListValue> args(new base::ListValue()); |
134 args->Append(info->ToValue().release()); | 135 args->Append(info->ToValue().release()); |
135 | 136 |
136 DispatchEvent(event_names::kOnCpuUpdated, args.Pass()); | 137 DispatchEvent(event_names::kOnCpuUpdated, args.Pass()); |
137 } | 138 } |
138 | 139 |
139 } // namespace extensions | 140 } // namespace extensions |
OLD | NEW |