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

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

Powered by Google App Engine
This is Rietveld 408576698