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