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.
5 // MediaFileSystemRegistry registers pictures directories and media devices as
6 // File API filesystems and keeps track of the path to filesystem ID mappings.
8 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FILE_SYSTEM_REGISTRY_H_
9 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FILE_SYSTEM_REGISTRY_H_
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"
24 class ExtensionGalleriesHost;
25 class MediaFileSystemContext;
26 class MediaGalleriesPreferences;
27 class MediaScanManager;
34 namespace extensions {
39 class IsolatedContext;
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,
53 MediaFileSystemInfo();
54 ~MediaFileSystemInfo();
59 MediaGalleryPrefId pref_id;
60 std::string transient_device_id;
65 typedef base::Callback<void(const std::vector<MediaFileSystemInfo>&)>
66 MediaFileSystemsCallback;
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 {
74 MediaFileSystemRegistry();
75 virtual ~MediaFileSystemRegistry();
77 // Passes to |callback| the list of media filesystem IDs and paths for a
79 void GetMediaFileSystemsForExtension(
80 const content::RenderViewHost* rvh,
81 const extensions::Extension* extension,
82 const MediaFileSystemsCallback& callback);
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);
92 // Returns the media galleries preferences for the specified |profile|.
93 // Caller is responsible for ensuring that the preferences are initialized
95 MediaGalleriesPreferences* GetPreferences(Profile* profile);
97 MediaScanManager* media_scan_manager();
99 // RemovableStorageObserver implementation.
100 virtual void OnRemovableStorageDetached(
101 const storage_monitor::StorageInfo& info) OVERRIDE;
104 class MediaFileSystemContextImpl;
106 friend class MediaFileSystemContextImpl;
107 friend class MediaFileSystemRegistryTest;
108 friend class TestMediaFileSystemContext;
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;
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;
122 // Look up or create the extension gallery host.
123 ExtensionGalleriesHost* GetExtensionGalleryHost(
125 MediaGalleriesPreferences* preferences,
126 const std::string& extension_id);
128 void OnExtensionGalleriesHostEmpty(Profile* profile,
129 const std::string& extension_id);
131 // This map owns all the ExtensionGalleriesHost objects created.
132 ExtensionGalleriesHostMap extension_hosts_map_;
134 scoped_ptr<MediaFileSystemContext> file_system_context_;
136 scoped_ptr<MediaScanManager> media_scan_manager_;
138 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistry);
141 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FILE_SYSTEM_REGISTRY_H_