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

Side by Side Diff: chrome/browser/extensions/api/file_system/entry_watcher_service.h

Issue 452043003: [ew] Add basic classes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleaned up. 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
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 "webkit/browser/fileapi/file_system_url.h"
18 #include "webkit/browser/fileapi/watcher_manager.h"
19
20 namespace content {
21 class BrowserContext;
22 } // namespace content
23
24 namespace fileapi {
25 class FileSystemContext;
26 } // namespace fileapi
27
28 namespace extensions {
29 struct Event;
30 class EventRouter;
31
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 fileapi::WatcherManager::Observer {
36 public:
37 typedef base::Callback<
38 void(const std::string& extension_id, scoped_ptr<Event> event)>
39 DispatchEventImplCallback;
40
41 typedef base::Callback<fileapi::FileSystemContext*(
42 const std::string& extension_id,
43 content::BrowserContext* context)> GetFileSystemContextImplCallback;
44
45 typedef base::Callback<void(std::vector<fileapi::FileSystemURL>)>
46 GetWatchedEntriesCallback;
47
48 explicit EntryWatcherService(content::BrowserContext* context);
49 virtual ~EntryWatcherService();
50
51 // Watches a directory. Only one watcher can be set per the same |url| and
52 // |extension_id|.
53 void WatchDirectory(const std::string& extension_id,
54 const fileapi::FileSystemURL& url,
55 bool recursive,
56 const fileapi::WatcherManager::StatusCallback& callback);
57
58 // Unwatches an entry (file or directory).
59 void UnwatchEntry(const std::string& extension_id,
60 const fileapi::FileSystemURL& url,
61 const fileapi::WatcherManager::StatusCallback& callback);
62
63 std::vector<fileapi::FileSystemURL> GetWatchedEntries(
64 const std::string& extension_id);
65
66 // fileapi::WatcherManager::Observer overrides.
67 virtual void OnEntryChanged(const fileapi::FileSystemURL& url) OVERRIDE;
68 virtual void OnEntryRemoved(const fileapi::FileSystemURL& url) OVERRIDE;
69
70 // Sets a custom dispatcher for tests in order to be able to verify dispatched
71 // events.
72 void SetDispatchEventImplForTesting(
73 const DispatchEventImplCallback& callback);
74
75 // Sets a custom context getter for tests in order to inject a testing
76 // file system context implementation.
77 void SetGetFileSystemContextImplForTesting(
78 const GetFileSystemContextImplCallback& callback);
79
80 private:
81 // Holds information about an entry watcher.
82 struct EntryWatcher {
83 EntryWatcher();
84 EntryWatcher(const fileapi::FileSystemURL& url,
85 bool directory,
86 bool recursive);
87 ~EntryWatcher();
88
89 fileapi::FileSystemURL url;
90 bool directory;
91 bool recursive;
92 };
93
94 // Map from a file system url to a map from an extension id to an entry
95 // watcher descriptor. Note, that GURL is used instead of fileapi::
96 // FileSystemURL, since the second one doesn't work well with stl containers.
97 typedef std::map<GURL, std::map<std::string, EntryWatcher> > WatcherMap;
98
99 // Called when adding a directory watcher is completed with either a success
100 // or an error.
101 void OnWatchDirectoryCompleted(
102 fileapi::WatcherManager* watcher_manager,
103 const std::string& extension_id,
104 const fileapi::FileSystemURL& url,
105 bool recursive,
106 const fileapi::WatcherManager::StatusCallback& callback,
107 base::File::Error result);
108
109 // Called when removing a watcher is completed with either a success or an
110 // error.
111 void OnUnwatchEntryCompleted(
112 const std::string& extension_id,
113 const fileapi::FileSystemURL& url,
114 const fileapi::WatcherManager::StatusCallback& callback,
115 base::File::Error result);
116
117 content::BrowserContext* context_;
118 WatcherMap watchers_;
119 DispatchEventImplCallback dispatch_event_impl_;
120 GetFileSystemContextImplCallback get_file_system_context_impl_;
121 ScopedObserver<fileapi::WatcherManager, fileapi::WatcherManager::Observer>
122 observing_;
123 base::WeakPtrFactory<EntryWatcherService> weak_ptr_factory_;
124
125 DISALLOW_COPY_AND_ASSIGN(EntryWatcherService);
126 };
127
128 } // namespace extensions
129
130 #endif // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698