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/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::StorageUnitInfo; |
| 18 using api::experimental_system_info_storage::StorageChangeInfo; |
| 19 using content::BrowserThread; |
| 20 |
| 21 const char kSystemInfoEventPrefix[] = "experimental.systemInfo"; |
| 22 const char kStorageEventPrefix[] = "experimental.systemInfo.storage"; |
| 23 |
| 24 // static |
| 25 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| 26 return Singleton<SystemInfoEventRouter>::get(); |
| 27 } |
| 28 |
| 29 SystemInfoEventRouter::SystemInfoEventRouter() { |
| 30 } |
| 31 |
| 32 SystemInfoEventRouter::~SystemInfoEventRouter() { |
| 33 } |
| 34 |
| 35 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
| 36 watching_event_set_.insert(event_name); |
| 37 if (watching_event_set_.count(event_name) > 1) |
| 38 return; |
| 39 // Start watching the |event_name| event if the first event listener arrives. |
| 40 std::string prefix(kStorageEventPrefix); |
| 41 if (event_name.compare(0, prefix.size(), kStorageEventPrefix) == 0) { |
| 42 // TODO (hongbo): Start watching storage device information via calling |
| 43 // SystemMonitor::StartWatchingStorage. |
| 44 } |
| 45 } |
| 46 |
| 47 void SystemInfoEventRouter::RemoveEventListener( |
| 48 const std::string& event_name) { |
| 49 watching_event_set_.erase(event_name); |
| 50 if (watching_event_set_.count(event_name) > 0) |
| 51 return; |
| 52 |
| 53 // In case of the last event listener is removed, we need to stop watching |
| 54 // it to avoid unnecessary overhead. |
| 55 std::string prefix(kStorageEventPrefix); |
| 56 if (event_name.compare( |
| 57 0, prefix.size(), kStorageEventPrefix) == 0) { |
| 58 // TODO(hongbo): Stop watching storage device information via calling |
| 59 // SystemMonitor::StopWatchingStorage. |
| 60 return; |
| 61 } |
| 62 } |
| 63 |
| 64 // static |
| 65 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
| 66 std::string prefix(kSystemInfoEventPrefix); |
| 67 return event_name.compare(0, prefix.size(), prefix) == 0; |
| 68 } |
| 69 |
| 70 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( |
| 71 const std::string& id, int64 available_capacity) { |
| 72 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 73 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 74 base::Bind(&SystemInfoEventRouter::OnStorageAvailableCapacityChanged, |
| 75 base::Unretained(this), id, available_capacity)); |
| 76 return; |
| 77 } |
| 78 |
| 79 // We are on the UI thread now. |
| 80 StorageChangeInfo info; |
| 81 info.id = id; |
| 82 info.available_capacity = static_cast<double>(available_capacity); |
| 83 |
| 84 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 85 args->Append(info.ToValue().release()); |
| 86 |
| 87 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); |
| 88 } |
| 89 |
| 90 void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id, |
| 91 const string16& name, const FilePath::StringType& location) { |
| 92 // TODO(hongbo): Handle storage device arrival/removal event. |
| 93 } |
| 94 |
| 95 void SystemInfoEventRouter::OnRemovableStorageDetached(const std::string& id) { |
| 96 // TODO(hongbo): Same as above. |
| 97 } |
| 98 |
| 99 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, |
| 100 scoped_ptr<base::ListValue> args) { |
| 101 g_browser_process->extension_event_router_forwarder()-> |
| 102 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); |
| 103 } |
| 104 |
| 105 } // namespace extensions |
OLD | NEW |