OLD | NEW |
---|---|
(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_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_ | |
6 #define CHROME_BROWSER_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/memory/singleton.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/scoped_observer.h" | |
16 #include "components/keyed_service/content/browser_context_keyed_service_factory .h" | |
17 #include "components/keyed_service/core/keyed_service.h" | |
18 #include "webkit/browser/fileapi/file_system_url.h" | |
19 #include "webkit/browser/fileapi/watcher_manager.h" | |
20 | |
21 class Profile; | |
22 | |
23 namespace extensions { | |
24 | |
25 struct Event; | |
26 class EventRouter; | |
27 | |
28 // Watches entries (files and directories) for changes. Created per profile. | |
29 // TODO(mtomasz): Add support for watching files. | |
30 class EntryWatcherService : public KeyedService, | |
31 public fileapi::WatcherManager::Observer { | |
32 public: | |
33 typedef base::Callback< | |
34 bool(const std::string& extension_id, scoped_ptr<Event> event)> | |
35 DispatchEventImplCallback; | |
36 | |
37 typedef base::Callback< | |
38 fileapi::FileSystemContext*(const std::string& extension_id, | |
39 Profile* profile)> GetContextImplCallback; | |
40 | |
41 typedef base::Callback<void(std::vector<fileapi::FileSystemURL>)> | |
42 GetWatchedEntriesCallback; | |
43 | |
44 explicit EntryWatcherService(Profile* profile); | |
45 virtual ~EntryWatcherService(); | |
46 | |
47 // Watches a directory. Only one watcher can be set per the same |url| and | |
48 // |extension_id|. | |
49 void WatchDirectory(const std::string& extension_id, | |
50 const fileapi::FileSystemURL& url, | |
51 bool recursive, | |
52 const fileapi::WatcherManager::StatusCallback& callback); | |
53 | |
54 // Unwatches an entry (file or directory). | |
55 void UnwatchEntry(const std::string& extension_id, | |
56 const fileapi::FileSystemURL& url, | |
57 const fileapi::WatcherManager::StatusCallback& callback); | |
58 | |
59 std::vector<fileapi::FileSystemURL> GetWatchedEntries( | |
60 const std::string& extension_id); | |
61 | |
62 // fileapi::WatcherManager::Observer overrides. | |
63 virtual void OnEntryChanged(const fileapi::FileSystemURL& url) OVERRIDE; | |
64 virtual void OnEntryRemoved(const fileapi::FileSystemURL& url) OVERRIDE; | |
65 | |
66 // Sets a custom dispatcher for tests in order to be able to verify dispatched | |
67 // events. | |
68 void SetDispatchEventImplForTesting( | |
69 const DispatchEventImplCallback& callback); | |
70 | |
71 // Sets a custom context getter for tests in order to inject a testing | |
72 // file system context implementation. | |
73 void SetGetContextImplForTesting(const GetContextImplCallback& callback); | |
74 | |
75 private: | |
76 // Holds an information about an entry watcher. | |
77 struct EntryWatcher { | |
78 public: | |
tzik
2014/08/12 10:40:46
not needed?
mtomasz
2014/08/13 03:08:44
Done.
| |
79 EntryWatcher(); | |
80 EntryWatcher(const fileapi::FileSystemURL& url, | |
81 bool directory, | |
82 bool recursive); | |
83 ~EntryWatcher(); | |
84 | |
85 fileapi::FileSystemURL url; | |
86 bool directory; | |
87 bool recursive; | |
88 }; | |
89 | |
90 typedef std::map<GURL, std::map<std::string, EntryWatcher> > WatcherMap; | |
91 | |
92 // Called when adding a directory watcher is completed with either a success | |
93 // or an error. | |
94 void OnWatchDirectoryCompleted( | |
95 const std::string& extension_id, | |
96 const fileapi::FileSystemURL& url, | |
97 bool recursive, | |
98 const fileapi::WatcherManager::StatusCallback& callback, | |
99 base::File::Error result); | |
100 | |
101 // Called when removing a watcher is completed with either a success or an | |
102 // error. | |
103 void OnUnwatchEntryCompleted( | |
104 const std::string& extension_id, | |
105 const fileapi::FileSystemURL& url, | |
106 const fileapi::WatcherManager::StatusCallback& callback, | |
107 base::File::Error result); | |
108 | |
109 Profile* profile_; | |
tzik
2014/08/12 10:40:46
All usage of this |profile_| is as a BrowserContex
mtomasz
2014/08/13 03:08:44
Done.
| |
110 WatcherMap watchers_; | |
111 DispatchEventImplCallback dispatch_event_impl_; | |
112 GetContextImplCallback get_context_impl_; | |
113 ScopedObserver<fileapi::WatcherManager, fileapi::WatcherManager::Observer> | |
114 observing_; | |
115 base::WeakPtrFactory<EntryWatcherService> weak_ptr_factory_; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(EntryWatcherService); | |
118 }; | |
119 | |
120 class EntryWatcherServiceFactory : public BrowserContextKeyedServiceFactory { | |
121 public: | |
122 // Returns a service instance singleton, after creating it (if necessary). | |
123 static EntryWatcherService* Get(content::BrowserContext* context); | |
124 | |
125 // Returns a service instance for the context if exists. Otherwise, returns | |
126 // NULL. | |
127 static EntryWatcherService* FindExisting(content::BrowserContext* context); | |
128 | |
129 // Gets a singleton instance of the factory. | |
130 static EntryWatcherServiceFactory* GetInstance(); | |
131 | |
132 private: | |
133 friend struct DefaultSingletonTraits<EntryWatcherServiceFactory>; | |
134 | |
135 EntryWatcherServiceFactory(); | |
136 virtual ~EntryWatcherServiceFactory(); | |
137 | |
138 // BrowserContextKeyedServiceFactory overrides: | |
139 virtual KeyedService* BuildServiceInstanceFor( | |
140 content::BrowserContext* profile) const OVERRIDE; | |
141 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(EntryWatcherServiceFactory); | |
144 }; | |
145 | |
146 } // namespace extensions | |
147 | |
148 #endif // CHROME_BROWSER_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_ | |
OLD | NEW |