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

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

Powered by Google App Engine
This is Rietveld 408576698