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/profiles/profile.h" 22 #include "chrome/common/extensions/api/file_system_provider.h" 23 #include "components/keyed_service/core/keyed_service.h" 24 #include "content/public/browser/browser_context.h" 25 #include "extensions/browser/extension_registry_observer.h" 26 #include "extensions/common/extension.h" 27 28 namespace extensions { 29 class ExtensionRegistry; 30 } // namespace extensions 31 32 namespace user_prefs { 33 class PrefRegistrySyncable; 34 } // namespace user_prefs 35 36 namespace chromeos { 37 namespace file_system_provider { 38 39 // Key names for preferences. 40 extern const char kPrefKeyFileSystemId[]; 41 extern const char kPrefKeyDisplayName[]; 42 extern const char kPrefKeyWritable[]; 43 44 class ProvidedFileSystemFactoryInterface; 45 class ProvidedFileSystemInfo; 46 class ProvidedFileSystemInterface; 47 class ServiceFactory; 48 49 // Registers preferences to remember registered file systems between reboots. 50 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 51 52 // Manages and registers the file system provider service. Maintains provided 53 // file systems. 54 class Service : public KeyedService, 55 public extensions::ExtensionRegistryObserver { 56 public: 57 typedef base::Callback<ProvidedFileSystemInterface*( 58 Profile* profile, 59 const ProvidedFileSystemInfo& file_system_info)> 60 FileSystemFactoryCallback; 61 62 // Reason for unmounting. In case of UNMOUNT_REASON_SHUTDOWN, the file system 63 // will be remounted automatically after a reboot. In case of 64 // UNMOUNT_REASON_USER it will be permanently unmounted. 65 enum UnmountReason { UNMOUNT_REASON_USER, UNMOUNT_REASON_SHUTDOWN }; 66 67 Service(Profile* profile, extensions::ExtensionRegistry* extension_registry); 68 virtual ~Service(); 69 70 // Sets a custom ProvidedFileSystemInterface factory. Used by unit tests, 71 // where an event router is not available. 72 void SetFileSystemFactoryForTesting( 73 const FileSystemFactoryCallback& factory_callback); 74 75 // Mounts a file system provided by an extension with the |extension_id|. If 76 // |writable| is set to true, then the file system is mounted in a R/W mode. 77 // Otherwise, only read-only operations are supported. For success, returns 78 // true, otherwise false. 79 bool MountFileSystem(const std::string& extension_id, 80 const std::string& file_system_id, 81 const std::string& display_name, 82 bool writable); 83 84 // Unmounts a file system with the specified |file_system_id| for the 85 // |extension_id|. For success returns true, otherwise false. 86 bool UnmountFileSystem(const std::string& extension_id, 87 const std::string& file_system_id, 88 UnmountReason reason); 89 90 // Requests unmounting of the file system. The callback is called when the 91 // request is accepted or rejected, with an error code. Returns false if the 92 // request could not been created, true otherwise. 93 bool RequestUnmount(const std::string& extension_id, 94 const std::string& file_system_id); 95 96 // Returns a list of information of all currently provided file systems. All 97 // items are copied. 98 std::vector<ProvidedFileSystemInfo> GetProvidedFileSystemInfoList(); 99 100 // Returns a provided file system with |file_system_id|, handled by 101 // the extension with |extension_id|. If not found, then returns NULL. 102 ProvidedFileSystemInterface* GetProvidedFileSystem( 103 const std::string& extension_id, 104 const std::string& file_system_id); 105 106 // Returns a provided file system attached to the the passed 107 // |mount_point_name|. If not found, then returns NULL. 108 ProvidedFileSystemInterface* GetProvidedFileSystem( 109 const std::string& mount_point_name); 110 111 // Adds and removes observers. 112 void AddObserver(Observer* observer); 113 void RemoveObserver(Observer* observer); 114 115 // Gets the singleton instance for the |context|. 116 static Service* Get(content::BrowserContext* context); 117 118 // extensions::ExtensionRegistryObserver overrides. 119 virtual void OnExtensionUnloaded( 120 content::BrowserContext* browser_context, 121 const extensions::Extension* extension, 122 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE; 123 virtual void OnExtensionLoaded( 124 content::BrowserContext* browser_context, 125 const extensions::Extension* extension) OVERRIDE; 126 127 private: 128 // Key is a pair of an extension id and file system id, which makes it 129 // unique among the entire service instance. 130 typedef std::pair<std::string, std::string> FileSystemKey; 131 132 typedef std::map<FileSystemKey, ProvidedFileSystemInterface*> 133 ProvidedFileSystemMap; 134 typedef std::map<std::string, FileSystemKey> MountPointNameToKeyMap; 135 136 // Called when the providing extension accepts or refuses a unmount request. 137 // If |error| is equal to FILE_OK, then the request is accepted. 138 void OnRequestUnmountStatus(const ProvidedFileSystemInfo& file_system_info, 139 base::File::Error error); 140 141 // Remembers the file system in preferences, in order to remount after a 142 // reboot. 143 void RememberFileSystem(const ProvidedFileSystemInfo& file_system_info); 144 145 // Removes the file system from preferences, so it is not remounmted anymore 146 // after a reboot. 147 void ForgetFileSystem(const std::string& extension_id, 148 const std::string& file_system_id); 149 150 // Restores from preferences file systems mounted previously by the 151 // |extension_id| providing extension. 152 void RestoreFileSystems(const std::string& extension_id); 153 154 Profile* profile_; 155 extensions::ExtensionRegistry* extension_registry_; // Not owned. 156 FileSystemFactoryCallback file_system_factory_; 157 ObserverList<Observer> observers_; 158 ProvidedFileSystemMap file_system_map_; // Owns pointers. 159 MountPointNameToKeyMap mount_point_name_to_key_map_; 160 base::ThreadChecker thread_checker_; 161 base::WeakPtrFactory<Service> weak_ptr_factory_; 162 163 DISALLOW_COPY_AND_ASSIGN(Service); 164 }; 165 166 } // namespace file_system_provider 167 } // namespace chromeos 168 169 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ 170