1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_
12 #include "base/memory/singleton.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "storage/browser/fileapi/file_system_url.h"
18 #include "storage/browser/fileapi/watcher_manager.h"
22 } // namespace content
25 class FileSystemContext;
26 } // namespace storage
28 namespace extensions {
32 // Watches entries (files and directories) for changes. Created per profile.
33 // TODO(mtomasz): Add support for watching files.
34 class EntryWatcherService : public KeyedService,
35 public storage::WatcherManager::Observer {
37 typedef base::Callback<
38 void(const std::string& extension_id, scoped_ptr<Event> event)>
39 DispatchEventImplCallback;
41 typedef base::Callback<storage::FileSystemContext*(
42 const std::string& extension_id,
43 content::BrowserContext* context)> GetFileSystemContextImplCallback;
45 explicit EntryWatcherService(content::BrowserContext* context);
46 virtual ~EntryWatcherService();
48 // Watches a directory. Only one watcher can be set per the same |url| and
50 void WatchDirectory(const std::string& extension_id,
51 const storage::FileSystemURL& url,
53 const storage::WatcherManager::StatusCallback& callback);
55 // Unwatches an entry (file or directory).
56 void UnwatchEntry(const std::string& extension_id,
57 const storage::FileSystemURL& url,
58 const storage::WatcherManager::StatusCallback& callback);
60 std::vector<storage::FileSystemURL> GetWatchedEntries(
61 const std::string& extension_id);
63 // storage::WatcherManager::Observer overrides.
64 virtual void OnEntryChanged(const storage::FileSystemURL& url) OVERRIDE;
65 virtual void OnEntryRemoved(const storage::FileSystemURL& url) OVERRIDE;
67 // Sets a custom dispatcher for tests in order to be able to verify dispatched
69 void SetDispatchEventImplForTesting(
70 const DispatchEventImplCallback& callback);
72 // Sets a custom context getter for tests in order to inject a testing
73 // file system context implementation.
74 void SetGetFileSystemContextImplForTesting(
75 const GetFileSystemContextImplCallback& callback);
78 // Holds information about an active entry watcher.
81 EntryWatcher(const storage::FileSystemURL& url,
86 storage::FileSystemURL url;
91 // Map from a file system url to a map from an extension id to an entry
92 // watcher descriptor.
93 typedef std::map<storage::FileSystemURL,
94 std::map<std::string, EntryWatcher>,
95 storage::FileSystemURL::Comparator> WatcherMap;
97 // Called when adding a directory watcher is completed with either a success
99 void OnWatchDirectoryCompleted(
100 storage::WatcherManager* watcher_manager,
101 const std::string& extension_id,
102 const storage::FileSystemURL& url,
104 const storage::WatcherManager::StatusCallback& callback,
105 base::File::Error result);
107 // Called when removing a watcher is completed with either a success or an
109 void OnUnwatchEntryCompleted(
110 const std::string& extension_id,
111 const storage::FileSystemURL& url,
112 const storage::WatcherManager::StatusCallback& callback,
113 base::File::Error result);
115 content::BrowserContext* context_;
116 WatcherMap watchers_;
117 DispatchEventImplCallback dispatch_event_impl_;
118 GetFileSystemContextImplCallback get_file_system_context_impl_;
119 ScopedObserver<storage::WatcherManager, storage::WatcherManager::Observer>
121 base::WeakPtrFactory<EntryWatcherService> weak_ptr_factory_;
123 DISALLOW_COPY_AND_ASSIGN(EntryWatcherService);
126 } // namespace extensions
128 #endif // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_