Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: chrome/browser/media_gallery/media_file_system_registry.cc

Issue 10829384: SystemMonitor: Pull device type into the device id (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comment Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 // TODO(vandebo) Handle MTP devices.
98 if (type != MediaStorageUtil::USB_MTP &&
99 IsGalleryPermittedForExtension(extension, media_devices[i].location)) {
100 device_id_map_.insert(std::make_pair(media_devices[i].unique_id, 100 device_id_map_.insert(std::make_pair(media_devices[i].unique_id,
101 media_devices[i])); 101 media_devices[i]));
102 FilePath path(media_devices[i].location); 102 FilePath path(media_devices[i].location);
103 const std::string fsid = RegisterPathAsFileSystem(media_devices[i].type, 103 const std::string fsid = RegisterPathAsFileSystem(path);
104 path);
105 child_it->second.insert(std::make_pair(path, fsid)); 104 child_it->second.insert(std::make_pair(path, fsid));
106 } 105 }
107 } 106 }
108 107
109 MediaPathToFSIDMap& child_map = child_it->second; 108 MediaPathToFSIDMap& child_map = child_it->second;
110 for (MediaPathToFSIDMap::const_iterator it = child_map.begin(); 109 for (MediaPathToFSIDMap::const_iterator it = child_map.begin();
111 it != child_map.end(); 110 it != child_map.end();
112 ++it) { 111 ++it) {
113 MediaFSInfo entry; 112 MediaFSInfo entry;
114 // TODO(vandebo) use a better name, fsid for now. 113 // TODO(vandebo) use a better name, fsid for now.
115 entry.name = it->second; 114 entry.name = it->second;
116 entry.fsid = it->second; 115 entry.fsid = it->second;
117 entry.path = it->first; 116 entry.path = it->first;
118 results.push_back(entry); 117 results.push_back(entry);
119 } 118 }
120 return results; 119 return results;
121 } 120 }
122 121
123 void MediaFileSystemRegistry::OnMediaDeviceDetached(const std::string& id) { 122 void MediaFileSystemRegistry::OnMediaDeviceDetached(const std::string& id) {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
125 124
126 DeviceIdToInfoMap::iterator it = device_id_map_.find(id); 125 DeviceIdToInfoMap::iterator it = device_id_map_.find(id);
127 if (it == device_id_map_.end()) 126 if (it == device_id_map_.end())
128 return; 127 return;
129 128
130 FilePath path(it->second.location); 129 FilePath path(it->second.location);
131 RevokeMediaFileSystem(it->second.type, path); 130 RevokeMediaFileSystem(path);
132 device_id_map_.erase(it); 131 device_id_map_.erase(it);
133 } 132 }
134 133
135 void MediaFileSystemRegistry::Observe( 134 void MediaFileSystemRegistry::Observe(
136 int type, 135 int type,
137 const content::NotificationSource& source, 136 const content::NotificationSource& source,
138 const content::NotificationDetails& details) { 137 const content::NotificationDetails& details) {
139 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED || 138 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED ||
140 type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); 139 type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 182
184 void MediaFileSystemRegistry::UnregisterForRPHGoneNotifications( 183 void MediaFileSystemRegistry::UnregisterForRPHGoneNotifications(
185 const content::RenderProcessHost* rph) { 184 const content::RenderProcessHost* rph) {
186 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 185 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
187 content::Source<RenderProcessHost>(rph)); 186 content::Source<RenderProcessHost>(rph));
188 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 187 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
189 content::Source<RenderProcessHost>(rph)); 188 content::Source<RenderProcessHost>(rph));
190 } 189 }
191 190
192 std::string MediaFileSystemRegistry::RegisterPathAsFileSystem( 191 std::string MediaFileSystemRegistry::RegisterPathAsFileSystem(
193 const SystemMonitor::MediaDeviceType& device_type, const FilePath& path) { 192 const FilePath& path) {
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
195 194
196 // Sanity checks for |path|. 195 // Sanity checks for |path|.
197 CHECK(path.IsAbsolute()); 196 CHECK(path.IsAbsolute());
198 CHECK(!path.ReferencesParent()); 197 CHECK(!path.ReferencesParent());
199 198
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 199 // 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). 200 // a fixed name (as we only register a single directory per file system).
212 std::string register_name(extension_misc::kMediaFileSystemPathPart); 201 std::string register_name(extension_misc::kMediaFileSystemPathPart);
213 const std::string fsid = 202 const std::string fsid =
214 IsolatedContext::GetInstance()->RegisterFileSystemForPath( 203 IsolatedContext::GetInstance()->RegisterFileSystemForPath(
215 type, path, &register_name); 204 fileapi::kFileSystemTypeNativeMedia, path, &register_name);
216 CHECK(!fsid.empty()); 205 CHECK(!fsid.empty());
217 return fsid; 206 return fsid;
218 } 207 }
219 208
220 void MediaFileSystemRegistry::RevokeMediaFileSystem( 209 void MediaFileSystemRegistry::RevokeMediaFileSystem(const FilePath& path) {
221 const SystemMonitor::MediaDeviceType& device_type, const FilePath& path) {
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
223 211
224 IsolatedContext* isolated_context = IsolatedContext::GetInstance(); 212 IsolatedContext* isolated_context = IsolatedContext::GetInstance();
225 isolated_context->RevokeFileSystemByPath(path); 213 isolated_context->RevokeFileSystemByPath(path);
226 214
227 for (ChildIdToMediaFSMap::iterator child_it = media_fs_map_.begin(); 215 for (ChildIdToMediaFSMap::iterator child_it = media_fs_map_.begin();
228 child_it != media_fs_map_.end(); 216 child_it != media_fs_map_.end();
229 ++child_it) { 217 ++child_it) {
230 MediaPathToFSIDMap& child_map = child_it->second; 218 MediaPathToFSIDMap& child_map = child_it->second;
231 MediaPathToFSIDMap::iterator media_path_it = child_map.find(path); 219 MediaPathToFSIDMap::iterator media_path_it = child_map.find(path);
232 if (media_path_it == child_map.end()) 220 if (media_path_it == child_map.end())
233 continue; 221 continue;
234 222
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); 223 child_map.erase(media_path_it);
248 } 224 }
249 } 225 }
250 226
251 } // namespace chrome 227 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/media_gallery/media_file_system_registry.h ('k') | chrome/browser/media_gallery/media_storage_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698