Index: chrome/browser/extensions/system_info_event_router.cc |
diff --git a/chrome/browser/extensions/system_info_event_router.cc b/chrome/browser/extensions/system_info_event_router.cc |
index f6dc20a1bcdbf7c01eb9c9a815c88cdb10aadfcc..a63a98e3c3b3676266f5651f9e3c56754cc8333b 100644 |
--- a/chrome/browser/extensions/system_info_event_router.cc |
+++ b/chrome/browser/extensions/system_info_event_router.cc |
@@ -6,6 +6,9 @@ |
#include "base/bind.h" |
#include "base/memory/scoped_ptr.h" |
+#include "base/stl_util.h" |
+#include "base/string_number_conversions.h" |
+#include "base/string_util.h" |
#include "chrome/browser/browser_process.h" |
#include "chrome/browser/extensions/event_router_forwarder.h" |
#include "chrome/browser/extensions/event_names.h" |
@@ -23,9 +26,13 @@ using content::BrowserThread; |
namespace { |
-const char kSystemInfoEventPrefix[] = "experimental.systemInfo"; |
-const char kStorageEventPrefix[] = "experimental.systemInfo.storage"; |
+const char kEventDelimiter[] = "#"; |
+ |
const char kCpuEventPrefix[] = "experimental.systemInfo.cpu"; |
+const char kStorageEventPrefix[] = "experimental.systemInfo.storage"; |
+const char kSystemInfoEventPrefix[] = "experimental.systemInfo"; |
+ |
+const int kDefaultThresholdBytes = 4096; |
static bool IsStorageEvent(const std::string& event_name) { |
return event_name.compare(0, strlen(kStorageEventPrefix), |
@@ -36,6 +43,37 @@ static bool IsCpuEvent(const std::string& event_name) { |
return event_name.compare(0, strlen(kCpuEventPrefix), kCpuEventPrefix) == 0; |
} |
+// Marshal the storage event name with the extra parameters. Returns the |
+// marshalled event name with the following format: |
+// event_names::kOnStorageChanged#<id>#threshold |
+std::string GetMarshalledStorageEventName(const std::string& id, |
+ int threshold) { |
+ std::string event_name(event_names::kOnStorageChanged); |
+ event_name += kEventDelimiter + id; |
+ event_name += kEventDelimiter + base::IntToString(threshold); |
+ return event_name; |
+} |
+ |
+// Unmarshall the event name to extract the storage id and the change |
+// threshold value. The |storage_id| and |threshold| parameters hold |
+// the results respectively. |
+void UnmarshallStorageEventName(const std::string& event_name, |
+ std::string* id, |
+ int* threshold) { |
+ std::vector<std::string> parts; |
+ size_t num = Tokenize(event_name, std::string(kEventDelimiter), &parts); |
+ DCHECK(num == 3u); |
+ DCHECK(parts[0] == event_names::kOnStorageChanged); |
+ |
+ *id = parts[1]; |
+ *threshold = 0; |
+ bool result = base::StringToInt(parts[2], threshold); |
+ DCHECK(result); |
+ |
+ if (!result) |
+ *threshold = kDefaultThresholdBytes; |
+} |
+ |
} // namespace |
// static |
@@ -44,23 +82,27 @@ SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
} |
SystemInfoEventRouter::SystemInfoEventRouter() { |
+ StorageInfoProvider::Get()->AddObserver(this); |
} |
SystemInfoEventRouter::~SystemInfoEventRouter() { |
+ StorageInfoProvider::Get()->RemoveObserver(this); |
} |
void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
- watching_event_set_.insert(event_name); |
- if (watching_event_set_.count(event_name) > 1) |
- return; |
- // Start watching the |event_name| event if the first event listener arrives. |
- // For systemInfo.storage event. |
if (IsStorageEvent(event_name)) { |
- // TODO (hongbo): Start watching storage device information via calling |
- // SystemMonitor::StartWatchingStorage. |
+ std::string storage_id; |
+ int threshold = 0; |
+ UnmarshallStorageEventName(event_name, &storage_id, &threshold); |
+ // StorageInfoProvider can handle the duplicated storage to be watched. |
+ StorageInfoProvider::Get()->StartWatching(storage_id, threshold); |
return; |
} |
+ watching_event_set_.insert(event_name); |
+ if (watching_event_set_.count(event_name) > 1) |
+ return; |
+ |
// For systemInfo.cpu event. |
if (IsCpuEvent(event_name)) { |
CpuInfoProvider::Get()->StartSampling( |
@@ -70,19 +112,21 @@ void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
} |
} |
-void SystemInfoEventRouter::RemoveEventListener( |
- const std::string& event_name) { |
+void SystemInfoEventRouter::RemoveEventListener(const std::string& event_name) { |
+ if (IsStorageEvent(event_name)) { |
+ std::string storage_id; |
+ int threshold = 0; |
+ UnmarshallStorageEventName(event_name, &storage_id, &threshold); |
+ StorageInfoProvider::Get()->StopWatching(storage_id, threshold); |
+ return; |
+ } |
+ |
watching_event_set_.erase(event_name); |
if (watching_event_set_.count(event_name) > 0) |
return; |
// In case of the last event listener is removed, we need to stop watching |
// it to avoid unnecessary overhead. |
- if (IsStorageEvent(event_name)) { |
- // TODO(hongbo): Stop watching storage device information via calling |
- // SystemMonitor::StopWatchingStorage. |
- return; |
- } |
if (IsCpuEvent(event_name)) { |
CpuInfoProvider::Get()->StopSampling(); |
} |
@@ -94,24 +138,29 @@ bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
return event_name.compare(0, prefix.size(), prefix) == 0; |
} |
-void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( |
- const std::string& id, int64 available_capacity) { |
+void SystemInfoEventRouter::OnStorageFreeSpaceChanged(const std::string& id, |
+ double old_value, |
+ double new_value, |
+ int threshold) { |
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
- base::Bind(&SystemInfoEventRouter::OnStorageAvailableCapacityChanged, |
- base::Unretained(this), id, available_capacity)); |
+ base::Bind(&SystemInfoEventRouter::OnStorageFreeSpaceChanged, |
+ base::Unretained(this), |
+ id, old_value, new_value, threshold)); |
return; |
} |
// We are on the UI thread now. |
StorageChangeInfo info; |
info.id = id; |
- info.available_capacity = static_cast<double>(available_capacity); |
- |
+ info.available_capacity = new_value; |
scoped_ptr<base::ListValue> args(new base::ListValue()); |
args->Append(info.ToValue().release()); |
- DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); |
+ std::string event_name = |
+ GetMarshalledStorageEventName(id, threshold); |
+ |
+ DispatchEvent(event_name, args.Pass()); |
} |
void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id, |