Chromium Code Reviews| 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/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/extensions/extension_constants.h" | 16 #include "chrome/common/extensions/extension_constants.h" |
| 17 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/browser/notification_types.h" | 20 #include "content/public/browser/notification_types.h" |
| 21 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 22 #include "webkit/fileapi/file_system_types.h" | 22 #include "webkit/fileapi/file_system_types.h" |
| 23 #include "webkit/fileapi/isolated_context.h" | 23 #include "webkit/fileapi/isolated_context.h" |
| 24 #include "webkit/fileapi/media/media_file_system_config.h" | |
| 25 | |
| 26 #if defined(SUPPORT_MEDIA_FILESYSTEM) | |
| 27 #include "webkit/fileapi/media/media_device_map_service.h" | |
| 28 | |
| 29 using fileapi::MediaDeviceMapService; | |
| 30 #endif | |
| 24 | 31 |
| 25 namespace chrome { | 32 namespace chrome { |
| 26 | 33 |
| 27 static base::LazyInstance<MediaFileSystemRegistry>::Leaky | 34 static base::LazyInstance<MediaFileSystemRegistry>::Leaky |
| 28 g_media_file_system_registry = LAZY_INSTANCE_INITIALIZER; | 35 g_media_file_system_registry = LAZY_INSTANCE_INITIALIZER; |
| 29 | 36 |
| 30 using base::SystemMonitor; | 37 using base::SystemMonitor; |
| 31 using content::BrowserThread; | 38 using content::BrowserThread; |
| 32 using content::RenderProcessHost; | 39 using content::RenderProcessHost; |
| 33 using fileapi::IsolatedContext; | 40 using fileapi::IsolatedContext; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 ChildIdToMediaFSMap::iterator& child_it = ret.first; | 76 ChildIdToMediaFSMap::iterator& child_it = ret.first; |
| 70 if (ret.second) { | 77 if (ret.second) { |
| 71 // Never seen a GetMediaFileSystems call from this RPH. Initialize its | 78 // Never seen a GetMediaFileSystems call from this RPH. Initialize its |
| 72 // file system mappings. | 79 // file system mappings. |
| 73 RegisterForRPHGoneNotifications(rph); | 80 RegisterForRPHGoneNotifications(rph); |
| 74 FilePath pictures_path; | 81 FilePath pictures_path; |
| 75 // TODO(vandebo) file system galleries need a unique id as well. | 82 // TODO(vandebo) file system galleries need a unique id as well. |
| 76 if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path) && | 83 if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path) && |
| 77 IsGalleryPermittedForExtension(extension, SystemMonitor::TYPE_PATH, | 84 IsGalleryPermittedForExtension(extension, SystemMonitor::TYPE_PATH, |
| 78 pictures_path.value())) { | 85 pictures_path.value())) { |
| 79 std::string fsid = RegisterPathAsFileSystem(pictures_path); | 86 std::string fsid = RegisterPathAsFileSystem(SystemMonitor::TYPE_PATH, |
| 87 pictures_path); | |
| 80 child_it->second.insert(std::make_pair(pictures_path, fsid)); | 88 child_it->second.insert(std::make_pair(pictures_path, fsid)); |
| 81 } | 89 } |
| 82 } | 90 } |
| 83 | 91 |
| 84 // TODO(thestig) Handle overlap between devices and media directories. | 92 // TODO(thestig) Handle overlap between devices and media directories. |
| 85 SystemMonitor* monitor = SystemMonitor::Get(); | 93 SystemMonitor* monitor = SystemMonitor::Get(); |
| 86 const std::vector<SystemMonitor::MediaDeviceInfo> media_devices = | 94 const std::vector<SystemMonitor::MediaDeviceInfo> media_devices = |
| 87 monitor->GetAttachedMediaDevices(); | 95 monitor->GetAttachedMediaDevices(); |
| 88 for (size_t i = 0; i < media_devices.size(); ++i) { | 96 for (size_t i = 0; i < media_devices.size(); ++i) { |
| 89 if (media_devices[i].type == SystemMonitor::TYPE_PATH && | 97 if (media_devices[i].type == SystemMonitor::TYPE_PATH && |
| 90 IsGalleryPermittedForExtension(extension, media_devices[i].type, | 98 IsGalleryPermittedForExtension(extension, media_devices[i].type, |
| 91 media_devices[i].location)) { | 99 media_devices[i].location)) { |
| 100 device_id_map_.insert(std::make_pair(media_devices[i].unique_id, | |
| 101 media_devices[i])); | |
| 92 FilePath path(media_devices[i].location); | 102 FilePath path(media_devices[i].location); |
| 93 device_id_map_.insert(std::make_pair(media_devices[i].unique_id, path)); | 103 const std::string fsid = RegisterPathAsFileSystem(media_devices[i].type, |
| 94 const std::string fsid = RegisterPathAsFileSystem(path); | 104 path); |
| 95 child_it->second.insert(std::make_pair(path, fsid)); | 105 child_it->second.insert(std::make_pair(path, fsid)); |
| 96 } | 106 } |
| 97 } | 107 } |
| 98 | 108 |
| 99 MediaPathToFSIDMap& child_map = child_it->second; | 109 MediaPathToFSIDMap& child_map = child_it->second; |
| 100 for (MediaPathToFSIDMap::const_iterator it = child_map.begin(); | 110 for (MediaPathToFSIDMap::const_iterator it = child_map.begin(); |
| 101 it != child_map.end(); | 111 it != child_map.end(); |
| 102 ++it) { | 112 ++it) { |
| 103 MediaFSInfo entry; | 113 MediaFSInfo entry; |
| 104 // TODO(vandebo) use a better name, fsid for now. | 114 // TODO(vandebo) use a better name, fsid for now. |
| 105 entry.name = it->second; | 115 entry.name = it->second; |
| 106 entry.fsid = it->second; | 116 entry.fsid = it->second; |
| 107 entry.path = it->first; | 117 entry.path = it->first; |
| 108 results.push_back(entry); | 118 results.push_back(entry); |
| 109 } | 119 } |
| 110 return results; | 120 return results; |
| 111 } | 121 } |
| 112 | 122 |
| 113 void MediaFileSystemRegistry::OnMediaDeviceDetached(const std::string& id) { | 123 void MediaFileSystemRegistry::OnMediaDeviceDetached(const std::string& id) { |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 115 | 125 |
| 116 DeviceIdToMediaPathMap::iterator it = device_id_map_.find(id); | 126 DeviceIdToInfoMap::iterator it = device_id_map_.find(id); |
| 117 if (it == device_id_map_.end()) | 127 if (it == device_id_map_.end()) |
| 118 return; | 128 return; |
| 119 RevokeMediaFileSystem(it->second); | 129 |
| 130 FilePath path(it->second.location); | |
| 131 RevokeMediaFileSystem(it->second.type, path); | |
| 120 device_id_map_.erase(it); | 132 device_id_map_.erase(it); |
| 121 } | 133 } |
| 122 | 134 |
| 123 void MediaFileSystemRegistry::Observe( | 135 void MediaFileSystemRegistry::Observe( |
| 124 int type, | 136 int type, |
| 125 const content::NotificationSource& source, | 137 const content::NotificationSource& source, |
| 126 const content::NotificationDetails& details) { | 138 const content::NotificationDetails& details) { |
| 127 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED || | 139 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED || |
| 128 type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 140 type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 158 | 170 |
| 159 void MediaFileSystemRegistry::UnregisterForRPHGoneNotifications( | 171 void MediaFileSystemRegistry::UnregisterForRPHGoneNotifications( |
| 160 const content::RenderProcessHost* rph) { | 172 const content::RenderProcessHost* rph) { |
| 161 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 173 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 162 content::Source<RenderProcessHost>(rph)); | 174 content::Source<RenderProcessHost>(rph)); |
| 163 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 175 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 164 content::Source<RenderProcessHost>(rph)); | 176 content::Source<RenderProcessHost>(rph)); |
| 165 } | 177 } |
| 166 | 178 |
| 167 std::string MediaFileSystemRegistry::RegisterPathAsFileSystem( | 179 std::string MediaFileSystemRegistry::RegisterPathAsFileSystem( |
| 168 const FilePath& path) { | 180 const SystemMonitor::MediaDeviceType& device_type, const FilePath& path) { |
| 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 170 | 182 |
| 171 // Sanity checks for |path|. | 183 // Sanity checks for |path|. |
| 172 CHECK(path.IsAbsolute()); | 184 CHECK(path.IsAbsolute()); |
| 173 CHECK(!path.ReferencesParent()); | 185 CHECK(!path.ReferencesParent()); |
| 174 | 186 |
| 187 fileapi::FileSystemType type; | |
|
kinuko
2012/08/02 20:33:44
nit: Please initialize this to kFileSystemTypeUnkn
kmadhusu
2012/08/02 22:05:39
Done.
| |
| 188 switch (device_type) { | |
| 189 case SystemMonitor::TYPE_MTP: | |
| 190 type = fileapi::kFileSystemTypeDeviceMedia; | |
| 191 break; | |
| 192 case SystemMonitor::TYPE_PATH: | |
| 193 // TODO(kmadhusu): This should be kFileSystemTypeNativeMedia. | |
| 194 type = fileapi::kFileSystemTypeIsolated; | |
|
kinuko
2012/08/02 20:33:44
I believe now you can use kFileSystemTypeNativeMed
kmadhusu
2012/08/02 22:05:39
Done.
| |
| 195 break; | |
| 196 } | |
| 197 | |
| 175 // 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 |
| 176 // 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). |
| 177 std::string register_name(extension_misc::kMediaFileSystemPathPart); | 200 std::string register_name(extension_misc::kMediaFileSystemPathPart); |
| 178 const std::string fsid = | 201 const std::string fsid = |
| 179 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 202 IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
| 180 fileapi::kFileSystemTypeIsolated, path, ®ister_name); | 203 type, path, ®ister_name); |
| 181 CHECK(!fsid.empty()); | 204 CHECK(!fsid.empty()); |
| 182 return fsid; | 205 return fsid; |
| 183 } | 206 } |
| 184 | 207 |
| 185 void MediaFileSystemRegistry::RevokeMediaFileSystem(const FilePath& path) { | 208 void MediaFileSystemRegistry::RevokeMediaFileSystem( |
| 209 const SystemMonitor::MediaDeviceType& device_type, const FilePath& path) { | |
| 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 187 | 211 |
| 188 IsolatedContext* isolated_context = IsolatedContext::GetInstance(); | 212 IsolatedContext* isolated_context = IsolatedContext::GetInstance(); |
| 189 for (ChildIdToMediaFSMap::iterator child_it = media_fs_map_.begin(); | 213 for (ChildIdToMediaFSMap::iterator child_it = media_fs_map_.begin(); |
| 190 child_it != media_fs_map_.end(); | 214 child_it != media_fs_map_.end(); |
| 191 ++child_it) { | 215 ++child_it) { |
| 192 MediaPathToFSIDMap& child_map = child_it->second; | 216 MediaPathToFSIDMap& child_map = child_it->second; |
| 193 MediaPathToFSIDMap::iterator media_path_it = child_map.find(path); | 217 MediaPathToFSIDMap::iterator media_path_it = child_map.find(path); |
| 194 if (media_path_it == child_map.end()) | 218 if (media_path_it == child_map.end()) |
| 195 continue; | 219 continue; |
| 196 isolated_context->RevokeFileSystem(media_path_it->second); | 220 |
| 221 // Always revoke the file sytem and then do the clean up operation. | |
|
Lei Zhang
2012/08/02 20:54:50
nit: indentation is off here.
kmadhusu
2012/08/02 22:05:39
Done.
| |
| 222 isolated_context->RevokeFileSystem(media_path_it->second); | |
| 223 | |
| 224 // Do the clean up tasks related to the file system. | |
| 225 switch (device_type) { | |
| 226 case SystemMonitor::TYPE_MTP: | |
| 227 #if defined(SUPPORT_MEDIA_FILESYSTEM) | |
| 228 MediaDeviceMapService::GetInstance()->RemoveMediaDevice(path.value()); | |
| 229 #endif | |
| 230 break; | |
| 231 case SystemMonitor::TYPE_PATH: | |
| 232 break; | |
| 233 } | |
| 234 | |
| 197 child_map.erase(media_path_it); | 235 child_map.erase(media_path_it); |
| 198 } | 236 } |
| 199 } | 237 } |
| 200 | 238 |
| 201 } // namespace chrome | 239 } // namespace chrome |
| OLD | NEW |