1effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved. 2effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// found in the LICENSE file. 4effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ 6effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ 7effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 8effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include <map> 9effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include <string> 10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <utility> 11effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include <vector> 12effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 13c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "base/files/file.h" 140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/files/file_path.h" 15c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "base/memory/scoped_ptr.h" 16c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "base/memory/weak_ptr.h" 17effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "base/observer_list.h" 1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/threading/thread_checker.h" 19c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "base/values.h" 20effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/chromeos/file_system_provider/observer.h" 21effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/profiles/profile.h" 22effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/common/extensions/api/file_system_provider.h" 23effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/keyed_service/core/keyed_service.h" 24effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/public/browser/browser_context.h" 250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "extensions/browser/extension_registry_observer.h" 260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "extensions/common/extension.h" 270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochnamespace extensions { 290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass ExtensionRegistry; 300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch} // namespace extensions 31effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 3246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)namespace user_prefs { 3346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class PrefRegistrySyncable; 3446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)} // namespace user_prefs 3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 36effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochnamespace chromeos { 37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochnamespace file_system_provider { 38effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Key names for preferences. 4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)extern const char kPrefKeyFileSystemId[]; 41116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern const char kPrefKeyDisplayName[]; 42116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern const char kPrefKeyWritable[]; 4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass ProvidedFileSystemFactoryInterface; 450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass ProvidedFileSystemInfo; 460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass ProvidedFileSystemInterface; 47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass ServiceFactory; 48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 4946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Registers preferences to remember registered file systems between reboots. 5046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 5146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Manages and registers the file system provider service. Maintains provided 530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// file systems. 540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass Service : public KeyedService, 550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch public extensions::ExtensionRegistryObserver { 56effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch public: 570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch typedef base::Callback<ProvidedFileSystemInterface*( 58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch Profile* profile, 590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const ProvidedFileSystemInfo& file_system_info)> 600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch FileSystemFactoryCallback; 610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Reason for unmounting. In case of UNMOUNT_REASON_SHUTDOWN, the file system 63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // will be remounted automatically after a reboot. In case of 64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // UNMOUNT_REASON_USER it will be permanently unmounted. 65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch enum UnmountReason { UNMOUNT_REASON_USER, UNMOUNT_REASON_SHUTDOWN }; 66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch Service(Profile* profile, extensions::ExtensionRegistry* extension_registry); 68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch virtual ~Service(); 69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Sets a custom ProvidedFileSystemInterface factory. Used by unit tests, 710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // where an event router is not available. 72f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) void SetFileSystemFactoryForTesting( 730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const FileSystemFactoryCallback& factory_callback); 740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Mounts a file system provided by an extension with the |extension_id|. If 76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |writable| is set to true, then the file system is mounted in a R/W mode. 77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Otherwise, only read-only operations are supported. For success, returns 78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // true, otherwise false. 79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool MountFileSystem(const std::string& extension_id, 80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const std::string& file_system_id, 81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& display_name, 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool writable); 83effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 84c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch // Unmounts a file system with the specified |file_system_id| for the 85effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // |extension_id|. For success returns true, otherwise false. 86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool UnmountFileSystem(const std::string& extension_id, 87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& file_system_id, 88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch UnmountReason reason); 89c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch 900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Requests unmounting of the file system. The callback is called when the 910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // request is accepted or rejected, with an error code. Returns false if the 920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // request could not been created, true otherwise. 93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool RequestUnmount(const std::string& extension_id, 94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const std::string& file_system_id); 95effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Returns a list of information of all currently provided file systems. All 970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // items are copied. 980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch std::vector<ProvidedFileSystemInfo> GetProvidedFileSystemInfoList(); 990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 1000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Returns a provided file system with |file_system_id|, handled by 1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // the extension with |extension_id|. If not found, then returns NULL. 1020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ProvidedFileSystemInterface* GetProvidedFileSystem( 1030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const std::string& extension_id, 104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const std::string& file_system_id); 1050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 1060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Returns a provided file system attached to the the passed 1070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // |mount_point_name|. If not found, then returns NULL. 1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ProvidedFileSystemInterface* GetProvidedFileSystem( 1090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const std::string& mount_point_name); 1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 111effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // Adds and removes observers. 112effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch void AddObserver(Observer* observer); 113effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch void RemoveObserver(Observer* observer); 114effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 115effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // Gets the singleton instance for the |context|. 116effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch static Service* Get(content::BrowserContext* context); 117effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 1180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // extensions::ExtensionRegistryObserver overrides. 1190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch virtual void OnExtensionUnloaded( 1200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch content::BrowserContext* browser_context, 1210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch const extensions::Extension* extension, 1220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE; 12346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) virtual void OnExtensionLoaded( 12446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) content::BrowserContext* browser_context, 12546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) const extensions::Extension* extension) OVERRIDE; 126c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch 127effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch private: 128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Key is a pair of an extension id and file system id, which makes it 129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // unique among the entire service instance. 130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) typedef std::pair<std::string, std::string> FileSystemKey; 131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) typedef std::map<FileSystemKey, ProvidedFileSystemInterface*> 133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ProvidedFileSystemMap; 134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) typedef std::map<std::string, FileSystemKey> MountPointNameToKeyMap; 135effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 1360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Called when the providing extension accepts or refuses a unmount request. 1370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // If |error| is equal to FILE_OK, then the request is accepted. 1380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch void OnRequestUnmountStatus(const ProvidedFileSystemInfo& file_system_info, 1390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch base::File::Error error); 140c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch 141116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Remembers the file system in preferences, in order to remount after a 142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // reboot. 143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void RememberFileSystem(const ProvidedFileSystemInfo& file_system_info); 14446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Removes the file system from preferences, so it is not remounmted anymore 146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // after a reboot. 147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ForgetFileSystem(const std::string& extension_id, 148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& file_system_id); 14946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 15046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Restores from preferences file systems mounted previously by the 15146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // |extension_id| providing extension. 15246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) void RestoreFileSystems(const std::string& extension_id); 15346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 154effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch Profile* profile_; 1550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch extensions::ExtensionRegistry* extension_registry_; // Not owned. 1560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch FileSystemFactoryCallback file_system_factory_; 157effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch ObserverList<Observer> observers_; 1580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch ProvidedFileSystemMap file_system_map_; // Owns pointers. 159cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) MountPointNameToKeyMap mount_point_name_to_key_map_; 16046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) base::ThreadChecker thread_checker_; 161c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch base::WeakPtrFactory<Service> weak_ptr_factory_; 162effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 163effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch DISALLOW_COPY_AND_ASSIGN(Service); 164effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}; 165effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 166effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch} // namespace file_system_provider 167effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch} // namespace chromeos 168effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 169effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ 170