Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / service.h
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_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"
31
32 namespace extensions {
33 class ExtensionRegistry;
34 }  // namespace extensions
35
36 namespace user_prefs {
37 class PrefRegistrySyncable;
38 }  // namespace user_prefs
39
40 namespace chromeos {
41 namespace file_system_provider {
42
43 class ProvidedFileSystemFactoryInterface;
44 class ProvidedFileSystemInfo;
45 class ProvidedFileSystemInterface;
46 class RegistryInterface;
47 class ServiceFactory;
48 struct MountOptions;
49
50 // Registers preferences to remember registered file systems between reboots.
51 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
52
53 // Manages and registers the file system provider service. Maintains provided
54 // file systems.
55 class Service : public KeyedService,
56                 public extensions::ExtensionRegistryObserver,
57                 public ProvidedFileSystemObserver {
58  public:
59   typedef base::Callback<ProvidedFileSystemInterface*(
60       Profile* profile,
61       const ProvidedFileSystemInfo& file_system_info)>
62       FileSystemFactoryCallback;
63
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 };
68
69   Service(Profile* profile, extensions::ExtensionRegistry* extension_registry);
70   virtual ~Service();
71
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);
76
77   // Sets a custom Registry implementation. Used by unit tests.
78   void SetRegistryForTesting(scoped_ptr<RegistryInterface> registry);
79
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   // true, otherwise false.
86   bool MountFileSystem(const std::string& extension_id,
87                        const MountOptions& options);
88
89   // Unmounts a file system with the specified |file_system_id| for the
90   // |extension_id|. For success returns true, otherwise false.
91   bool UnmountFileSystem(const std::string& extension_id,
92                          const std::string& file_system_id,
93                          UnmountReason reason);
94
95   // Requests unmounting of the file system. The callback is called when the
96   // request is accepted or rejected, with an error code. Returns false if the
97   // request could not been created, true otherwise.
98   bool RequestUnmount(const std::string& extension_id,
99                       const std::string& file_system_id);
100
101   // Returns a list of information of all currently provided file systems. All
102   // items are copied.
103   std::vector<ProvidedFileSystemInfo> GetProvidedFileSystemInfoList();
104
105   // Returns a provided file system with |file_system_id|, handled by
106   // the extension with |extension_id|. If not found, then returns NULL.
107   ProvidedFileSystemInterface* GetProvidedFileSystem(
108       const std::string& extension_id,
109       const std::string& file_system_id);
110
111   // Returns a provided file system attached to the the passed
112   // |mount_point_name|. If not found, then returns NULL.
113   ProvidedFileSystemInterface* GetProvidedFileSystem(
114       const std::string& mount_point_name);
115
116   // Adds and removes observers.
117   void AddObserver(Observer* observer);
118   void RemoveObserver(Observer* observer);
119
120   // Gets the singleton instance for the |context|.
121   static Service* Get(content::BrowserContext* context);
122
123   // extensions::ExtensionRegistryObserver overrides.
124   virtual void OnExtensionUnloaded(
125       content::BrowserContext* browser_context,
126       const extensions::Extension* extension,
127       extensions::UnloadedExtensionInfo::Reason reason) override;
128   virtual void OnExtensionLoaded(
129       content::BrowserContext* browser_context,
130       const extensions::Extension* extension) override;
131
132   // ProvidedFileSystemInterface::Observer overrides.
133   virtual void OnWatcherChanged(
134       const ProvidedFileSystemInfo& file_system_info,
135       const Watcher& watcher,
136       storage::WatcherManager::ChangeType change_type,
137       const ProvidedFileSystemObserver::Changes& changes,
138       const base::Closure& callback) override;
139   virtual void OnWatcherTagUpdated(
140       const ProvidedFileSystemInfo& file_system_info,
141       const Watcher& watcher) override;
142   virtual void OnWatcherListChanged(
143       const ProvidedFileSystemInfo& file_system_info,
144       const Watchers& watchers) override;
145
146  private:
147   FRIEND_TEST_ALL_PREFIXES(FileSystemProviderServiceTest, RememberFileSystem);
148
149   // Key is a pair of an extension id and file system id, which makes it
150   // unique among the entire service instance.
151   typedef std::pair<std::string, std::string> FileSystemKey;
152
153   typedef std::map<FileSystemKey, ProvidedFileSystemInterface*>
154       ProvidedFileSystemMap;
155   typedef std::map<std::string, FileSystemKey> MountPointNameToKeyMap;
156
157   // Called when the providing extension accepts or refuses a unmount request.
158   // If |error| is equal to FILE_OK, then the request is accepted.
159   void OnRequestUnmountStatus(const ProvidedFileSystemInfo& file_system_info,
160                               base::File::Error error);
161
162   // Remembers the file system in preferences, in order to remount after a
163   // reboot.
164   void RememberFileSystem(const ProvidedFileSystemInfo& file_system_info,
165                           const Watchers& watchers);
166
167   // Removes the file system from preferences, so it is not remounmted anymore
168   // after a reboot.
169   void ForgetFileSystem(const std::string& extension_id,
170                         const std::string& file_system_id);
171
172   // Restores from preferences file systems mounted previously by the
173   // |extension_id| providing extension.
174   void RestoreFileSystems(const std::string& extension_id);
175
176   Profile* profile_;
177   extensions::ExtensionRegistry* extension_registry_;  // Not owned.
178   FileSystemFactoryCallback file_system_factory_;
179   ObserverList<Observer> observers_;
180   ProvidedFileSystemMap file_system_map_;  // Owns pointers.
181   MountPointNameToKeyMap mount_point_name_to_key_map_;
182   scoped_ptr<RegistryInterface> registry_;
183   base::ThreadChecker thread_checker_;
184
185   base::WeakPtrFactory<Service> weak_ptr_factory_;
186   DISALLOW_COPY_AND_ASSIGN(Service);
187 };
188
189 }  // namespace file_system_provider
190 }  // namespace chromeos
191
192 #endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_