Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / service.h
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 <vector>
11
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"
25
26 namespace extensions {
27 class EventRouter;
28 class ExtensionRegistry;
29 }  // namespace extensions
30
31 namespace chromeos {
32 namespace file_system_provider {
33
34 class ProvidedFileSystemFactoryInterface;
35 class ProvidedFileSystemInfo;
36 class ProvidedFileSystemInterface;
37 class ServiceFactory;
38
39 // Manages and registers the file system provider service. Maintains provided
40 // file systems.
41 class Service : public KeyedService,
42                 public extensions::ExtensionRegistryObserver {
43  public:
44   typedef base::Callback<ProvidedFileSystemInterface*(
45       extensions::EventRouter* event_router,
46       const ProvidedFileSystemInfo& file_system_info)>
47       FileSystemFactoryCallback;
48
49   Service(Profile* profile, extensions::ExtensionRegistry* extension_registry);
50   virtual ~Service();
51
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);
56
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);
62
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);
66
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);
71
72   // Returns a list of information of all currently provided file systems. All
73   // items are copied.
74   std::vector<ProvidedFileSystemInfo> GetProvidedFileSystemInfoList();
75
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,
80       int file_system_id);
81
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);
86
87   // Adds and removes observers.
88   void AddObserver(Observer* observer);
89   void RemoveObserver(Observer* observer);
90
91   // Gets the singleton instance for the |context|.
92   static Service* Get(content::BrowserContext* context);
93
94   // extensions::ExtensionRegistryObserver overrides.
95   virtual void OnExtensionUnloaded(
96       content::BrowserContext* browser_context,
97       const extensions::Extension* extension,
98       extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
99
100  private:
101   typedef std::map<int, ProvidedFileSystemInterface*> ProvidedFileSystemMap;
102   typedef std::map<std::string, int> MountPointNameToIdMap;
103
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);
108
109   Profile* profile_;
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_;
115   int next_id_;
116   base::WeakPtrFactory<Service> weak_ptr_factory_;
117
118   DISALLOW_COPY_AND_ASSIGN(Service);
119 };
120
121 }  // namespace file_system_provider
122 }  // namespace chromeos
123
124 #endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_SERVICE_H_