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_path.h"
     18 #include "base/memory/ref_counted.h"
     19 #include "base/prefs/pref_change_registrar.h"
     20 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
     21 #include "chrome/browser/media_galleries/mtp_device_delegate_impl.h"
     22 #include "chrome/browser/storage_monitor/removable_storage_observer.h"
     23 
     24 class Profile;
     25 
     26 namespace content {
     27 class RenderViewHost;
     28 }
     29 
     30 namespace extensions {
     31 class Extension;
     32 }
     33 
     34 namespace fileapi {
     35 class IsolatedContext;
     36 }
     37 
     38 namespace chrome {
     39 
     40 class ExtensionGalleriesHost;
     41 class MediaFileSystemContext;
     42 class MediaGalleriesPreferences;
     43 class ScopedMTPDeviceMapEntry;
     44 
     45 struct MediaFileSystemInfo {
     46   MediaFileSystemInfo(const 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   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 class MediaFileSystemRegistry : public RemovableStorageObserver {
     69  public:
     70   MediaFileSystemRegistry();
     71   virtual ~MediaFileSystemRegistry();
     72 
     73   // Passes to |callback| the list of media filesystem IDs and paths for a
     74   // given RVH. Called on the UI thread.
     75   void GetMediaFileSystemsForExtension(
     76       const content::RenderViewHost* rvh,
     77       const extensions::Extension* extension,
     78       const MediaFileSystemsCallback& callback);
     79 
     80   // Returns the initialized media galleries preferences for the specified
     81   // |profile|. This method should be used instead of calling
     82   // MediaGalleriesPreferences directly because this method also ensures that
     83   // currently attached removable devices are added to the preferences.
     84   // Called on the UI thread.
     85   // Note: Caller must ensure that the storage monitor is initialized before
     86   // calling this method.
     87   MediaGalleriesPreferences* GetPreferences(Profile* profile);
     88 
     89   // RemovableStorageObserver implementation.
     90   virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE;
     91 
     92  private:
     93   friend class MediaFileSystemRegistryTest;
     94   friend class TestMediaFileSystemContext;
     95   class MediaFileSystemContextImpl;
     96 
     97   // Map an extension to the ExtensionGalleriesHost.
     98   typedef std::map<std::string /*extension_id*/,
     99                    scoped_refptr<ExtensionGalleriesHost> > ExtensionHostMap;
    100   // Map a profile and extension to the ExtensionGalleriesHost.
    101   typedef std::map<Profile*, ExtensionHostMap> ExtensionGalleriesHostMap;
    102   // Map a profile to a PrefChangeRegistrar.
    103   typedef std::map<Profile*, PrefChangeRegistrar*> PrefChangeRegistrarMap;
    104 
    105   // Map a MTP or PTP device location to the raw pointer of
    106   // ScopedMTPDeviceMapEntry. It is safe to store a raw pointer in this
    107   // map.
    108   typedef std::map<const base::FilePath::StringType, ScopedMTPDeviceMapEntry*>
    109       MTPDeviceDelegateMap;
    110 
    111   void OnRememberedGalleriesChanged(PrefService* service);
    112 
    113   // Returns ScopedMTPDeviceMapEntry object for the given |device_location|.
    114   scoped_refptr<ScopedMTPDeviceMapEntry> GetOrCreateScopedMTPDeviceMapEntry(
    115       const base::FilePath::StringType& device_location);
    116 
    117   // Removes the ScopedMTPDeviceMapEntry associated with the given
    118   // |device_location|.
    119   void RemoveScopedMTPDeviceMapEntry(
    120       const base::FilePath::StringType& device_location);
    121 
    122   void OnExtensionGalleriesHostEmpty(Profile* profile,
    123                                      const std::string& extension_id);
    124 
    125   // Only accessed on the UI thread. This map owns all the
    126   // ExtensionGalleriesHost objects created.
    127   ExtensionGalleriesHostMap extension_hosts_map_;
    128 
    129   PrefChangeRegistrarMap pref_change_registrar_map_;
    130 
    131   // Only accessed on the UI thread.
    132   MTPDeviceDelegateMap mtp_device_delegate_map_;
    133 
    134   scoped_ptr<MediaFileSystemContext> file_system_context_;
    135 
    136   DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistry);
    137 };
    138 
    139 }  // namespace chrome
    140 
    141 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FILE_SYSTEM_REGISTRY_H_
    142