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 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 5 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | |
16 #include "chrome/browser/media_galleries/fileapi/mtp_device_map_service.h" | 17 #include "chrome/browser/media_galleries/fileapi/mtp_device_map_service.h" |
17 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 18 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
18 #include "chrome/browser/media_galleries/media_file_system_context.h" | 19 #include "chrome/browser/media_galleries/media_file_system_context.h" |
19 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" | 20 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" |
20 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 21 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
21 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h" | 22 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h" |
22 #include "chrome/browser/media_galleries/media_scan_manager.h" | 23 #include "chrome/browser/media_galleries/media_scan_manager.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
25 #include "chrome/common/extensions/extension_constants.h" | 26 #include "chrome/common/extensions/extension_constants.h" |
26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
27 #include "components/storage_monitor/media_storage_util.h" | 28 #include "components/storage_monitor/media_storage_util.h" |
28 #include "components/storage_monitor/storage_monitor.h" | 29 #include "components/storage_monitor/storage_monitor.h" |
29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/browser/navigation_details.h" | 31 #include "content/public/browser/navigation_details.h" |
31 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
32 #include "content/public/browser/render_process_host_observer.h" | 33 #include "content/public/browser/render_process_host_observer.h" |
33 #include "content/public/browser/render_view_host.h" | 34 #include "content/public/browser/render_view_host.h" |
34 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
35 #include "content/public/browser/web_contents_observer.h" | 36 #include "content/public/browser/web_contents_observer.h" |
36 #include "extensions/browser/extension_system.h" | 37 #include "extensions/browser/extension_system.h" |
37 #include "extensions/common/extension.h" | 38 #include "extensions/common/extension.h" |
38 #include "extensions/common/extension_set.h" | 39 #include "extensions/common/extension_set.h" |
39 #include "webkit/browser/fileapi/isolated_context.h" | 40 #include "webkit/browser/fileapi/external_mount_points.h" |
41 #include "webkit/common/fileapi/file_system_mount_option.h" | |
40 #include "webkit/common/fileapi/file_system_types.h" | 42 #include "webkit/common/fileapi/file_system_types.h" |
41 | 43 |
42 using content::BrowserThread; | 44 using content::BrowserThread; |
43 using content::NavigationController; | 45 using content::NavigationController; |
44 using content::RenderProcessHost; | 46 using content::RenderProcessHost; |
45 using content::WebContents; | 47 using content::WebContents; |
46 using fileapi::IsolatedContext; | 48 using fileapi::ExternalMountPoints; |
47 using storage_monitor::MediaStorageUtil; | 49 using storage_monitor::MediaStorageUtil; |
48 using storage_monitor::StorageInfo; | 50 using storage_monitor::StorageInfo; |
49 using storage_monitor::StorageMonitor; | 51 using storage_monitor::StorageMonitor; |
50 | 52 |
51 namespace { | 53 namespace { |
52 | 54 |
53 struct InvalidatedGalleriesInfo { | 55 struct InvalidatedGalleriesInfo { |
54 std::set<ExtensionGalleriesHost*> extension_hosts; | 56 std::set<ExtensionGalleriesHost*> extension_hosts; |
55 std::set<MediaGalleryPrefId> pref_ids; | 57 std::set<MediaGalleryPrefId> pref_ids; |
56 }; | 58 }; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 | 256 |
255 // The main owner of this class is | 257 // The main owner of this class is |
256 // |MediaFileSystemRegistry::extension_hosts_map_|, but a callback may | 258 // |MediaFileSystemRegistry::extension_hosts_map_|, but a callback may |
257 // temporarily hold a reference. | 259 // temporarily hold a reference. |
258 class ExtensionGalleriesHost | 260 class ExtensionGalleriesHost |
259 : public base::RefCountedThreadSafe<ExtensionGalleriesHost> { | 261 : public base::RefCountedThreadSafe<ExtensionGalleriesHost> { |
260 public: | 262 public: |
261 // |no_references_callback| is called when the last RenderViewHost reference | 263 // |no_references_callback| is called when the last RenderViewHost reference |
262 // goes away. RenderViewHost references are added through ReferenceFromRVH(). | 264 // goes away. RenderViewHost references are added through ReferenceFromRVH(). |
263 ExtensionGalleriesHost(MediaFileSystemContext* file_system_context, | 265 ExtensionGalleriesHost(MediaFileSystemContext* file_system_context, |
266 const base::FilePath& profile_path, | |
267 const std::string& extension_id, | |
264 const base::Closure& no_references_callback) | 268 const base::Closure& no_references_callback) |
265 : file_system_context_(file_system_context), | 269 : file_system_context_(file_system_context), |
270 profile_path_(profile_path), | |
271 extension_id_(extension_id), | |
266 no_references_callback_(no_references_callback), | 272 no_references_callback_(no_references_callback), |
267 rph_refs_(base::Bind(&ExtensionGalleriesHost::CleanUp, | 273 rph_refs_(base::Bind(&ExtensionGalleriesHost::CleanUp, |
268 base::Unretained(this))) { | 274 base::Unretained(this))) { |
269 } | 275 } |
270 | 276 |
271 // For each gallery in the list of permitted |galleries|, checks if the | 277 // For each gallery in the list of permitted |galleries|, checks if the |
272 // device is attached and if so looks up or creates a file system id and | 278 // device is attached and if so looks up or creates a file system name and |
273 // passes the information needed for the renderer to create those file | 279 // passes the information needed for the renderer to create those file |
274 // system objects to the |callback|. | 280 // system objects to the |callback|. |
275 void GetMediaFileSystems(const MediaGalleryPrefIdSet& galleries, | 281 void GetMediaFileSystems(const MediaGalleryPrefIdSet& galleries, |
276 const MediaGalleriesPrefInfoMap& galleries_info, | 282 const MediaGalleriesPrefInfoMap& galleries_info, |
277 const MediaFileSystemsCallback& callback) { | 283 const MediaFileSystemsCallback& callback) { |
278 // Extract all the device ids so we can make sure they are attached. | 284 // Extract all the device ids so we can make sure they are attached. |
279 MediaStorageUtil::DeviceIdSet* device_ids = | 285 MediaStorageUtil::DeviceIdSet* device_ids = |
280 new MediaStorageUtil::DeviceIdSet; | 286 new MediaStorageUtil::DeviceIdSet; |
281 for (std::set<MediaGalleryPrefId>::const_iterator id = galleries.begin(); | 287 for (std::set<MediaGalleryPrefId>::const_iterator id = galleries.begin(); |
282 id != galleries.end(); | 288 id != galleries.end(); |
283 ++id) { | 289 ++id) { |
284 device_ids->insert(galleries_info.find(*id)->second.device_id); | 290 device_ids->insert(galleries_info.find(*id)->second.device_id); |
285 } | 291 } |
286 MediaStorageUtil::FilterAttachedDevices(device_ids, base::Bind( | 292 MediaStorageUtil::FilterAttachedDevices(device_ids, base::Bind( |
287 &ExtensionGalleriesHost::GetMediaFileSystemsForAttachedDevices, this, | 293 &ExtensionGalleriesHost::GetMediaFileSystemsForAttachedDevices, this, |
288 base::Owned(device_ids), galleries, galleries_info, callback)); | 294 base::Owned(device_ids), galleries, galleries_info, callback)); |
289 } | 295 } |
290 | 296 |
297 // Checks if |gallery| is attached and if so, registers the file system and | |
298 // then calls |callback| with the result. | |
299 void RegisterMediaFileSystem( | |
300 const MediaGalleryPrefInfo& gallery, | |
301 const base::Callback<void(base::File::Error result)>& callback) { | |
302 // Extract all the device ids so we can make sure they are attached. | |
303 MediaStorageUtil::DeviceIdSet* device_ids = | |
304 new MediaStorageUtil::DeviceIdSet; | |
305 device_ids->insert(gallery.device_id); | |
306 MediaStorageUtil::FilterAttachedDevices(device_ids, base::Bind( | |
307 &ExtensionGalleriesHost::RegisterAttachedMediaFileSystem, this, | |
308 base::Owned(device_ids), gallery, callback)); | |
309 } | |
310 | |
291 // Revoke the file system for |id| if this extension has created one for |id|. | 311 // Revoke the file system for |id| if this extension has created one for |id|. |
292 void RevokeGalleryByPrefId(MediaGalleryPrefId id) { | 312 void RevokeGalleryByPrefId(MediaGalleryPrefId id) { |
293 PrefIdFsInfoMap::iterator gallery = pref_id_map_.find(id); | 313 PrefIdFsInfoMap::iterator gallery = pref_id_map_.find(id); |
294 if (gallery == pref_id_map_.end()) | 314 if (gallery == pref_id_map_.end()) |
295 return; | 315 return; |
296 | 316 |
297 file_system_context_->RevokeFileSystem(gallery->second.fsid); | 317 file_system_context_->RevokeFileSystem(gallery->second.fsid); |
298 pref_id_map_.erase(gallery); | 318 pref_id_map_.erase(gallery); |
299 | 319 |
300 if (pref_id_map_.empty()) { | 320 if (pref_id_map_.empty()) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 continue; | 367 continue; |
348 | 368 |
349 PrefIdFsInfoMap::const_iterator existing_info = | 369 PrefIdFsInfoMap::const_iterator existing_info = |
350 pref_id_map_.find(pref_id); | 370 pref_id_map_.find(pref_id); |
351 if (existing_info != pref_id_map_.end()) { | 371 if (existing_info != pref_id_map_.end()) { |
352 result.push_back(existing_info->second); | 372 result.push_back(existing_info->second); |
353 continue; | 373 continue; |
354 } | 374 } |
355 | 375 |
356 base::FilePath path = gallery_info.AbsolutePath(); | 376 base::FilePath path = gallery_info.AbsolutePath(); |
357 if (!MediaStorageUtil::CanCreateFileSystem(device_id, path)) | 377 if (path.empty() || |
378 !MediaStorageUtil::CanCreateFileSystem(device_id, path)) | |
Lei Zhang
2014/03/20 21:58:54
CanCreateFileSystem() probably should do the path.
vandebo (ex-Chrome)
2014/03/24 17:50:19
Done.
| |
358 continue; | 379 continue; |
359 | 380 |
360 std::string fsid = | 381 std::string fs_name = MediaFileSystemBackend::ConstructMountName( |
361 file_system_context_->RegisterFileSystem(device_id, path); | 382 profile_path_, extension_id_, pref_id); |
362 if (fsid.empty()) | 383 if (!file_system_context_->RegisterFileSystem(device_id, fs_name, path)) |
363 continue; | 384 continue; |
364 | 385 |
365 MediaFileSystemInfo new_entry( | 386 MediaFileSystemInfo new_entry( |
366 gallery_info.GetGalleryDisplayName(), | 387 gallery_info.GetGalleryDisplayName(), |
367 path, | 388 file_system_context_->GetRegisteredPath(fs_name), |
368 fsid, | 389 fs_name, |
369 pref_id, | 390 pref_id, |
370 GetTransientIdForRemovableDeviceId(device_id), | 391 GetTransientIdForRemovableDeviceId(device_id), |
371 StorageInfo::IsRemovableDevice(device_id), | 392 StorageInfo::IsRemovableDevice(device_id), |
372 StorageInfo::IsMediaDevice(device_id)); | 393 StorageInfo::IsMediaDevice(device_id)); |
373 result.push_back(new_entry); | 394 result.push_back(new_entry); |
374 pref_id_map_[pref_id] = new_entry; | 395 pref_id_map_[pref_id] = new_entry; |
375 } | 396 } |
376 | 397 |
377 if (result.size() == 0) { | 398 if (result.size() == 0) { |
378 rph_refs_.Reset(); | 399 rph_refs_.Reset(); |
379 CleanUp(); | 400 CleanUp(); |
380 } | 401 } |
381 | 402 |
382 DCHECK_EQ(pref_id_map_.size(), result.size()); | 403 DCHECK_EQ(pref_id_map_.size(), result.size()); |
383 callback.Run(result); | 404 callback.Run(result); |
384 } | 405 } |
385 | 406 |
407 void RegisterAttachedMediaFileSystem( | |
408 const MediaStorageUtil::DeviceIdSet* attached_device, | |
409 const MediaGalleryPrefInfo& gallery, | |
410 const base::Callback<void(base::File::Error result)>& callback) { | |
411 base::File::Error result = base::File::FILE_ERROR_NOT_FOUND; | |
412 | |
413 // If rph_refs is empty then we're actually in the middle of shutdown, and | |
414 // Filter...() lagging which can invoke this method interleaved in the | |
415 // destruction callback sequence and re-populate pref_id_map_. | |
416 if (!attached_device->empty() && !rph_refs_.empty()) { | |
417 std::string fs_name = MediaFileSystemBackend::ConstructMountName( | |
418 profile_path_, extension_id_, gallery.pref_id); | |
419 base::FilePath path = gallery.AbsolutePath(); | |
420 const std::string& device_id = gallery.device_id; | |
421 | |
422 if (ContainsKey(pref_id_map_, gallery.pref_id)) { | |
423 result = base::File::FILE_OK; | |
424 } else if (!path.empty() && | |
425 MediaStorageUtil::CanCreateFileSystem(device_id, path) && | |
426 file_system_context_->RegisterFileSystem(device_id, fs_name, | |
427 path)) { | |
428 result = base::File::FILE_OK; | |
429 pref_id_map_[gallery.pref_id] = MediaFileSystemInfo( | |
430 gallery.GetGalleryDisplayName(), | |
431 file_system_context_->GetRegisteredPath(fs_name), | |
432 fs_name, | |
433 gallery.pref_id, | |
434 GetTransientIdForRemovableDeviceId(device_id), | |
435 StorageInfo::IsRemovableDevice(device_id), | |
436 StorageInfo::IsMediaDevice(device_id)); | |
437 } | |
438 } | |
439 | |
440 if (pref_id_map_.empty()) { | |
441 rph_refs_.Reset(); | |
442 CleanUp(); | |
443 } | |
444 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | |
Lei Zhang
2014/03/20 21:58:54
nit: There's already "using content::BrowserThread
vandebo (ex-Chrome)
2014/03/24 17:50:19
Changed all.
| |
445 base::Bind(callback, result)); | |
446 } | |
447 | |
386 std::string GetTransientIdForRemovableDeviceId(const std::string& device_id) { | 448 std::string GetTransientIdForRemovableDeviceId(const std::string& device_id) { |
387 if (!StorageInfo::IsRemovableDevice(device_id)) | 449 if (!StorageInfo::IsRemovableDevice(device_id)) |
388 return std::string(); | 450 return std::string(); |
389 | 451 |
390 return StorageMonitor::GetInstance()->GetTransientIdForDeviceId(device_id); | 452 return StorageMonitor::GetInstance()->GetTransientIdForDeviceId(device_id); |
391 } | 453 } |
392 | 454 |
393 void CleanUp() { | 455 void CleanUp() { |
394 DCHECK(rph_refs_.empty()); | 456 DCHECK(rph_refs_.empty()); |
395 for (PrefIdFsInfoMap::const_iterator it = pref_id_map_.begin(); | 457 for (PrefIdFsInfoMap::const_iterator it = pref_id_map_.begin(); |
396 it != pref_id_map_.end(); | 458 it != pref_id_map_.end(); |
397 ++it) { | 459 ++it) { |
398 file_system_context_->RevokeFileSystem(it->second.fsid); | 460 file_system_context_->RevokeFileSystem(it->second.fsid); |
399 } | 461 } |
400 pref_id_map_.clear(); | 462 pref_id_map_.clear(); |
401 | 463 |
402 no_references_callback_.Run(); | 464 no_references_callback_.Run(); |
403 } | 465 } |
404 | 466 |
405 // MediaFileSystemRegistry owns |this| and |file_system_context_|, so it's | 467 // MediaFileSystemRegistry owns |this| and |file_system_context_|, so it's |
406 // safe to store a raw pointer. | 468 // safe to store a raw pointer. |
407 MediaFileSystemContext* file_system_context_; | 469 MediaFileSystemContext* file_system_context_; |
408 | 470 |
471 // Path for the active profile. | |
472 base::FilePath profile_path_; | |
Lei Zhang
2014/03/20 21:58:54
might as well make const
vandebo (ex-Chrome)
2014/03/24 17:50:19
Done.
| |
473 | |
474 // Id of the extension this host belongs to. | |
475 std::string extension_id_; | |
Lei Zhang
2014/03/20 21:58:54
const as well.
vandebo (ex-Chrome)
2014/03/24 17:50:19
Done.
| |
476 | |
409 // A callback to call when the last RVH reference goes away. | 477 // A callback to call when the last RVH reference goes away. |
410 base::Closure no_references_callback_; | 478 base::Closure no_references_callback_; |
411 | 479 |
412 // A map from the gallery preferences id to the file system information. | 480 // A map from the gallery preferences id to the file system information. |
413 PrefIdFsInfoMap pref_id_map_; | 481 PrefIdFsInfoMap pref_id_map_; |
414 | 482 |
415 // The set of render processes and web contents that may have references to | 483 // The set of render processes and web contents that may have references to |
416 // the file system ids this instance manages. | 484 // the file system ids this instance manages. |
417 RPHReferenceManager rph_refs_; | 485 RPHReferenceManager rph_refs_; |
418 | 486 |
(...skipping 14 matching lines...) Expand all Loading... | |
433 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); | 501 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); |
434 MediaGalleriesPreferences* preferences = GetPreferences(profile); | 502 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
435 MediaGalleryPrefIdSet galleries = | 503 MediaGalleryPrefIdSet galleries = |
436 preferences->GalleriesForExtension(*extension); | 504 preferences->GalleriesForExtension(*extension); |
437 | 505 |
438 if (galleries.empty()) { | 506 if (galleries.empty()) { |
439 callback.Run(std::vector<MediaFileSystemInfo>()); | 507 callback.Run(std::vector<MediaFileSystemInfo>()); |
440 return; | 508 return; |
441 } | 509 } |
442 | 510 |
443 ExtensionGalleriesHostMap::iterator extension_hosts = | 511 ExtensionGalleriesHost* extension_host = |
444 extension_hosts_map_.find(profile); | 512 GetExtensionGalleryHost(profile, preferences, extension->id()); |
445 if (extension_hosts->second.empty()) | |
446 preferences->AddGalleryChangeObserver(this); | |
447 | 513 |
448 ExtensionGalleriesHost* extension_host = | |
449 extension_hosts->second[extension->id()].get(); | |
450 if (!extension_host) { | |
451 extension_host = new ExtensionGalleriesHost( | |
452 file_system_context_.get(), | |
453 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, | |
454 base::Unretained(this), | |
455 profile, | |
456 extension->id())); | |
457 extension_hosts_map_[profile][extension->id()] = extension_host; | |
458 } | |
459 // This must come before the GetMediaFileSystems call to make sure the | 514 // This must come before the GetMediaFileSystems call to make sure the |
460 // RVH of the context is referenced before the filesystems are retrieved. | 515 // RVH of the context is referenced before the filesystems are retrieved. |
461 extension_host->ReferenceFromRVH(rvh); | 516 extension_host->ReferenceFromRVH(rvh); |
462 | 517 |
463 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), | 518 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), |
464 callback); | 519 callback); |
465 } | 520 } |
466 | 521 |
522 void MediaFileSystemRegistry::RegisterMediaFileSystemForExtension( | |
523 const content::RenderViewHost* rvh, | |
524 const extensions::Extension* extension, | |
525 MediaGalleryPrefId pref_id, | |
Lei Zhang
2014/03/20 21:58:54
Somewhere between here and eventually calling Regi
vandebo (ex-Chrome)
2014/03/24 17:50:19
Done.
| |
526 const base::Callback<void(base::File::Error result)>& callback) { | |
527 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
528 | |
529 Profile* profile = | |
530 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); | |
531 MediaGalleriesPreferences* preferences = GetPreferences(profile); | |
532 MediaGalleriesPrefInfoMap::const_iterator gallery = | |
533 preferences->known_galleries().find(pref_id); | |
534 MediaGalleryPrefIdSet permitted_galleries = | |
535 preferences->GalleriesForExtension(*extension); | |
536 | |
537 if (gallery == preferences->known_galleries().end() || | |
538 !ContainsKey(permitted_galleries, pref_id)) { | |
539 content::BrowserThread::PostTask( | |
540 content::BrowserThread::IO, FROM_HERE, | |
541 base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); | |
542 return; | |
543 } | |
544 | |
545 ExtensionGalleriesHost* extension_host = | |
546 GetExtensionGalleryHost(profile, preferences, extension->id()); | |
547 | |
548 // This must come before the GetMediaFileSystems call to make sure the | |
549 // RVH of the context is referenced before the filesystems are retrieved. | |
550 extension_host->ReferenceFromRVH(rvh); | |
551 | |
552 extension_host->RegisterMediaFileSystem(gallery->second, callback); | |
553 } | |
554 | |
467 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( | 555 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( |
468 Profile* profile) { | 556 Profile* profile) { |
469 // Create an empty ExtensionHostMap for this profile on first initialization. | 557 // Create an empty ExtensionHostMap for this profile on first initialization. |
470 if (!ContainsKey(extension_hosts_map_, profile)) { | 558 if (!ContainsKey(extension_hosts_map_, profile)) { |
471 extension_hosts_map_[profile] = ExtensionHostMap(); | 559 extension_hosts_map_[profile] = ExtensionHostMap(); |
472 media_galleries::UsageCount(media_galleries::PROFILES_WITH_USAGE); | 560 media_galleries::UsageCount(media_galleries::PROFILES_WITH_USAGE); |
473 } | 561 } |
474 | 562 |
475 return MediaGalleriesPreferencesFactory::GetForProfile(profile); | 563 return MediaGalleriesPreferencesFactory::GetForProfile(profile); |
476 } | 564 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 } | 619 } |
532 } | 620 } |
533 | 621 |
534 /****************** | 622 /****************** |
535 * Private methods | 623 * Private methods |
536 ******************/ | 624 ******************/ |
537 | 625 |
538 class MediaFileSystemRegistry::MediaFileSystemContextImpl | 626 class MediaFileSystemRegistry::MediaFileSystemContextImpl |
539 : public MediaFileSystemContext { | 627 : public MediaFileSystemContext { |
540 public: | 628 public: |
541 explicit MediaFileSystemContextImpl(MediaFileSystemRegistry* registry) | 629 MediaFileSystemContextImpl() {} |
542 : registry_(registry) { | |
543 DCHECK(registry_); // Suppresses unused warning on Android. | |
544 } | |
545 virtual ~MediaFileSystemContextImpl() {} | 630 virtual ~MediaFileSystemContextImpl() {} |
546 | 631 |
547 virtual std::string RegisterFileSystem( | 632 virtual bool RegisterFileSystem(const std::string& device_id, |
548 const std::string& device_id, const base::FilePath& path) OVERRIDE { | 633 const std::string& fs_name, |
634 const base::FilePath& path) OVERRIDE { | |
549 if (StorageInfo::IsMassStorageDevice(device_id)) { | 635 if (StorageInfo::IsMassStorageDevice(device_id)) { |
550 return RegisterFileSystemForMassStorage(device_id, path); | 636 return RegisterFileSystemForMassStorage(device_id, fs_name, path); |
551 } else { | 637 } else { |
552 return RegisterFileSystemForMTPDevice(device_id, path); | 638 return RegisterFileSystemForMTPDevice(device_id, fs_name, path); |
553 } | 639 } |
554 } | 640 } |
555 | 641 |
556 virtual void RevokeFileSystem(const std::string& fsid) OVERRIDE { | 642 virtual void RevokeFileSystem(const std::string& fs_name) OVERRIDE { |
557 ImportedMediaGalleryRegistry* imported_registry = | 643 ImportedMediaGalleryRegistry* imported_registry = |
558 ImportedMediaGalleryRegistry::GetInstance(); | 644 ImportedMediaGalleryRegistry::GetInstance(); |
559 if (imported_registry->RevokeImportedFilesystemOnUIThread(fsid)) | 645 if (imported_registry->RevokeImportedFilesystemOnUIThread(fs_name)) |
560 return; | 646 return; |
561 | 647 |
562 IsolatedContext::GetInstance()->RevokeFileSystem(fsid); | 648 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name); |
563 | 649 |
564 content::BrowserThread::PostTask( | 650 content::BrowserThread::PostTask( |
565 content::BrowserThread::IO, FROM_HERE, base::Bind( | 651 content::BrowserThread::IO, FROM_HERE, base::Bind( |
566 &MTPDeviceMapService::RevokeMTPFileSystem, | 652 &MTPDeviceMapService::RevokeMTPFileSystem, |
567 base::Unretained(MTPDeviceMapService::GetInstance()), | 653 base::Unretained(MTPDeviceMapService::GetInstance()), |
568 fsid)); | 654 fs_name)); |
655 } | |
656 | |
657 virtual base::FilePath GetRegisteredPath( | |
658 const std::string& fs_name) const OVERRIDE { | |
659 base::FilePath result; | |
660 if (!ExternalMountPoints::GetSystemInstance()->GetRegisteredPath(fs_name, | |
661 &result)) { | |
662 return base::FilePath(); | |
663 } | |
664 return result; | |
569 } | 665 } |
570 | 666 |
571 private: | 667 private: |
572 // Registers and returns the file system id for the mass storage device | 668 // Registers and returns the file system id for the mass storage device |
573 // specified by |device_id| and |path|. | 669 // specified by |device_id| and |path|. |
574 std::string RegisterFileSystemForMassStorage( | 670 bool RegisterFileSystemForMassStorage(const std::string& device_id, |
575 const std::string& device_id, const base::FilePath& path) { | 671 const std::string& fs_name, |
672 const base::FilePath& path) { | |
576 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 673 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
577 DCHECK(StorageInfo::IsMassStorageDevice(device_id)); | 674 DCHECK(StorageInfo::IsMassStorageDevice(device_id)); |
578 | 675 |
579 // Sanity checks for |path|. | 676 // Sanity checks for |path|. |
580 CHECK(path.IsAbsolute()); | 677 CHECK(path.IsAbsolute()); |
581 CHECK(!path.ReferencesParent()); | 678 CHECK(!path.ReferencesParent()); |
582 | 679 |
583 // TODO(gbillock): refactor ImportedMediaGalleryRegistry to delegate this | 680 // TODO(gbillock): refactor ImportedMediaGalleryRegistry to delegate this |
584 // call tree, probably by having it figure out by device id what | 681 // call tree, probably by having it figure out by device id what |
585 // registration is needed, or having per-device-type handlers at the | 682 // registration is needed, or having per-device-type handlers at the |
586 // next higher level. | 683 // next higher level. |
587 std::string fsid; | 684 bool result = false; |
588 if (StorageInfo::IsITunesDevice(device_id)) { | 685 if (StorageInfo::IsITunesDevice(device_id)) { |
589 ImportedMediaGalleryRegistry* imported_registry = | 686 ImportedMediaGalleryRegistry* registry = |
590 ImportedMediaGalleryRegistry::GetInstance(); | 687 ImportedMediaGalleryRegistry::GetInstance(); |
591 fsid = imported_registry->RegisterITunesFilesystemOnUIThread(path); | 688 result = registry->RegisterITunesFilesystemOnUIThread(fs_name, path); |
592 } else if (StorageInfo::IsPicasaDevice(device_id)) { | 689 } else if (StorageInfo::IsPicasaDevice(device_id)) { |
593 ImportedMediaGalleryRegistry* imported_registry = | 690 ImportedMediaGalleryRegistry* registry = |
594 ImportedMediaGalleryRegistry::GetInstance(); | 691 ImportedMediaGalleryRegistry::GetInstance(); |
595 fsid = imported_registry->RegisterPicasaFilesystemOnUIThread( | 692 result = registry->RegisterPicasaFilesystemOnUIThread(fs_name, path); |
596 path); | |
597 } else if (StorageInfo::IsIPhotoDevice(device_id)) { | 693 } else if (StorageInfo::IsIPhotoDevice(device_id)) { |
598 ImportedMediaGalleryRegistry* imported_registry = | 694 ImportedMediaGalleryRegistry* registry = |
599 ImportedMediaGalleryRegistry::GetInstance(); | 695 ImportedMediaGalleryRegistry::GetInstance(); |
600 fsid = imported_registry->RegisterIPhotoFilesystemOnUIThread( | 696 result = registry->RegisterIPhotoFilesystemOnUIThread(fs_name, path); |
601 path); | |
602 } else { | 697 } else { |
603 std::string fs_name(extension_misc::kMediaFileSystemPathPart); | 698 result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
604 fsid = IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 699 fs_name, fileapi::kFileSystemTypeNativeMedia, |
605 fileapi::kFileSystemTypeNativeMedia, path, &fs_name); | 700 fileapi::FileSystemMountOption(), path); |
606 } | 701 } |
607 return fsid; | 702 return result; |
608 } | 703 } |
609 | 704 |
610 std::string RegisterFileSystemForMTPDevice( | 705 bool RegisterFileSystemForMTPDevice(const std::string& device_id, |
611 const std::string& device_id, const base::FilePath& path) { | 706 const std::string fs_name, |
707 const base::FilePath& path) { | |
612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 708 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
613 DCHECK(!StorageInfo::IsMassStorageDevice(device_id)); | 709 DCHECK(!StorageInfo::IsMassStorageDevice(device_id)); |
614 | 710 |
615 // Sanity checks for |path|. | 711 // Sanity checks for |path|. |
616 CHECK(MediaStorageUtil::CanCreateFileSystem(device_id, path)); | 712 CHECK(MediaStorageUtil::CanCreateFileSystem(device_id, path)); |
617 std::string fs_name(extension_misc::kMediaFileSystemPathPart); | 713 bool result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
618 const std::string fsid = | 714 fs_name, fileapi::kFileSystemTypeDeviceMedia, |
619 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 715 fileapi::FileSystemMountOption(), path); |
620 fileapi::kFileSystemTypeDeviceMedia, path, &fs_name); | 716 CHECK(result); |
621 CHECK(!fsid.empty()); | |
622 content::BrowserThread::PostTask( | 717 content::BrowserThread::PostTask( |
623 content::BrowserThread::IO, FROM_HERE, base::Bind( | 718 content::BrowserThread::IO, FROM_HERE, base::Bind( |
624 &MTPDeviceMapService::RegisterMTPFileSystem, | 719 &MTPDeviceMapService::RegisterMTPFileSystem, |
625 base::Unretained(MTPDeviceMapService::GetInstance()), | 720 base::Unretained(MTPDeviceMapService::GetInstance()), |
626 path.value(), fsid)); | 721 path.value(), fs_name)); |
627 return fsid; | 722 return result; |
628 } | 723 } |
629 | 724 |
630 MediaFileSystemRegistry* registry_; | |
631 | |
632 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); | 725 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); |
633 }; | 726 }; |
634 | 727 |
635 // Constructor in 'private' section because depends on private class definition. | 728 // Constructor in 'private' section because depends on private class definition. |
636 MediaFileSystemRegistry::MediaFileSystemRegistry() | 729 MediaFileSystemRegistry::MediaFileSystemRegistry() |
637 : file_system_context_(new MediaFileSystemContextImpl(this)) { | 730 : file_system_context_(new MediaFileSystemContextImpl) { |
638 StorageMonitor::GetInstance()->AddObserver(this); | 731 StorageMonitor::GetInstance()->AddObserver(this); |
639 } | 732 } |
640 | 733 |
641 MediaFileSystemRegistry::~MediaFileSystemRegistry() { | 734 MediaFileSystemRegistry::~MediaFileSystemRegistry() { |
642 // TODO(gbillock): This is needed because the unit test uses the | 735 // TODO(gbillock): This is needed because the unit test uses the |
643 // g_browser_process registry. We should create one in the unit test, | 736 // g_browser_process registry. We should create one in the unit test, |
644 // and then can remove this. | 737 // and then can remove this. |
645 if (StorageMonitor::GetInstance()) | 738 if (StorageMonitor::GetInstance()) |
646 StorageMonitor::GetInstance()->RemoveObserver(this); | 739 StorageMonitor::GetInstance()->RemoveObserver(this); |
647 } | 740 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
691 if (!ContainsKey(extension_hosts_map_, profile)) | 784 if (!ContainsKey(extension_hosts_map_, profile)) |
692 break; | 785 break; |
693 ExtensionHostMap::const_iterator gallery_host_it = | 786 ExtensionHostMap::const_iterator gallery_host_it = |
694 extension_host_map.find(extensions[i]->id()); | 787 extension_host_map.find(extensions[i]->id()); |
695 if (gallery_host_it == extension_host_map.end()) | 788 if (gallery_host_it == extension_host_map.end()) |
696 continue; | 789 continue; |
697 gallery_host_it->second->RevokeGalleryByPrefId(pref_id); | 790 gallery_host_it->second->RevokeGalleryByPrefId(pref_id); |
698 } | 791 } |
699 } | 792 } |
700 | 793 |
794 ExtensionGalleriesHost* MediaFileSystemRegistry::GetExtensionGalleryHost( | |
795 Profile* profile, | |
796 MediaGalleriesPreferences* preferences, | |
797 const std::string& extension_id) { | |
798 ExtensionGalleriesHostMap::iterator extension_hosts = | |
799 extension_hosts_map_.find(profile); | |
800 // GetPreferences(), which had to be called because preferences is an | |
801 // argument, ensures that profile is in the map. | |
802 DCHECK(extension_hosts != extension_hosts_map_.end()); | |
803 if (extension_hosts->second.empty()) | |
804 preferences->AddGalleryChangeObserver(this); | |
805 | |
806 ExtensionGalleriesHost* result = extension_hosts->second[extension_id].get(); | |
807 if (!result) { | |
808 result = new ExtensionGalleriesHost( | |
809 file_system_context_.get(), | |
810 profile->GetPath(), | |
811 extension_id, | |
812 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, | |
813 base::Unretained(this), | |
814 profile, | |
815 extension_id)); | |
816 extension_hosts_map_[profile][extension_id] = result; | |
817 } | |
818 return result; | |
819 } | |
820 | |
701 void MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty( | 821 void MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty( |
702 Profile* profile, const std::string& extension_id) { | 822 Profile* profile, const std::string& extension_id) { |
703 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 823 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
704 | 824 |
705 ExtensionGalleriesHostMap::iterator extension_hosts = | 825 ExtensionGalleriesHostMap::iterator extension_hosts = |
706 extension_hosts_map_.find(profile); | 826 extension_hosts_map_.find(profile); |
707 DCHECK(extension_hosts != extension_hosts_map_.end()); | 827 DCHECK(extension_hosts != extension_hosts_map_.end()); |
708 ExtensionHostMap::size_type erase_count = | 828 ExtensionHostMap::size_type erase_count = |
709 extension_hosts->second.erase(extension_id); | 829 extension_hosts->second.erase(extension_id); |
710 DCHECK_EQ(1U, erase_count); | 830 DCHECK_EQ(1U, erase_count); |
711 if (extension_hosts->second.empty()) { | 831 if (extension_hosts->second.empty()) { |
712 // When a profile has no ExtensionGalleriesHosts left, remove the | 832 // When a profile has no ExtensionGalleriesHosts left, remove the |
713 // matching gallery-change-watcher since it is no longer needed. Leave the | 833 // matching gallery-change-watcher since it is no longer needed. Leave the |
714 // |extension_hosts| entry alone, since it indicates the profile has been | 834 // |extension_hosts| entry alone, since it indicates the profile has been |
715 // previously used. | 835 // previously used. |
716 MediaGalleriesPreferences* preferences = GetPreferences(profile); | 836 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
717 preferences->RemoveGalleryChangeObserver(this); | 837 preferences->RemoveGalleryChangeObserver(this); |
718 } | 838 } |
719 } | 839 } |
OLD | NEW |