OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 5 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
6 | 6 |
7 #include "base/callback.h" | |
7 #include "base/i18n/time_formatting.h" | 8 #include "base/i18n/time_formatting.h" |
8 #include "base/path_service.h" | 9 #include "base/path_service.h" |
9 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
11 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api.h" | 17 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api.h" |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
325 | 326 |
326 bool MediaGalleryPrefInfo::IsGalleryAvailable() const { | 327 bool MediaGalleryPrefInfo::IsGalleryAvailable() const { |
327 return !StorageInfo::IsRemovableDevice(device_id) || | 328 return !StorageInfo::IsRemovableDevice(device_id) || |
328 MediaStorageUtil::IsRemovableStorageAttached(device_id); | 329 MediaStorageUtil::IsRemovableStorageAttached(device_id); |
329 } | 330 } |
330 | 331 |
331 MediaGalleriesPreferences::GalleryChangeObserver::~GalleryChangeObserver() {} | 332 MediaGalleriesPreferences::GalleryChangeObserver::~GalleryChangeObserver() {} |
332 | 333 |
333 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) | 334 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) |
334 : weak_factory_(this), | 335 : weak_factory_(this), |
336 initializing_(false), | |
337 initialized_(false), | |
338 pre_initialization_callbacks_waiting_(0), | |
335 profile_(profile), | 339 profile_(profile), |
336 extension_prefs_for_testing_(NULL) { | 340 extension_prefs_for_testing_(NULL) {} |
337 AddDefaultGalleriesIfFreshProfile(); | |
338 | |
339 // Look for optional default galleries every time. | |
340 itunes::ITunesFinder::FindITunesLibrary( | |
341 base::Bind(&MediaGalleriesPreferences::OnITunesDeviceID, | |
342 weak_factory_.GetWeakPtr())); | |
343 | |
344 // TODO(tommycli): Turn on when Picasa code is ready. | |
345 #if 0 | |
346 picasa::PicasaFinder::FindPicasaDatabaseOnUIThread( | |
347 base::Bind(&MediaGalleriesPreferences::OnPicasaDeviceID, | |
348 weak_factory_.GetWeakPtr())); | |
349 #endif | |
350 | |
351 InitFromPrefs(); | |
352 | |
353 StorageMonitor::GetInstance()->AddObserver(this); | |
354 } | |
355 | 341 |
356 MediaGalleriesPreferences::~MediaGalleriesPreferences() { | 342 MediaGalleriesPreferences::~MediaGalleriesPreferences() { |
357 if (StorageMonitor::GetInstance()) | 343 if (StorageMonitor::GetInstance()) |
358 StorageMonitor::GetInstance()->RemoveObserver(this); | 344 StorageMonitor::GetInstance()->RemoveObserver(this); |
359 } | 345 } |
360 | 346 |
361 Profile* MediaGalleriesPreferences::profile() { | 347 void MediaGalleriesPreferences::EnsureInitialized(base::Closure callback) { |
362 return profile_; | 348 DCHECK(thread_checker_.CalledOnValidThread()); |
349 | |
350 if (initialized_) { | |
351 if (!callback.is_null()) | |
352 callback.Run(); | |
353 return; | |
354 } | |
355 | |
356 if (!callback.is_null()) { | |
vandebo (ex-Chrome)
2013/09/21 01:20:59
nit: always push the callback and then you can rem
tommycli
2013/09/23 20:39:12
Done.
| |
357 on_initialize_callbacks_.push_back(callback); | |
358 } | |
359 | |
360 if (initializing_) | |
361 return; | |
362 | |
363 initializing_ = true; | |
364 | |
365 AddDefaultGalleriesIfFreshProfile(); | |
366 | |
367 pre_initialization_callbacks_waiting_ = 2; | |
368 | |
369 // Ensure StorageMonitor is initialized. | |
370 StorageMonitor::GetInstance()->EnsureInitialized( | |
371 base::Bind(&MediaGalleriesPreferences::OnInitializationCallbackReturned, | |
372 weak_factory_.GetWeakPtr())); | |
373 | |
374 // Look for optional default galleries every time. | |
375 itunes::ITunesFinder::FindITunesLibrary( | |
376 base::Bind(&MediaGalleriesPreferences::OnFinderDeviceID, | |
377 weak_factory_.GetWeakPtr())); | |
378 | |
379 // TODO(tommycli): Turn on when Picasa code is ready. | |
380 #if 0 | |
381 picasa::PicasaFinder::FindPicasaDatabaseOnUIThread( | |
382 base::Bind(&MediaGalleriesPreferences::OnFinderDeviceID, | |
383 weak_factory_.GetWeakPtr())); | |
384 #endif | |
385 } | |
386 | |
387 bool MediaGalleriesPreferences::IsInitialized() const { return initialized_; } | |
388 | |
389 Profile* MediaGalleriesPreferences::profile() { return profile_; } | |
390 | |
391 void MediaGalleriesPreferences::OnInitializationCallbackReturned() { | |
392 DCHECK(!IsInitialized()); | |
393 DCHECK(pre_initialization_callbacks_waiting_ > 0); | |
394 if (--pre_initialization_callbacks_waiting_ == 0) | |
395 FinishInitialization(); | |
396 } | |
397 | |
398 void MediaGalleriesPreferences::FinishInitialization() { | |
399 DCHECK(!IsInitialized()); | |
400 | |
401 initialized_ = true; | |
402 | |
403 StorageMonitor* monitor = StorageMonitor::GetInstance(); | |
404 DCHECK(monitor->IsInitialized()); | |
405 | |
406 std::vector<StorageInfo> existing_devices = | |
407 monitor->GetAllAvailableStorages(); | |
408 for (size_t i = 0; i < existing_devices.size(); i++) { | |
409 if (!(StorageInfo::IsMediaDevice(existing_devices[i].device_id()) && | |
410 StorageInfo::IsRemovableDevice(existing_devices[i].device_id()))) | |
411 continue; | |
412 AddGallery(existing_devices[i].device_id(), | |
413 base::FilePath(), | |
414 false, | |
415 existing_devices[i].storage_label(), | |
416 existing_devices[i].vendor_name(), | |
417 existing_devices[i].model_name(), | |
418 existing_devices[i].total_size_in_bytes(), | |
419 base::Time::Now()); | |
420 } | |
421 | |
422 InitFromPrefs(); | |
423 | |
424 StorageMonitor::GetInstance()->AddObserver(this); | |
vandebo (ex-Chrome)
2013/09/21 01:20:59
Add the observer before iterating the available st
tommycli
2013/09/23 20:39:12
Done.
| |
425 | |
426 for (std::vector<base::Closure>::iterator iter = | |
427 on_initialize_callbacks_.begin(); | |
428 iter != on_initialize_callbacks_.end(); | |
429 ++iter) { | |
430 iter->Run(); | |
431 } | |
432 on_initialize_callbacks_.clear(); | |
363 } | 433 } |
364 | 434 |
365 void MediaGalleriesPreferences::AddDefaultGalleriesIfFreshProfile() { | 435 void MediaGalleriesPreferences::AddDefaultGalleriesIfFreshProfile() { |
366 // Only add defaults the first time. | 436 // Only add defaults the first time. |
367 if (APIHasBeenUsed(profile_)) | 437 if (APIHasBeenUsed(profile_)) |
368 return; | 438 return; |
369 | 439 |
370 // Fresh profile case. | 440 // Fresh profile case. |
371 const int kDirectoryKeys[] = { | 441 const int kDirectoryKeys[] = { |
372 chrome::DIR_USER_MUSIC, | 442 chrome::DIR_USER_MUSIC, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
423 FOR_EACH_OBSERVER(GalleryChangeObserver, | 493 FOR_EACH_OBSERVER(GalleryChangeObserver, |
424 gallery_change_observers_, | 494 gallery_change_observers_, |
425 OnGalleryInfoUpdated(this, pref_id)); | 495 OnGalleryInfoUpdated(this, pref_id)); |
426 } | 496 } |
427 return true; | 497 return true; |
428 } | 498 } |
429 } | 499 } |
430 return false; | 500 return false; |
431 } | 501 } |
432 | 502 |
433 void MediaGalleriesPreferences::OnITunesDeviceID(const std::string& device_id) { | 503 void MediaGalleriesPreferences::OnFinderDeviceID(const std::string& device_id) { |
434 if (device_id.empty()) | 504 if (!device_id.empty() && !UpdateDeviceIDForSingletonType(device_id)) { |
435 return; | 505 std::string gallery_name; |
436 if (!UpdateDeviceIDForSingletonType(device_id)) { | 506 if (StorageInfo::IsITunesDevice(device_id)) |
437 AddGalleryInternal(device_id, ASCIIToUTF16(kITunesGalleryName), | 507 gallery_name = kITunesGalleryName; |
508 else if (StorageInfo::IsPicasaDevice(device_id)) | |
509 gallery_name = kPicasaGalleryName; | |
510 else | |
511 NOTREACHED(); | |
512 | |
513 AddGalleryInternal(device_id, ASCIIToUTF16(gallery_name), | |
438 base::FilePath(), false /*not user added*/, | 514 base::FilePath(), false /*not user added*/, |
439 string16(), string16(), string16(), 0, | 515 string16(), string16(), string16(), 0, |
440 base::Time(), false, 2); | 516 base::Time(), false, 2); |
441 } | 517 } |
442 } | |
443 | 518 |
444 void MediaGalleriesPreferences::OnPicasaDeviceID(const std::string& device_id) { | 519 if (!IsInitialized()) |
445 DCHECK(!device_id.empty()); | 520 OnInitializationCallbackReturned(); |
446 if (!UpdateDeviceIDForSingletonType(device_id)) { | |
447 AddGalleryInternal(device_id, ASCIIToUTF16(kPicasaGalleryName), | |
448 base::FilePath(), false /*not user added*/, | |
449 string16(), string16(), string16(), 0, | |
450 base::Time(), false, 2); | |
451 } | |
452 } | 521 } |
453 | 522 |
454 void MediaGalleriesPreferences::InitFromPrefs() { | 523 void MediaGalleriesPreferences::InitFromPrefs() { |
455 known_galleries_.clear(); | 524 known_galleries_.clear(); |
456 device_map_.clear(); | 525 device_map_.clear(); |
457 | 526 |
458 PrefService* prefs = profile_->GetPrefs(); | 527 PrefService* prefs = profile_->GetPrefs(); |
459 const ListValue* list = prefs->GetList( | 528 const ListValue* list = prefs->GetList( |
460 prefs::kMediaGalleriesRememberedGalleries); | 529 prefs::kMediaGalleriesRememberedGalleries); |
461 if (list) { | 530 if (list) { |
462 for (ListValue::const_iterator it = list->begin(); | 531 for (ListValue::const_iterator it = list->begin(); |
463 it != list->end(); ++it) { | 532 it != list->end(); ++it) { |
464 const DictionaryValue* dict = NULL; | 533 const DictionaryValue* dict = NULL; |
465 if (!(*it)->GetAsDictionary(&dict)) | 534 if (!(*it)->GetAsDictionary(&dict)) |
466 continue; | 535 continue; |
467 | 536 |
468 MediaGalleryPrefInfo gallery_info; | 537 MediaGalleryPrefInfo gallery_info; |
469 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) | 538 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) |
470 continue; | 539 continue; |
471 | 540 |
472 known_galleries_[gallery_info.pref_id] = gallery_info; | 541 known_galleries_[gallery_info.pref_id] = gallery_info; |
473 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); | 542 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); |
474 } | 543 } |
475 } | 544 } |
476 } | 545 } |
477 | 546 |
478 void MediaGalleriesPreferences::AddGalleryChangeObserver( | 547 void MediaGalleriesPreferences::AddGalleryChangeObserver( |
479 GalleryChangeObserver* observer) { | 548 GalleryChangeObserver* observer) { |
549 DCHECK(IsInitialized()); | |
480 gallery_change_observers_.AddObserver(observer); | 550 gallery_change_observers_.AddObserver(observer); |
481 } | 551 } |
482 | 552 |
483 void MediaGalleriesPreferences::RemoveGalleryChangeObserver( | 553 void MediaGalleriesPreferences::RemoveGalleryChangeObserver( |
484 GalleryChangeObserver* observer) { | 554 GalleryChangeObserver* observer) { |
555 DCHECK(IsInitialized()); | |
485 gallery_change_observers_.RemoveObserver(observer); | 556 gallery_change_observers_.RemoveObserver(observer); |
486 } | 557 } |
487 | 558 |
488 void MediaGalleriesPreferences::OnRemovableStorageAttached( | 559 void MediaGalleriesPreferences::OnRemovableStorageAttached( |
489 const StorageInfo& info) { | 560 const StorageInfo& info) { |
561 DCHECK(IsInitialized()); | |
490 if (!StorageInfo::IsMediaDevice(info.device_id())) | 562 if (!StorageInfo::IsMediaDevice(info.device_id())) |
491 return; | 563 return; |
492 | 564 |
493 AddGallery(info.device_id(), base::FilePath(), | 565 AddGallery(info.device_id(), base::FilePath(), |
494 false /*not user added*/, | 566 false /*not user added*/, |
495 info.storage_label(), | 567 info.storage_label(), |
496 info.vendor_name(), | 568 info.vendor_name(), |
497 info.model_name(), | 569 info.model_name(), |
498 info.total_size_in_bytes(), | 570 info.total_size_in_bytes(), |
499 base::Time::Now()); | 571 base::Time::Now()); |
500 } | 572 } |
501 | 573 |
502 bool MediaGalleriesPreferences::LookUpGalleryByPath( | 574 bool MediaGalleriesPreferences::LookUpGalleryByPath( |
503 const base::FilePath& path, | 575 const base::FilePath& path, |
504 MediaGalleryPrefInfo* gallery_info) const { | 576 MediaGalleryPrefInfo* gallery_info) const { |
577 DCHECK(IsInitialized()); | |
505 StorageInfo info; | 578 StorageInfo info; |
506 base::FilePath relative_path; | 579 base::FilePath relative_path; |
507 if (!MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path)) { | 580 if (!MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path)) { |
508 if (gallery_info) | 581 if (gallery_info) |
509 *gallery_info = MediaGalleryPrefInfo(); | 582 *gallery_info = MediaGalleryPrefInfo(); |
510 return false; | 583 return false; |
511 } | 584 } |
512 | 585 |
513 relative_path = relative_path.NormalizePathSeparators(); | 586 relative_path = relative_path.NormalizePathSeparators(); |
514 MediaGalleryPrefIdSet galleries_on_device = | 587 MediaGalleryPrefIdSet galleries_on_device = |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
553 DeviceIdPrefIdsMap::const_iterator found = device_map_.find(device_id); | 626 DeviceIdPrefIdsMap::const_iterator found = device_map_.find(device_id); |
554 if (found == device_map_.end()) | 627 if (found == device_map_.end()) |
555 return MediaGalleryPrefIdSet(); | 628 return MediaGalleryPrefIdSet(); |
556 return found->second; | 629 return found->second; |
557 } | 630 } |
558 | 631 |
559 base::FilePath MediaGalleriesPreferences::LookUpGalleryPathForExtension( | 632 base::FilePath MediaGalleriesPreferences::LookUpGalleryPathForExtension( |
560 MediaGalleryPrefId gallery_id, | 633 MediaGalleryPrefId gallery_id, |
561 const extensions::Extension* extension, | 634 const extensions::Extension* extension, |
562 bool include_unpermitted_galleries) { | 635 bool include_unpermitted_galleries) { |
636 DCHECK(IsInitialized()); | |
563 DCHECK(extension); | 637 DCHECK(extension); |
564 if (!include_unpermitted_galleries && | 638 if (!include_unpermitted_galleries && |
565 !ContainsKey(GalleriesForExtension(*extension), gallery_id)) | 639 !ContainsKey(GalleriesForExtension(*extension), gallery_id)) |
566 return base::FilePath(); | 640 return base::FilePath(); |
567 | 641 |
568 MediaGalleriesPrefInfoMap::const_iterator it = | 642 MediaGalleriesPrefInfoMap::const_iterator it = |
569 known_galleries_.find(gallery_id); | 643 known_galleries_.find(gallery_id); |
570 if (it == known_galleries_.end()) | 644 if (it == known_galleries_.end()) |
571 return base::FilePath(); | 645 return base::FilePath(); |
572 return MediaStorageUtil::FindDevicePathById(it->second.device_id); | 646 return MediaStorageUtil::FindDevicePathById(it->second.device_id); |
573 } | 647 } |
574 | 648 |
575 MediaGalleryPrefId MediaGalleriesPreferences::AddGallery( | 649 MediaGalleryPrefId MediaGalleriesPreferences::AddGallery( |
576 const std::string& device_id, | 650 const std::string& device_id, |
577 const base::FilePath& relative_path, bool user_added, | 651 const base::FilePath& relative_path, bool user_added, |
578 const string16& volume_label, const string16& vendor_name, | 652 const string16& volume_label, const string16& vendor_name, |
579 const string16& model_name, uint64 total_size_in_bytes, | 653 const string16& model_name, uint64 total_size_in_bytes, |
580 base::Time last_attach_time) { | 654 base::Time last_attach_time) { |
655 DCHECK(IsInitialized()); | |
581 return AddGalleryInternal(device_id, string16(), relative_path, user_added, | 656 return AddGalleryInternal(device_id, string16(), relative_path, user_added, |
582 volume_label, vendor_name, model_name, | 657 volume_label, vendor_name, model_name, |
583 total_size_in_bytes, last_attach_time, true, 2); | 658 total_size_in_bytes, last_attach_time, true, 2); |
584 } | 659 } |
585 | 660 |
586 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryInternal( | 661 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryInternal( |
587 const std::string& device_id, const string16& display_name, | 662 const std::string& device_id, const string16& display_name, |
588 const base::FilePath& relative_path, bool user_added, | 663 const base::FilePath& relative_path, bool user_added, |
589 const string16& volume_label, const string16& vendor_name, | 664 const string16& volume_label, const string16& vendor_name, |
590 const string16& model_name, uint64 total_size_in_bytes, | 665 const string16& model_name, uint64 total_size_in_bytes, |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
703 InitFromPrefs(); | 778 InitFromPrefs(); |
704 FOR_EACH_OBSERVER(GalleryChangeObserver, | 779 FOR_EACH_OBSERVER(GalleryChangeObserver, |
705 gallery_change_observers_, | 780 gallery_change_observers_, |
706 OnGalleryAdded(this, gallery_info.pref_id)); | 781 OnGalleryAdded(this, gallery_info.pref_id)); |
707 | 782 |
708 return gallery_info.pref_id; | 783 return gallery_info.pref_id; |
709 } | 784 } |
710 | 785 |
711 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryByPath( | 786 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryByPath( |
712 const base::FilePath& path) { | 787 const base::FilePath& path) { |
788 DCHECK(IsInitialized()); | |
713 MediaGalleryPrefInfo gallery_info; | 789 MediaGalleryPrefInfo gallery_info; |
714 if (LookUpGalleryByPath(path, &gallery_info) && | 790 if (LookUpGalleryByPath(path, &gallery_info) && |
715 gallery_info.type != MediaGalleryPrefInfo::kBlackListed) { | 791 gallery_info.type != MediaGalleryPrefInfo::kBlackListed) { |
716 return gallery_info.pref_id; | 792 return gallery_info.pref_id; |
717 } | 793 } |
718 return AddGalleryInternal(gallery_info.device_id, | 794 return AddGalleryInternal(gallery_info.device_id, |
719 gallery_info.display_name, | 795 gallery_info.display_name, |
720 gallery_info.path, | 796 gallery_info.path, |
721 true /*user added*/, | 797 true /*user added*/, |
722 gallery_info.volume_label, | 798 gallery_info.volume_label, |
723 gallery_info.vendor_name, | 799 gallery_info.vendor_name, |
724 gallery_info.model_name, | 800 gallery_info.model_name, |
725 gallery_info.total_size_in_bytes, | 801 gallery_info.total_size_in_bytes, |
726 gallery_info.last_attach_time, | 802 gallery_info.last_attach_time, |
727 gallery_info.volume_metadata_valid, | 803 gallery_info.volume_metadata_valid, |
728 gallery_info.prefs_version); | 804 gallery_info.prefs_version); |
729 } | 805 } |
730 | 806 |
731 void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) { | 807 void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) { |
808 DCHECK(IsInitialized()); | |
732 PrefService* prefs = profile_->GetPrefs(); | 809 PrefService* prefs = profile_->GetPrefs(); |
733 scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate( | 810 scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate( |
734 prefs, prefs::kMediaGalleriesRememberedGalleries)); | 811 prefs, prefs::kMediaGalleriesRememberedGalleries)); |
735 ListValue* list = update->Get(); | 812 ListValue* list = update->Get(); |
736 | 813 |
737 if (!ContainsKey(known_galleries_, pref_id)) | 814 if (!ContainsKey(known_galleries_, pref_id)) |
738 return; | 815 return; |
739 | 816 |
740 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { | 817 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { |
741 DictionaryValue* dict; | 818 DictionaryValue* dict; |
(...skipping 15 matching lines...) Expand all Loading... | |
757 FOR_EACH_OBSERVER(GalleryChangeObserver, | 834 FOR_EACH_OBSERVER(GalleryChangeObserver, |
758 gallery_change_observers_, | 835 gallery_change_observers_, |
759 OnGalleryRemoved(this, pref_id)); | 836 OnGalleryRemoved(this, pref_id)); |
760 return; | 837 return; |
761 } | 838 } |
762 } | 839 } |
763 } | 840 } |
764 | 841 |
765 MediaGalleryPrefIdSet MediaGalleriesPreferences::GalleriesForExtension( | 842 MediaGalleryPrefIdSet MediaGalleriesPreferences::GalleriesForExtension( |
766 const extensions::Extension& extension) const { | 843 const extensions::Extension& extension) const { |
844 DCHECK(IsInitialized()); | |
767 MediaGalleryPrefIdSet result; | 845 MediaGalleryPrefIdSet result; |
768 | 846 |
769 if (HasAutoDetectedGalleryPermission(extension)) { | 847 if (HasAutoDetectedGalleryPermission(extension)) { |
770 for (MediaGalleriesPrefInfoMap::const_iterator it = | 848 for (MediaGalleriesPrefInfoMap::const_iterator it = |
771 known_galleries_.begin(); it != known_galleries_.end(); ++it) { | 849 known_galleries_.begin(); it != known_galleries_.end(); ++it) { |
772 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) | 850 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) |
773 result.insert(it->second.pref_id); | 851 result.insert(it->second.pref_id); |
774 } | 852 } |
775 } | 853 } |
776 | 854 |
(...skipping 14 matching lines...) Expand all Loading... | |
791 } | 869 } |
792 } | 870 } |
793 } | 871 } |
794 return result; | 872 return result; |
795 } | 873 } |
796 | 874 |
797 void MediaGalleriesPreferences::SetGalleryPermissionForExtension( | 875 void MediaGalleriesPreferences::SetGalleryPermissionForExtension( |
798 const extensions::Extension& extension, | 876 const extensions::Extension& extension, |
799 MediaGalleryPrefId pref_id, | 877 MediaGalleryPrefId pref_id, |
800 bool has_permission) { | 878 bool has_permission) { |
879 DCHECK(IsInitialized()); | |
801 // The gallery may not exist anymore if the user opened a second config | 880 // The gallery may not exist anymore if the user opened a second config |
802 // surface concurrently and removed it. Drop the permission update if so. | 881 // surface concurrently and removed it. Drop the permission update if so. |
803 MediaGalleriesPrefInfoMap::const_iterator gallery_info = | 882 MediaGalleriesPrefInfoMap::const_iterator gallery_info = |
804 known_galleries_.find(pref_id); | 883 known_galleries_.find(pref_id); |
805 if (gallery_info == known_galleries_.end()) | 884 if (gallery_info == known_galleries_.end()) |
806 return; | 885 return; |
807 | 886 |
808 bool default_permission = false; | 887 bool default_permission = false; |
809 if (gallery_info->second.type == MediaGalleryPrefInfo::kAutoDetected) | 888 if (gallery_info->second.type == MediaGalleryPrefInfo::kAutoDetected) |
810 default_permission = HasAutoDetectedGalleryPermission(extension); | 889 default_permission = HasAutoDetectedGalleryPermission(extension); |
811 // When the permission matches the default, we don't need to remember it. | 890 // When the permission matches the default, we don't need to remember it. |
812 if (has_permission == default_permission) { | 891 if (has_permission == default_permission) { |
813 if (!UnsetGalleryPermissionInPrefs(extension.id(), pref_id)) | 892 if (!UnsetGalleryPermissionInPrefs(extension.id(), pref_id)) |
814 // If permission wasn't set, assume nothing has changed. | 893 // If permission wasn't set, assume nothing has changed. |
815 return; | 894 return; |
816 } else { | 895 } else { |
817 if (!SetGalleryPermissionInPrefs(extension.id(), pref_id, has_permission)) | 896 if (!SetGalleryPermissionInPrefs(extension.id(), pref_id, has_permission)) |
818 return; | 897 return; |
819 } | 898 } |
820 if (has_permission) | 899 if (has_permission) |
821 FOR_EACH_OBSERVER(GalleryChangeObserver, | 900 FOR_EACH_OBSERVER(GalleryChangeObserver, |
822 gallery_change_observers_, | 901 gallery_change_observers_, |
823 OnPermissionAdded(this, extension.id(), pref_id)); | 902 OnPermissionAdded(this, extension.id(), pref_id)); |
824 else | 903 else |
825 FOR_EACH_OBSERVER(GalleryChangeObserver, | 904 FOR_EACH_OBSERVER(GalleryChangeObserver, |
826 gallery_change_observers_, | 905 gallery_change_observers_, |
827 OnPermissionRemoved(this, extension.id(), pref_id)); | 906 OnPermissionRemoved(this, extension.id(), pref_id)); |
828 } | 907 } |
829 | 908 |
909 const MediaGalleriesPrefInfoMap& MediaGalleriesPreferences::known_galleries() | |
910 const { | |
911 DCHECK(IsInitialized()); | |
912 return known_galleries_; | |
913 } | |
914 | |
830 void MediaGalleriesPreferences::Shutdown() { | 915 void MediaGalleriesPreferences::Shutdown() { |
831 weak_factory_.InvalidateWeakPtrs(); | 916 weak_factory_.InvalidateWeakPtrs(); |
832 profile_ = NULL; | 917 profile_ = NULL; |
833 } | 918 } |
834 | 919 |
835 // static | 920 // static |
836 bool MediaGalleriesPreferences::APIHasBeenUsed(Profile* profile) { | 921 bool MediaGalleriesPreferences::APIHasBeenUsed(Profile* profile) { |
837 MediaGalleryPrefId current_id = | 922 MediaGalleryPrefId current_id = |
838 profile->GetPrefs()->GetUint64(prefs::kMediaGalleriesUniqueId); | 923 profile->GetPrefs()->GetUint64(prefs::kMediaGalleriesUniqueId); |
839 return current_id != kInvalidMediaGalleryPrefId + 1; | 924 return current_id != kInvalidMediaGalleryPrefId + 1; |
840 } | 925 } |
841 | 926 |
842 // static | 927 // static |
843 void MediaGalleriesPreferences::RegisterProfilePrefs( | 928 void MediaGalleriesPreferences::RegisterProfilePrefs( |
844 user_prefs::PrefRegistrySyncable* registry) { | 929 user_prefs::PrefRegistrySyncable* registry) { |
845 registry->RegisterListPref(prefs::kMediaGalleriesRememberedGalleries, | 930 registry->RegisterListPref(prefs::kMediaGalleriesRememberedGalleries, |
846 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 931 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
847 registry->RegisterUint64Pref( | 932 registry->RegisterUint64Pref( |
848 prefs::kMediaGalleriesUniqueId, | 933 prefs::kMediaGalleriesUniqueId, |
849 kInvalidMediaGalleryPrefId + 1, | 934 kInvalidMediaGalleryPrefId + 1, |
850 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 935 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
851 } | 936 } |
852 | 937 |
853 bool MediaGalleriesPreferences::SetGalleryPermissionInPrefs( | 938 bool MediaGalleriesPreferences::SetGalleryPermissionInPrefs( |
854 const std::string& extension_id, | 939 const std::string& extension_id, |
855 MediaGalleryPrefId gallery_id, | 940 MediaGalleryPrefId gallery_id, |
856 bool has_access) { | 941 bool has_access) { |
857 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), | 942 DCHECK(IsInitialized()); |
858 extension_id, | 943 ExtensionPrefs::ScopedListUpdate update( |
vandebo (ex-Chrome)
2013/09/21 01:20:59
Style guide seems to prefer this the way it was. h
tommycli
2013/09/23 20:39:12
Done.
| |
859 kMediaGalleriesPermissions); | 944 GetExtensionPrefs(), extension_id, kMediaGalleriesPermissions); |
860 ListValue* permissions = update.Get(); | 945 ListValue* permissions = update.Get(); |
861 if (!permissions) { | 946 if (!permissions) { |
862 permissions = update.Create(); | 947 permissions = update.Create(); |
863 } else { | 948 } else { |
864 // If the gallery is already in the list, update the permission... | 949 // If the gallery is already in the list, update the permission... |
865 for (ListValue::iterator iter = permissions->begin(); | 950 for (ListValue::iterator iter = permissions->begin(); |
866 iter != permissions->end(); ++iter) { | 951 iter != permissions->end(); ++iter) { |
867 DictionaryValue* dict = NULL; | 952 DictionaryValue* dict = NULL; |
868 if (!(*iter)->GetAsDictionary(&dict)) | 953 if (!(*iter)->GetAsDictionary(&dict)) |
869 continue; | 954 continue; |
(...skipping 14 matching lines...) Expand all Loading... | |
884 DictionaryValue* dict = new DictionaryValue; | 969 DictionaryValue* dict = new DictionaryValue; |
885 dict->SetString(kMediaGalleryIdKey, base::Uint64ToString(gallery_id)); | 970 dict->SetString(kMediaGalleryIdKey, base::Uint64ToString(gallery_id)); |
886 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); | 971 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); |
887 permissions->Append(dict); | 972 permissions->Append(dict); |
888 return true; | 973 return true; |
889 } | 974 } |
890 | 975 |
891 bool MediaGalleriesPreferences::UnsetGalleryPermissionInPrefs( | 976 bool MediaGalleriesPreferences::UnsetGalleryPermissionInPrefs( |
892 const std::string& extension_id, | 977 const std::string& extension_id, |
893 MediaGalleryPrefId gallery_id) { | 978 MediaGalleryPrefId gallery_id) { |
894 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), | 979 DCHECK(IsInitialized()); |
895 extension_id, | 980 ExtensionPrefs::ScopedListUpdate update( |
896 kMediaGalleriesPermissions); | 981 GetExtensionPrefs(), extension_id, kMediaGalleriesPermissions); |
897 ListValue* permissions = update.Get(); | 982 ListValue* permissions = update.Get(); |
898 if (!permissions) | 983 if (!permissions) |
899 return false; | 984 return false; |
900 | 985 |
901 for (ListValue::iterator iter = permissions->begin(); | 986 for (ListValue::iterator iter = permissions->begin(); |
902 iter != permissions->end(); ++iter) { | 987 iter != permissions->end(); ++iter) { |
903 const DictionaryValue* dict = NULL; | 988 const DictionaryValue* dict = NULL; |
904 if (!(*iter)->GetAsDictionary(&dict)) | 989 if (!(*iter)->GetAsDictionary(&dict)) |
905 continue; | 990 continue; |
906 MediaGalleryPermission perm; | 991 MediaGalleryPermission perm; |
907 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) | 992 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
908 continue; | 993 continue; |
909 if (perm.pref_id == gallery_id) { | 994 if (perm.pref_id == gallery_id) { |
910 permissions->Erase(iter, NULL); | 995 permissions->Erase(iter, NULL); |
911 return true; | 996 return true; |
912 } | 997 } |
913 } | 998 } |
914 return false; | 999 return false; |
915 } | 1000 } |
916 | 1001 |
917 std::vector<MediaGalleryPermission> | 1002 std::vector<MediaGalleryPermission> |
918 MediaGalleriesPreferences::GetGalleryPermissionsFromPrefs( | 1003 MediaGalleriesPreferences::GetGalleryPermissionsFromPrefs( |
919 const std::string& extension_id) const { | 1004 const std::string& extension_id) const { |
1005 DCHECK(IsInitialized()); | |
920 std::vector<MediaGalleryPermission> result; | 1006 std::vector<MediaGalleryPermission> result; |
921 const ListValue* permissions; | 1007 const ListValue* permissions; |
922 if (!GetExtensionPrefs()->ReadPrefAsList(extension_id, | 1008 if (!GetExtensionPrefs()->ReadPrefAsList(extension_id, |
923 kMediaGalleriesPermissions, | 1009 kMediaGalleriesPermissions, |
924 &permissions)) { | 1010 &permissions)) { |
925 return result; | 1011 return result; |
926 } | 1012 } |
927 | 1013 |
928 for (ListValue::const_iterator iter = permissions->begin(); | 1014 for (ListValue::const_iterator iter = permissions->begin(); |
929 iter != permissions->end(); ++iter) { | 1015 iter != permissions->end(); ++iter) { |
930 DictionaryValue* dict = NULL; | 1016 DictionaryValue* dict = NULL; |
931 if (!(*iter)->GetAsDictionary(&dict)) | 1017 if (!(*iter)->GetAsDictionary(&dict)) |
932 continue; | 1018 continue; |
933 MediaGalleryPermission perm; | 1019 MediaGalleryPermission perm; |
934 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) | 1020 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
935 continue; | 1021 continue; |
936 result.push_back(perm); | 1022 result.push_back(perm); |
937 } | 1023 } |
938 | 1024 |
939 return result; | 1025 return result; |
940 } | 1026 } |
941 | 1027 |
942 void MediaGalleriesPreferences::RemoveGalleryPermissionsFromPrefs( | 1028 void MediaGalleriesPreferences::RemoveGalleryPermissionsFromPrefs( |
943 MediaGalleryPrefId gallery_id) { | 1029 MediaGalleryPrefId gallery_id) { |
1030 DCHECK(IsInitialized()); | |
944 ExtensionPrefs* prefs = GetExtensionPrefs(); | 1031 ExtensionPrefs* prefs = GetExtensionPrefs(); |
945 const DictionaryValue* extensions = | 1032 const DictionaryValue* extensions = |
946 prefs->pref_service()->GetDictionary(prefs::kExtensionsPref); | 1033 prefs->pref_service()->GetDictionary(prefs::kExtensionsPref); |
947 if (!extensions) | 1034 if (!extensions) |
948 return; | 1035 return; |
949 | 1036 |
950 for (DictionaryValue::Iterator iter(*extensions); !iter.IsAtEnd(); | 1037 for (DictionaryValue::Iterator iter(*extensions); !iter.IsAtEnd(); |
951 iter.Advance()) { | 1038 iter.Advance()) { |
952 if (!extensions::Extension::IdIsValid(iter.key())) { | 1039 if (!extensions::Extension::IdIsValid(iter.key())) { |
953 NOTREACHED(); | 1040 NOTREACHED(); |
954 continue; | 1041 continue; |
955 } | 1042 } |
956 UnsetGalleryPermissionInPrefs(iter.key(), gallery_id); | 1043 UnsetGalleryPermissionInPrefs(iter.key(), gallery_id); |
957 } | 1044 } |
958 } | 1045 } |
959 | 1046 |
960 ExtensionPrefs* MediaGalleriesPreferences::GetExtensionPrefs() const { | 1047 ExtensionPrefs* MediaGalleriesPreferences::GetExtensionPrefs() const { |
1048 DCHECK(IsInitialized()); | |
961 if (extension_prefs_for_testing_) | 1049 if (extension_prefs_for_testing_) |
962 return extension_prefs_for_testing_; | 1050 return extension_prefs_for_testing_; |
963 return extensions::ExtensionPrefs::Get(profile_); | 1051 return extensions::ExtensionPrefs::Get(profile_); |
964 } | 1052 } |
965 | 1053 |
966 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( | 1054 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( |
967 extensions::ExtensionPrefs* extension_prefs) { | 1055 extensions::ExtensionPrefs* extension_prefs) { |
1056 DCHECK(IsInitialized()); | |
968 extension_prefs_for_testing_ = extension_prefs; | 1057 extension_prefs_for_testing_ = extension_prefs; |
969 } | 1058 } |
OLD | NEW |