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_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/files/file.h" | |
14 #include "base/files/file_path.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/observer_list.h" | |
18 #include "base/threading/thread_checker.h" | |
19 #include "base/values.h" | |
20 #include "chrome/browser/chromeos/file_system_provider/observer.h" | |
21 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | |
22 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_obse
rver.h" | |
23 #include "chrome/browser/chromeos/file_system_provider/watcher.h" | |
24 #include "chrome/browser/profiles/profile.h" | |
25 #include "chrome/common/extensions/api/file_system_provider.h" | |
26 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_sy
stem_provider_capabilities_handler.h" | |
27 #include "components/keyed_service/core/keyed_service.h" | |
28 #include "content/public/browser/browser_context.h" | |
29 #include "extensions/browser/extension_registry_observer.h" | |
30 #include "extensions/common/extension.h" | |
31 #include "storage/browser/fileapi/watcher_manager.h" | |
32 | |
33 namespace extensions { | |
34 class ExtensionRegistry; | |
35 } // namespace extensions | |
36 | |
37 namespace user_prefs { | |
38 class PrefRegistrySyncable; | |
39 } // namespace user_prefs | |
40 | |
41 namespace chromeos { | |
42 namespace file_system_provider { | |
43 | |
44 class ProvidedFileSystemFactoryInterface; | |
45 class ProvidedFileSystemInfo; | |
46 class ProvidedFileSystemInterface; | |
47 class RegistryInterface; | |
48 class ServiceFactory; | |
49 struct MountOptions; | |
50 | |
51 // Registers preferences to remember registered file systems between reboots. | |
52 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
53 | |
54 // Holds information for a providing extension. | |
55 struct ProvidingExtensionInfo { | |
56 ProvidingExtensionInfo(); | |
57 ~ProvidingExtensionInfo(); | |
58 | |
59 std::string extension_id; | |
60 std::string name; | |
61 extensions::FileSystemProviderCapabilities capabilities; | |
62 }; | |
63 | |
64 // Manages and registers the file system provider service. Maintains provided | |
65 // file systems. | |
66 class Service : public KeyedService, | |
67 public extensions::ExtensionRegistryObserver, | |
68 public ProvidedFileSystemObserver { | |
69 public: | |
70 typedef base::Callback<ProvidedFileSystemInterface*( | |
71 Profile* profile, | |
72 const ProvidedFileSystemInfo& file_system_info)> | |
73 FileSystemFactoryCallback; | |
74 | |
75 // Reason for unmounting. In case of UNMOUNT_REASON_SHUTDOWN, the file system | |
76 // will be remounted automatically after a reboot. In case of | |
77 // UNMOUNT_REASON_USER it will be permanently unmounted. | |
78 enum UnmountReason { UNMOUNT_REASON_USER, UNMOUNT_REASON_SHUTDOWN }; | |
79 | |
80 Service(Profile* profile, extensions::ExtensionRegistry* extension_registry); | |
81 ~Service() override; | |
82 | |
83 // Sets a custom ProvidedFileSystemInterface factory. Used by unit tests, | |
84 // where an event router is not available. | |
85 void SetFileSystemFactoryForTesting( | |
86 const FileSystemFactoryCallback& factory_callback); | |
87 | |
88 // Sets a custom Registry implementation. Used by unit tests. | |
89 void SetRegistryForTesting(scoped_ptr<RegistryInterface> registry); | |
90 | |
91 // Mounts a file system provided by an extension with the |extension_id|. If | |
92 // |writable| is set to true, then the file system is mounted in a R/W mode. | |
93 // Otherwise, only read-only operations are supported. If change notification | |
94 // tags are supported, then |supports_notify_tag| must be true. Note, that | |
95 // it is required in order to enable the internal cache. For success, returns | |
96 // base::File::FILE_OK, otherwise an error code. | |
97 base::File::Error MountFileSystem(const std::string& extension_id, | |
98 const MountOptions& options); | |
99 | |
100 // Unmounts a file system with the specified |file_system_id| for the | |
101 // |extension_id|. For success returns base::File::FILE_OK, otherwise an error | |
102 // code. | |
103 base::File::Error UnmountFileSystem(const std::string& extension_id, | |
104 const std::string& file_system_id, | |
105 UnmountReason reason); | |
106 | |
107 // Requests unmounting of the file system. Returns false if the request could | |
108 // not been created, true otherwise. | |
109 bool RequestUnmount(const std::string& extension_id, | |
110 const std::string& file_system_id); | |
111 | |
112 // Requests mounting a new file system by the providing extension with | |
113 // |extension_id|. Returns false if the request could not been created, true | |
114 // otherwise. | |
115 bool RequestMount(const std::string& extension_id); | |
116 | |
117 // Returns a list of information of all currently provided file systems. All | |
118 // items are copied. | |
119 std::vector<ProvidedFileSystemInfo> GetProvidedFileSystemInfoList(); | |
120 | |
121 // Returns a provided file system with |file_system_id|, handled by | |
122 // the extension with |extension_id|. If not found, then returns NULL. | |
123 ProvidedFileSystemInterface* GetProvidedFileSystem( | |
124 const std::string& extension_id, | |
125 const std::string& file_system_id); | |
126 | |
127 // Returns a provided file system attached to the the passed | |
128 // |mount_point_name|. If not found, then returns NULL. | |
129 ProvidedFileSystemInterface* GetProvidedFileSystem( | |
130 const std::string& mount_point_name); | |
131 | |
132 // Returns a list of information of all currently installed providing | |
133 // extensions. | |
134 std::vector<ProvidingExtensionInfo> GetProvidingExtensionInfoList() const; | |
135 | |
136 // Fills information of the specified providing extension and returns true. | |
137 // If the extension is not a provider, or it doesn't exist, then false is | |
138 // returned. | |
139 bool GetProvidingExtensionInfo(const std::string& extension_id, | |
140 ProvidingExtensionInfo* result) const; | |
141 | |
142 // Adds and removes observers. | |
143 void AddObserver(Observer* observer); | |
144 void RemoveObserver(Observer* observer); | |
145 | |
146 // Gets the singleton instance for the |context|. | |
147 static Service* Get(content::BrowserContext* context); | |
148 | |
149 // extensions::ExtensionRegistryObserver overrides. | |
150 void OnExtensionUnloaded( | |
151 content::BrowserContext* browser_context, | |
152 const extensions::Extension* extension, | |
153 extensions::UnloadedExtensionInfo::Reason reason) override; | |
154 void OnExtensionLoaded(content::BrowserContext* browser_context, | |
155 const extensions::Extension* extension) override; | |
156 | |
157 // ProvidedFileSystemInterface::Observer overrides. | |
158 void OnWatcherChanged(const ProvidedFileSystemInfo& file_system_info, | |
159 const Watcher& watcher, | |
160 storage::WatcherManager::ChangeType change_type, | |
161 const ProvidedFileSystemObserver::Changes& changes, | |
162 const base::Closure& callback) override; | |
163 void OnWatcherTagUpdated(const ProvidedFileSystemInfo& file_system_info, | |
164 const Watcher& watcher) override; | |
165 void OnWatcherListChanged(const ProvidedFileSystemInfo& file_system_info, | |
166 const Watchers& watchers) override; | |
167 | |
168 private: | |
169 FRIEND_TEST_ALL_PREFIXES(FileSystemProviderServiceTest, RememberFileSystem); | |
170 | |
171 // Key is a pair of an extension id and file system id, which makes it | |
172 // unique among the entire service instance. | |
173 typedef std::pair<std::string, std::string> FileSystemKey; | |
174 | |
175 typedef std::map<FileSystemKey, ProvidedFileSystemInterface*> | |
176 ProvidedFileSystemMap; | |
177 typedef std::map<std::string, FileSystemKey> MountPointNameToKeyMap; | |
178 | |
179 // Mounts the file system in the specified context. See MountFileSystem() for | |
180 // more information. | |
181 base::File::Error MountFileSystemInternal(const std::string& extension_id, | |
182 const MountOptions& options, | |
183 MountContext context); | |
184 | |
185 // Called when the providing extension accepts or refuses a unmount request. | |
186 // If |error| is equal to FILE_OK, then the request is accepted. | |
187 void OnRequestUnmountStatus(const ProvidedFileSystemInfo& file_system_info, | |
188 base::File::Error error); | |
189 | |
190 // Remembers the file system in preferences, in order to remount after a | |
191 // reboot. | |
192 void RememberFileSystem(const ProvidedFileSystemInfo& file_system_info, | |
193 const Watchers& watchers); | |
194 | |
195 // Removes the file system from preferences, so it is not remounmted anymore | |
196 // after a reboot. | |
197 void ForgetFileSystem(const std::string& extension_id, | |
198 const std::string& file_system_id); | |
199 | |
200 // Restores from preferences file systems mounted previously by the | |
201 // |extension_id| providing extension. | |
202 void RestoreFileSystems(const std::string& extension_id); | |
203 | |
204 Profile* profile_; | |
205 extensions::ExtensionRegistry* extension_registry_; // Not owned. | |
206 FileSystemFactoryCallback file_system_factory_; | |
207 ObserverList<Observer> observers_; | |
208 ProvidedFileSystemMap file_system_map_; // Owns pointers. | |
209 MountPointNameToKeyMap mount_point_name_to_key_map_; | |
210 scoped_ptr<RegistryInterface> registry_; | |
211 base::ThreadChecker thread_checker_; | |
212 | |
213 base::WeakPtrFactory<Service> weak_ptr_factory_; | |
214 DISALLOW_COPY_AND_ASSIGN(Service); | |
215 }; | |
216 | |
217 } // namespace file_system_provider | |
218 } // namespace chromeos | |
219 | |
220 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ | |
OLD | NEW |