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 implementation. | 5 // MediaFileSystemRegistry implementation. |
6 | 6 |
7 #include "chrome/browser/media_gallery/media_file_system_registry.h" | 7 #include "chrome/browser/media_gallery/media_file_system_registry.h" |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/system_monitor/system_monitor.h" | 13 #include "base/system_monitor/system_monitor.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "chrome/browser/media_gallery/media_storage_util.h" | |
15 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
16 #include "chrome/common/extensions/extension_constants.h" | 17 #include "chrome/common/extensions/extension_constants.h" |
17 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
20 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
21 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
22 #include "webkit/fileapi/file_system_types.h" | 23 #include "webkit/fileapi/file_system_types.h" |
23 #include "webkit/fileapi/isolated_context.h" | 24 #include "webkit/fileapi/isolated_context.h" |
24 #include "webkit/fileapi/media/media_file_system_config.h" | 25 #include "webkit/fileapi/media/media_file_system_config.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
35 g_media_file_system_registry = LAZY_INSTANCE_INITIALIZER; | 36 g_media_file_system_registry = LAZY_INSTANCE_INITIALIZER; |
36 | 37 |
37 using base::SystemMonitor; | 38 using base::SystemMonitor; |
38 using content::BrowserThread; | 39 using content::BrowserThread; |
39 using content::RenderProcessHost; | 40 using content::RenderProcessHost; |
40 using fileapi::IsolatedContext; | 41 using fileapi::IsolatedContext; |
41 | 42 |
42 namespace { | 43 namespace { |
43 | 44 |
44 bool IsGalleryPermittedForExtension(const extensions::Extension& extension, | 45 bool IsGalleryPermittedForExtension(const extensions::Extension& extension, |
45 SystemMonitor::MediaDeviceType type, | |
46 const FilePath::StringType& location) { | 46 const FilePath::StringType& location) { |
47 if (extension.HasAPIPermission( | 47 if (extension.HasAPIPermission( |
48 extensions::APIPermission::kMediaGalleriesAllGalleries)) { | 48 extensions::APIPermission::kMediaGalleriesAllGalleries)) { |
49 return true; | 49 return true; |
50 } | 50 } |
51 // TODO(vandebo) Check with prefs for permission to this gallery. | 51 // TODO(vandebo) Check with prefs for permission to this gallery. |
52 return false; | 52 return false; |
53 } | 53 } |
54 | 54 |
55 } // namespace | 55 } // namespace |
(...skipping 18 matching lines...) Expand all Loading... | |
74 std::pair<ChildIdToMediaFSMap::iterator, bool> ret = | 74 std::pair<ChildIdToMediaFSMap::iterator, bool> ret = |
75 media_fs_map_.insert(std::make_pair(rph, MediaPathToFSIDMap())); | 75 media_fs_map_.insert(std::make_pair(rph, MediaPathToFSIDMap())); |
76 ChildIdToMediaFSMap::iterator& child_it = ret.first; | 76 ChildIdToMediaFSMap::iterator& child_it = ret.first; |
77 if (ret.second) { | 77 if (ret.second) { |
78 // Never seen a GetMediaFileSystems call from this RPH. Initialize its | 78 // Never seen a GetMediaFileSystems call from this RPH. Initialize its |
79 // file system mappings. | 79 // file system mappings. |
80 RegisterForRPHGoneNotifications(rph); | 80 RegisterForRPHGoneNotifications(rph); |
81 FilePath pictures_path; | 81 FilePath pictures_path; |
82 // TODO(vandebo) file system galleries need a unique id as well. | 82 // TODO(vandebo) file system galleries need a unique id as well. |
83 if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path) && | 83 if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path) && |
84 IsGalleryPermittedForExtension(extension, SystemMonitor::TYPE_PATH, | 84 IsGalleryPermittedForExtension(extension, pictures_path.value())) { |
85 pictures_path.value())) { | 85 std::string fsid = RegisterPathAsFileSystem(pictures_path); |
86 std::string fsid = RegisterPathAsFileSystem(SystemMonitor::TYPE_PATH, | |
87 pictures_path); | |
88 child_it->second.insert(std::make_pair(pictures_path, fsid)); | 86 child_it->second.insert(std::make_pair(pictures_path, fsid)); |
89 } | 87 } |
90 } | 88 } |
91 | 89 |
92 // TODO(thestig) Handle overlap between devices and media directories. | 90 // TODO(thestig) Handle overlap between devices and media directories. |
93 SystemMonitor* monitor = SystemMonitor::Get(); | 91 SystemMonitor* monitor = SystemMonitor::Get(); |
94 const std::vector<SystemMonitor::MediaDeviceInfo> media_devices = | 92 const std::vector<SystemMonitor::MediaDeviceInfo> media_devices = |
95 monitor->GetAttachedMediaDevices(); | 93 monitor->GetAttachedMediaDevices(); |
96 for (size_t i = 0; i < media_devices.size(); ++i) { | 94 for (size_t i = 0; i < media_devices.size(); ++i) { |
97 if (media_devices[i].type == SystemMonitor::TYPE_PATH && | 95 MediaStorageUtil::Type type; |
98 IsGalleryPermittedForExtension(extension, media_devices[i].type, | 96 MediaStorageUtil::CrackDeviceId(media_devices[i].unique_id, &type, NULL); |
99 media_devices[i].location)) { | 97 if (type != MediaStorageUtil::USB_MTP && |
kmadhusu
2012/08/20 17:13:50
How about adding a TODO to handle MTP devices?
vandebo (ex-Chrome)
2012/08/20 18:35:14
Done.
| |
98 IsGalleryPermittedForExtension(extension, media_devices[i].location)) { | |
100 device_id_map_.insert(std::make_pair(media_devices[i].unique_id, | 99 device_id_map_.insert(std::make_pair(media_devices[i].unique_id, |
101 media_devices[i])); | 100 media_devices[i])); |
102 FilePath path(media_devices[i].location); | 101 FilePath path(media_devices[i].location); |
103 const std::string fsid = RegisterPathAsFileSystem(media_devices[i].type, | 102 const std::string fsid = RegisterPathAsFileSystem(path); |
104 path); | |
105 child_it->second.insert(std::make_pair(path, fsid)); | 103 child_it->second.insert(std::make_pair(path, fsid)); |
106 } | 104 } |
107 } | 105 } |
108 | 106 |
109 MediaPathToFSIDMap& child_map = child_it->second; | 107 MediaPathToFSIDMap& child_map = child_it->second; |
110 for (MediaPathToFSIDMap::const_iterator it = child_map.begin(); | 108 for (MediaPathToFSIDMap::const_iterator it = child_map.begin(); |
111 it != child_map.end(); | 109 it != child_map.end(); |
112 ++it) { | 110 ++it) { |
113 MediaFSInfo entry; | 111 MediaFSInfo entry; |
114 // TODO(vandebo) use a better name, fsid for now. | 112 // TODO(vandebo) use a better name, fsid for now. |
115 entry.name = it->second; | 113 entry.name = it->second; |
116 entry.fsid = it->second; | 114 entry.fsid = it->second; |
117 entry.path = it->first; | 115 entry.path = it->first; |
118 results.push_back(entry); | 116 results.push_back(entry); |
119 } | 117 } |
120 return results; | 118 return results; |
121 } | 119 } |
122 | 120 |
123 void MediaFileSystemRegistry::OnMediaDeviceDetached(const std::string& id) { | 121 void MediaFileSystemRegistry::OnMediaDeviceDetached(const std::string& id) { |
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
125 | 123 |
126 DeviceIdToInfoMap::iterator it = device_id_map_.find(id); | 124 DeviceIdToInfoMap::iterator it = device_id_map_.find(id); |
127 if (it == device_id_map_.end()) | 125 if (it == device_id_map_.end()) |
128 return; | 126 return; |
129 | 127 |
130 FilePath path(it->second.location); | 128 FilePath path(it->second.location); |
131 RevokeMediaFileSystem(it->second.type, path); | 129 RevokeMediaFileSystem(path); |
132 device_id_map_.erase(it); | 130 device_id_map_.erase(it); |
133 } | 131 } |
134 | 132 |
135 void MediaFileSystemRegistry::Observe( | 133 void MediaFileSystemRegistry::Observe( |
136 int type, | 134 int type, |
137 const content::NotificationSource& source, | 135 const content::NotificationSource& source, |
138 const content::NotificationDetails& details) { | 136 const content::NotificationDetails& details) { |
139 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED || | 137 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED || |
140 type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 138 type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 | 181 |
184 void MediaFileSystemRegistry::UnregisterForRPHGoneNotifications( | 182 void MediaFileSystemRegistry::UnregisterForRPHGoneNotifications( |
185 const content::RenderProcessHost* rph) { | 183 const content::RenderProcessHost* rph) { |
186 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 184 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
187 content::Source<RenderProcessHost>(rph)); | 185 content::Source<RenderProcessHost>(rph)); |
188 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 186 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
189 content::Source<RenderProcessHost>(rph)); | 187 content::Source<RenderProcessHost>(rph)); |
190 } | 188 } |
191 | 189 |
192 std::string MediaFileSystemRegistry::RegisterPathAsFileSystem( | 190 std::string MediaFileSystemRegistry::RegisterPathAsFileSystem( |
193 const SystemMonitor::MediaDeviceType& device_type, const FilePath& path) { | 191 const FilePath& path) { |
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
195 | 193 |
196 // Sanity checks for |path|. | 194 // Sanity checks for |path|. |
197 CHECK(path.IsAbsolute()); | 195 CHECK(path.IsAbsolute()); |
198 CHECK(!path.ReferencesParent()); | 196 CHECK(!path.ReferencesParent()); |
199 | 197 |
200 fileapi::FileSystemType type = fileapi::kFileSystemTypeUnknown; | |
201 switch (device_type) { | |
202 case SystemMonitor::TYPE_MTP: | |
203 type = fileapi::kFileSystemTypeDeviceMedia; | |
204 break; | |
205 case SystemMonitor::TYPE_PATH: | |
206 type = fileapi::kFileSystemTypeNativeMedia; | |
207 break; | |
208 } | |
209 | |
210 // The directory name is not exposed to the js layer and we simply use | 198 // The directory name is not exposed to the js layer and we simply use |
211 // a fixed name (as we only register a single directory per file system). | 199 // a fixed name (as we only register a single directory per file system). |
212 std::string register_name(extension_misc::kMediaFileSystemPathPart); | 200 std::string register_name(extension_misc::kMediaFileSystemPathPart); |
213 const std::string fsid = | 201 const std::string fsid = |
214 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 202 IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
215 type, path, ®ister_name); | 203 fileapi::kFileSystemTypeNativeMedia, path, ®ister_name); |
216 CHECK(!fsid.empty()); | 204 CHECK(!fsid.empty()); |
217 return fsid; | 205 return fsid; |
218 } | 206 } |
219 | 207 |
220 void MediaFileSystemRegistry::RevokeMediaFileSystem( | 208 void MediaFileSystemRegistry::RevokeMediaFileSystem(const FilePath& path) { |
221 const SystemMonitor::MediaDeviceType& device_type, const FilePath& path) { | |
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
223 | 210 |
224 IsolatedContext* isolated_context = IsolatedContext::GetInstance(); | 211 IsolatedContext* isolated_context = IsolatedContext::GetInstance(); |
225 isolated_context->RevokeFileSystemByPath(path); | 212 isolated_context->RevokeFileSystemByPath(path); |
226 | 213 |
227 for (ChildIdToMediaFSMap::iterator child_it = media_fs_map_.begin(); | 214 for (ChildIdToMediaFSMap::iterator child_it = media_fs_map_.begin(); |
228 child_it != media_fs_map_.end(); | 215 child_it != media_fs_map_.end(); |
229 ++child_it) { | 216 ++child_it) { |
230 MediaPathToFSIDMap& child_map = child_it->second; | 217 MediaPathToFSIDMap& child_map = child_it->second; |
231 MediaPathToFSIDMap::iterator media_path_it = child_map.find(path); | 218 MediaPathToFSIDMap::iterator media_path_it = child_map.find(path); |
232 if (media_path_it == child_map.end()) | 219 if (media_path_it == child_map.end()) |
233 continue; | 220 continue; |
234 | 221 |
235 // TODO(kmadhusu, vandebo): Clean up this code. http://crbug.com/140340. | |
236 | |
237 // Do the clean up tasks related to the file system. | |
238 switch (device_type) { | |
239 case SystemMonitor::TYPE_MTP: | |
240 #if defined(SUPPORT_MEDIA_FILESYSTEM) | |
241 MediaDeviceMapService::GetInstance()->RemoveMediaDevice(path.value()); | |
242 #endif | |
243 break; | |
244 case SystemMonitor::TYPE_PATH: | |
245 break; | |
246 } | |
247 child_map.erase(media_path_it); | 222 child_map.erase(media_path_it); |
248 } | 223 } |
249 } | 224 } |
250 | 225 |
251 } // namespace chrome | 226 } // namespace chrome |
OLD | NEW |