diff options
author | mtomasz <mtomasz@chromium.org> | 2014-08-27 22:37:34 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-08-28 05:38:33 +0000 |
commit | b75244fdf1d75e67485649e6a96a69c50676bbc3 (patch) | |
tree | 447e6d0b48e058ac28e5374dc9532bf4237f9302 /content/public | |
parent | eacf155f2d41b79a64c9b4eca997e62ebc77b8cb (diff) | |
download | chromium_src-b75244fdf1d75e67485649e6a96a69c50676bbc3.zip chromium_src-b75244fdf1d75e67485649e6a96a69c50676bbc3.tar.gz chromium_src-b75244fdf1d75e67485649e6a96a69c50676bbc3.tar.bz2 |
[ew] Add basic classes.
This patch adds a EntryWatcherService, which is a bridge between extensions and
fileapi. Also, WatcherManager interface has been created to let backends
implement their own watching logic.
Note, that EntryWatcherService is not wired up to File System API yet. Also,
a lot of features are missing.
TBR=noamsml, jcivelli
TEST=unit_tests: *EntryWatcherService*
BUG=261491
Review URL: https://codereview.chromium.org/452043003
Cr-Commit-Position: refs/heads/master@{#292327}
Diffstat (limited to 'content/public')
-rw-r--r-- | content/public/test/test_file_system_backend.cc | 107 | ||||
-rw-r--r-- | content/public/test/test_file_system_backend.h | 3 |
2 files changed, 110 insertions, 0 deletions
diff --git a/content/public/test/test_file_system_backend.cc b/content/public/test/test_file_system_backend.cc index f48a034..47f48d4 100644 --- a/content/public/test/test_file_system_backend.cc +++ b/content/public/test/test_file_system_backend.cc @@ -9,7 +9,11 @@ #include <vector> #include "base/file_util.h" +#include "base/files/file.h" +#include "base/memory/weak_ptr.h" +#include "base/observer_list.h" #include "base/sequenced_task_runner.h" +#include "base/thread_task_runner_handle.h" #include "webkit/browser/blob/file_stream_reader.h" #include "webkit/browser/fileapi/copy_or_move_file_validator.h" #include "webkit/browser/fileapi/file_observers.h" @@ -20,6 +24,7 @@ #include "webkit/browser/fileapi/native_file_util.h" #include "webkit/browser/fileapi/quota/quota_reservation.h" #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" +#include "webkit/browser/fileapi/watcher_manager.h" #include "webkit/browser/quota/quota_manager.h" #include "webkit/common/fileapi/file_system_util.h" @@ -32,6 +37,7 @@ namespace content { namespace { +// Stub implementation of storage::LocalFileUtil. class TestFileUtil : public storage::LocalFileUtil { public: explicit TestFileUtil(const base::FilePath& base_path) @@ -51,6 +57,101 @@ class TestFileUtil : public storage::LocalFileUtil { base::FilePath base_path_; }; +// Stub implementation of storage::WatcherManager. Emits a fake notification +// after a directory watcher is set successfully. +class TestWatcherManager : public storage::WatcherManager { + public: + TestWatcherManager() : weak_ptr_factory_(this) {} + virtual ~TestWatcherManager() {} + + // storage::WatcherManager overrides. + virtual void AddObserver(Observer* observer) OVERRIDE { + observers_.AddObserver(observer); + } + + virtual void RemoveObserver(Observer* observer) OVERRIDE { + observers_.RemoveObserver(observer); + } + + virtual bool HasObserver(Observer* observer) const OVERRIDE { + return observers_.HasObserver(observer); + } + + virtual void WatchDirectory(const storage::FileSystemURL& url, + bool recursive, + const StatusCallback& callback) OVERRIDE { + if (recursive) { + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, + base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); + return; + } + + const GURL gurl = url.ToGURL(); + if (watched_urls_.find(gurl) != watched_urls_.end()) { + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_EXISTS)); + return; + } + + watched_urls_.insert(gurl); + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, base::Bind(callback, base::File::FILE_OK)); + + // Send a fake changed notification. + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, + base::Bind(&TestWatcherManager::SendFakeChangeNotification, + weak_ptr_factory_.GetWeakPtr(), + url)); + + // Send a fake removed notification. + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, + base::Bind(&TestWatcherManager::SendFakeRemoveNotification, + weak_ptr_factory_.GetWeakPtr(), + url)); + } + + virtual void UnwatchEntry(const storage::FileSystemURL& url, + const StatusCallback& callback) OVERRIDE { + const GURL gurl = url.ToGURL(); + if (watched_urls_.find(gurl) == watched_urls_.end()) { + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); + return; + } + + watched_urls_.erase(gurl); + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, base::Bind(callback, base::File::FILE_OK)); + } + + private: + // Sends a fake notification to each observer about a changed entry + // represented by |url|, as long as it is still being watched. + void SendFakeChangeNotification(const storage::FileSystemURL& url) { + if (watched_urls_.find(url.ToGURL()) == watched_urls_.end()) + return; + + FOR_EACH_OBSERVER(Observer, observers_, OnEntryChanged(url)); + } + + // Sends a fake notification to each observer about a removed entry + // represented by |url|, as long as it is still being watched. + void SendFakeRemoveNotification(const storage::FileSystemURL& url) { + if (watched_urls_.find(url.ToGURL()) == watched_urls_.end()) + return; + + FOR_EACH_OBSERVER(Observer, observers_, OnEntryRemoved(url)); + } + + ObserverList<Observer> observers_; + std::set<GURL> watched_urls_; + + base::WeakPtrFactory<TestWatcherManager> weak_ptr_factory_; +}; + } // namespace // This only supports single origin. @@ -162,6 +263,7 @@ TestFileSystemBackend::TestFileSystemBackend( : base_path_(base_path), file_util_( new storage::AsyncFileUtilAdapter(new TestFileUtil(base_path))), + watcher_manager_(new TestWatcherManager()), quota_util_(new QuotaUtil(task_runner)), require_copy_or_move_validator_(false) { } @@ -189,6 +291,11 @@ storage::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil( return file_util_.get(); } +storage::WatcherManager* TestFileSystemBackend::GetWatcherManager( + storage::FileSystemType type) { + return watcher_manager_.get(); +} + storage::CopyOrMoveFileValidatorFactory* TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory( storage::FileSystemType type, diff --git a/content/public/test/test_file_system_backend.h b/content/public/test/test_file_system_backend.h index 2c80e62..bcea543 100644 --- a/content/public/test/test_file_system_backend.h +++ b/content/public/test/test_file_system_backend.h @@ -41,6 +41,8 @@ class TestFileSystemBackend : public storage::FileSystemBackend { const OpenFileSystemCallback& callback) OVERRIDE; virtual storage::AsyncFileUtil* GetAsyncFileUtil( storage::FileSystemType type) OVERRIDE; + virtual storage::WatcherManager* GetWatcherManager( + storage::FileSystemType type) OVERRIDE; virtual storage::CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory(storage::FileSystemType type, base::File::Error* error_code) OVERRIDE; @@ -85,6 +87,7 @@ class TestFileSystemBackend : public storage::FileSystemBackend { base::FilePath base_path_; scoped_refptr<base::SequencedTaskRunner> task_runner_; scoped_ptr<storage::AsyncFileUtilAdapter> file_util_; + scoped_ptr<storage::WatcherManager> watcher_manager_; scoped_ptr<QuotaUtil> quota_util_; bool require_copy_or_move_validator_; |