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

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

Issue 24269007: Media Galleries API: Fix MediaGalleriesPreferences finders race. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 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 #include "chrome/browser/media_galleries/media_galleries_preferences.h" 5 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
6 6
7 #include "base/base_paths_posix.h" 7 #include "base/base_paths_posix.h"
8 #include "base/callback.h"
8 #include "base/i18n/time_formatting.h" 9 #include "base/i18n/time_formatting.h"
9 #include "base/path_service.h" 10 #include "base/path_service.h"
10 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h" 12 #include "base/stl_util.h"
12 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h" 16 #include "base/values.h"
16 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api.h" 18 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api.h"
18 #include "chrome/browser/extensions/extension_service.h" 19 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/extension_system.h" 20 #include "chrome/browser/extensions/extension_system.h"
20 #include "chrome/browser/media_galleries/fileapi/iphoto_finder_mac.h" 21 #include "chrome/browser/media_galleries/fileapi/iphoto_finder_mac.h"
21 #include "chrome/browser/media_galleries/fileapi/itunes_finder.h" 22 #include "chrome/browser/media_galleries/fileapi/itunes_finder.h"
22 #include "chrome/browser/media_galleries/fileapi/picasa_finder.h" 23 #include "chrome/browser/media_galleries/fileapi/picasa_finder.h"
23 #include "chrome/browser/media_galleries/media_file_system_registry.h" 24 #include "chrome/browser/media_galleries/media_file_system_registry.h"
24 #include "chrome/browser/prefs/scoped_user_pref_update.h" 25 #include "chrome/browser/prefs/scoped_user_pref_update.h"
25 #include "chrome/browser/profiles/profile.h" 26 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/storage_monitor/media_storage_util.h" 27 #include "chrome/browser/storage_monitor/media_storage_util.h"
27 #include "chrome/browser/storage_monitor/storage_monitor.h" 28 #include "chrome/browser/storage_monitor/storage_monitor.h"
28 #include "chrome/common/chrome_paths.h" 29 #include "chrome/common/chrome_paths.h"
29 #include "chrome/common/extensions/extension.h" 30 #include "chrome/common/extensions/extension.h"
30 #include "chrome/common/extensions/permissions/api_permission.h" 31 #include "chrome/common/extensions/permissions/api_permission.h"
31 #include "chrome/common/extensions/permissions/media_galleries_permission.h" 32 #include "chrome/common/extensions/permissions/media_galleries_permission.h"
32 #include "chrome/common/extensions/permissions/permissions_data.h" 33 #include "chrome/common/extensions/permissions/permissions_data.h"
33 #include "chrome/common/pref_names.h" 34 #include "chrome/common/pref_names.h"
34 #include "components/user_prefs/pref_registry_syncable.h" 35 #include "components/user_prefs/pref_registry_syncable.h"
36 #include "content/public/browser/browser_thread.h"
35 #include "grit/generated_resources.h" 37 #include "grit/generated_resources.h"
36 #include "ui/base/l10n/l10n_util.h" 38 #include "ui/base/l10n/l10n_util.h"
37 #include "ui/base/text/bytes_formatting.h" 39 #include "ui/base/text/bytes_formatting.h"
38 40
39 using base::DictionaryValue; 41 using base::DictionaryValue;
40 using base::ListValue; 42 using base::ListValue;
41 using extensions::ExtensionPrefs; 43 using extensions::ExtensionPrefs;
42 44
43 namespace { 45 namespace {
44 46
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 341
340 bool MediaGalleryPrefInfo::IsGalleryAvailable() const { 342 bool MediaGalleryPrefInfo::IsGalleryAvailable() const {
341 return !StorageInfo::IsRemovableDevice(device_id) || 343 return !StorageInfo::IsRemovableDevice(device_id) ||
342 MediaStorageUtil::IsRemovableStorageAttached(device_id); 344 MediaStorageUtil::IsRemovableStorageAttached(device_id);
343 } 345 }
344 346
345 MediaGalleriesPreferences::GalleryChangeObserver::~GalleryChangeObserver() {} 347 MediaGalleriesPreferences::GalleryChangeObserver::~GalleryChangeObserver() {}
346 348
347 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) 349 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile)
348 : weak_factory_(this), 350 : weak_factory_(this),
351 initialized_(false),
352 pre_initialization_callbacks_waiting_(0),
349 profile_(profile), 353 profile_(profile),
350 extension_prefs_for_testing_(NULL) { 354 extension_prefs_for_testing_(NULL) {}
355
356 MediaGalleriesPreferences::~MediaGalleriesPreferences() {
357 if (StorageMonitor::GetInstance())
358 StorageMonitor::GetInstance()->RemoveObserver(this);
359 }
360
361 void MediaGalleriesPreferences::EnsureInitialized(base::Closure callback) {
362 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
363
364 if (IsInitialized()) {
365 if (!callback.is_null())
366 callback.Run();
367 return;
368 }
369
370 on_initialize_callbacks_.push_back(callback);
371 if (on_initialize_callbacks_.size() > 1)
372 return;
373
351 AddDefaultGalleriesIfFreshProfile(); 374 AddDefaultGalleriesIfFreshProfile();
352 375
376 // This counter must match the number of async methods dispatched below.
377 // It cannot be incremented inline with each callback, as some may return
378 // synchronously, decrement the counter to 0, and prematurely trigger
379 // FinishInitialization.
380 pre_initialization_callbacks_waiting_ = 2;
381
382 // Ensure StorageMonitor is initialized.
383 StorageMonitor::GetInstance()->EnsureInitialized(
384 base::Bind(&MediaGalleriesPreferences::OnInitializationCallbackReturned,
385 weak_factory_.GetWeakPtr()));
386
353 // Look for optional default galleries every time. 387 // Look for optional default galleries every time.
354 itunes::ITunesFinder::FindITunesLibrary( 388 itunes::ITunesFinder::FindITunesLibrary(
355 base::Bind(&MediaGalleriesPreferences::OnITunesDeviceID, 389 base::Bind(&MediaGalleriesPreferences::OnFinderDeviceID,
356 weak_factory_.GetWeakPtr())); 390 weak_factory_.GetWeakPtr()));
357 391
358 #if 0 392 #if 0
359 iphoto::FindIPhotoLibrary( 393 iphoto::FindIPhotoLibrary(
360 base::Bind(&MediaGalleriesPreferences::OnIPhotoDeviceID, 394 base::Bind(&MediaGalleriesPreferences::OnFinderDeviceID,
361 weak_factory_.GetWeakPtr())); 395 weak_factory_.GetWeakPtr()));
362 #endif 396 #endif
363 397
364 // TODO(tommycli): Turn on when Picasa code is ready. 398 // TODO(tommycli): Turn on when Picasa code is ready.
365 #if 0 399 #if 0
366 picasa::PicasaFinder::FindPicasaDatabaseOnUIThread( 400 picasa::PicasaFinder::FindPicasaDatabaseOnUIThread(
367 base::Bind(&MediaGalleriesPreferences::OnPicasaDeviceID, 401 base::Bind(&MediaGalleriesPreferences::OnFinderDeviceID,
368 weak_factory_.GetWeakPtr())); 402 weak_factory_.GetWeakPtr()));
369 #endif 403 #endif
404 }
405
406 bool MediaGalleriesPreferences::IsInitialized() const { return initialized_; }
407
408 Profile* MediaGalleriesPreferences::profile() { return profile_; }
409
410 void MediaGalleriesPreferences::OnInitializationCallbackReturned() {
411 DCHECK(!IsInitialized());
412 DCHECK(pre_initialization_callbacks_waiting_ > 0);
413 if (--pre_initialization_callbacks_waiting_ == 0)
414 FinishInitialization();
415 }
416
417 void MediaGalleriesPreferences::FinishInitialization() {
418 DCHECK(!IsInitialized());
419
420 initialized_ = true;
421
422 StorageMonitor* monitor = StorageMonitor::GetInstance();
423 DCHECK(monitor->IsInitialized());
370 424
371 InitFromPrefs(); 425 InitFromPrefs();
372 426
373 StorageMonitor::GetInstance()->AddObserver(this); 427 StorageMonitor::GetInstance()->AddObserver(this);
374 }
375 428
376 MediaGalleriesPreferences::~MediaGalleriesPreferences() { 429 std::vector<StorageInfo> existing_devices =
377 if (StorageMonitor::GetInstance()) 430 monitor->GetAllAvailableStorages();
378 StorageMonitor::GetInstance()->RemoveObserver(this); 431 for (size_t i = 0; i < existing_devices.size(); i++) {
379 } 432 if (!(StorageInfo::IsMediaDevice(existing_devices[i].device_id()) &&
433 StorageInfo::IsRemovableDevice(existing_devices[i].device_id())))
434 continue;
435 AddGallery(existing_devices[i].device_id(),
436 base::FilePath(),
437 false,
438 existing_devices[i].storage_label(),
439 existing_devices[i].vendor_name(),
440 existing_devices[i].model_name(),
441 existing_devices[i].total_size_in_bytes(),
442 base::Time::Now());
443 }
380 444
381 Profile* MediaGalleriesPreferences::profile() { 445 for (std::vector<base::Closure>::iterator iter =
382 return profile_; 446 on_initialize_callbacks_.begin();
447 iter != on_initialize_callbacks_.end();
448 ++iter) {
449 iter->Run();
450 }
451 on_initialize_callbacks_.clear();
383 } 452 }
384 453
385 void MediaGalleriesPreferences::AddDefaultGalleriesIfFreshProfile() { 454 void MediaGalleriesPreferences::AddDefaultGalleriesIfFreshProfile() {
386 // Only add defaults the first time. 455 // Only add defaults the first time.
387 if (APIHasBeenUsed(profile_)) 456 if (APIHasBeenUsed(profile_))
388 return; 457 return;
389 458
390 // Fresh profile case. 459 // Fresh profile case.
391 const int kDirectoryKeys[] = { 460 const int kDirectoryKeys[] = {
392 chrome::DIR_USER_MUSIC, 461 chrome::DIR_USER_MUSIC,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 FOR_EACH_OBSERVER(GalleryChangeObserver, 512 FOR_EACH_OBSERVER(GalleryChangeObserver,
444 gallery_change_observers_, 513 gallery_change_observers_,
445 OnGalleryInfoUpdated(this, pref_id)); 514 OnGalleryInfoUpdated(this, pref_id));
446 } 515 }
447 return true; 516 return true;
448 } 517 }
449 } 518 }
450 return false; 519 return false;
451 } 520 }
452 521
453 void MediaGalleriesPreferences::OnITunesDeviceID(const std::string& device_id) { 522 void MediaGalleriesPreferences::OnFinderDeviceID(const std::string& device_id) {
454 if (device_id.empty()) 523 if (!device_id.empty() && !UpdateDeviceIDForSingletonType(device_id)) {
455 return; 524 std::string gallery_name;
456 if (!UpdateDeviceIDForSingletonType(device_id)) { 525 if (StorageInfo::IsIPhotoDevice(device_id))
457 AddGalleryInternal(device_id, ASCIIToUTF16(kITunesGalleryName), 526 gallery_name = kIPhotoGalleryName;
527 else if (StorageInfo::IsITunesDevice(device_id))
528 gallery_name = kITunesGalleryName;
529 else if (StorageInfo::IsPicasaDevice(device_id))
530 gallery_name = kPicasaGalleryName;
531 else
532 NOTREACHED();
533
534 AddGalleryInternal(device_id, ASCIIToUTF16(gallery_name),
458 base::FilePath(), false /*not user added*/, 535 base::FilePath(), false /*not user added*/,
459 string16(), string16(), string16(), 0, 536 string16(), string16(), string16(), 0,
460 base::Time(), false, 2); 537 base::Time(), false, 2);
461 } 538 }
462 }
463 539
464 void MediaGalleriesPreferences::OnIPhotoDeviceID(const std::string& device_id) { 540 OnInitializationCallbackReturned();
465 if (device_id.empty())
466 return;
467 if (!UpdateDeviceIDForSingletonType(device_id)) {
468 AddGalleryInternal(device_id, ASCIIToUTF16(kIPhotoGalleryName),
469 base::FilePath(), false /*not user added*/,
470 string16(), string16(), string16(), 0,
471 base::Time(), false, 2);
472 }
473 }
474
475 void MediaGalleriesPreferences::OnPicasaDeviceID(const std::string& device_id) {
476 DCHECK(!device_id.empty());
477 if (!UpdateDeviceIDForSingletonType(device_id)) {
478 AddGalleryInternal(device_id, ASCIIToUTF16(kPicasaGalleryName),
479 base::FilePath(), false /*not user added*/,
480 string16(), string16(), string16(), 0,
481 base::Time(), false, 2);
482 }
483 } 541 }
484 542
485 void MediaGalleriesPreferences::InitFromPrefs() { 543 void MediaGalleriesPreferences::InitFromPrefs() {
486 known_galleries_.clear(); 544 known_galleries_.clear();
487 device_map_.clear(); 545 device_map_.clear();
488 546
489 PrefService* prefs = profile_->GetPrefs(); 547 PrefService* prefs = profile_->GetPrefs();
490 const ListValue* list = prefs->GetList( 548 const ListValue* list = prefs->GetList(
491 prefs::kMediaGalleriesRememberedGalleries); 549 prefs::kMediaGalleriesRememberedGalleries);
492 if (list) { 550 if (list) {
493 for (ListValue::const_iterator it = list->begin(); 551 for (ListValue::const_iterator it = list->begin();
494 it != list->end(); ++it) { 552 it != list->end(); ++it) {
495 const DictionaryValue* dict = NULL; 553 const DictionaryValue* dict = NULL;
496 if (!(*it)->GetAsDictionary(&dict)) 554 if (!(*it)->GetAsDictionary(&dict))
497 continue; 555 continue;
498 556
499 MediaGalleryPrefInfo gallery_info; 557 MediaGalleryPrefInfo gallery_info;
500 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) 558 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info))
501 continue; 559 continue;
502 560
503 known_galleries_[gallery_info.pref_id] = gallery_info; 561 known_galleries_[gallery_info.pref_id] = gallery_info;
504 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); 562 device_map_[gallery_info.device_id].insert(gallery_info.pref_id);
505 } 563 }
506 } 564 }
507 } 565 }
508 566
509 void MediaGalleriesPreferences::AddGalleryChangeObserver( 567 void MediaGalleriesPreferences::AddGalleryChangeObserver(
510 GalleryChangeObserver* observer) { 568 GalleryChangeObserver* observer) {
569 DCHECK(IsInitialized());
511 gallery_change_observers_.AddObserver(observer); 570 gallery_change_observers_.AddObserver(observer);
512 } 571 }
513 572
514 void MediaGalleriesPreferences::RemoveGalleryChangeObserver( 573 void MediaGalleriesPreferences::RemoveGalleryChangeObserver(
515 GalleryChangeObserver* observer) { 574 GalleryChangeObserver* observer) {
575 DCHECK(IsInitialized());
516 gallery_change_observers_.RemoveObserver(observer); 576 gallery_change_observers_.RemoveObserver(observer);
517 } 577 }
518 578
519 void MediaGalleriesPreferences::OnRemovableStorageAttached( 579 void MediaGalleriesPreferences::OnRemovableStorageAttached(
520 const StorageInfo& info) { 580 const StorageInfo& info) {
581 DCHECK(IsInitialized());
521 if (!StorageInfo::IsMediaDevice(info.device_id())) 582 if (!StorageInfo::IsMediaDevice(info.device_id()))
522 return; 583 return;
523 584
524 AddGallery(info.device_id(), base::FilePath(), 585 AddGallery(info.device_id(), base::FilePath(),
525 false /*not user added*/, 586 false /*not user added*/,
526 info.storage_label(), 587 info.storage_label(),
527 info.vendor_name(), 588 info.vendor_name(),
528 info.model_name(), 589 info.model_name(),
529 info.total_size_in_bytes(), 590 info.total_size_in_bytes(),
530 base::Time::Now()); 591 base::Time::Now());
531 } 592 }
532 593
533 bool MediaGalleriesPreferences::LookUpGalleryByPath( 594 bool MediaGalleriesPreferences::LookUpGalleryByPath(
534 const base::FilePath& path, 595 const base::FilePath& path,
535 MediaGalleryPrefInfo* gallery_info) const { 596 MediaGalleryPrefInfo* gallery_info) const {
597 DCHECK(IsInitialized());
536 StorageInfo info; 598 StorageInfo info;
537 base::FilePath relative_path; 599 base::FilePath relative_path;
538 if (!MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path)) { 600 if (!MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path)) {
539 if (gallery_info) 601 if (gallery_info)
540 *gallery_info = MediaGalleryPrefInfo(); 602 *gallery_info = MediaGalleryPrefInfo();
541 return false; 603 return false;
542 } 604 }
543 605
544 relative_path = relative_path.NormalizePathSeparators(); 606 relative_path = relative_path.NormalizePathSeparators();
545 MediaGalleryPrefIdSet galleries_on_device = 607 MediaGalleryPrefIdSet galleries_on_device =
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 DeviceIdPrefIdsMap::const_iterator found = device_map_.find(device_id); 646 DeviceIdPrefIdsMap::const_iterator found = device_map_.find(device_id);
585 if (found == device_map_.end()) 647 if (found == device_map_.end())
586 return MediaGalleryPrefIdSet(); 648 return MediaGalleryPrefIdSet();
587 return found->second; 649 return found->second;
588 } 650 }
589 651
590 base::FilePath MediaGalleriesPreferences::LookUpGalleryPathForExtension( 652 base::FilePath MediaGalleriesPreferences::LookUpGalleryPathForExtension(
591 MediaGalleryPrefId gallery_id, 653 MediaGalleryPrefId gallery_id,
592 const extensions::Extension* extension, 654 const extensions::Extension* extension,
593 bool include_unpermitted_galleries) { 655 bool include_unpermitted_galleries) {
656 DCHECK(IsInitialized());
594 DCHECK(extension); 657 DCHECK(extension);
595 if (!include_unpermitted_galleries && 658 if (!include_unpermitted_galleries &&
596 !ContainsKey(GalleriesForExtension(*extension), gallery_id)) 659 !ContainsKey(GalleriesForExtension(*extension), gallery_id))
597 return base::FilePath(); 660 return base::FilePath();
598 661
599 MediaGalleriesPrefInfoMap::const_iterator it = 662 MediaGalleriesPrefInfoMap::const_iterator it =
600 known_galleries_.find(gallery_id); 663 known_galleries_.find(gallery_id);
601 if (it == known_galleries_.end()) 664 if (it == known_galleries_.end())
602 return base::FilePath(); 665 return base::FilePath();
603 return MediaStorageUtil::FindDevicePathById(it->second.device_id); 666 return MediaStorageUtil::FindDevicePathById(it->second.device_id);
604 } 667 }
605 668
606 MediaGalleryPrefId MediaGalleriesPreferences::AddGallery( 669 MediaGalleryPrefId MediaGalleriesPreferences::AddGallery(
607 const std::string& device_id, 670 const std::string& device_id,
608 const base::FilePath& relative_path, bool user_added, 671 const base::FilePath& relative_path, bool user_added,
609 const string16& volume_label, const string16& vendor_name, 672 const string16& volume_label, const string16& vendor_name,
610 const string16& model_name, uint64 total_size_in_bytes, 673 const string16& model_name, uint64 total_size_in_bytes,
611 base::Time last_attach_time) { 674 base::Time last_attach_time) {
675 DCHECK(IsInitialized());
612 return AddGalleryInternal(device_id, string16(), relative_path, user_added, 676 return AddGalleryInternal(device_id, string16(), relative_path, user_added,
613 volume_label, vendor_name, model_name, 677 volume_label, vendor_name, model_name,
614 total_size_in_bytes, last_attach_time, true, 2); 678 total_size_in_bytes, last_attach_time, true, 2);
615 } 679 }
616 680
617 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryInternal( 681 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryInternal(
618 const std::string& device_id, const string16& display_name, 682 const std::string& device_id, const string16& display_name,
619 const base::FilePath& relative_path, bool user_added, 683 const base::FilePath& relative_path, bool user_added,
620 const string16& volume_label, const string16& vendor_name, 684 const string16& volume_label, const string16& vendor_name,
621 const string16& model_name, uint64 total_size_in_bytes, 685 const string16& model_name, uint64 total_size_in_bytes,
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 InitFromPrefs(); 798 InitFromPrefs();
735 FOR_EACH_OBSERVER(GalleryChangeObserver, 799 FOR_EACH_OBSERVER(GalleryChangeObserver,
736 gallery_change_observers_, 800 gallery_change_observers_,
737 OnGalleryAdded(this, gallery_info.pref_id)); 801 OnGalleryAdded(this, gallery_info.pref_id));
738 802
739 return gallery_info.pref_id; 803 return gallery_info.pref_id;
740 } 804 }
741 805
742 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryByPath( 806 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryByPath(
743 const base::FilePath& path) { 807 const base::FilePath& path) {
808 DCHECK(IsInitialized());
744 MediaGalleryPrefInfo gallery_info; 809 MediaGalleryPrefInfo gallery_info;
745 if (LookUpGalleryByPath(path, &gallery_info) && 810 if (LookUpGalleryByPath(path, &gallery_info) &&
746 gallery_info.type != MediaGalleryPrefInfo::kBlackListed) { 811 gallery_info.type != MediaGalleryPrefInfo::kBlackListed) {
747 return gallery_info.pref_id; 812 return gallery_info.pref_id;
748 } 813 }
749 return AddGalleryInternal(gallery_info.device_id, 814 return AddGalleryInternal(gallery_info.device_id,
750 gallery_info.display_name, 815 gallery_info.display_name,
751 gallery_info.path, 816 gallery_info.path,
752 true /*user added*/, 817 true /*user added*/,
753 gallery_info.volume_label, 818 gallery_info.volume_label,
754 gallery_info.vendor_name, 819 gallery_info.vendor_name,
755 gallery_info.model_name, 820 gallery_info.model_name,
756 gallery_info.total_size_in_bytes, 821 gallery_info.total_size_in_bytes,
757 gallery_info.last_attach_time, 822 gallery_info.last_attach_time,
758 gallery_info.volume_metadata_valid, 823 gallery_info.volume_metadata_valid,
759 gallery_info.prefs_version); 824 gallery_info.prefs_version);
760 } 825 }
761 826
762 void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) { 827 void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) {
828 DCHECK(IsInitialized());
763 PrefService* prefs = profile_->GetPrefs(); 829 PrefService* prefs = profile_->GetPrefs();
764 scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate( 830 scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate(
765 prefs, prefs::kMediaGalleriesRememberedGalleries)); 831 prefs, prefs::kMediaGalleriesRememberedGalleries));
766 ListValue* list = update->Get(); 832 ListValue* list = update->Get();
767 833
768 if (!ContainsKey(known_galleries_, pref_id)) 834 if (!ContainsKey(known_galleries_, pref_id))
769 return; 835 return;
770 836
771 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { 837 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) {
772 DictionaryValue* dict; 838 DictionaryValue* dict;
(...skipping 15 matching lines...) Expand all
788 FOR_EACH_OBSERVER(GalleryChangeObserver, 854 FOR_EACH_OBSERVER(GalleryChangeObserver,
789 gallery_change_observers_, 855 gallery_change_observers_,
790 OnGalleryRemoved(this, pref_id)); 856 OnGalleryRemoved(this, pref_id));
791 return; 857 return;
792 } 858 }
793 } 859 }
794 } 860 }
795 861
796 MediaGalleryPrefIdSet MediaGalleriesPreferences::GalleriesForExtension( 862 MediaGalleryPrefIdSet MediaGalleriesPreferences::GalleriesForExtension(
797 const extensions::Extension& extension) const { 863 const extensions::Extension& extension) const {
864 DCHECK(IsInitialized());
798 MediaGalleryPrefIdSet result; 865 MediaGalleryPrefIdSet result;
799 866
800 if (HasAutoDetectedGalleryPermission(extension)) { 867 if (HasAutoDetectedGalleryPermission(extension)) {
801 for (MediaGalleriesPrefInfoMap::const_iterator it = 868 for (MediaGalleriesPrefInfoMap::const_iterator it =
802 known_galleries_.begin(); it != known_galleries_.end(); ++it) { 869 known_galleries_.begin(); it != known_galleries_.end(); ++it) {
803 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) 870 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
804 result.insert(it->second.pref_id); 871 result.insert(it->second.pref_id);
805 } 872 }
806 } 873 }
807 874
(...skipping 14 matching lines...) Expand all
822 } 889 }
823 } 890 }
824 } 891 }
825 return result; 892 return result;
826 } 893 }
827 894
828 bool MediaGalleriesPreferences::SetGalleryPermissionForExtension( 895 bool MediaGalleriesPreferences::SetGalleryPermissionForExtension(
829 const extensions::Extension& extension, 896 const extensions::Extension& extension,
830 MediaGalleryPrefId pref_id, 897 MediaGalleryPrefId pref_id,
831 bool has_permission) { 898 bool has_permission) {
899 DCHECK(IsInitialized());
832 // The gallery may not exist anymore if the user opened a second config 900 // The gallery may not exist anymore if the user opened a second config
833 // surface concurrently and removed it. Drop the permission update if so. 901 // surface concurrently and removed it. Drop the permission update if so.
834 MediaGalleriesPrefInfoMap::const_iterator gallery_info = 902 MediaGalleriesPrefInfoMap::const_iterator gallery_info =
835 known_galleries_.find(pref_id); 903 known_galleries_.find(pref_id);
836 if (gallery_info == known_galleries_.end()) 904 if (gallery_info == known_galleries_.end())
837 return false; 905 return false;
838 906
839 bool default_permission = false; 907 bool default_permission = false;
840 if (gallery_info->second.type == MediaGalleryPrefInfo::kAutoDetected) 908 if (gallery_info->second.type == MediaGalleryPrefInfo::kAutoDetected)
841 default_permission = HasAutoDetectedGalleryPermission(extension); 909 default_permission = HasAutoDetectedGalleryPermission(extension);
(...skipping 10 matching lines...) Expand all
852 FOR_EACH_OBSERVER(GalleryChangeObserver, 920 FOR_EACH_OBSERVER(GalleryChangeObserver,
853 gallery_change_observers_, 921 gallery_change_observers_,
854 OnPermissionAdded(this, extension.id(), pref_id)); 922 OnPermissionAdded(this, extension.id(), pref_id));
855 else 923 else
856 FOR_EACH_OBSERVER(GalleryChangeObserver, 924 FOR_EACH_OBSERVER(GalleryChangeObserver,
857 gallery_change_observers_, 925 gallery_change_observers_,
858 OnPermissionRemoved(this, extension.id(), pref_id)); 926 OnPermissionRemoved(this, extension.id(), pref_id));
859 return true; 927 return true;
860 } 928 }
861 929
930 const MediaGalleriesPrefInfoMap& MediaGalleriesPreferences::known_galleries()
931 const {
932 DCHECK(IsInitialized());
933 return known_galleries_;
934 }
935
862 void MediaGalleriesPreferences::Shutdown() { 936 void MediaGalleriesPreferences::Shutdown() {
863 weak_factory_.InvalidateWeakPtrs(); 937 weak_factory_.InvalidateWeakPtrs();
864 profile_ = NULL; 938 profile_ = NULL;
865 } 939 }
866 940
867 // static 941 // static
868 bool MediaGalleriesPreferences::APIHasBeenUsed(Profile* profile) { 942 bool MediaGalleriesPreferences::APIHasBeenUsed(Profile* profile) {
869 MediaGalleryPrefId current_id = 943 MediaGalleryPrefId current_id =
870 profile->GetPrefs()->GetUint64(prefs::kMediaGalleriesUniqueId); 944 profile->GetPrefs()->GetUint64(prefs::kMediaGalleriesUniqueId);
871 return current_id != kInvalidMediaGalleryPrefId + 1; 945 return current_id != kInvalidMediaGalleryPrefId + 1;
872 } 946 }
873 947
874 // static 948 // static
875 void MediaGalleriesPreferences::RegisterProfilePrefs( 949 void MediaGalleriesPreferences::RegisterProfilePrefs(
876 user_prefs::PrefRegistrySyncable* registry) { 950 user_prefs::PrefRegistrySyncable* registry) {
877 registry->RegisterListPref(prefs::kMediaGalleriesRememberedGalleries, 951 registry->RegisterListPref(prefs::kMediaGalleriesRememberedGalleries,
878 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 952 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
879 registry->RegisterUint64Pref( 953 registry->RegisterUint64Pref(
880 prefs::kMediaGalleriesUniqueId, 954 prefs::kMediaGalleriesUniqueId,
881 kInvalidMediaGalleryPrefId + 1, 955 kInvalidMediaGalleryPrefId + 1,
882 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 956 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
883 } 957 }
884 958
885 bool MediaGalleriesPreferences::SetGalleryPermissionInPrefs( 959 bool MediaGalleriesPreferences::SetGalleryPermissionInPrefs(
886 const std::string& extension_id, 960 const std::string& extension_id,
887 MediaGalleryPrefId gallery_id, 961 MediaGalleryPrefId gallery_id,
888 bool has_access) { 962 bool has_access) {
963 DCHECK(IsInitialized());
889 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), 964 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(),
890 extension_id, 965 extension_id,
891 kMediaGalleriesPermissions); 966 kMediaGalleriesPermissions);
892 ListValue* permissions = update.Get(); 967 ListValue* permissions = update.Get();
893 if (!permissions) { 968 if (!permissions) {
894 permissions = update.Create(); 969 permissions = update.Create();
895 } else { 970 } else {
896 // If the gallery is already in the list, update the permission... 971 // If the gallery is already in the list, update the permission...
897 for (ListValue::iterator iter = permissions->begin(); 972 for (ListValue::iterator iter = permissions->begin();
898 iter != permissions->end(); ++iter) { 973 iter != permissions->end(); ++iter) {
(...skipping 17 matching lines...) Expand all
916 DictionaryValue* dict = new DictionaryValue; 991 DictionaryValue* dict = new DictionaryValue;
917 dict->SetString(kMediaGalleryIdKey, base::Uint64ToString(gallery_id)); 992 dict->SetString(kMediaGalleryIdKey, base::Uint64ToString(gallery_id));
918 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); 993 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access);
919 permissions->Append(dict); 994 permissions->Append(dict);
920 return true; 995 return true;
921 } 996 }
922 997
923 bool MediaGalleriesPreferences::UnsetGalleryPermissionInPrefs( 998 bool MediaGalleriesPreferences::UnsetGalleryPermissionInPrefs(
924 const std::string& extension_id, 999 const std::string& extension_id,
925 MediaGalleryPrefId gallery_id) { 1000 MediaGalleryPrefId gallery_id) {
1001 DCHECK(IsInitialized());
926 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), 1002 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(),
927 extension_id, 1003 extension_id,
928 kMediaGalleriesPermissions); 1004 kMediaGalleriesPermissions);
929 ListValue* permissions = update.Get(); 1005 ListValue* permissions = update.Get();
930 if (!permissions) 1006 if (!permissions)
931 return false; 1007 return false;
932 1008
933 for (ListValue::iterator iter = permissions->begin(); 1009 for (ListValue::iterator iter = permissions->begin();
934 iter != permissions->end(); ++iter) { 1010 iter != permissions->end(); ++iter) {
935 const DictionaryValue* dict = NULL; 1011 const DictionaryValue* dict = NULL;
936 if (!(*iter)->GetAsDictionary(&dict)) 1012 if (!(*iter)->GetAsDictionary(&dict))
937 continue; 1013 continue;
938 MediaGalleryPermission perm; 1014 MediaGalleryPermission perm;
939 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) 1015 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm))
940 continue; 1016 continue;
941 if (perm.pref_id == gallery_id) { 1017 if (perm.pref_id == gallery_id) {
942 permissions->Erase(iter, NULL); 1018 permissions->Erase(iter, NULL);
943 return true; 1019 return true;
944 } 1020 }
945 } 1021 }
946 return false; 1022 return false;
947 } 1023 }
948 1024
949 std::vector<MediaGalleryPermission> 1025 std::vector<MediaGalleryPermission>
950 MediaGalleriesPreferences::GetGalleryPermissionsFromPrefs( 1026 MediaGalleriesPreferences::GetGalleryPermissionsFromPrefs(
951 const std::string& extension_id) const { 1027 const std::string& extension_id) const {
1028 DCHECK(IsInitialized());
952 std::vector<MediaGalleryPermission> result; 1029 std::vector<MediaGalleryPermission> result;
953 const ListValue* permissions; 1030 const ListValue* permissions;
954 if (!GetExtensionPrefs()->ReadPrefAsList(extension_id, 1031 if (!GetExtensionPrefs()->ReadPrefAsList(extension_id,
955 kMediaGalleriesPermissions, 1032 kMediaGalleriesPermissions,
956 &permissions)) { 1033 &permissions)) {
957 return result; 1034 return result;
958 } 1035 }
959 1036
960 for (ListValue::const_iterator iter = permissions->begin(); 1037 for (ListValue::const_iterator iter = permissions->begin();
961 iter != permissions->end(); ++iter) { 1038 iter != permissions->end(); ++iter) {
962 DictionaryValue* dict = NULL; 1039 DictionaryValue* dict = NULL;
963 if (!(*iter)->GetAsDictionary(&dict)) 1040 if (!(*iter)->GetAsDictionary(&dict))
964 continue; 1041 continue;
965 MediaGalleryPermission perm; 1042 MediaGalleryPermission perm;
966 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) 1043 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm))
967 continue; 1044 continue;
968 result.push_back(perm); 1045 result.push_back(perm);
969 } 1046 }
970 1047
971 return result; 1048 return result;
972 } 1049 }
973 1050
974 void MediaGalleriesPreferences::RemoveGalleryPermissionsFromPrefs( 1051 void MediaGalleriesPreferences::RemoveGalleryPermissionsFromPrefs(
975 MediaGalleryPrefId gallery_id) { 1052 MediaGalleryPrefId gallery_id) {
1053 DCHECK(IsInitialized());
976 ExtensionPrefs* prefs = GetExtensionPrefs(); 1054 ExtensionPrefs* prefs = GetExtensionPrefs();
977 const DictionaryValue* extensions = 1055 const DictionaryValue* extensions =
978 prefs->pref_service()->GetDictionary(prefs::kExtensionsPref); 1056 prefs->pref_service()->GetDictionary(prefs::kExtensionsPref);
979 if (!extensions) 1057 if (!extensions)
980 return; 1058 return;
981 1059
982 for (DictionaryValue::Iterator iter(*extensions); !iter.IsAtEnd(); 1060 for (DictionaryValue::Iterator iter(*extensions); !iter.IsAtEnd();
983 iter.Advance()) { 1061 iter.Advance()) {
984 if (!extensions::Extension::IdIsValid(iter.key())) { 1062 if (!extensions::Extension::IdIsValid(iter.key())) {
985 NOTREACHED(); 1063 NOTREACHED();
986 continue; 1064 continue;
987 } 1065 }
988 UnsetGalleryPermissionInPrefs(iter.key(), gallery_id); 1066 UnsetGalleryPermissionInPrefs(iter.key(), gallery_id);
989 } 1067 }
990 } 1068 }
991 1069
992 ExtensionPrefs* MediaGalleriesPreferences::GetExtensionPrefs() const { 1070 ExtensionPrefs* MediaGalleriesPreferences::GetExtensionPrefs() const {
1071 DCHECK(IsInitialized());
993 if (extension_prefs_for_testing_) 1072 if (extension_prefs_for_testing_)
994 return extension_prefs_for_testing_; 1073 return extension_prefs_for_testing_;
995 return extensions::ExtensionPrefs::Get(profile_); 1074 return extensions::ExtensionPrefs::Get(profile_);
996 } 1075 }
997 1076
998 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( 1077 void MediaGalleriesPreferences::SetExtensionPrefsForTesting(
999 extensions::ExtensionPrefs* extension_prefs) { 1078 extensions::ExtensionPrefs* extension_prefs) {
1079 DCHECK(IsInitialized());
1000 extension_prefs_for_testing_ = extension_prefs; 1080 extension_prefs_for_testing_ = extension_prefs;
1001 } 1081 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698