OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // MediaFileSystemRegistry registers pictures directories and media devices as | 5 // MediaFileSystemRegistry registers pictures directories and media devices as |
6 // File API filesystems and keeps track of the path to filesystem ID mappings. | 6 // File API filesystems and keeps track of the path to filesystem ID mappings. |
7 | 7 |
8 #ifndef CHROME_BROWSER_MEDIA_GALLERY_MEDIA_FILE_SYSTEM_REGISTRY_H_ | 8 #ifndef CHROME_BROWSER_MEDIA_GALLERY_MEDIA_FILE_SYSTEM_REGISTRY_H_ |
9 #define CHROME_BROWSER_MEDIA_GALLERY_MEDIA_FILE_SYSTEM_REGISTRY_H_ | 9 #define CHROME_BROWSER_MEDIA_GALLERY_MEDIA_FILE_SYSTEM_REGISTRY_H_ |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <set> | |
12 #include <string> | 13 #include <string> |
13 #include <utility> | 14 #include <utility> |
14 #include <vector> | 15 #include <vector> |
15 | 16 |
16 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
17 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
18 #include "base/file_path.h" | 19 #include "base/file_path.h" |
19 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
20 #include "base/memory/weak_ptr.h" | |
21 #include "base/prefs/public/pref_change_registrar.h" | 21 #include "base/prefs/public/pref_change_registrar.h" |
22 #include "base/system_monitor/system_monitor.h" | 22 #include "base/system_monitor/system_monitor.h" |
23 #include "webkit/fileapi/media/mtp_device_file_system_config.h" | 23 #include "webkit/fileapi/media/mtp_device_file_system_config.h" |
24 | 24 |
25 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 25 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
26 #include "chrome/browser/media_gallery/mtp_device_delegate_impl.h" | 26 #include "chrome/browser/media_gallery/mtp_device_delegate_impl.h" |
27 #endif | 27 #endif |
28 | 28 |
29 class Profile; | 29 class Profile; |
30 | 30 |
(...skipping 19 matching lines...) Expand all Loading... | |
50 const FilePath& fs_path, | 50 const FilePath& fs_path, |
51 const std::string& filesystem_id); | 51 const std::string& filesystem_id); |
52 MediaFileSystemInfo(); | 52 MediaFileSystemInfo(); |
53 | 53 |
54 std::string name; // JSON string, must not contain slashes. | 54 std::string name; // JSON string, must not contain slashes. |
55 FilePath path; | 55 FilePath path; |
56 std::string fsid; | 56 std::string fsid; |
57 }; | 57 }; |
58 | 58 |
59 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 59 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
60 // Class to manage MTPDeviceDelegateImpl object for the attached MTP device. | 60 // Class to manage the lifetime of MTPDeviceDelegateImpl object for the attached |
61 // Refcounted to reuse the same MTP device delegate entry across extensions. | 61 // media transfer protocol(MTP) device. This object is constructed for each MTP |
Lei Zhang
2012/11/21 02:32:48
nit: space after protocol.
kmadhusu
2012/11/21 04:09:53
Done.
| |
62 // This class supports WeakPtr (extends SupportsWeakPtr) to expose itself as | 62 // device. |
63 // a weak pointer to MediaFileSystemRegistry. | 63 class ScopedMTPDeviceMapEntry { |
64 class ScopedMTPDeviceMapEntry | |
65 : public base::RefCounted<ScopedMTPDeviceMapEntry>, | |
66 public base::SupportsWeakPtr<ScopedMTPDeviceMapEntry> { | |
67 public: | 64 public: |
68 // |no_references_callback| is called when the last ScopedMTPDeviceMapEntry | 65 explicit ScopedMTPDeviceMapEntry(const FilePath::StringType& device_location); |
69 // reference goes away. | 66 |
70 ScopedMTPDeviceMapEntry(const FilePath::StringType& device_location, | 67 // Destructed when the last user of this MTP device is destroyed or when the |
71 const base::Closure& no_references_callback); | 68 // MTP device is detached from the system or when the browser is in shutdown |
69 // mode or when the last extension revokes the MTP device gallery permissions. | |
70 ~ScopedMTPDeviceMapEntry(); | |
72 | 71 |
73 private: | 72 private: |
74 // Friend declaration for ref counted implementation. | 73 // The MTP or PTP device location. |
75 friend class base::RefCounted<ScopedMTPDeviceMapEntry>; | |
76 | |
77 // Private because this class is ref-counted. | |
78 ~ScopedMTPDeviceMapEntry(); | |
79 | |
80 // Store the MTP or PTP device location. | |
81 const FilePath::StringType device_location_; | 74 const FilePath::StringType device_location_; |
82 | 75 |
83 // Store a raw pointer of MTPDeviceDelegateImpl object. | |
84 // MTPDeviceDelegateImpl is ref-counted and owned by MTPDeviceMapService. | |
85 // This class tells MTPDeviceMapService to dispose of it when the last | |
86 // reference to |this| goes away. | |
87 MTPDeviceDelegateImpl* delegate_; | |
88 | |
89 // A callback to call when the last reference of this object goes away. | |
90 base::Closure no_references_callback_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(ScopedMTPDeviceMapEntry); | 76 DISALLOW_COPY_AND_ASSIGN(ScopedMTPDeviceMapEntry); |
93 }; | 77 }; |
94 #endif | 78 #endif |
95 | 79 |
96 class MediaFileSystemContext { | 80 class MediaFileSystemContext { |
97 public: | 81 public: |
98 virtual ~MediaFileSystemContext() {} | 82 virtual ~MediaFileSystemContext() {} |
99 | 83 |
100 // Register a media file system (filtered to media files) for |path| and | 84 // Register a media file system (filtered to media files) for |path| and |
101 // return the new file system id. | 85 // return the new file system id. |
102 virtual std::string RegisterFileSystemForMassStorage( | 86 virtual std::string RegisterFileSystemForMassStorage( |
103 const std::string& device_id, const FilePath& path) = 0; | 87 const std::string& device_id, const FilePath& path) = 0; |
104 | 88 |
105 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 89 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
106 // Registers and returns the file system id for the MTP or PTP device | 90 // For the given |galleries_host|, this function returns the file system id |
107 // specified by |device_id| and |path|. Updates |entry| with the corresponding | 91 // of the registered MTP or PTP device specified by |device_id| and |path|. |
108 // ScopedMTPDeviceMapEntry object. | |
109 virtual std::string RegisterFileSystemForMTPDevice( | 92 virtual std::string RegisterFileSystemForMTPDevice( |
110 const std::string& device_id, const FilePath& path, | 93 const std::string& device_id, const FilePath& path, |
111 scoped_refptr<ScopedMTPDeviceMapEntry>* entry) = 0; | 94 const ExtensionGalleriesHost* galleries_host) = 0; |
95 | |
96 // Removes the MTP or PTP device reference for the given |galleries_host|. | |
97 virtual void RemoveMTPDeviceReferenceForHost( | |
98 const FilePath::StringType& device_location, | |
99 const ExtensionGalleriesHost* galleries_host) = 0; | |
112 #endif | 100 #endif |
113 | 101 |
114 // Revoke the passed |fsid|. | 102 // Revoke the passed |fsid|. |
115 virtual void RevokeFileSystem(const std::string& fsid) = 0; | 103 virtual void RevokeFileSystem(const std::string& fsid) = 0; |
116 }; | 104 }; |
117 | 105 |
118 typedef base::Callback<void(const std::vector<MediaFileSystemInfo>&)> | 106 typedef base::Callback<void(const std::vector<MediaFileSystemInfo>&)> |
119 MediaFileSystemsCallback; | 107 MediaFileSystemsCallback; |
120 | 108 |
121 class MediaFileSystemRegistry | 109 class MediaFileSystemRegistry |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 | 142 |
155 // Map an extension to the ExtensionGalleriesHost. | 143 // Map an extension to the ExtensionGalleriesHost. |
156 typedef std::map<std::string /*extension_id*/, | 144 typedef std::map<std::string /*extension_id*/, |
157 scoped_refptr<ExtensionGalleriesHost> > ExtensionHostMap; | 145 scoped_refptr<ExtensionGalleriesHost> > ExtensionHostMap; |
158 // Map a profile and extension to the ExtensionGalleriesHost. | 146 // Map a profile and extension to the ExtensionGalleriesHost. |
159 typedef std::map<Profile*, ExtensionHostMap> ExtensionGalleriesHostMap; | 147 typedef std::map<Profile*, ExtensionHostMap> ExtensionGalleriesHostMap; |
160 // Map a profile to a PrefChangeRegistrar. | 148 // Map a profile to a PrefChangeRegistrar. |
161 typedef std::map<Profile*, PrefChangeRegistrar*> PrefChangeRegistrarMap; | 149 typedef std::map<Profile*, PrefChangeRegistrar*> PrefChangeRegistrarMap; |
162 | 150 |
163 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 151 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
164 // Map a MTP or PTP device location to the weak pointer of | 152 // Map a MTP or PTP device location to the ScopedMTPDeviceMapEntry. |
165 // ScopedMTPDeviceMapEntry. | 153 // This map owns ScopedMTPDeviceMapEntry. |
166 typedef std::map<const FilePath::StringType, | 154 typedef std::map<const FilePath::StringType, ScopedMTPDeviceMapEntry*> |
167 base::WeakPtr<ScopedMTPDeviceMapEntry> > | |
168 MTPDeviceDelegateMap; | 155 MTPDeviceDelegateMap; |
156 | |
157 // Set of extension galleries host. | |
158 typedef std::set<const ExtensionGalleriesHost*> ExtensionGalleriesHostSet; | |
159 | |
160 // Map a device location to a set of extension galleries host. | |
161 // This map keeps track of extension galleries that currently registered | |
162 // the MTP device gallery as a file system. | |
163 typedef std::map<const FilePath::StringType, ExtensionGalleriesHostSet> | |
Lei Zhang
2012/11/21 02:32:48
Isn't this just used as another way of ref-countin
kmadhusu
2012/11/21 04:09:53
Yes.
Lei Zhang
2012/11/21 06:46:39
So why can't ScopedMTPDeviceMapEntry just be ref-c
kmadhusu
2012/11/22 00:49:07
As we discussed, reverted ScopedMTPDeviceMapEntry
| |
164 MTPDeviceReferencesMap; | |
169 #endif | 165 #endif |
170 | 166 |
171 // Obtain an instance of this class via GetInstance(). | 167 // Obtain an instance of this class via GetInstance(). |
172 MediaFileSystemRegistry(); | 168 MediaFileSystemRegistry(); |
173 virtual ~MediaFileSystemRegistry(); | 169 virtual ~MediaFileSystemRegistry(); |
174 | 170 |
175 void OnMediaGalleriesRememberedGalleriesChanged(PrefServiceBase* service); | 171 void OnMediaGalleriesRememberedGalleriesChanged(PrefServiceBase* service); |
176 | 172 |
177 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 173 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
178 // Returns ScopedMTPDeviceMapEntry object for the given |device_location|. | 174 // Adds the |galleries_host| reference for the MTP device specified by the |
179 ScopedMTPDeviceMapEntry* GetOrCreateScopedMTPDeviceMapEntry( | 175 // |device_location|. |
180 const FilePath::StringType& device_location); | 176 void AddGalleriesHostReferenceForMTPDevice( |
177 const FilePath::StringType& device_location, | |
178 const ExtensionGalleriesHost* galleries_host); | |
181 | 179 |
182 // Removes the ScopedMTPDeviceMapEntry associated with the given | 180 // Removes the |galleries_host| reference for the MTP device specified by the |
183 // |device_location|. | 181 // |device_location|. |
184 void RemoveScopedMTPDeviceMapEntry( | 182 void RemoveGalleriesHostReferenceForMTPDevice( |
185 const FilePath::StringType& device_location); | 183 const FilePath::StringType& device_location, |
184 const ExtensionGalleriesHost* galleries_host); | |
186 #endif | 185 #endif |
187 | 186 |
188 void OnExtensionGalleriesHostEmpty(Profile* profile, | 187 void OnExtensionGalleriesHostEmpty(Profile* profile, |
189 const std::string& extension_id); | 188 const std::string& extension_id); |
190 | 189 |
191 // Only accessed on the UI thread. This map owns all the | 190 // Only accessed on the UI thread. This map owns all the |
192 // ExtensionGalleriesHost objects created. | 191 // ExtensionGalleriesHost objects created. |
193 ExtensionGalleriesHostMap extension_hosts_map_; | 192 ExtensionGalleriesHostMap extension_hosts_map_; |
194 | 193 |
195 PrefChangeRegistrarMap pref_change_registrar_map_; | 194 PrefChangeRegistrarMap pref_change_registrar_map_; |
196 | 195 |
197 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 196 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
198 // Only accessed on the UI thread. | 197 // Only accessed on the UI thread. |
199 MTPDeviceDelegateMap mtp_delegate_map_; | 198 MTPDeviceDelegateMap mtp_device_delegate_map_; |
199 MTPDeviceReferencesMap mtp_device_references_map_; | |
200 #endif | 200 #endif |
201 | 201 |
202 scoped_ptr<MediaFileSystemContext> file_system_context_; | 202 scoped_ptr<MediaFileSystemContext> file_system_context_; |
203 | 203 |
204 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistry); | 204 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistry); |
205 }; | 205 }; |
206 | 206 |
207 } // namespace chrome | 207 } // namespace chrome |
208 | 208 |
209 #endif // CHROME_BROWSER_MEDIA_GALLERY_MEDIA_FILE_SYSTEM_REGISTRY_H_ | 209 #endif // CHROME_BROWSER_MEDIA_GALLERY_MEDIA_FILE_SYSTEM_REGISTRY_H_ |
OLD | NEW |