93446987565dc3adc8ccdded0912ed8ce5d62a62
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / media_file_system_registry.h
1 // Copyright (c) 2012 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 // MediaFileSystemRegistry registers pictures directories and media devices as
6 // File API filesystems and keeps track of the path to filesystem ID mappings.
7
8 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FILE_SYSTEM_REGISTRY_H_
9 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FILE_SYSTEM_REGISTRY_H_
10
11 #include <map>
12 #include <string>
13 #include <utility>
14 #include <vector>
15
16 #include "base/basictypes.h"
17 #include "base/files/file.h"
18 #include "base/files/file_path.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
22 #include "components/storage_monitor/removable_storage_observer.h"
23
24 class ExtensionGalleriesHost;
25 class MediaFileSystemContext;
26 class MediaGalleriesPreferences;
27 class MediaScanManager;
28 class Profile;
29
30 namespace content {
31 class RenderViewHost;
32 }
33
34 namespace extensions {
35 class Extension;
36 }
37
38 namespace fileapi {
39 class IsolatedContext;
40 }
41
42 // Contains information about a particular filesystem being provided to a
43 // client, including metadata like the name and ID, and API handles like the
44 // fsid (filesystem ID) used to hook up the API objects.
45 struct MediaFileSystemInfo {
46   MediaFileSystemInfo(const base::string16& fs_name,
47                       const base::FilePath& fs_path,
48                       const std::string& filesystem_id,
49                       MediaGalleryPrefId pref_id,
50                       const std::string& transient_device_id,
51                       bool removable,
52                       bool media_device);
53   MediaFileSystemInfo();
54   ~MediaFileSystemInfo();
55
56   base::string16 name;
57   base::FilePath path;
58   std::string fsid;
59   MediaGalleryPrefId pref_id;
60   std::string transient_device_id;
61   bool removable;
62   bool media_device;
63 };
64
65 typedef base::Callback<void(const std::vector<MediaFileSystemInfo>&)>
66     MediaFileSystemsCallback;
67
68 // Tracks usage of filesystems by extensions.
69 // This object lives on the UI thread.
70 class MediaFileSystemRegistry
71     : public storage_monitor::RemovableStorageObserver,
72       public MediaGalleriesPreferences::GalleryChangeObserver {
73  public:
74   MediaFileSystemRegistry();
75   virtual ~MediaFileSystemRegistry();
76
77   // Passes to |callback| the list of media filesystem IDs and paths for a
78   // given RVH.
79   void GetMediaFileSystemsForExtension(
80       const content::RenderViewHost* rvh,
81       const extensions::Extension* extension,
82       const MediaFileSystemsCallback& callback);
83
84   // Attempt to register the file system for |pref_id|. If |extension| does not
85   // have permission to |pref_id|, sends |callback| FILE_ERROR_NOT_FOUND.
86   void RegisterMediaFileSystemForExtension(
87       const content::RenderViewHost* rvh,
88       const extensions::Extension* extension,
89       MediaGalleryPrefId pref_id,
90       const base::Callback<void(base::File::Error result)>& callback);
91
92   // Returns the media galleries preferences for the specified |profile|.
93   // Caller is responsible for ensuring that the preferences are initialized
94   // before use.
95   MediaGalleriesPreferences* GetPreferences(Profile* profile);
96
97   MediaScanManager* media_scan_manager();
98
99   // RemovableStorageObserver implementation.
100   virtual void OnRemovableStorageDetached(
101       const storage_monitor::StorageInfo& info) OVERRIDE;
102
103  private:
104   class MediaFileSystemContextImpl;
105
106   friend class MediaFileSystemContextImpl;
107   friend class MediaFileSystemRegistryTest;
108   friend class TestMediaFileSystemContext;
109
110   // Map an extension to the ExtensionGalleriesHost.
111   typedef std::map<std::string /*extension_id*/,
112                    scoped_refptr<ExtensionGalleriesHost> > ExtensionHostMap;
113   // Map a profile and extension to the ExtensionGalleriesHost.
114   typedef std::map<Profile*, ExtensionHostMap> ExtensionGalleriesHostMap;
115
116   virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref,
117                                    const std::string& extension_id,
118                                    MediaGalleryPrefId pref_id) OVERRIDE;
119   virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref,
120                                 MediaGalleryPrefId pref_id) OVERRIDE;
121
122   // Look up or create the extension gallery host.
123   ExtensionGalleriesHost* GetExtensionGalleryHost(
124       Profile* profile,
125       MediaGalleriesPreferences* preferences,
126       const std::string& extension_id);
127
128   void OnExtensionGalleriesHostEmpty(Profile* profile,
129                                      const std::string& extension_id);
130
131   // This map owns all the ExtensionGalleriesHost objects created.
132   ExtensionGalleriesHostMap extension_hosts_map_;
133
134   scoped_ptr<MediaFileSystemContext> file_system_context_;
135
136   scoped_ptr<MediaScanManager> media_scan_manager_;
137
138   DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistry);
139 };
140
141 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FILE_SYSTEM_REGISTRY_H_