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/chromeos/customization_document.h" | 5 #include "chrome/browser/chromeos/customization_document.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/path_service.h" |
15 #include "base/prefs/pref_registry_simple.h" | 16 #include "base/prefs/pref_registry_simple.h" |
16 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
17 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
18 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
22 #include "chrome/browser/browser_process.h" | 23 #include "chrome/browser/browser_process.h" |
| 24 #include "chrome/browser/chromeos/customization_wallpaper_downloader.h" |
| 25 #include "chrome/browser/chromeos/login/wallpaper_manager.h" |
23 #include "chrome/browser/chromeos/login/wizard_controller.h" | 26 #include "chrome/browser/chromeos/login/wizard_controller.h" |
24 #include "chrome/browser/chromeos/net/delay_network_call.h" | 27 #include "chrome/browser/chromeos/net/delay_network_call.h" |
25 #include "chrome/browser/extensions/external_loader.h" | 28 #include "chrome/browser/extensions/external_loader.h" |
26 #include "chrome/browser/extensions/external_provider_impl.h" | 29 #include "chrome/browser/extensions/external_provider_impl.h" |
27 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" | 31 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" |
29 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h" | 32 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h" |
| 33 #include "chrome/common/chrome_paths.h" |
30 #include "chrome/common/extensions/extension_constants.h" | 34 #include "chrome/common/extensions/extension_constants.h" |
| 35 #include "chrome/common/pref_names.h" |
31 #include "chromeos/network/network_state.h" | 36 #include "chromeos/network/network_state.h" |
32 #include "chromeos/network/network_state_handler.h" | 37 #include "chromeos/network/network_state_handler.h" |
33 #include "chromeos/system/statistics_provider.h" | 38 #include "chromeos/system/statistics_provider.h" |
34 #include "components/user_prefs/pref_registry_syncable.h" | 39 #include "components/user_prefs/pref_registry_syncable.h" |
35 #include "content/public/browser/browser_thread.h" | 40 #include "content/public/browser/browser_thread.h" |
36 #include "net/base/load_flags.h" | 41 #include "net/base/load_flags.h" |
37 #include "net/http/http_response_headers.h" | 42 #include "net/http/http_response_headers.h" |
38 #include "net/http/http_status_code.h" | 43 #include "net/http/http_status_code.h" |
39 #include "net/url_request/url_fetcher.h" | 44 #include "net/url_request/url_fetcher.h" |
40 | 45 |
(...skipping 16 matching lines...) Expand all Loading... |
57 const char kDefaultAppsAttr[] = "default_apps"; | 62 const char kDefaultAppsAttr[] = "default_apps"; |
58 const char kLocalizedContent[] = "localized_content"; | 63 const char kLocalizedContent[] = "localized_content"; |
59 const char kDefaultAppsFolderName[] = "default_apps_folder_name"; | 64 const char kDefaultAppsFolderName[] = "default_apps_folder_name"; |
60 | 65 |
61 const char kAcceptedManifestVersion[] = "1.0"; | 66 const char kAcceptedManifestVersion[] = "1.0"; |
62 | 67 |
63 // Path to OEM partner startup customization manifest. | 68 // Path to OEM partner startup customization manifest. |
64 const char kStartupCustomizationManifestPath[] = | 69 const char kStartupCustomizationManifestPath[] = |
65 "/opt/oem/etc/startup_manifest.json"; | 70 "/opt/oem/etc/startup_manifest.json"; |
66 | 71 |
| 72 // THis is subdirectory relative to PathService(DIR_CHROMEOS_CUSTOM_WALLPAPERS), |
| 73 // where downloaded (and resized) wallpaper is stored. |
| 74 const char kCustomizationDefaultWallpaperDir[] = "customization"; |
| 75 |
| 76 // The original downloaded image file is stored under this name. |
| 77 const char kCustomizationDefaultWallpaperDownloadedFile[] = |
| 78 "default.downloaded"; |
| 79 |
67 // Name of local state option that tracks if services customization has been | 80 // Name of local state option that tracks if services customization has been |
68 // applied. | 81 // applied. |
69 const char kServicesCustomizationAppliedPref[] = "ServicesCustomizationApplied"; | 82 const char kServicesCustomizationAppliedPref[] = "ServicesCustomizationApplied"; |
70 | 83 |
71 // Maximum number of retries to fetch file if network is not available. | 84 // Maximum number of retries to fetch file if network is not available. |
72 const int kMaxFetchRetries = 3; | 85 const int kMaxFetchRetries = 3; |
73 | 86 |
74 // Delay between file fetch retries if network is not available. | 87 // Delay between file fetch retries if network is not available. |
75 const int kRetriesDelayInSec = 2; | 88 const int kRetriesDelayInSec = 2; |
76 | 89 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 const base::DictionaryValue* default_dictionary = NULL; | 129 const base::DictionaryValue* default_dictionary = NULL; |
117 if (dictionary_content->GetDictionary(kDefaultAttr, &default_dictionary)) { | 130 if (dictionary_content->GetDictionary(kDefaultAttr, &default_dictionary)) { |
118 std::string result; | 131 std::string result; |
119 if (default_dictionary->GetString(entry_name, &result)) | 132 if (default_dictionary->GetString(entry_name, &result)) |
120 return result; | 133 return result; |
121 } | 134 } |
122 | 135 |
123 return std::string(); | 136 return std::string(); |
124 } | 137 } |
125 | 138 |
| 139 void CheckWallpaperCacheExists(const base::FilePath& path, bool* exists) { |
| 140 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 141 DCHECK(exists); |
| 142 *exists = base::PathExists(path); |
| 143 } |
| 144 |
126 } // anonymous namespace | 145 } // anonymous namespace |
127 | 146 |
128 namespace chromeos { | 147 namespace chromeos { |
129 | 148 |
130 // Template URL where to fetch OEM services customization manifest from. | 149 // Template URL where to fetch OEM services customization manifest from. |
131 const char ServicesCustomizationDocument::kManifestUrl[] = | 150 const char ServicesCustomizationDocument::kManifestUrl[] = |
132 "https://ssl.gstatic.com/chrome/chromeos-customization/%s.json"; | 151 "https://ssl.gstatic.com/chrome/chromeos-customization/%s.json"; |
133 | 152 |
134 // A custom extensions::ExternalLoader that the ServicesCustomizationDocument | 153 // A custom extensions::ExternalLoader that the ServicesCustomizationDocument |
135 // creates and uses to publish OEM default apps to the extensions system. | 154 // creates and uses to publish OEM default apps to the extensions system. |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 // static | 368 // static |
350 ServicesCustomizationDocument* ServicesCustomizationDocument::GetInstance() { | 369 ServicesCustomizationDocument* ServicesCustomizationDocument::GetInstance() { |
351 return Singleton<ServicesCustomizationDocument, | 370 return Singleton<ServicesCustomizationDocument, |
352 DefaultSingletonTraits<ServicesCustomizationDocument> >::get(); | 371 DefaultSingletonTraits<ServicesCustomizationDocument> >::get(); |
353 } | 372 } |
354 | 373 |
355 // static | 374 // static |
356 void ServicesCustomizationDocument::RegisterPrefs( | 375 void ServicesCustomizationDocument::RegisterPrefs( |
357 PrefRegistrySimple* registry) { | 376 PrefRegistrySimple* registry) { |
358 registry->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); | 377 registry->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); |
| 378 registry->RegisterStringPref(prefs::kCustomizationDefaultWallpaperURL, ""); |
359 } | 379 } |
360 | 380 |
361 // static | 381 // static |
362 void ServicesCustomizationDocument::RegisterProfilePrefs( | 382 void ServicesCustomizationDocument::RegisterProfilePrefs( |
363 user_prefs::PrefRegistrySyncable* registry) { | 383 user_prefs::PrefRegistrySyncable* registry) { |
364 registry->RegisterDictionaryPref( | 384 registry->RegisterDictionaryPref( |
365 kServicesCustomizationKey, | 385 kServicesCustomizationKey, |
366 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 386 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
367 } | 387 } |
368 | 388 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 LOG(ERROR) << "URL fetch for services customization failed:" | 530 LOG(ERROR) << "URL fetch for services customization failed:" |
511 << " response code = " << source->GetResponseCode() | 531 << " response code = " << source->GetResponseCode() |
512 << " URL = " << source->GetURL().spec(); | 532 << " URL = " << source->GetURL().spec(); |
513 | 533 |
514 LogManifestLoadResult(HISTOGRAM_LOAD_RESULT_RETRIES_FAIL); | 534 LogManifestLoadResult(HISTOGRAM_LOAD_RESULT_RETRIES_FAIL); |
515 } | 535 } |
516 fetch_started_ = false; | 536 fetch_started_ = false; |
517 } | 537 } |
518 | 538 |
519 bool ServicesCustomizationDocument::ApplyOOBECustomization() { | 539 bool ServicesCustomizationDocument::ApplyOOBECustomization() { |
520 // TODO(dpolukhin): apply default wallpaper, crbug.com/348136. | 540 CheckAndApplyWallpaper(); |
521 SetApplied(true); | 541 SetApplied(true); |
522 return true; | 542 return true; |
523 } | 543 } |
524 | 544 |
525 GURL ServicesCustomizationDocument::GetDefaultWallpaperUrl() const { | 545 GURL ServicesCustomizationDocument::GetDefaultWallpaperUrl() const { |
526 if (!IsReady()) | 546 if (!IsReady()) |
527 return GURL(); | 547 return GURL(); |
528 | 548 |
529 std::string url; | 549 std::string url; |
530 root_->GetString(kDefaultWallpaperAttr, &url); | 550 root_->GetString(kDefaultWallpaperAttr, &url); |
(...skipping 24 matching lines...) Expand all Loading... |
555 } | 575 } |
556 | 576 |
557 std::string ServicesCustomizationDocument::GetOemAppsFolderName( | 577 std::string ServicesCustomizationDocument::GetOemAppsFolderName( |
558 const std::string& locale) const { | 578 const std::string& locale) const { |
559 if (!IsReady()) | 579 if (!IsReady()) |
560 return std::string(); | 580 return std::string(); |
561 | 581 |
562 return GetOemAppsFolderNameImpl(locale, *root_); | 582 return GetOemAppsFolderNameImpl(locale, *root_); |
563 } | 583 } |
564 | 584 |
| 585 // static |
| 586 base::FilePath ServicesCustomizationDocument::GetCustomizedWallpaperCacheDir() { |
| 587 base::FilePath custom_wallpaper_dir; |
| 588 const bool success = PathService::Get(chrome::DIR_CHROMEOS_CUSTOM_WALLPAPERS, |
| 589 &custom_wallpaper_dir); |
| 590 DCHECK(success); |
| 591 |
| 592 if (success) |
| 593 return custom_wallpaper_dir.Append(kCustomizationDefaultWallpaperDir); |
| 594 |
| 595 return custom_wallpaper_dir; |
| 596 } |
| 597 |
| 598 // static |
| 599 base::FilePath |
| 600 ServicesCustomizationDocument::GetCustomizedWallpaperDownloadedFileName() { |
| 601 const base::FilePath dir = GetCustomizedWallpaperCacheDir(); |
| 602 if (dir.empty()) |
| 603 return dir; |
| 604 return dir.Append(kCustomizationDefaultWallpaperDownloadedFile); |
| 605 } |
| 606 |
| 607 // static |
| 608 void ServicesCustomizationDocument::OnCustomizedWallpaperDownloaded( |
| 609 const GURL& wallpaper_url) { |
| 610 DCHECK(wallpaper_url.is_valid()); |
| 611 |
| 612 VLOG(1) << "Setting default wallpaper to '" |
| 613 << GetCustomizedWallpaperDownloadedFileName().value() << "' ('" |
| 614 << wallpaper_url.spec() << "')"; |
| 615 WallpaperManager::Get()->SetCustomizedDefaultWallpaper( |
| 616 wallpaper_url, |
| 617 GetCustomizedWallpaperDownloadedFileName(), |
| 618 GetCustomizedWallpaperCacheDir()); |
| 619 } |
| 620 |
| 621 void ServicesCustomizationDocument::DestroyWallpaperDownloader() { |
| 622 wallpaper_downloader_.reset(); |
| 623 } |
| 624 |
565 scoped_ptr<base::DictionaryValue> | 625 scoped_ptr<base::DictionaryValue> |
566 ServicesCustomizationDocument::GetDefaultAppsInProviderFormat( | 626 ServicesCustomizationDocument::GetDefaultAppsInProviderFormat( |
567 const base::DictionaryValue& root) { | 627 const base::DictionaryValue& root) { |
568 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue); | 628 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue); |
569 const base::ListValue* apps_list = NULL; | 629 const base::ListValue* apps_list = NULL; |
570 if (root.GetList(kDefaultAppsAttr, &apps_list)) { | 630 if (root.GetList(kDefaultAppsAttr, &apps_list)) { |
571 for (size_t i = 0; i < apps_list->GetSize(); ++i) { | 631 for (size_t i = 0; i < apps_list->GetSize(); ++i) { |
572 std::string app_id; | 632 std::string app_id; |
573 if (apps_list->GetString(i, &app_id)) { | 633 if (apps_list->GetString(i, &app_id)) { |
574 base::DictionaryValue* entry = new base::DictionaryValue; | 634 base::DictionaryValue* entry = new base::DictionaryValue; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 service->SetOemFolderName(name); | 700 service->SetOemFolderName(name); |
641 } | 701 } |
642 | 702 |
643 std::string ServicesCustomizationDocument::GetOemAppsFolderNameImpl( | 703 std::string ServicesCustomizationDocument::GetOemAppsFolderNameImpl( |
644 const std::string& locale, | 704 const std::string& locale, |
645 const base::DictionaryValue& root) const { | 705 const base::DictionaryValue& root) const { |
646 return GetLocaleSpecificStringImpl( | 706 return GetLocaleSpecificStringImpl( |
647 &root, locale, kLocalizedContent, kDefaultAppsFolderName); | 707 &root, locale, kLocalizedContent, kDefaultAppsFolderName); |
648 } | 708 } |
649 | 709 |
| 710 void ServicesCustomizationDocument::StartOEMWallpaperDownload( |
| 711 const GURL& wallpaper_url) { |
| 712 DCHECK(wallpaper_url.is_valid()); |
| 713 |
| 714 const base::FilePath dir = GetCustomizedWallpaperCacheDir(); |
| 715 const base::FilePath file = GetCustomizedWallpaperDownloadedFileName(); |
| 716 if (!dir.empty() && !file.empty()) { |
| 717 wallpaper_downloader_.reset(new CustomizationWallpaperDownloader( |
| 718 g_browser_process->system_request_context(), wallpaper_url, dir, file)); |
| 719 |
| 720 wallpaper_downloader_->Start(); |
| 721 } |
| 722 } |
| 723 |
| 724 void ServicesCustomizationDocument::CheckAndApplyWallpaper() { |
| 725 if (wallpaper_downloader_.get()) { |
| 726 VLOG(1) << "CheckAndApplyWallpaper(): download has already started."; |
| 727 return; |
| 728 } |
| 729 GURL wallpaper_url = GetDefaultWallpaperUrl(); |
| 730 // Should fail if this ever happens in tests. |
| 731 DCHECK(wallpaper_url.is_valid() || wallpaper_url.is_empty()); |
| 732 if (!wallpaper_url.is_valid()) { |
| 733 if (!wallpaper_url.is_empty()) { |
| 734 LOG(WARNING) << "Invalid Customized Wallpaper URL."; |
| 735 } |
| 736 return; |
| 737 } |
| 738 scoped_ptr<bool> exists(new bool(false)); |
| 739 |
| 740 base::Closure check_file_exists = |
| 741 base::Bind(&CheckWallpaperCacheExists, |
| 742 GetCustomizedWallpaperDownloadedFileName(), |
| 743 base::Unretained(exists.get())); |
| 744 base::Closure on_checked_closure = |
| 745 base::Bind(&ServicesCustomizationDocument::OnCheckedWallpaperCacheExists, |
| 746 weak_ptr_factory_.GetWeakPtr(), |
| 747 base::Passed(exists.Pass())); |
| 748 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 749 FROM_HERE, check_file_exists, on_checked_closure)) { |
| 750 LOG(WARNING) << "Failed to start check Wallpaper cache exists."; |
| 751 } |
| 752 } |
| 753 |
| 754 void ServicesCustomizationDocument::OnCheckedWallpaperCacheExists( |
| 755 scoped_ptr<bool> exists) { |
| 756 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 757 DCHECK(exists.get()); |
| 758 |
| 759 ApplyWallpaper(*exists); |
| 760 } |
| 761 |
| 762 void ServicesCustomizationDocument::ApplyWallpaper( |
| 763 bool default_wallpaper_file_exists) { |
| 764 GURL wallpaper_url = GetDefaultWallpaperUrl(); |
| 765 DCHECK(wallpaper_url.is_valid()); |
| 766 |
| 767 PrefService* pref_service = g_browser_process->local_state(); |
| 768 |
| 769 std::string current_url = |
| 770 pref_service->GetString(prefs::kCustomizationDefaultWallpaperURL); |
| 771 if (current_url != wallpaper_url.spec()) { |
| 772 VLOG(1) << "ServicesCustomizationDocument::ApplyWallpaper() : " |
| 773 << "Wallpaper URL in customization document '" |
| 774 << wallpaper_url.spec() << "' differs from current '" << current_url |
| 775 << "'." |
| 776 << (GURL(current_url).is_valid() && default_wallpaper_file_exists |
| 777 ? " Ignored." |
| 778 : " Will refetch."); |
| 779 } |
| 780 // Never update system-wide wallpaper (i.e. do not check |
| 781 // current_url == wallpaper_url.spec() ) |
| 782 if (GURL(current_url).is_valid() && default_wallpaper_file_exists) { |
| 783 VLOG(1) |
| 784 << "ServicesCustomizationDocument::ApplyWallpaper() : reuse existing"; |
| 785 OnCustomizedWallpaperDownloaded(GURL(current_url)); |
| 786 } else { |
| 787 VLOG(1) |
| 788 << "ServicesCustomizationDocument::ApplyWallpaper() : start download"; |
| 789 StartOEMWallpaperDownload(wallpaper_url); |
| 790 } |
| 791 } |
| 792 |
650 } // namespace chromeos | 793 } // namespace chromeos |
OLD | NEW |