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

Unified Diff: chrome/browser/file_system/entry_watcher_service.h

Issue 452043003: [ew] Add basic classes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Uploaded. Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/file_system/entry_watcher_service.h
diff --git a/chrome/browser/file_system/entry_watcher_service.h b/chrome/browser/file_system/entry_watcher_service.h
new file mode 100644
index 0000000000000000000000000000000000000000..63477c794adf017262eee2ea8050d239968a5881
--- /dev/null
+++ b/chrome/browser/file_system/entry_watcher_service.h
@@ -0,0 +1,124 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_
+#define CHROME_BROWSER_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include "base/memory/singleton.h"
+#include "base/memory/weak_ptr.h"
+#include "base/scoped_observer.h"
+#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
+#include "components/keyed_service/core/keyed_service.h"
+#include "webkit/browser/fileapi/file_system_url.h"
+#include "webkit/browser/fileapi/watcher_manager.h"
+
+namespace content {
+class BrowserContext;
+} // namespace content
+
+namespace extensions {
+struct Event;
+class EventRouter;
+} // namespace extensions
+
+// Watches entries (files and directories) for changes. Created per profile.
+// TODO(mtomasz): Add support for watching files.
+class EntryWatcherService : public KeyedService,
+ public fileapi::WatcherManager::Observer {
+ public:
+ typedef base::Callback<void(const std::string& extension_id,
+ scoped_ptr<extensions::Event> event)>
+ DispatchEventImplCallback;
+
+ typedef base::Callback<fileapi::FileSystemContext*(
+ const std::string& extension_id,
+ content::BrowserContext* context)> GetFileSystemContextImplCallback;
+
+ typedef base::Callback<void(std::vector<fileapi::FileSystemURL>)>
+ GetWatchedEntriesCallback;
+
+ explicit EntryWatcherService(content::BrowserContext* context);
+ virtual ~EntryWatcherService();
+
+ // Watches a directory. Only one watcher can be set per the same |url| and
+ // |extension_id|.
+ void WatchDirectory(const std::string& extension_id,
+ const fileapi::FileSystemURL& url,
+ bool recursive,
+ const fileapi::WatcherManager::StatusCallback& callback);
+
+ // Unwatches an entry (file or directory).
+ void UnwatchEntry(const std::string& extension_id,
+ const fileapi::FileSystemURL& url,
+ const fileapi::WatcherManager::StatusCallback& callback);
+
+ std::vector<fileapi::FileSystemURL> GetWatchedEntries(
+ const std::string& extension_id);
+
+ // fileapi::WatcherManager::Observer overrides.
+ virtual void OnEntryChanged(const fileapi::FileSystemURL& url) OVERRIDE;
+ virtual void OnEntryRemoved(const fileapi::FileSystemURL& url) OVERRIDE;
+
+ // Sets a custom dispatcher for tests in order to be able to verify dispatched
+ // events.
+ void SetDispatchEventImplForTesting(
+ const DispatchEventImplCallback& callback);
+
+ // Sets a custom context getter for tests in order to inject a testing
+ // file system context implementation.
+ void SetGetFileSystemContextImplForTesting(
+ const GetFileSystemContextImplCallback& callback);
+
+ private:
+ // Holds information about an entry watcher.
+ struct EntryWatcher {
+ EntryWatcher();
+ EntryWatcher(const fileapi::FileSystemURL& url,
+ bool directory,
+ bool recursive);
+ ~EntryWatcher();
+
+ fileapi::FileSystemURL url;
+ bool directory;
+ bool recursive;
+ };
+
+ // Map from a file system url to a map from an extension id to an entry
+ // watcher descriptor. Note, that GURL is used instead of fileapi::
+ // FileSystemURL, since the second one doesn't work well with stl containers.
+ typedef std::map<GURL, std::map<std::string, EntryWatcher> > WatcherMap;
+
+ // Called when adding a directory watcher is completed with either a success
+ // or an error.
+ void OnWatchDirectoryCompleted(
+ const std::string& extension_id,
+ const fileapi::FileSystemURL& url,
+ bool recursive,
+ const fileapi::WatcherManager::StatusCallback& callback,
+ base::File::Error result);
+
+ // Called when removing a watcher is completed with either a success or an
+ // error.
+ void OnUnwatchEntryCompleted(
+ const std::string& extension_id,
+ const fileapi::FileSystemURL& url,
+ const fileapi::WatcherManager::StatusCallback& callback,
+ base::File::Error result);
+
+ content::BrowserContext* context_;
+ WatcherMap watchers_;
+ DispatchEventImplCallback dispatch_event_impl_;
+ GetFileSystemContextImplCallback get_file_system_context_impl_;
+ ScopedObserver<fileapi::WatcherManager, fileapi::WatcherManager::Observer>
+ observing_;
+ base::WeakPtrFactory<EntryWatcherService> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(EntryWatcherService);
+};
+
+#endif // CHROME_BROWSER_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_

Powered by Google App Engine
This is Rietveld 408576698