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/base_paths_posix.h" | 7 #include "base/base_paths_posix.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/i18n/time_formatting.h" | 9 #include "base/i18n/time_formatting.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 on_initialize_callbacks_.push_back(callback); | 370 on_initialize_callbacks_.push_back(callback); |
371 if (on_initialize_callbacks_.size() > 1) | 371 if (on_initialize_callbacks_.size() > 1) |
372 return; | 372 return; |
373 | 373 |
374 // This counter must match the number of async methods dispatched below. | 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 | 375 // It cannot be incremented inline with each callback, as some may return |
376 // synchronously, decrement the counter to 0, and prematurely trigger | 376 // synchronously, decrement the counter to 0, and prematurely trigger |
377 // FinishInitialization. | 377 // FinishInitialization. |
378 pre_initialization_callbacks_waiting_ = 2; | 378 pre_initialization_callbacks_waiting_ = 2; |
379 | 379 |
380 // Ensure StorageMonitor is initialized. | 380 // We determine the freshness of the profile here, before any of the finders |
381 // return and add media galleries to it. | |
381 StorageMonitor::GetInstance()->EnsureInitialized( | 382 StorageMonitor::GetInstance()->EnsureInitialized( |
382 base::Bind(&MediaGalleriesPreferences::OnInitializationCallbackReturned, | 383 base::Bind(&MediaGalleriesPreferences::OnStorageMonitorInit, |
383 weak_factory_.GetWeakPtr())); | 384 weak_factory_.GetWeakPtr(), |
385 APIHasBeenUsed(profile_) /* add_default_galleries */)); | |
384 | 386 |
385 // Look for optional default galleries every time. | 387 // Look for optional default galleries every time. |
386 itunes::ITunesFinder::FindITunesLibrary( | 388 itunes::ITunesFinder::FindITunesLibrary( |
387 base::Bind(&MediaGalleriesPreferences::OnFinderDeviceID, | 389 base::Bind(&MediaGalleriesPreferences::OnFinderDeviceID, |
388 weak_factory_.GetWeakPtr())); | 390 weak_factory_.GetWeakPtr())); |
389 | 391 |
390 #if 0 | 392 #if 0 |
391 iphoto::FindIPhotoLibrary( | 393 iphoto::FindIPhotoLibrary( |
392 base::Bind(&MediaGalleriesPreferences::OnFinderDeviceID, | 394 base::Bind(&MediaGalleriesPreferences::OnFinderDeviceID, |
393 weak_factory_.GetWeakPtr())); | 395 weak_factory_.GetWeakPtr())); |
(...skipping 19 matching lines...) Expand all Loading... | |
413 } | 415 } |
414 | 416 |
415 void MediaGalleriesPreferences::FinishInitialization() { | 417 void MediaGalleriesPreferences::FinishInitialization() { |
416 DCHECK(!IsInitialized()); | 418 DCHECK(!IsInitialized()); |
417 | 419 |
418 initialized_ = true; | 420 initialized_ = true; |
419 | 421 |
420 StorageMonitor* monitor = StorageMonitor::GetInstance(); | 422 StorageMonitor* monitor = StorageMonitor::GetInstance(); |
421 DCHECK(monitor->IsInitialized()); | 423 DCHECK(monitor->IsInitialized()); |
422 | 424 |
423 AddDefaultGalleriesIfFreshProfile(); | |
424 InitFromPrefs(); | 425 InitFromPrefs(); |
425 | 426 |
426 StorageMonitor::GetInstance()->AddObserver(this); | 427 StorageMonitor::GetInstance()->AddObserver(this); |
427 | 428 |
428 std::vector<StorageInfo> existing_devices = | 429 std::vector<StorageInfo> existing_devices = |
429 monitor->GetAllAvailableStorages(); | 430 monitor->GetAllAvailableStorages(); |
430 for (size_t i = 0; i < existing_devices.size(); i++) { | 431 for (size_t i = 0; i < existing_devices.size(); i++) { |
431 if (!(StorageInfo::IsMediaDevice(existing_devices[i].device_id()) && | 432 if (!(StorageInfo::IsMediaDevice(existing_devices[i].device_id()) && |
432 StorageInfo::IsRemovableDevice(existing_devices[i].device_id()))) | 433 StorageInfo::IsRemovableDevice(existing_devices[i].device_id()))) |
433 continue; | 434 continue; |
434 AddGallery(existing_devices[i].device_id(), | 435 AddGallery(existing_devices[i].device_id(), |
435 base::FilePath(), | 436 base::FilePath(), |
436 false, | 437 false, |
437 existing_devices[i].storage_label(), | 438 existing_devices[i].storage_label(), |
438 existing_devices[i].vendor_name(), | 439 existing_devices[i].vendor_name(), |
439 existing_devices[i].model_name(), | 440 existing_devices[i].model_name(), |
440 existing_devices[i].total_size_in_bytes(), | 441 existing_devices[i].total_size_in_bytes(), |
441 base::Time::Now()); | 442 base::Time::Now()); |
442 } | 443 } |
443 | 444 |
444 for (std::vector<base::Closure>::iterator iter = | 445 for (std::vector<base::Closure>::iterator iter = |
445 on_initialize_callbacks_.begin(); | 446 on_initialize_callbacks_.begin(); |
446 iter != on_initialize_callbacks_.end(); | 447 iter != on_initialize_callbacks_.end(); |
447 ++iter) { | 448 ++iter) { |
448 iter->Run(); | 449 iter->Run(); |
449 } | 450 } |
450 on_initialize_callbacks_.clear(); | 451 on_initialize_callbacks_.clear(); |
451 } | 452 } |
452 | 453 |
453 void MediaGalleriesPreferences::AddDefaultGalleriesIfFreshProfile() { | 454 void MediaGalleriesPreferences::AddDefaultGalleries() { |
454 // Only add defaults the first time. | |
455 if (APIHasBeenUsed(profile_)) | |
456 return; | |
457 | |
458 // Fresh profile case. | |
459 const int kDirectoryKeys[] = { | 455 const int kDirectoryKeys[] = { |
460 chrome::DIR_USER_MUSIC, | 456 chrome::DIR_USER_MUSIC, |
461 chrome::DIR_USER_PICTURES, | 457 chrome::DIR_USER_PICTURES, |
462 chrome::DIR_USER_VIDEOS, | 458 chrome::DIR_USER_VIDEOS, |
463 }; | 459 }; |
464 | 460 |
465 for (size_t i = 0; i < arraysize(kDirectoryKeys); ++i) { | 461 for (size_t i = 0; i < arraysize(kDirectoryKeys); ++i) { |
466 base::FilePath path; | 462 base::FilePath path; |
467 if (!PathService::Get(kDirectoryKeys[i], &path)) | 463 if (!PathService::Get(kDirectoryKeys[i], &path)) |
468 continue; | 464 continue; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
511 FOR_EACH_OBSERVER(GalleryChangeObserver, | 507 FOR_EACH_OBSERVER(GalleryChangeObserver, |
512 gallery_change_observers_, | 508 gallery_change_observers_, |
513 OnGalleryInfoUpdated(this, pref_id)); | 509 OnGalleryInfoUpdated(this, pref_id)); |
514 } | 510 } |
515 return true; | 511 return true; |
516 } | 512 } |
517 } | 513 } |
518 return false; | 514 return false; |
519 } | 515 } |
520 | 516 |
517 void MediaGalleriesPreferences::OnStorageMonitorInit( | |
518 bool need_to_add_default_galleries) { | |
Lei Zhang
2013/10/08 01:18:34
nit: the parameter name here does not match the .h
| |
519 if (need_to_add_default_galleries) | |
520 AddDefaultGalleries(); | |
521 OnInitializationCallbackReturned(); | |
522 } | |
523 | |
521 void MediaGalleriesPreferences::OnFinderDeviceID(const std::string& device_id) { | 524 void MediaGalleriesPreferences::OnFinderDeviceID(const std::string& device_id) { |
522 if (!device_id.empty() && !UpdateDeviceIDForSingletonType(device_id)) { | 525 if (!device_id.empty() && !UpdateDeviceIDForSingletonType(device_id)) { |
523 std::string gallery_name; | 526 std::string gallery_name; |
524 if (StorageInfo::IsIPhotoDevice(device_id)) | 527 if (StorageInfo::IsIPhotoDevice(device_id)) |
525 gallery_name = kIPhotoGalleryName; | 528 gallery_name = kIPhotoGalleryName; |
526 else if (StorageInfo::IsITunesDevice(device_id)) | 529 else if (StorageInfo::IsITunesDevice(device_id)) |
527 gallery_name = kITunesGalleryName; | 530 gallery_name = kITunesGalleryName; |
528 else if (StorageInfo::IsPicasaDevice(device_id)) | 531 else if (StorageInfo::IsPicasaDevice(device_id)) |
529 gallery_name = kPicasaGalleryName; | 532 gallery_name = kPicasaGalleryName; |
530 else | 533 else |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1071 if (extension_prefs_for_testing_) | 1074 if (extension_prefs_for_testing_) |
1072 return extension_prefs_for_testing_; | 1075 return extension_prefs_for_testing_; |
1073 return extensions::ExtensionPrefs::Get(profile_); | 1076 return extensions::ExtensionPrefs::Get(profile_); |
1074 } | 1077 } |
1075 | 1078 |
1076 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( | 1079 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( |
1077 extensions::ExtensionPrefs* extension_prefs) { | 1080 extensions::ExtensionPrefs* extension_prefs) { |
1078 DCHECK(IsInitialized()); | 1081 DCHECK(IsInitialized()); |
1079 extension_prefs_for_testing_ = extension_prefs; | 1082 extension_prefs_for_testing_ = extension_prefs; |
1080 } | 1083 } |
OLD | NEW |