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.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
19 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
20 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
21 #include "webkit/fileapi/file_system_types.h" | 21 #include "webkit/fileapi/file_system_types.h" |
22 #include "webkit/fileapi/isolated_context.h" | 22 #include "webkit/fileapi/isolated_context.h" |
| 23 #include "webkit/fileapi/media/media_file_system_config.h" |
| 24 |
| 25 #if defined(SUPPORT_MEDIA_FILESYSTEM) |
| 26 #include "webkit/fileapi/media/media_device_map_service.h" |
| 27 |
| 28 using fileapi::MediaDeviceMapService; |
| 29 #endif |
23 | 30 |
24 namespace chrome { | 31 namespace chrome { |
25 | 32 |
26 static base::LazyInstance<MediaFileSystemRegistry>::Leaky | 33 static base::LazyInstance<MediaFileSystemRegistry>::Leaky |
27 g_media_file_system_registry = LAZY_INSTANCE_INITIALIZER; | 34 g_media_file_system_registry = LAZY_INSTANCE_INITIALIZER; |
28 | 35 |
29 using base::SystemMonitor; | 36 using base::SystemMonitor; |
30 using content::BrowserThread; | 37 using content::BrowserThread; |
31 using content::RenderProcessHost; | 38 using content::RenderProcessHost; |
32 using fileapi::IsolatedContext; | 39 using fileapi::IsolatedContext; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 ChildIdToMediaFSMap::iterator& child_it = ret.first; | 75 ChildIdToMediaFSMap::iterator& child_it = ret.first; |
69 if (ret.second) { | 76 if (ret.second) { |
70 // Never seen a GetMediaFileSystems call from this RPH. Initialize its | 77 // Never seen a GetMediaFileSystems call from this RPH. Initialize its |
71 // file system mappings. | 78 // file system mappings. |
72 RegisterForRPHGoneNotifications(rph); | 79 RegisterForRPHGoneNotifications(rph); |
73 FilePath pictures_path; | 80 FilePath pictures_path; |
74 // TODO(vandebo) file system galleries need a unique id as well. | 81 // TODO(vandebo) file system galleries need a unique id as well. |
75 if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path) && | 82 if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path) && |
76 IsGalleryPermittedForExtension(extension, SystemMonitor::TYPE_PATH, | 83 IsGalleryPermittedForExtension(extension, SystemMonitor::TYPE_PATH, |
77 pictures_path.value())) { | 84 pictures_path.value())) { |
78 std::string fsid = RegisterPathAsFileSystem(pictures_path); | 85 std::string fsid = RegisterPathAsFileSystem(pictures_path, |
| 86 SystemMonitor::TYPE_PATH); |
79 child_it->second.insert(std::make_pair(pictures_path, fsid)); | 87 child_it->second.insert(std::make_pair(pictures_path, fsid)); |
80 } | 88 } |
81 } | 89 } |
82 | 90 |
83 // TODO(thestig) Handle overlap between devices and media directories. | 91 // TODO(thestig) Handle overlap between devices and media directories. |
84 SystemMonitor* monitor = SystemMonitor::Get(); | 92 SystemMonitor* monitor = SystemMonitor::Get(); |
85 const std::vector<SystemMonitor::MediaDeviceInfo> media_devices = | 93 const std::vector<SystemMonitor::MediaDeviceInfo> media_devices = |
86 monitor->GetAttachedMediaDevices(); | 94 monitor->GetAttachedMediaDevices(); |
87 for (size_t i = 0; i < media_devices.size(); ++i) { | 95 for (size_t i = 0; i < media_devices.size(); ++i) { |
88 if (media_devices[i].type == SystemMonitor::TYPE_PATH && | 96 if (media_devices[i].type == SystemMonitor::TYPE_PATH && |
89 IsGalleryPermittedForExtension(extension, media_devices[i].type, | 97 IsGalleryPermittedForExtension(extension, media_devices[i].type, |
90 media_devices[i].location)) { | 98 media_devices[i].location)) { |
91 FilePath path(media_devices[i].location); | 99 FilePath path(media_devices[i].location); |
92 device_id_map_.insert(std::make_pair(media_devices[i].unique_id, path)); | 100 device_id_map_.insert(std::make_pair(media_devices[i].unique_id, path)); |
93 const std::string fsid = RegisterPathAsFileSystem(path); | 101 const std::string fsid = RegisterPathAsFileSystem(path, |
| 102 media_devices[i].type); |
94 child_it->second.insert(std::make_pair(path, fsid)); | 103 child_it->second.insert(std::make_pair(path, fsid)); |
95 } | 104 } |
96 } | 105 } |
97 | 106 |
98 MediaPathToFSIDMap& child_map = child_it->second; | 107 MediaPathToFSIDMap& child_map = child_it->second; |
99 for (MediaPathToFSIDMap::const_iterator it = child_map.begin(); | 108 for (MediaPathToFSIDMap::const_iterator it = child_map.begin(); |
100 it != child_map.end(); | 109 it != child_map.end(); |
101 ++it) { | 110 ++it) { |
102 const FilePath path = it->first; | 111 const FilePath path = it->first; |
103 const std::string fsid = it->second; | 112 const std::string fsid = it->second; |
104 results.push_back(std::make_pair(fsid, path)); | 113 results.push_back(std::make_pair(fsid, path)); |
105 } | 114 } |
106 return results; | 115 return results; |
107 } | 116 } |
108 | 117 |
109 void MediaFileSystemRegistry::OnMediaDeviceDetached(const std::string& id) { | 118 void MediaFileSystemRegistry::OnMediaDeviceDetached( |
| 119 const std::string& id, |
| 120 const FilePath::StringType& location) { |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
111 | 122 |
112 DeviceIdToMediaPathMap::iterator it = device_id_map_.find(id); | 123 DeviceIdToMediaPathMap::iterator it = device_id_map_.find(id); |
113 if (it == device_id_map_.end()) | 124 if (it == device_id_map_.end()) |
114 return; | 125 return; |
| 126 |
| 127 #if defined(SUPPORT_MEDIA_FILESYSTEM) |
| 128 MediaDeviceMapService::GetInstance()->RemoveMediaDevice(location); |
| 129 #endif |
| 130 |
115 RevokeMediaFileSystem(it->second); | 131 RevokeMediaFileSystem(it->second); |
116 device_id_map_.erase(it); | 132 device_id_map_.erase(it); |
117 } | 133 } |
118 | 134 |
119 void MediaFileSystemRegistry::Observe( | 135 void MediaFileSystemRegistry::Observe( |
120 int type, | 136 int type, |
121 const content::NotificationSource& source, | 137 const content::NotificationSource& source, |
122 const content::NotificationDetails& details) { | 138 const content::NotificationDetails& details) { |
123 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED || | 139 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED || |
124 type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 140 type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
(...skipping 29 matching lines...) Expand all Loading... |
154 | 170 |
155 void MediaFileSystemRegistry::UnregisterForRPHGoneNotifications( | 171 void MediaFileSystemRegistry::UnregisterForRPHGoneNotifications( |
156 const content::RenderProcessHost* rph) { | 172 const content::RenderProcessHost* rph) { |
157 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 173 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
158 content::Source<RenderProcessHost>(rph)); | 174 content::Source<RenderProcessHost>(rph)); |
159 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 175 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
160 content::Source<RenderProcessHost>(rph)); | 176 content::Source<RenderProcessHost>(rph)); |
161 } | 177 } |
162 | 178 |
163 std::string MediaFileSystemRegistry::RegisterPathAsFileSystem( | 179 std::string MediaFileSystemRegistry::RegisterPathAsFileSystem( |
164 const FilePath& path) { | 180 const FilePath& path, const SystemMonitor::MediaDeviceType& device_type) { |
165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
166 | 182 |
167 // Sanity checks for |path|. | 183 // Sanity checks for |path|. |
168 CHECK(path.IsAbsolute()); | 184 CHECK(path.IsAbsolute()); |
169 CHECK(!path.ReferencesParent()); | 185 CHECK(!path.ReferencesParent()); |
170 | 186 |
| 187 fileapi::FileSystemType type = fileapi::kFileSystemTypeIsolated; |
| 188 if (device_type == SystemMonitor::TYPE_MTP) |
| 189 type = fileapi::kFileSystemTypeDeviceMedia; |
| 190 |
171 // The directory name is not exposed to the js layer and we simply use | 191 // The directory name is not exposed to the js layer and we simply use |
172 // a fixed name (as we only register a single directory per file system). | 192 // a fixed name (as we only register a single directory per file system). |
173 std::string register_name("_"); | 193 std::string register_name("_"); |
174 const std::string fsid = | 194 const std::string fsid = |
175 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 195 IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
176 fileapi::kFileSystemTypeIsolated, path, ®ister_name); | 196 type, path, ®ister_name); |
177 CHECK(!fsid.empty()); | 197 CHECK(!fsid.empty()); |
178 return fsid; | 198 return fsid; |
179 } | 199 } |
180 | 200 |
181 void MediaFileSystemRegistry::RevokeMediaFileSystem(const FilePath& path) { | 201 void MediaFileSystemRegistry::RevokeMediaFileSystem(const FilePath& path) { |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
183 | 203 |
184 IsolatedContext* isolated_context = IsolatedContext::GetInstance(); | 204 IsolatedContext* isolated_context = IsolatedContext::GetInstance(); |
185 for (ChildIdToMediaFSMap::iterator child_it = media_fs_map_.begin(); | 205 for (ChildIdToMediaFSMap::iterator child_it = media_fs_map_.begin(); |
186 child_it != media_fs_map_.end(); | 206 child_it != media_fs_map_.end(); |
187 ++child_it) { | 207 ++child_it) { |
188 MediaPathToFSIDMap& child_map = child_it->second; | 208 MediaPathToFSIDMap& child_map = child_it->second; |
189 MediaPathToFSIDMap::iterator media_path_it = child_map.find(path); | 209 MediaPathToFSIDMap::iterator media_path_it = child_map.find(path); |
190 if (media_path_it == child_map.end()) | 210 if (media_path_it == child_map.end()) |
191 continue; | 211 continue; |
192 isolated_context->RevokeFileSystem(media_path_it->second); | 212 isolated_context->RevokeFileSystem(media_path_it->second); |
193 child_map.erase(media_path_it); | 213 child_map.erase(media_path_it); |
194 } | 214 } |
195 } | 215 } |
196 | 216 |
197 } // namespace chrome | 217 } // namespace chrome |
OLD | NEW |