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

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

Issue 14556015: [Media Galleries] Lazily initialize the storage monitor. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Improve tracker code Created 7 years, 7 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
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_galleries/media_file_system_registry.h" 7 #include "chrome/browser/media_galleries/media_file_system_registry.h"
8 8
9 #include <set> 9 #include <set>
10 #include <vector> 10 #include <vector>
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 ******************/ 428 ******************/
429 429
430 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension( 430 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension(
431 const content::RenderViewHost* rvh, 431 const content::RenderViewHost* rvh,
432 const extensions::Extension* extension, 432 const extensions::Extension* extension,
433 const MediaFileSystemsCallback& callback) { 433 const MediaFileSystemsCallback& callback) {
434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
435 435
436 Profile* profile = 436 Profile* profile =
437 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); 437 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext());
438 MediaGalleriesPreferences* preferences = GetPreferences(profile); 438 GetPreferencesAsync(
439 profile, base::Bind(
440 &MediaFileSystemRegistry::GetMediaFileSystemsPostStorageMonitorInit,
441 weak_ptr_factory_.GetWeakPtr(),
442 rvh, extension, callback));
443 }
444
445 void MediaFileSystemRegistry::GetMediaFileSystemsPostStorageMonitorInit(
446 const content::RenderViewHost* rvh,
447 const extensions::Extension* extension,
448 const MediaFileSystemsCallback& callback,
449 MediaGalleriesPreferences* preferences) {
439 MediaGalleryPrefIdSet galleries = 450 MediaGalleryPrefIdSet galleries =
440 preferences->GalleriesForExtension(*extension); 451 preferences->GalleriesForExtension(*extension);
441 452
442 if (galleries.empty()) { 453 if (galleries.empty()) {
443 callback.Run(std::vector<MediaFileSystemInfo>()); 454 callback.Run(std::vector<MediaFileSystemInfo>());
444 return; 455 return;
445 } 456 }
446 457
458 Profile* profile =
459 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext());
vandebo (ex-Chrome) 2013/05/17 22:19:58 Hmm, could the rvh have gone away while we were ge
Greg Billock 2013/05/18 04:44:58 I think so. Here's my current thinking. We need t
447 if (!ContainsKey(pref_change_registrar_map_, profile)) { 460 if (!ContainsKey(pref_change_registrar_map_, profile)) {
448 PrefChangeRegistrar* pref_registrar = new PrefChangeRegistrar; 461 PrefChangeRegistrar* pref_registrar = new PrefChangeRegistrar;
449 pref_registrar->Init(profile->GetPrefs()); 462 pref_registrar->Init(profile->GetPrefs());
450 pref_registrar->Add( 463 pref_registrar->Add(
451 prefs::kMediaGalleriesRememberedGalleries, 464 prefs::kMediaGalleriesRememberedGalleries,
452 base::Bind(&MediaFileSystemRegistry::OnRememberedGalleriesChanged, 465 base::Bind(&MediaFileSystemRegistry::OnRememberedGalleriesChanged,
453 base::Unretained(this), 466 base::Unretained(this),
454 pref_registrar->prefs())); 467 pref_registrar->prefs()));
455 pref_change_registrar_map_[profile] = pref_registrar; 468 pref_change_registrar_map_[profile] = pref_registrar;
456 } 469 }
457 470
458 ExtensionGalleriesHost* extension_host = 471 ExtensionGalleriesHost* extension_host =
459 extension_hosts_map_[profile][extension->id()].get(); 472 extension_hosts_map_[profile][extension->id()].get();
460 if (!extension_host) { 473 if (!extension_host) {
461 extension_host = new ExtensionGalleriesHost( 474 extension_host = new ExtensionGalleriesHost(
462 file_system_context_.get(), 475 file_system_context_.get(),
463 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, 476 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty,
464 base::Unretained(this), profile, extension->id())); 477 base::Unretained(this), profile, extension->id()));
465 extension_hosts_map_[profile][extension->id()] = extension_host; 478 extension_hosts_map_[profile][extension->id()] = extension_host;
466 } 479 }
467 extension_host->ReferenceFromRVH(rvh); 480 extension_host->ReferenceFromRVH(rvh);
468 481
469 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), 482 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(),
470 callback); 483 callback);
471 } 484 }
472 485
486 void MediaFileSystemRegistry::GetPreferencesAsync(
487 Profile* profile,
488 base::Callback<void(MediaGalleriesPreferences*)> callback) {
489 StorageMonitor* monitor = StorageMonitor::GetInstance();
490 // TODO(gbillock): Make sure tests all have a test storage monitor
491 // and eliminate the checks here and elsewhere.
492 if (!monitor) {
493 callback.Run(MediaGalleriesPreferencesFactory::GetForProfile(profile));
494 return;
495 }
496
497 if (monitor->IsInitialized()) {
498 callback.Run(GetPreferences(profile));
499 return;
500 }
501
502 monitor->Initialize(
503 base::Bind(&MediaFileSystemRegistry::OnStorageMonitorInitialized,
504 weak_ptr_factory_.GetWeakPtr(),
505 profile, callback));
506
507 // TODO(gbillock): set a timeout here to bail out of init?
508 }
509
510 void MediaFileSystemRegistry::OnStorageMonitorInitialized(
511 Profile* profile,
512 base::Callback<void(MediaGalleriesPreferences*)> callback) {
513 callback.Run(GetPreferences(profile));
514 }
515
473 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( 516 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences(
474 Profile* profile) { 517 Profile* profile) {
475 MediaGalleriesPreferences* preferences = 518 MediaGalleriesPreferences* preferences =
476 MediaGalleriesPreferencesFactory::GetForProfile(profile); 519 MediaGalleriesPreferencesFactory::GetForProfile(profile);
477 if (ContainsKey(extension_hosts_map_, profile)) 520 if (ContainsKey(extension_hosts_map_, profile))
478 return preferences; 521 return preferences;
479 522
480 // Create an empty entry so the initialization code below only gets called 523 // Create an empty entry so the initialization code below only gets called
481 // once per profile. 524 // once per profile.
482 extension_hosts_map_[profile] = ExtensionHostMap(); 525 extension_hosts_map_[profile] = ExtensionHostMap();
483 526
484 // TODO(gbillock): Move this stanza to MediaGalleriesPreferences init code. 527 // TODO(gbillock): Move this stanza to MediaGalleriesPreferences init code.
485 // StorageMonitor may be NULL in unit tests. 528 // StorageMonitor may be NULL in unit tests.
486 StorageMonitor* monitor = StorageMonitor::GetInstance(); 529 StorageMonitor* monitor = StorageMonitor::GetInstance();
487 if (!monitor) 530 if (!monitor)
488 return preferences; 531 return preferences;
532 DCHECK(monitor->IsInitialized());
489 std::vector<StorageInfo> existing_devices = monitor->GetAttachedStorage(); 533 std::vector<StorageInfo> existing_devices = monitor->GetAttachedStorage();
490 for (size_t i = 0; i < existing_devices.size(); i++) { 534 for (size_t i = 0; i < existing_devices.size(); i++) {
491 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id)) 535 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id))
492 continue; 536 continue;
493 if (!existing_devices[i].name.empty()) { 537 if (!existing_devices[i].name.empty()) {
494 preferences->AddGalleryWithName(existing_devices[i].device_id, 538 preferences->AddGalleryWithName(existing_devices[i].device_id,
495 existing_devices[i].name, 539 existing_devices[i].name,
496 base::FilePath(), 540 base::FilePath(),
497 false /*not user added*/); 541 false /*not user added*/);
498 } else { 542 } else {
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 return registry_; 669 return registry_;
626 } 670 }
627 671
628 private: 672 private:
629 MediaFileSystemRegistry* registry_; 673 MediaFileSystemRegistry* registry_;
630 674
631 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); 675 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl);
632 }; 676 };
633 677
634 MediaFileSystemRegistry::MediaFileSystemRegistry() 678 MediaFileSystemRegistry::MediaFileSystemRegistry()
635 : file_system_context_(new MediaFileSystemContextImpl(this)) { 679 : file_system_context_(new MediaFileSystemContextImpl(this)),
680 weak_ptr_factory_(this) {
636 // StorageMonitor may be NULL in unit tests. 681 // StorageMonitor may be NULL in unit tests.
637 StorageMonitor* monitor = StorageMonitor::GetInstance(); 682 StorageMonitor* monitor = StorageMonitor::GetInstance();
638 if (monitor) 683 if (monitor)
639 monitor->AddObserver(this); 684 monitor->AddObserver(this);
640 } 685 }
641 686
642 MediaFileSystemRegistry::~MediaFileSystemRegistry() { 687 MediaFileSystemRegistry::~MediaFileSystemRegistry() {
643 // StorageMonitor may be NULL in unit tests. 688 // StorageMonitor may be NULL in unit tests.
644 StorageMonitor* monitor = StorageMonitor::GetInstance(); 689 StorageMonitor* monitor = StorageMonitor::GetInstance();
645 if (monitor) 690 if (monitor)
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 // previously used. 785 // previously used.
741 PrefChangeRegistrarMap::iterator pref_it = 786 PrefChangeRegistrarMap::iterator pref_it =
742 pref_change_registrar_map_.find(profile); 787 pref_change_registrar_map_.find(profile);
743 DCHECK(pref_it != pref_change_registrar_map_.end()); 788 DCHECK(pref_it != pref_change_registrar_map_.end());
744 delete pref_it->second; 789 delete pref_it->second;
745 pref_change_registrar_map_.erase(pref_it); 790 pref_change_registrar_map_.erase(pref_it);
746 } 791 }
747 } 792 }
748 793
749 } // namespace chrome 794 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698