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_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_
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_interface.h"
22 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_observer.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 "components/keyed_service/core/keyed_service.h"
27 #include "content/public/browser/browser_context.h"
28 #include "extensions/browser/extension_registry_observer.h"
29 #include "extensions/common/extension.h"
30 #include "storage/browser/fileapi/watcher_manager.h"
32 namespace extensions
{
33 class ExtensionRegistry
;
34 } // namespace extensions
36 namespace user_prefs
{
37 class PrefRegistrySyncable
;
38 } // namespace user_prefs
41 namespace file_system_provider
{
43 class ProvidedFileSystemFactoryInterface
;
44 class ProvidedFileSystemInfo
;
45 class ProvidedFileSystemInterface
;
46 class RegistryInterface
;
50 // Registers preferences to remember registered file systems between reboots.
51 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
53 // Manages and registers the file system provider service. Maintains provided
55 class Service
: public KeyedService
,
56 public extensions::ExtensionRegistryObserver
,
57 public ProvidedFileSystemObserver
{
59 typedef base::Callback
<ProvidedFileSystemInterface
*(
61 const ProvidedFileSystemInfo
& file_system_info
)>
62 FileSystemFactoryCallback
;
64 // Reason for unmounting. In case of UNMOUNT_REASON_SHUTDOWN, the file system
65 // will be remounted automatically after a reboot. In case of
66 // UNMOUNT_REASON_USER it will be permanently unmounted.
67 enum UnmountReason
{ UNMOUNT_REASON_USER
, UNMOUNT_REASON_SHUTDOWN
};
69 Service(Profile
* profile
, extensions::ExtensionRegistry
* extension_registry
);
72 // Sets a custom ProvidedFileSystemInterface factory. Used by unit tests,
73 // where an event router is not available.
74 void SetFileSystemFactoryForTesting(
75 const FileSystemFactoryCallback
& factory_callback
);
77 // Sets a custom Registry implementation. Used by unit tests.
78 void SetRegistryForTesting(scoped_ptr
<RegistryInterface
> registry
);
80 // Mounts a file system provided by an extension with the |extension_id|. If
81 // |writable| is set to true, then the file system is mounted in a R/W mode.
82 // Otherwise, only read-only operations are supported. If change notification
83 // tags are supported, then |supports_notify_tag| must be true. Note, that
84 // it is required in order to enable the internal cache. For success, returns
85 // base::File::FILE_OK, otherwise an error code.
86 base::File::Error
MountFileSystem(const std::string
& extension_id
,
87 const MountOptions
& options
);
89 // Unmounts a file system with the specified |file_system_id| for the
90 // |extension_id|. For success returns base::File::FILE_OK, otherwise an error
92 base::File::Error
UnmountFileSystem(const std::string
& extension_id
,
93 const std::string
& file_system_id
,
94 UnmountReason reason
);
96 // Requests unmounting of the file system. The callback is called when the
97 // request is accepted or rejected, with an error code. Returns false if the
98 // request could not been created, true otherwise.
99 bool RequestUnmount(const std::string
& extension_id
,
100 const std::string
& file_system_id
);
102 // Returns a list of information of all currently provided file systems. All
104 std::vector
<ProvidedFileSystemInfo
> GetProvidedFileSystemInfoList();
106 // Returns a provided file system with |file_system_id|, handled by
107 // the extension with |extension_id|. If not found, then returns NULL.
108 ProvidedFileSystemInterface
* GetProvidedFileSystem(
109 const std::string
& extension_id
,
110 const std::string
& file_system_id
);
112 // Returns a provided file system attached to the the passed
113 // |mount_point_name|. If not found, then returns NULL.
114 ProvidedFileSystemInterface
* GetProvidedFileSystem(
115 const std::string
& mount_point_name
);
117 // Adds and removes observers.
118 void AddObserver(Observer
* observer
);
119 void RemoveObserver(Observer
* observer
);
121 // Gets the singleton instance for the |context|.
122 static Service
* Get(content::BrowserContext
* context
);
124 // extensions::ExtensionRegistryObserver overrides.
125 void OnExtensionUnloaded(
126 content::BrowserContext
* browser_context
,
127 const extensions::Extension
* extension
,
128 extensions::UnloadedExtensionInfo::Reason reason
) override
;
129 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
130 const extensions::Extension
* extension
) override
;
132 // ProvidedFileSystemInterface::Observer overrides.
133 void OnWatcherChanged(const ProvidedFileSystemInfo
& file_system_info
,
134 const Watcher
& watcher
,
135 storage::WatcherManager::ChangeType change_type
,
136 const ProvidedFileSystemObserver::Changes
& changes
,
137 const base::Closure
& callback
) override
;
138 void OnWatcherTagUpdated(const ProvidedFileSystemInfo
& file_system_info
,
139 const Watcher
& watcher
) override
;
140 void OnWatcherListChanged(const ProvidedFileSystemInfo
& file_system_info
,
141 const Watchers
& watchers
) override
;
144 FRIEND_TEST_ALL_PREFIXES(FileSystemProviderServiceTest
, RememberFileSystem
);
146 // Key is a pair of an extension id and file system id, which makes it
147 // unique among the entire service instance.
148 typedef std::pair
<std::string
, std::string
> FileSystemKey
;
150 typedef std::map
<FileSystemKey
, ProvidedFileSystemInterface
*>
151 ProvidedFileSystemMap
;
152 typedef std::map
<std::string
, FileSystemKey
> MountPointNameToKeyMap
;
154 // Mounts the file system in the specified context. See MountFileSystem() for
156 base::File::Error
MountFileSystemInternal(const std::string
& extension_id
,
157 const MountOptions
& options
,
158 MountContext context
);
160 // Called when the providing extension accepts or refuses a unmount request.
161 // If |error| is equal to FILE_OK, then the request is accepted.
162 void OnRequestUnmountStatus(const ProvidedFileSystemInfo
& file_system_info
,
163 base::File::Error error
);
165 // Remembers the file system in preferences, in order to remount after a
167 void RememberFileSystem(const ProvidedFileSystemInfo
& file_system_info
,
168 const Watchers
& watchers
);
170 // Removes the file system from preferences, so it is not remounmted anymore
172 void ForgetFileSystem(const std::string
& extension_id
,
173 const std::string
& file_system_id
);
175 // Restores from preferences file systems mounted previously by the
176 // |extension_id| providing extension.
177 void RestoreFileSystems(const std::string
& extension_id
);
180 extensions::ExtensionRegistry
* extension_registry_
; // Not owned.
181 FileSystemFactoryCallback file_system_factory_
;
182 ObserverList
<Observer
> observers_
;
183 ProvidedFileSystemMap file_system_map_
; // Owns pointers.
184 MountPointNameToKeyMap mount_point_name_to_key_map_
;
185 scoped_ptr
<RegistryInterface
> registry_
;
186 base::ThreadChecker thread_checker_
;
188 base::WeakPtrFactory
<Service
> weak_ptr_factory_
;
189 DISALLOW_COPY_AND_ASSIGN(Service
);
192 } // namespace file_system_provider
193 } // namespace chromeos
195 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_