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

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

Powered by Google App Engine
This is Rietveld 408576698