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 EventRouter; 30 class ExtensionRegistry; 31 } // namespace extensions 32 33 namespace user_prefs { 34 class PrefRegistrySyncable; 35 } // namespace user_prefs 36 37 namespace chromeos { 38 namespace file_system_provider { 39 40 // Key names for preferences. 41 extern const char kPrefKeyFileSystemId[]; 42 extern const char kPrefKeyFileSystemName[]; 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 extensions::EventRouter* event_router, 59 const ProvidedFileSystemInfo& file_system_info)> 60 FileSystemFactoryCallback; 61 62 Service(Profile* profile, extensions::ExtensionRegistry* extension_registry); 63 virtual ~Service(); 64 65 // Sets a custom ProvidedFileSystemInterface factory. Used by unit tests, 66 // where an event router is not available. 67 void SetFileSystemFactoryForTesting( 68 const FileSystemFactoryCallback& factory_callback); 69 70 // Mounts a file system provided by an extension with the |extension_id|. 71 // For success, returns true, otherwise false. 72 bool MountFileSystem(const std::string& extension_id, 73 const std::string& file_system_id, 74 const std::string& file_system_name); 75 76 // Unmounts a file system with the specified |file_system_id| for the 77 // |extension_id|. For success returns true, otherwise false. 78 bool UnmountFileSystem(const std::string& extension_id, 79 const std::string& file_system_id); 80 81 // Requests unmounting of the file system. The callback is called when the 82 // request is accepted or rejected, with an error code. Returns false if the 83 // request could not been created, true otherwise. 84 bool RequestUnmount(const std::string& extension_id, 85 const std::string& file_system_id); 86 87 // Returns a list of information of all currently provided file systems. All 88 // items are copied. 89 std::vector<ProvidedFileSystemInfo> GetProvidedFileSystemInfoList(); 90 91 // Returns a provided file system with |file_system_id|, handled by 92 // the extension with |extension_id|. If not found, then returns NULL. 93 ProvidedFileSystemInterface* GetProvidedFileSystem( 94 const std::string& extension_id, 95 const std::string& file_system_id); 96 97 // Returns a provided file system attached to the the passed 98 // |mount_point_name|. If not found, then returns NULL. 99 ProvidedFileSystemInterface* GetProvidedFileSystem( 100 const std::string& mount_point_name); 101 102 // Adds and removes observers. 103 void AddObserver(Observer* observer); 104 void RemoveObserver(Observer* observer); 105 106 // Gets the singleton instance for the |context|. 107 static Service* Get(content::BrowserContext* context); 108 109 // extensions::ExtensionRegistryObserver overrides. 110 virtual void OnExtensionUnloaded( 111 content::BrowserContext* browser_context, 112 const extensions::Extension* extension, 113 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE; 114 virtual void OnExtensionLoaded( 115 content::BrowserContext* browser_context, 116 const extensions::Extension* extension) OVERRIDE; 117 118 private: 119 // Key is a pair of an extension id and file system id, which makes it 120 // unique among the entire service instance. 121 typedef std::pair<std::string, std::string> FileSystemKey; 122 123 typedef std::map<FileSystemKey, ProvidedFileSystemInterface*> 124 ProvidedFileSystemMap; 125 typedef std::map<std::string, FileSystemKey> MountPointNameToKeyMap; 126 127 // Called when the providing extension accepts or refuses a unmount request. 128 // If |error| is equal to FILE_OK, then the request is accepted. 129 void OnRequestUnmountStatus(const ProvidedFileSystemInfo& file_system_info, 130 base::File::Error error); 131 132 // Saves a list of currently mounted file systems to preferences. Called 133 // from a destructor (on shutdown). 134 void RememberFileSystems(); 135 136 // Removes all of the file systems mounted by the |extension_id| from 137 // preferences, so they are not loaded again after reboot. 138 void ForgetFileSystems(const std::string& extension_id); 139 140 // Restores from preferences file systems mounted previously by the 141 // |extension_id| providing extension. 142 void RestoreFileSystems(const std::string& extension_id); 143 144 Profile* profile_; 145 extensions::ExtensionRegistry* extension_registry_; // Not owned. 146 FileSystemFactoryCallback file_system_factory_; 147 ObserverList<Observer> observers_; 148 ProvidedFileSystemMap file_system_map_; // Owns pointers. 149 MountPointNameToKeyMap mount_point_name_to_key_map_; 150 base::ThreadChecker thread_checker_; 151 base::WeakPtrFactory<Service> weak_ptr_factory_; 152 153 DISALLOW_COPY_AND_ASSIGN(Service); 154 }; 155 156 } // namespace file_system_provider 157 } // namespace chromeos 158 159 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ 160