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

Side by Side Diff: chrome/browser/extensions/system_info_event_router.cc

Issue 10836341: Add the basic code skeleton for system info event router (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: use event router forwarder instead Created 8 years, 3 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
(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/browser_process.h"
10 #include "chrome/browser/extensions/event_router_forwarder.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 namespace extensions {
16
17 using api::experimental_system_info_storage::StorageInfo;
18 using api::experimental_system_info_storage::StorageUnitInfo;
19 using api::experimental_system_info_storage::StorageChangeInfo;
20 using content::BrowserThread;
21
22 const char kSystemInfoEventPrefix[] = "experimental.systemInfo";
23 const char kStorageEventPrefix[] = "experimental.systemInfo.storage";
24
25 // static
26 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() {
27 return Singleton<SystemInfoEventRouter>::get();
28 }
29
30 SystemInfoEventRouter::SystemInfoEventRouter() {
31 }
32
33 SystemInfoEventRouter::~SystemInfoEventRouter() {
34 }
35
36 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) {
37 // Start watching the |event_name| event if the first event listener arrives.
38 if (event_watching_map_.find(event_name) == event_watching_map_.end()) {
39 std::string prefix(kStorageEventPrefix);
40 if (event_name.compare(0, prefix.size(), kStorageEventPrefix) == 0) {
41 // TODO (hongbo): Start watching storage device information via calling
42 // SystemMonitor::StartWatchingStorage.
43 }
44 event_watching_map_[event_name] = 1;
45 return;
46 }
47
48 // Increase the watching count.
49 event_watching_map_[event_name]++;
50 }
51
52 void SystemInfoEventRouter::RemoveEventListener(
53 const std::string& event_name) {
54 if (event_watching_map_.find(event_name) == event_watching_map_.end()) {
55 NOTREACHED() << "The event is NOT watched yet: " << event_name;
56 return;
57 }
58
59 if (--event_watching_map_[event_name] > 0)
60 return;
61
62 // The watching count is down to 0, it means the last event listener is
63 // removed, we need to stop watching it.
64 std::string prefix(kStorageEventPrefix);
65 if (event_name.compare(
66 0, prefix.size(), kStorageEventPrefix) == 0) {
67 // TODO(hongbo): Stop watching storage device information via calling
68 // SystemMonitor::StopWatchingStorage.
69 return;
70 }
71 }
72
73 // static
74 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) {
75 std::string prefix(kSystemInfoEventPrefix);
76 return event_name.compare(0, prefix.size(), prefix) == 0;
77 }
78
79 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged(
80 const std::string& id, int64 available_capacity) {
81 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
82 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
83 base::Bind(&SystemInfoEventRouter::OnStorageAvailableCapacityChanged,
84 base::Unretained(this), id, available_capacity));
85 return;
86 }
87
88 // We are on the UI thread now.
89 StorageChangeInfo info;
90 info.id = id;
91 info.available_capacity = static_cast<double>(available_capacity);
92
93 scoped_ptr<base::ListValue> args(new base::ListValue());
94 args->Append(info.ToValue().release());
95
96 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass());
97 }
98
99 void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id,
100 const string16& name, const FilePath::StringType& location) {
101 // TODO(hongbo): Handle storage device arrival/removal event.
102 }
103
104 void SystemInfoEventRouter::OnRemovableStorageDetached(const std::string& id) {
105 // TODO(hongbo): Same as above.
106 }
107
108 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name,
109 scoped_ptr<base::ListValue> args) {
110 g_browser_process->extension_event_router_forwarder()->
111 BroadcastEventToRenderers(event_name, args.Pass(), GURL());
112 }
113
114 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698