Chromium Code Reviews| 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> | |
|
benwells
2014/08/13 05:50:47
Nit: <set> doesn't appear to be used.
mtomasz
2014/08/13 07:02:09
Done.
| |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/memory/singleton.h" | |
|
benwells
2014/08/13 05:50:47
Super nit: you can forward declare "template <type
mtomasz
2014/08/13 07:02:09
Our style guide says that for class templates, #in
benwells
2014/08/14 05:16:32
Ah, good to know.
| |
| 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 namespace content { | |
| 22 class BrowserContext; | |
| 23 } // namespace content | |
| 24 | |
| 25 namespace extensions { | |
|
benwells
2014/08/13 05:50:47
If this isn't in the extensions folder it shouldn'
mtomasz
2014/08/13 07:02:09
Good point. It was there in the past, but I moved
| |
| 26 | |
| 27 struct Event; | |
| 28 class EventRouter; | |
| 29 | |
| 30 // Watches entries (files and directories) for changes. Created per profile. | |
| 31 // TODO(mtomasz): Add support for watching files. | |
| 32 class EntryWatcherService : public KeyedService, | |
| 33 public fileapi::WatcherManager::Observer { | |
| 34 public: | |
| 35 typedef base::Callback< | |
| 36 bool(const std::string& extension_id, scoped_ptr<Event> event)> | |
| 37 DispatchEventImplCallback; | |
| 38 | |
| 39 typedef base::Callback<fileapi::FileSystemContext*( | |
| 40 const std::string& extension_id, | |
| 41 content::BrowserContext* context)> GetFileSystemContextImplCallback; | |
| 42 | |
| 43 typedef base::Callback<void(std::vector<fileapi::FileSystemURL>)> | |
| 44 GetWatchedEntriesCallback; | |
|
tzik
2014/08/13 09:21:49
Not used? / Can this argument be const ref?
benwells
2014/08/18 22:58:32
^^^
mtomasz
2014/08/19 06:50:36
Done. Missed this one.
| |
| 45 | |
| 46 explicit EntryWatcherService(content::BrowserContext* context); | |
| 47 virtual ~EntryWatcherService(); | |
| 48 | |
| 49 // Watches a directory. Only one watcher can be set per the same |url| and | |
| 50 // |extension_id|. | |
| 51 void WatchDirectory(const std::string& extension_id, | |
| 52 const fileapi::FileSystemURL& url, | |
| 53 bool recursive, | |
| 54 const fileapi::WatcherManager::StatusCallback& callback); | |
| 55 | |
| 56 // Unwatches an entry (file or directory). | |
| 57 void UnwatchEntry(const std::string& extension_id, | |
| 58 const fileapi::FileSystemURL& url, | |
| 59 const fileapi::WatcherManager::StatusCallback& callback); | |
| 60 | |
| 61 std::vector<fileapi::FileSystemURL> GetWatchedEntries( | |
| 62 const std::string& extension_id); | |
| 63 | |
| 64 // fileapi::WatcherManager::Observer overrides. | |
| 65 virtual void OnEntryChanged(const fileapi::FileSystemURL& url) OVERRIDE; | |
| 66 virtual void OnEntryRemoved(const fileapi::FileSystemURL& url) OVERRIDE; | |
| 67 | |
| 68 // Sets a custom dispatcher for tests in order to be able to verify dispatched | |
| 69 // events. | |
| 70 void SetDispatchEventImplForTesting( | |
| 71 const DispatchEventImplCallback& callback); | |
| 72 | |
| 73 // Sets a custom context getter for tests in order to inject a testing | |
| 74 // file system context implementation. | |
| 75 void SetGetFileSystemContextImplForTesting( | |
| 76 const GetFileSystemContextImplCallback& callback); | |
| 77 | |
| 78 private: | |
| 79 // Holds an information about an entry watcher. | |
|
benwells
2014/08/13 05:50:47
Nit: Just "Holds information"
mtomasz
2014/08/13 07:02:09
Done.
| |
| 80 struct EntryWatcher { | |
| 81 EntryWatcher(); | |
| 82 EntryWatcher(const fileapi::FileSystemURL& url, | |
| 83 bool directory, | |
| 84 bool recursive); | |
| 85 ~EntryWatcher(); | |
| 86 | |
| 87 fileapi::FileSystemURL url; | |
|
benwells
2014/08/13 05:50:47
Nit: Could these fields all be const? Or do you ch
mtomasz
2014/08/13 07:02:09
We don't modify them directly, but with const they
| |
| 88 bool directory; | |
| 89 bool recursive; | |
| 90 }; | |
| 91 | |
| 92 typedef std::map<GURL, std::map<std::string, EntryWatcher> > WatcherMap; | |
|
tzik
2014/08/13 09:21:49
Can this be FileSystemURL with FileSystemURL::Comp
mtomasz
2014/08/19 06:50:36
Good point. Done.
| |
| 93 | |
| 94 // Called when adding a directory watcher is completed with either a success | |
| 95 // or an error. | |
| 96 void OnWatchDirectoryCompleted( | |
| 97 const std::string& extension_id, | |
| 98 const fileapi::FileSystemURL& url, | |
| 99 bool recursive, | |
| 100 const fileapi::WatcherManager::StatusCallback& callback, | |
| 101 base::File::Error result); | |
| 102 | |
| 103 // Called when removing a watcher is completed with either a success or an | |
| 104 // error. | |
| 105 void OnUnwatchEntryCompleted( | |
| 106 const std::string& extension_id, | |
| 107 const fileapi::FileSystemURL& url, | |
| 108 const fileapi::WatcherManager::StatusCallback& callback, | |
| 109 base::File::Error result); | |
| 110 | |
| 111 content::BrowserContext* context_; | |
| 112 WatcherMap watchers_; | |
|
benwells
2014/08/13 05:50:47
Nit: Could you comment (here or at the typedef) wh
mtomasz
2014/08/13 07:02:09
Done.
| |
| 113 DispatchEventImplCallback dispatch_event_impl_; | |
| 114 GetFileSystemContextImplCallback get_file_system_context_impl_; | |
| 115 ScopedObserver<fileapi::WatcherManager, fileapi::WatcherManager::Observer> | |
| 116 observing_; | |
| 117 base::WeakPtrFactory<EntryWatcherService> weak_ptr_factory_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(EntryWatcherService); | |
| 120 }; | |
| 121 | |
| 122 class EntryWatcherServiceFactory : public BrowserContextKeyedServiceFactory { | |
|
benwells
2014/08/13 05:50:47
This isn't always done, but often factories are ke
mtomasz
2014/08/13 07:02:09
Done.
| |
| 123 public: | |
| 124 // Returns a service instance singleton, after creating it (if necessary). | |
|
benwells
2014/08/13 05:50:47
Nit: it isn't a singleton.
mtomasz
2014/08/13 07:02:09
Singleton per context?
| |
| 125 static EntryWatcherService* Get(content::BrowserContext* context); | |
| 126 | |
| 127 // Returns a service instance for the context if exists. Otherwise, returns | |
| 128 // NULL. | |
| 129 static EntryWatcherService* FindExisting(content::BrowserContext* context); | |
| 130 | |
| 131 // Gets a singleton instance of the factory. | |
| 132 static EntryWatcherServiceFactory* GetInstance(); | |
| 133 | |
| 134 private: | |
| 135 friend struct DefaultSingletonTraits<EntryWatcherServiceFactory>; | |
| 136 | |
| 137 EntryWatcherServiceFactory(); | |
| 138 virtual ~EntryWatcherServiceFactory(); | |
| 139 | |
| 140 // BrowserContextKeyedServiceFactory overrides: | |
| 141 virtual KeyedService* BuildServiceInstanceFor( | |
| 142 content::BrowserContext* profile) const OVERRIDE; | |
| 143 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(EntryWatcherServiceFactory); | |
| 146 }; | |
| 147 | |
| 148 } // namespace extensions | |
| 149 | |
| 150 #endif // CHROME_BROWSER_FILE_SYSTEM_ENTRY_WATCHER_SERVICE_H_ | |
| OLD | NEW |