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