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

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: Switch approaches 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 19 matching lines...) Expand all
30 #include "chrome/common/extensions/extension_set.h" 30 #include "chrome/common/extensions/extension_set.h"
31 #include "chrome/common/pref_names.h" 31 #include "chrome/common/pref_names.h"
32 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/notification_details.h" 33 #include "content/public/browser/notification_details.h"
34 #include "content/public/browser/notification_observer.h" 34 #include "content/public/browser/notification_observer.h"
35 #include "content/public/browser/notification_registrar.h" 35 #include "content/public/browser/notification_registrar.h"
36 #include "content/public/browser/notification_source.h" 36 #include "content/public/browser/notification_source.h"
37 #include "content/public/browser/notification_types.h" 37 #include "content/public/browser/notification_types.h"
38 #include "content/public/browser/render_process_host.h" 38 #include "content/public/browser/render_process_host.h"
39 #include "content/public/browser/render_view_host.h" 39 #include "content/public/browser/render_view_host.h"
40 #include "content/public/browser/render_view_host_observer.h"
40 #include "content/public/browser/web_contents.h" 41 #include "content/public/browser/web_contents.h"
41 #include "webkit/fileapi/file_system_types.h" 42 #include "webkit/fileapi/file_system_types.h"
42 #include "webkit/fileapi/isolated_context.h" 43 #include "webkit/fileapi/isolated_context.h"
43 44
44 using content::BrowserThread; 45 using content::BrowserThread;
45 using content::NavigationController; 46 using content::NavigationController;
46 using content::RenderProcessHost; 47 using content::RenderProcessHost;
47 using content::WebContents; 48 using content::WebContents;
48 using fileapi::IsolatedContext; 49 using fileapi::IsolatedContext;
49 50
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 // host object. 403 // host object.
403 MediaDeviceEntryReferencesMap media_device_map_references_; 404 MediaDeviceEntryReferencesMap media_device_map_references_;
404 405
405 // The set of render processes and web contents that may have references to 406 // The set of render processes and web contents that may have references to
406 // the file system ids this instance manages. 407 // the file system ids this instance manages.
407 RPHReferenceManager rph_refs_; 408 RPHReferenceManager rph_refs_;
408 409
409 DISALLOW_COPY_AND_ASSIGN(ExtensionGalleriesHost); 410 DISALLOW_COPY_AND_ASSIGN(ExtensionGalleriesHost);
410 }; 411 };
411 412
413 // This self-deleting transaction object contains the state needed to
414 // asynchronously initialize the storage monitor if needed when an
415 // extension calles |GetMediaFileSystemsForExtension|. Specifically,
416 // it tracks if the RenderViewHost with which it was initialized was
417 // destroyed, and if so, it eats the callback.
418 // When it receives the OnPreferences callback, it deletes itself.
419 class GetPreferencesTransaction : public content::RenderViewHostObserver {
420 public:
421 GetPreferencesTransaction(
422 content::RenderViewHost* rvh,
423 base::Callback<void(MediaGalleriesPreferences::Vendor*)> callback)
424 : content::RenderViewHostObserver(rvh),
425 callback_(callback) {}
426 virtual ~GetPreferencesTransaction() {}
427
428 virtual void RenderViewHostDestroyed(content::RenderViewHost* rvh) OVERRIDE {
429 callback_.Reset();
430 }
431
432 void OnPreferences(MediaGalleriesPreferences::Vendor* vendor) {
433 if (!callback_.is_null())
434 callback_.Run(vendor);
435
436 delete this;
437 }
438
439 private:
440 base::Callback<void(MediaGalleriesPreferences::Vendor*)> callback_;
441 };
442
412 /****************** 443 /******************
413 * Public methods 444 * Public methods
414 ******************/ 445 ******************/
415 446
416 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension( 447 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension(
417 const content::RenderViewHost* rvh, 448 content::RenderViewHost* rvh,
418 const extensions::Extension* extension, 449 const extensions::Extension* extension,
419 const MediaFileSystemsCallback& callback) { 450 const MediaFileSystemsCallback& callback) {
420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
421 452
453 // This object is self-deleting when it receives the callback
454 // from GetPreferencesAsync.
455 GetPreferencesTransaction* transaction = new GetPreferencesTransaction(
456 rvh,
457 base::Bind(
458 &MediaFileSystemRegistry::GetMediaFileSystemsWithPreferences,
459 weak_ptr_factory_.GetWeakPtr(),
460 rvh, extension, callback));
461 GetPreferencesAsync(
462 base::Bind(&GetPreferencesTransaction::OnPreferences,
463 base::Unretained(transaction)));
464 }
465
466 void MediaFileSystemRegistry::GetMediaFileSystemsWithPreferences(
467 const content::RenderViewHost* rvh,
468 const extensions::Extension* extension,
469 const MediaFileSystemsCallback& callback,
470 MediaGalleriesPreferences::Vendor* vendor) {
422 Profile* profile = 471 Profile* profile =
423 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); 472 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext());
424 MediaGalleriesPreferences* preferences = GetPreferences(profile); 473 MediaGalleriesPreferences* preferences = vendor->GetPreferences(profile);
425 MediaGalleryPrefIdSet galleries = 474 MediaGalleryPrefIdSet galleries =
426 preferences->GalleriesForExtension(*extension); 475 preferences->GalleriesForExtension(*extension);
427 476
428 if (galleries.empty()) { 477 if (galleries.empty()) {
429 callback.Run(std::vector<MediaFileSystemInfo>()); 478 callback.Run(std::vector<MediaFileSystemInfo>());
430 return; 479 return;
431 } 480 }
432 481
433 if (!ContainsKey(pref_change_registrar_map_, profile)) { 482 if (!ContainsKey(pref_change_registrar_map_, profile)) {
434 PrefChangeRegistrar* pref_registrar = new PrefChangeRegistrar; 483 PrefChangeRegistrar* pref_registrar = new PrefChangeRegistrar;
(...skipping 14 matching lines...) Expand all
449 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, 498 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty,
450 base::Unretained(this), profile, extension->id())); 499 base::Unretained(this), profile, extension->id()));
451 extension_hosts_map_[profile][extension->id()] = extension_host; 500 extension_hosts_map_[profile][extension->id()] = extension_host;
452 } 501 }
453 extension_host->ReferenceFromRVH(rvh); 502 extension_host->ReferenceFromRVH(rvh);
454 503
455 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), 504 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(),
456 callback); 505 callback);
457 } 506 }
458 507
508 void MediaFileSystemRegistry::GetPreferencesAsync(
509 base::Callback<void(MediaGalleriesPreferences::Vendor*)> callback) {
510 StorageMonitor* monitor = StorageMonitor::GetInstance();
511 // TODO(gbillock): Make sure tests all have a test storage monitor
512 // and eliminate the checks here and elsewhere.
513 if (!monitor) {
514 callback.Run(this);
515 return;
516 }
517
518 if (monitor->IsInitialized()) {
519 callback.Run(this);
520 return;
521 }
522
523 monitor->Initialize(
524 base::Bind(&MediaFileSystemRegistry::OnStorageMonitorInitialized,
525 weak_ptr_factory_.GetWeakPtr(), callback));
526
527 // TODO(gbillock): set a timeout here to bail out of init?
528 }
529
530 void MediaFileSystemRegistry::OnStorageMonitorInitialized(
531 base::Callback<void(MediaGalleriesPreferences::Vendor*)> callback) {
532 callback.Run(this);
533 }
534
459 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( 535 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences(
460 Profile* profile) { 536 Profile* profile) {
461 MediaGalleriesPreferences* preferences = 537 MediaGalleriesPreferences* preferences =
462 MediaGalleriesPreferencesFactory::GetForProfile(profile); 538 MediaGalleriesPreferencesFactory::GetForProfile(profile);
463 if (ContainsKey(extension_hosts_map_, profile)) 539 if (ContainsKey(extension_hosts_map_, profile))
464 return preferences; 540 return preferences;
465 541
466 // Create an empty entry so the initialization code below only gets called 542 // Create an empty entry so the initialization code below only gets called
467 // once per profile. 543 // once per profile.
468 extension_hosts_map_[profile] = ExtensionHostMap(); 544 extension_hosts_map_[profile] = ExtensionHostMap();
469 545
470 // TODO(gbillock): Move this stanza to MediaGalleriesPreferences init code. 546 // TODO(gbillock): Move this stanza to MediaGalleriesPreferences init code.
547 // Or better, just make sure tests have a storage monitor.
471 // StorageMonitor may be NULL in unit tests. 548 // StorageMonitor may be NULL in unit tests.
472 StorageMonitor* monitor = StorageMonitor::GetInstance(); 549 StorageMonitor* monitor = StorageMonitor::GetInstance();
473 if (!monitor) 550 if (!monitor)
474 return preferences; 551 return preferences;
552 DCHECK(monitor->IsInitialized());
475 std::vector<StorageInfo> existing_devices = monitor->GetAttachedStorage(); 553 std::vector<StorageInfo> existing_devices = monitor->GetAttachedStorage();
476 for (size_t i = 0; i < existing_devices.size(); i++) { 554 for (size_t i = 0; i < existing_devices.size(); i++) {
477 if (!StorageInfo::IsMediaDevice(existing_devices[i].device_id)) 555 if (!StorageInfo::IsMediaDevice(existing_devices[i].device_id))
478 continue; 556 continue;
479 if (!existing_devices[i].name.empty()) { 557 if (!existing_devices[i].name.empty()) {
480 preferences->AddGalleryWithName(existing_devices[i].device_id, 558 preferences->AddGalleryWithName(existing_devices[i].device_id,
481 existing_devices[i].name, 559 existing_devices[i].name,
482 base::FilePath(), 560 base::FilePath(),
483 false /*not user added*/); 561 false /*not user added*/);
484 } else { 562 } else {
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 return registry_; 687 return registry_;
610 } 688 }
611 689
612 private: 690 private:
613 MediaFileSystemRegistry* registry_; 691 MediaFileSystemRegistry* registry_;
614 692
615 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); 693 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl);
616 }; 694 };
617 695
618 MediaFileSystemRegistry::MediaFileSystemRegistry() 696 MediaFileSystemRegistry::MediaFileSystemRegistry()
619 : file_system_context_(new MediaFileSystemContextImpl(this)) { 697 : file_system_context_(new MediaFileSystemContextImpl(this)),
698 weak_ptr_factory_(this) {
620 // StorageMonitor may be NULL in unit tests. 699 // StorageMonitor may be NULL in unit tests.
621 StorageMonitor* monitor = StorageMonitor::GetInstance(); 700 StorageMonitor* monitor = StorageMonitor::GetInstance();
622 if (monitor) 701 if (monitor)
623 monitor->AddObserver(this); 702 monitor->AddObserver(this);
624 } 703 }
625 704
626 MediaFileSystemRegistry::~MediaFileSystemRegistry() { 705 MediaFileSystemRegistry::~MediaFileSystemRegistry() {
627 // StorageMonitor may be NULL in unit tests. 706 // StorageMonitor may be NULL in unit tests.
628 StorageMonitor* monitor = StorageMonitor::GetInstance(); 707 StorageMonitor* monitor = StorageMonitor::GetInstance();
629 if (monitor) 708 if (monitor)
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 // previously used. 799 // previously used.
721 PrefChangeRegistrarMap::iterator pref_it = 800 PrefChangeRegistrarMap::iterator pref_it =
722 pref_change_registrar_map_.find(profile); 801 pref_change_registrar_map_.find(profile);
723 DCHECK(pref_it != pref_change_registrar_map_.end()); 802 DCHECK(pref_it != pref_change_registrar_map_.end());
724 delete pref_it->second; 803 delete pref_it->second;
725 pref_change_registrar_map_.erase(pref_it); 804 pref_change_registrar_map_.erase(pref_it);
726 } 805 }
727 } 806 }
728 807
729 } // namespace chrome 808 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698