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.
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_
12 #include "base/files/file.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/values.h"
18 #include "chrome/browser/chromeos/file_system_provider/observer.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/extensions/api/file_system_provider.h"
21 #include "components/keyed_service/core/keyed_service.h"
22 #include "content/public/browser/browser_context.h"
23 #include "extensions/browser/extension_registry_observer.h"
24 #include "extensions/common/extension.h"
26 namespace extensions {
28 class ExtensionRegistry;
29 } // namespace extensions
32 namespace file_system_provider {
34 class ProvidedFileSystemFactoryInterface;
35 class ProvidedFileSystemInfo;
36 class ProvidedFileSystemInterface;
39 // Manages and registers the file system provider service. Maintains provided
41 class Service : public KeyedService,
42 public extensions::ExtensionRegistryObserver {
44 typedef base::Callback<ProvidedFileSystemInterface*(
45 extensions::EventRouter* event_router,
46 const ProvidedFileSystemInfo& file_system_info)>
47 FileSystemFactoryCallback;
49 Service(Profile* profile, extensions::ExtensionRegistry* extension_registry);
52 // Sets a custom ProvidedFileSystemInterface factory. Used by unit tests,
53 // where an event router is not available.
54 void SetFileSystemFactoryForTests(
55 const FileSystemFactoryCallback& factory_callback);
57 // Mounts a file system provided by an extension with the |extension_id|.
58 // For success, it returns a numeric file system id, which is an
59 // auto-incremented non-zero value. For failures, it returns zero.
60 int MountFileSystem(const std::string& extension_id,
61 const std::string& file_system_name);
63 // Unmounts a file system with the specified |file_system_id| for the
64 // |extension_id|. For success returns true, otherwise false.
65 bool UnmountFileSystem(const std::string& extension_id, int file_system_id);
67 // Requests unmounting of the file system. The callback is called when the
68 // request is accepted or rejected, with an error code. Returns false if the
69 // request could not been created, true otherwise.
70 bool RequestUnmount(int file_system_id);
72 // Returns a list of information of all currently provided file systems. All
74 std::vector<ProvidedFileSystemInfo> GetProvidedFileSystemInfoList();
76 // Returns a provided file system with |file_system_id|, handled by
77 // the extension with |extension_id|. If not found, then returns NULL.
78 ProvidedFileSystemInterface* GetProvidedFileSystem(
79 const std::string& extension_id,
82 // Returns a provided file system attached to the the passed
83 // |mount_point_name|. If not found, then returns NULL.
84 ProvidedFileSystemInterface* GetProvidedFileSystem(
85 const std::string& mount_point_name);
87 // Adds and removes observers.
88 void AddObserver(Observer* observer);
89 void RemoveObserver(Observer* observer);
91 // Gets the singleton instance for the |context|.
92 static Service* Get(content::BrowserContext* context);
94 // extensions::ExtensionRegistryObserver overrides.
95 virtual void OnExtensionUnloaded(
96 content::BrowserContext* browser_context,
97 const extensions::Extension* extension,
98 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
101 typedef std::map<int, ProvidedFileSystemInterface*> ProvidedFileSystemMap;
102 typedef std::map<std::string, int> MountPointNameToIdMap;
104 // Called when the providing extension accepts or refuses a unmount request.
105 // If |error| is equal to FILE_OK, then the request is accepted.
106 void OnRequestUnmountStatus(const ProvidedFileSystemInfo& file_system_info,
107 base::File::Error error);
110 extensions::ExtensionRegistry* extension_registry_; // Not owned.
111 FileSystemFactoryCallback file_system_factory_;
112 ObserverList<Observer> observers_;
113 ProvidedFileSystemMap file_system_map_; // Owns pointers.
114 MountPointNameToIdMap mount_point_name_to_id_map_;
116 base::WeakPtrFactory<Service> weak_ptr_factory_;
118 DISALLOW_COPY_AND_ASSIGN(Service);
121 } // namespace file_system_provider
122 } // namespace chromeos
124 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_