Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ | 
| 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ | 
| 7 | 7 | 
| 8 #include <map> | 8 #include <map> | 
| 9 #include <string> | 9 #include <string> | 
| 10 #include <vector> | 10 #include <vector> | 
| 11 | 11 | 
| 12 #include "base/files/file.h" | 12 #include "base/files/file.h" | 
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" | 
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" | 
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" | 
| 16 #include "base/values.h" | 16 #include "base/values.h" | 
| 17 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 17 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 
| 18 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" | 18 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" | 
| 19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" | 
| 20 #include "chrome/common/extensions/api/file_system_provider.h" | 20 #include "chrome/common/extensions/api/file_system_provider.h" | 
| 21 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" | 
| 22 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" | 
| 23 | 23 | 
| 24 namespace chromeos { | 24 namespace chromeos { | 
| 25 namespace file_system_provider { | 25 namespace file_system_provider { | 
| 26 | 26 | 
| 27 class ProvidedFileSystemFactoryInterface; | |
| 28 class ProvidedFileSystemInfo; | |
| 29 class ProvidedFileSystemInterface; | |
| 27 class ServiceFactory; | 30 class ServiceFactory; | 
| 28 | 31 | 
| 29 // Manages and registers the fileSystemProvider service. | 32 // Manages and registers the file system provider service. Maintains provided | 
| 33 // file systems. | |
| 34 // | |
| 35 // Internally creates ProvidedFileSystem instances which require a browser | |
| 36 // running. For unit tests, create with a custom factory (in most cases | |
| 37 // FakeProvidedFileSystemFactory will do) via CreateForTesting(). | |
| 30 class Service : public KeyedService { | 38 class Service : public KeyedService { | 
| 31 public: | 39 public: | 
| 32 explicit Service(Profile* profile); | 40 explicit Service(Profile* profile); | 
| 33 virtual ~Service(); | 41 virtual ~Service(); | 
| 34 | 42 | 
| 43 // Creates a service instance for tests. Takes ownership on the | |
| 44 // |file_system_factory| instance. | |
| 
 
hashimoto
2014/04/15 07:22:45
scoped_ptr always takes the ownership.
No need to
 
mtomasz
2014/04/15 09:42:12
Done.
 
 | |
| 45 static Service* CreateForTesting( | |
| 46 Profile* profile, | |
| 47 scoped_ptr<ProvidedFileSystemFactoryInterface> file_system_factory); | |
| 48 | |
| 35 // Mounts a file system provided by an extension with the |extension_id|. | 49 // Mounts a file system provided by an extension with the |extension_id|. | 
| 36 // For success, it returns a numeric file system id, which is an | 50 // For success, it returns a numeric file system id, which is an | 
| 37 // auto-incremented non-zero value. For failures, it returns zero. | 51 // auto-incremented non-zero value. For failures, it returns zero. | 
| 38 int MountFileSystem(const std::string& extension_id, | 52 int MountFileSystem(const std::string& extension_id, | 
| 39 const std::string& file_system_name); | 53 const std::string& file_system_name); | 
| 40 | 54 | 
| 41 // Unmounts a file system with the specified |file_system_id| for the | 55 // Unmounts a file system with the specified |file_system_id| for the | 
| 42 // |extension_id|. For success returns true, otherwise false. | 56 // |extension_id|. For success returns true, otherwise false. | 
| 43 bool UnmountFileSystem(const std::string& extension_id, int file_system_id); | 57 bool UnmountFileSystem(const std::string& extension_id, int file_system_id); | 
| 44 | 58 | 
| 45 // Returns a list of currently mounted file systems. All items are copied. | 59 // Requests unmounting of the file system. The callback is called when the | 
| 46 std::vector<ProvidedFileSystem> GetMountedFileSystems(); | 60 // request is accepted or rejected, with an error code. Returns false if the | 
| 61 // request could not been created, true otherwise. | |
| 62 bool RequestUnmount(int file_system_id); | |
| 47 | 63 | 
| 48 // Handles successful response for the |request_id|. If |has_next| is false, | 64 // Returns a list of information of all currently provided file systems. All | 
| 49 // then the request is disposed, after handling the |response|. On error, | 65 // items are copied. | 
| 50 // returns false, and the request is disposed. | 66 std::vector<ProvidedFileSystemInfo> GetProvidedFileSystemInfoList(); | 
| 51 bool FulfillRequest(const std::string& extension_id, | |
| 52 int file_system_id, | |
| 53 int request_id, | |
| 54 scoped_ptr<base::DictionaryValue> result, | |
| 55 bool has_next); | |
| 56 | 67 | 
| 57 // Handles error response for the |request_id|. If handling the error fails, | 68 // Returns a provided file system with |file_system_id|, handled by | 
| 58 // returns false. Always disposes the request. | 69 // the extension with |extension_id|. If not found, then returns NULL. | 
| 59 bool RejectRequest(const std::string& extension_id, | 70 ProvidedFileSystemInterface* GetProvidedFileSystem( | 
| 60 int file_system_id, | 71 const std::string& extension_id, | 
| 61 int request_id, | 72 int file_system_id); | 
| 62 base::File::Error error); | |
| 63 | |
| 64 // Requests unmounting of a file system with the passed |file_system_id|. | |
| 65 // Returns true is unmounting has been requested. False, if the request is | |
| 66 // invalid (eg. already unmounted). | |
| 67 bool RequestUnmount(int file_system_id); | |
| 68 | 73 | 
| 69 // Adds and removes observers. | 74 // Adds and removes observers. | 
| 70 void AddObserver(Observer* observer); | 75 void AddObserver(Observer* observer); | 
| 71 void RemoveObserver(Observer* observer); | 76 void RemoveObserver(Observer* observer); | 
| 72 | 77 | 
| 73 // Gets the singleton instance for the |context|. | 78 // Gets the singleton instance for the |context|. | 
| 74 static Service* Get(content::BrowserContext* context); | 79 static Service* Get(content::BrowserContext* context); | 
| 75 | 80 | 
| 76 // BrowserContextKeyedService overrides. | 81 // BrowserContextKeyedService overrides. | 
| 77 virtual void Shutdown() OVERRIDE; | 82 virtual void Shutdown() OVERRIDE; | 
| 78 | 83 | 
| 84 // Getter for the request manager. Used by the extension API to forward | |
| 85 // replies. Valid as long as the service. | |
| 86 RequestManager* request_manager() { return &request_manager_; } | |
| 87 | |
| 79 private: | 88 private: | 
| 80 typedef std::map<int, ProvidedFileSystem> FileSystemMap; | 89 typedef std::map<int, ProvidedFileSystemInterface*> ProvidedFileSystemMap; | 
| 81 | 90 | 
| 82 // Called when the providing extension calls the success callback for the | 91 // Creates a service with a custom factory. Takes ownership on the | 
| 83 // onUnmountRequested event. | 92 // |file_system_factory| instance. | 
| 84 void OnRequestUnmountError(const ProvidedFileSystem& file_system, | 93 Service(Profile* profile, | 
| 85 base::File::Error error); | 94 scoped_ptr<ProvidedFileSystemFactoryInterface> file_system_factory); | 
| 95 | |
| 96 // Called when the providing extension accepts or refuses a unmount request. | |
| 97 // If |error| is equal to FILE_OK, then the request is accepted. | |
| 98 void OnRequestUnmountStatus(const ProvidedFileSystemInfo& file_system_info, | |
| 99 base::File::Error error); | |
| 86 | 100 | 
| 87 RequestManager request_manager_; | 101 RequestManager request_manager_; | 
| 88 Profile* profile_; | 102 Profile* profile_; | 
| 103 scoped_ptr<ProvidedFileSystemFactoryInterface> file_system_factory_; | |
| 89 ObserverList<Observer> observers_; | 104 ObserverList<Observer> observers_; | 
| 90 FileSystemMap file_systems_; | 105 ProvidedFileSystemMap file_system_map_; // Owns pointers. | 
| 91 int next_id_; | 106 int next_id_; | 
| 92 base::WeakPtrFactory<Service> weak_ptr_factory_; | 107 base::WeakPtrFactory<Service> weak_ptr_factory_; | 
| 93 | 108 | 
| 94 DISALLOW_COPY_AND_ASSIGN(Service); | 109 DISALLOW_COPY_AND_ASSIGN(Service); | 
| 95 }; | 110 }; | 
| 96 | 111 | 
| 97 } // namespace file_system_provider | 112 } // namespace file_system_provider | 
| 98 } // namespace chromeos | 113 } // namespace chromeos | 
| 99 | 114 | 
| 100 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ | 115 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_ | 
| OLD | NEW |