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

Unified Diff: chrome/browser/extensions/api/system_info_storage/storage_info_provider.h

Issue 11419279: Implement systemInfo.storage.onChanged event (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: refine browser test and code comments Created 8 years 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/system_info_storage/storage_info_provider.h
diff --git a/chrome/browser/extensions/api/system_info_storage/storage_info_provider.h b/chrome/browser/extensions/api/system_info_storage/storage_info_provider.h
index 4f1b0efbf3937c2a9b05cae4fec14c19de3d1170..dc839d3db2090afdfe17545c1d51c36a99a57f60 100644
--- a/chrome/browser/extensions/api/system_info_storage/storage_info_provider.h
+++ b/chrome/browser/extensions/api/system_info_storage/storage_info_provider.h
@@ -4,11 +4,10 @@
#ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_STORAGE_STORAGE_INFO_PROVIDER_H_
#define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_STORAGE_STORAGE_INFO_PROVIDER_H_
-#include <set>
-
#include "base/memory/ref_counted.h"
#include "base/observer_list_threadsafe.h"
#include "base/timer.h"
+#include "chrome/browser/extensions/storage_info_observer.h"
#include "chrome/browser/extensions/system_info_provider.h"
#include "chrome/common/extensions/api/experimental_system_info_storage.h"
#include "content/public/browser/notification_observer.h"
@@ -37,31 +36,26 @@ class StorageInfoProvider
: public content::NotificationObserver,
public SystemInfoProvider<StorageInfo> {
public:
- class Observer {
- public:
- virtual ~Observer() {}
-
- // Called from FILE thread when the storage free space changes.
- virtual void OnStorageFreeSpaceChanged(const std::string& id,
- double old_value,
- double new_value) = 0;
- };
-
virtual ~StorageInfoProvider();
// Get the single shared instance of StorageInfoProvider.
static StorageInfoProvider* Get();
// Add and remove observer, both can be called from any thread.
- void AddObserver(Observer* obs);
- void RemoveObserver(Observer* obs);
+ void AddObserver(StorageInfoObserver* obs);
+ void RemoveObserver(StorageInfoObserver* obs);
- // Start and stop watching the given storage |id|.
- virtual void StartWatching(const std::string& id);
- virtual void StopWatching(const std::string& id);
+ // Start watching |id| with the given |threshold|. The |observers_| receives
+ // notification only if the free space change for the storage |id| is greater
+ // than |threshold| bytes.
+ virtual void StartWatching(const std::string& id, int threshold);
+
+ // Stop watching the storage with the given |threshold| value. Nothing to do
+ // if no matched watcher is found.
+ virtual void StopWatching(const std::string& id, int threshold);
// Set the watching time interval.
- void set_watching_interval(unsigned int ms) { watching_interval_ = ms; }
+ void set_watching_interval(int ms) { watching_interval_ = ms; }
// Get the information for the storage unit specified by the |id| parameter,
// and output the result to the |info|.
@@ -72,29 +66,56 @@ class StorageInfoProvider
StorageInfoProvider();
private:
- typedef std::map<std::string, double> StorageIDToSizeMap;
+ class FreeSpaceWatcher {
benwells 2012/12/11 04:40:40 May as well be a struct.
+ public:
+ FreeSpaceWatcher(const std::string& id, int threshold)
+ : id(id), threshold(threshold), recent_free_space(0.0) {
+ }
+ // The storage id being watched.
+ std::string id;
benwells 2012/12/11 04:40:40 Is the id needed in this class / struct?
+ // The threshold for free space change.
+ int threshold;
+ // The available capacity value got from the recent querying operation.
+ double recent_free_space;
benwells 2012/12/11 04:40:40 I think this is actually the last free space repor
+ };
+ typedef std::vector<FreeSpaceWatcher> FreeSpaceWatchers;
+ typedef std::map<std::string, FreeSpaceWatchers> StorageIDToWatcherMap;
// content::NotificationObserver implementation.
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
- // Start watching the given storage |id| on FILE thread.
- void StartWatchingOnFileThread(const std::string& id);
+ // Start watching the given storage |id| on FILE thread. The |threshold|
+ // parameter indicates that the |observers_| will receive notification only
+ // if the free space change delta is equal to or greater than the specified
+ // |threshold| value.
+ void StartWatchingOnFileThread(const std::string& id, int threshold);
- // Stop the watching the the given storage |id| on FILE thread.
- void StopWatchingOnFileThread(const std::string& id);
+ // Stop the watching the the given storage |id| with the |threshold|. Nothing
+ // to do if no matched wather is found. Called on FILE thread
+ void StopWatchingOnFileThread(const std::string& id, int threshold);
// Check if the free space changes for the watched storages by iterating over
- // the |storage_id_to_size_map_|. It is called on FILE thread.
+ // the |id_to_watcher_map_|. It is called on FILE thread.
void CheckWatchedStorages();
+ // Abort watching the given storage in case of any error occurs on it. It
+ // will remove all watchers on it. Called on FILE thread.
+ void AbortWatching(const std::string& id);
+
+ // Notify the observer that the storage free space has been changed.
+ // Called on FILE thread.
+ void NotifyFreeSpaceChangedIfNeeded(const std::string &id,
+ double latest_free_space);
+
// Force to destory the watching timer if the application is terminating or
// there is no any one storage to be watched. It is called on FILE thread.
void DestroyWatchingTimer();
- // Mapping of the storage being watched and the recent free space value.
- StorageIDToSizeMap storage_id_to_size_map_;
+ // Mapping of the sotrage being watched and its watchers. A storage
+ // might have more than one watcher.
+ StorageIDToWatcherMap id_to_watcher_map_;
// The timer used for watching the storage free space changes periodically.
// It lives on the FILE thread.
@@ -102,7 +123,7 @@ class StorageInfoProvider
// The thread-safe observer list that observe the changes happening on the
// storages.
- scoped_refptr<ObserverListThreadSafe<Observer> > observers_;
+ scoped_refptr<ObserverListThreadSafe<StorageInfoObserver> > observers_;
// The time interval for watching the free space change, in milliseconds.
unsigned int watching_interval_;

Powered by Google App Engine
This is Rietveld 408576698