OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/system_info_event_router.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "chrome/browser/extensions/extension_system.h" | |
10 #include "chrome/browser/extensions/event_router.h" | |
11 #include "chrome/browser/extensions/event_names.h" | |
12 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide r.h" | |
13 #include "chrome/common/extensions/api/experimental_system_info_storage.h" | |
14 | |
15 | |
16 namespace extensions { | |
17 | |
18 using api::experimental_system_info_storage::StorageInfo; | |
19 using api::experimental_system_info_storage::StorageUnitInfo; | |
20 using api::experimental_system_info_storage::StorageChangeInfo; | |
21 using content::BrowserThread; | |
22 | |
23 const char kSystemInfoEventPrefix[] = "experimental.systemInfo"; | |
24 const char kStorageEventPrefix[] = "experimental.systemInfo.storage"; | |
25 | |
26 // static | |
27 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | |
28 return Singleton<SystemInfoEventRouter>::get(); | |
29 } | |
30 | |
31 SystemInfoEventRouter::SystemInfoEventRouter() { | |
32 } | |
33 | |
34 SystemInfoEventRouter::~SystemInfoEventRouter() { | |
35 } | |
36 | |
37 void SystemInfoEventRouter::AddEventListener(Profile* profile, | |
38 const std::string& event_name) { | |
39 // Start watching the |event_name| event if the first event listener | |
40 // arrives. | |
41 if (!HasEventListener(event_name)) { | |
42 event_listener_map_[profile].insert(event_name); | |
43 std::string prefix(kStorageEventPrefix); | |
44 if (event_name.compare(0, prefix.size(), kStorageEventPrefix) == 0) { | |
45 // TODO (hongbo): Start watching storage device information via calling | |
46 // SystemMonitor::StartWatchingStorage. | |
47 return; | |
48 } | |
49 } | |
50 } | |
51 | |
52 void SystemInfoEventRouter::RemoveEventListener(Profile* profile, | |
53 const std::string& event_name) { | |
54 event_listener_map_[profile].erase(event_name); | |
55 | |
56 // Stop watching the |event_name| event if the last event listener | |
57 // has been removed. | |
58 if (!HasEventListener(event_name)) { | |
59 std::string prefix(kStorageEventPrefix); | |
60 if (event_name.compare( | |
61 0, prefix.size(), kStorageEventPrefix) == 0) { | |
62 // TODO(hongbo): Stop watching storage device information via calling | |
63 // SystemMonitor::StopWatchingStorage. | |
64 return; | |
65 } | |
66 } | |
67 } | |
68 | |
69 bool SystemInfoEventRouter::HasEventListener(const std::string& event_name) { | |
70 for (EventListenerMap::iterator iter = event_listener_map_.begin(); | |
71 iter != event_listener_map_.end(); | |
72 ++iter) { | |
73 if (iter->second.count(event_name) > 0) | |
74 return true; | |
75 } | |
76 return false; | |
77 } | |
78 | |
79 // static | |
80 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { | |
81 std::string prefix(kSystemInfoEventPrefix); | |
82 return event_name.compare(0, prefix.size(), prefix) == 0; | |
83 } | |
84 | |
85 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( | |
86 const std::string& id, int64 available_capacity) { | |
87 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
88 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
89 base::Bind( | |
90 &SystemInfoEventRouter::OnStorageAvailableCapacityChanged, | |
91 base::Unretained(this), id, available_capacity)); | |
Mihai Parparita -not on Chrome
2012/08/31 01:01:00
Add an early return here, so that the rest of the
Hongbo Min
2012/08/31 15:29:53
Done.
| |
92 } else { | |
93 // We are on the UI thread now. | |
94 StorageChangeInfo info; | |
95 info.id = id; | |
96 info.available_capacity = static_cast<double>(available_capacity); | |
97 | |
98 base::ListValue args; | |
99 args.Append(info.ToValue().release()); | |
100 | |
101 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args); | |
102 } | |
103 } | |
104 | |
105 void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id, | |
106 const string16& name, const FilePath::StringType& location) { | |
107 // TODO(hongbo): Handle storage device arrival/removal event. | |
108 } | |
109 | |
110 void SystemInfoEventRouter::OnRemovableStorageDetached(const std::string& id) { | |
111 // TODO(hongbo): Same as above. | |
112 } | |
113 | |
114 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, | |
115 const base::ListValue& args) { | |
116 for (EventListenerMap::iterator iter = event_listener_map_.begin(); | |
117 iter != event_listener_map_.end(); | |
118 ++iter) { | |
119 // Ignore the Profile without listening the |event_name| event. | |
120 if (iter->second.count(event_name) == 0) | |
121 continue; | |
122 | |
123 Profile* profile = iter->first; | |
124 if (profile->GetExtensionEventRouter()) { | |
125 scoped_ptr<base::ListValue> scoped_args(args.DeepCopy()); | |
126 ExtensionSystem::Get(profile)->event_router()->DispatchEventToRenderers( | |
127 event_name, scoped_args.Pass(), NULL, GURL(), EventFilteringInfo()); | |
128 } | |
129 } | |
130 } | |
131 | |
132 } // namespace extensions | |
OLD | NEW |