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 const char kCustomizationDefaultWallpaperDir[] = "customization/"; |
| 73 |
| 74 // The original downloaded image file is stored under this name. |
| 75 const char kCustomizationDefaultWallpaperDownloadedFile[] = |
| 76 "default.downloaded"; |
| 77 |
67 // Name of local state option that tracks if services customization has been | 78 // Name of local state option that tracks if services customization has been |
68 // applied. | 79 // applied. |
69 const char kServicesCustomizationAppliedPref[] = "ServicesCustomizationApplied"; | 80 const char kServicesCustomizationAppliedPref[] = "ServicesCustomizationApplied"; |
70 | 81 |
71 // Maximum number of retries to fetch file if network is not available. | 82 // Maximum number of retries to fetch file if network is not available. |
72 const int kMaxFetchRetries = 3; | 83 const int kMaxFetchRetries = 3; |
73 | 84 |
74 // Delay between file fetch retries if network is not available. | 85 // Delay between file fetch retries if network is not available. |
75 const int kRetriesDelayInSec = 2; | 86 const int kRetriesDelayInSec = 2; |
76 | 87 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 const base::DictionaryValue* default_dictionary = NULL; | 127 const base::DictionaryValue* default_dictionary = NULL; |
117 if (dictionary_content->GetDictionary(kDefaultAttr, &default_dictionary)) { | 128 if (dictionary_content->GetDictionary(kDefaultAttr, &default_dictionary)) { |
118 std::string result; | 129 std::string result; |
119 if (default_dictionary->GetString(entry_name, &result)) | 130 if (default_dictionary->GetString(entry_name, &result)) |
120 return result; | 131 return result; |
121 } | 132 } |
122 | 133 |
123 return std::string(); | 134 return std::string(); |
124 } | 135 } |
125 | 136 |
| 137 void CheckWallpaperCacheExists(const base::FilePath& path, bool* exists) { |
| 138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 139 DCHECK(exists); |
| 140 *exists = base::PathExists(path); |
| 141 } |
| 142 |
126 } // anonymous namespace | 143 } // anonymous namespace |
127 | 144 |
128 namespace chromeos { | 145 namespace chromeos { |
129 | 146 |
130 // Template URL where to fetch OEM services customization manifest from. | 147 // Template URL where to fetch OEM services customization manifest from. |
131 const char ServicesCustomizationDocument::kManifestUrl[] = | 148 const char ServicesCustomizationDocument::kManifestUrl[] = |
132 "https://ssl.gstatic.com/chrome/chromeos-customization/%s.json"; | 149 "https://ssl.gstatic.com/chrome/chromeos-customization/%s.json"; |
133 | 150 |
134 // A custom extensions::ExternalLoader that the ServicesCustomizationDocument | 151 // A custom extensions::ExternalLoader that the ServicesCustomizationDocument |
135 // creates and uses to publish OEM default apps to the extensions system. | 152 // 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 | 366 // static |
350 ServicesCustomizationDocument* ServicesCustomizationDocument::GetInstance() { | 367 ServicesCustomizationDocument* ServicesCustomizationDocument::GetInstance() { |
351 return Singleton<ServicesCustomizationDocument, | 368 return Singleton<ServicesCustomizationDocument, |
352 DefaultSingletonTraits<ServicesCustomizationDocument> >::get(); | 369 DefaultSingletonTraits<ServicesCustomizationDocument> >::get(); |
353 } | 370 } |
354 | 371 |
355 // static | 372 // static |
356 void ServicesCustomizationDocument::RegisterPrefs( | 373 void ServicesCustomizationDocument::RegisterPrefs( |
357 PrefRegistrySimple* registry) { | 374 PrefRegistrySimple* registry) { |
358 registry->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); | 375 registry->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); |
| 376 registry->RegisterStringPref(prefs::kCustomizationDefaultWallpaperURL, ""); |
359 } | 377 } |
360 | 378 |
361 // static | 379 // static |
362 void ServicesCustomizationDocument::RegisterProfilePrefs( | 380 void ServicesCustomizationDocument::RegisterProfilePrefs( |
363 user_prefs::PrefRegistrySyncable* registry) { | 381 user_prefs::PrefRegistrySyncable* registry) { |
364 registry->RegisterDictionaryPref( | 382 registry->RegisterDictionaryPref( |
365 kServicesCustomizationKey, | 383 kServicesCustomizationKey, |
366 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 384 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
367 } | 385 } |
368 | 386 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 // This doesn't stop fetching manifest on next restart. | 527 // This doesn't stop fetching manifest on next restart. |
510 LOG(ERROR) << "URL fetch for services customization failed:" | 528 LOG(ERROR) << "URL fetch for services customization failed:" |
511 << " response code = " << source->GetResponseCode() | 529 << " response code = " << source->GetResponseCode() |
512 << " URL = " << source->GetURL().spec(); | 530 << " URL = " << source->GetURL().spec(); |
513 | 531 |
514 LogManifestLoadResult(HISTOGRAM_LOAD_RESULT_RETRIES_FAIL); | 532 LogManifestLoadResult(HISTOGRAM_LOAD_RESULT_RETRIES_FAIL); |
515 } | 533 } |
516 fetch_started_ = false; | 534 fetch_started_ = false; |
517 } | 535 } |
518 | 536 |
| 537 void ServicesCustomizationDocument::StartOEMWallpaperDownload( |
| 538 const GURL& wallpaper_url) { |
| 539 DCHECK(wallpaper_url.is_valid()); |
| 540 |
| 541 const base::FilePath dir = GetCustomizedWallpaperCacheDir(); |
| 542 const base::FilePath file = GetCustomizedWallpaperDownloadedFileName(); |
| 543 if (!dir.empty() && !file.empty()) { |
| 544 wallpaper_downloader_.reset(new CustomizationWallpaperDownloader( |
| 545 g_browser_process->system_request_context(), wallpaper_url, dir, file)); |
| 546 |
| 547 wallpaper_downloader_->Start(); |
| 548 } |
| 549 } |
| 550 |
| 551 // static |
| 552 base::FilePath ServicesCustomizationDocument::GetCustomizedWallpaperCacheDir() { |
| 553 base::FilePath custom_wallpaper_dir; |
| 554 const bool success = PathService::Get(chrome::DIR_CHROMEOS_CUSTOM_WALLPAPERS, |
| 555 &custom_wallpaper_dir); |
| 556 DCHECK(success); |
| 557 |
| 558 if (success) |
| 559 return custom_wallpaper_dir.Append(kCustomizationDefaultWallpaperDir); |
| 560 |
| 561 return custom_wallpaper_dir; |
| 562 } |
| 563 |
| 564 // static |
| 565 base::FilePath |
| 566 ServicesCustomizationDocument::GetCustomizedWallpaperDownloadedFileName() { |
| 567 const base::FilePath dir = GetCustomizedWallpaperCacheDir(); |
| 568 if (dir.empty()) |
| 569 return dir; |
| 570 return dir.Append(kCustomizationDefaultWallpaperDownloadedFile); |
| 571 } |
| 572 |
| 573 // static |
| 574 void ServicesCustomizationDocument::SetDefaultWallpaperPath( |
| 575 const GURL& wallpaper_url) { |
| 576 DCHECK(wallpaper_url.is_valid()); |
| 577 |
| 578 VLOG(1) << "Setting default wallpaper to '" |
| 579 << GetCustomizedWallpaperDownloadedFileName().value() << "' ('" |
| 580 << wallpaper_url.spec() << "')"; |
| 581 WallpaperManager::Get()->SetCustomizedDefaultWallpaper( |
| 582 wallpaper_url, |
| 583 GetCustomizedWallpaperDownloadedFileName(), |
| 584 GetCustomizedWallpaperCacheDir()); |
| 585 } |
| 586 |
| 587 // static |
| 588 void ServicesCustomizationDocument::DestroyWallpaperDownloader() { |
| 589 ServicesCustomizationDocument* self = |
| 590 ServicesCustomizationDocument::GetInstance(); |
| 591 DCHECK(self); |
| 592 self->wallpaper_downloader_.reset(); |
| 593 } |
| 594 |
| 595 void ServicesCustomizationDocument::ApplyWallpaper( |
| 596 bool default_wallpaper_file_exists) { |
| 597 GURL wallpaper_url = GetDefaultWallpaperUrl(); |
| 598 DCHECK(wallpaper_url.is_valid()); |
| 599 |
| 600 PrefService* prefService = g_browser_process->local_state(); |
| 601 |
| 602 std::string current_url = |
| 603 prefService->GetString(prefs::kCustomizationDefaultWallpaperURL); |
| 604 if (current_url != wallpaper_url.spec()) { |
| 605 VLOG(1) << "ServicesCustomizationDocument::ApplyWallpaper() : " |
| 606 << "Wallpaper URL in customization document '" |
| 607 << wallpaper_url.spec() << "' differs from current '" << current_url |
| 608 << "'. Ignored."; |
| 609 } |
| 610 // Never update system-wide wallpaper (i.e. do not check |
| 611 // current_url == wallpaper_url.spec() ) |
| 612 if (GURL(current_url).is_valid() && default_wallpaper_file_exists) { |
| 613 VLOG(1) |
| 614 << "ServicesCustomizationDocument::ApplyWallpaper() : reuse existing"; |
| 615 SetDefaultWallpaperPath(GURL(current_url)); |
| 616 } else { |
| 617 VLOG(1) |
| 618 << "ServicesCustomizationDocument::ApplyWallpaper() : start download"; |
| 619 StartOEMWallpaperDownload(wallpaper_url); |
| 620 } |
| 621 } |
| 622 |
| 623 void ServicesCustomizationDocument::OnCheckedWallpaperCacheExists( |
| 624 scoped_ptr<bool> exists) { |
| 625 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 626 DCHECK(exists.get()); |
| 627 |
| 628 ApplyWallpaper(*exists); |
| 629 } |
| 630 |
| 631 void ServicesCustomizationDocument::CheckAndApplyWallpaper() { |
| 632 GURL wallpaper_url = GetDefaultWallpaperUrl(); |
| 633 // Should fail if this ever happens in tests. |
| 634 DCHECK(wallpaper_url.is_valid() || wallpaper_url.is_empty()); |
| 635 if (!wallpaper_url.is_valid()) { |
| 636 if (!wallpaper_url.is_empty()) { |
| 637 LOG(WARNING) << "Invalid Customized Wallpaper URL."; |
| 638 } |
| 639 return; |
| 640 } |
| 641 scoped_ptr<bool> exists(new bool(false)); |
| 642 |
| 643 base::Closure check_file_exists = |
| 644 base::Bind(&CheckWallpaperCacheExists, |
| 645 GetCustomizedWallpaperDownloadedFileName(), |
| 646 base::Unretained(exists.get())); |
| 647 base::Closure on_checked_closure = |
| 648 base::Bind(&ServicesCustomizationDocument::OnCheckedWallpaperCacheExists, |
| 649 weak_ptr_factory_.GetWeakPtr(), |
| 650 base::Passed(exists.Pass())); |
| 651 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 652 FROM_HERE, check_file_exists, on_checked_closure)) { |
| 653 LOG(WARNING) << "Failed to start check Wallpaper cache exists."; |
| 654 } |
| 655 } |
| 656 |
519 bool ServicesCustomizationDocument::ApplyOOBECustomization() { | 657 bool ServicesCustomizationDocument::ApplyOOBECustomization() { |
520 // TODO(dpolukhin): apply default wallpaper, crbug.com/348136. | 658 CheckAndApplyWallpaper(); |
521 SetApplied(true); | 659 SetApplied(true); |
522 return true; | 660 return true; |
523 } | 661 } |
524 | 662 |
525 GURL ServicesCustomizationDocument::GetDefaultWallpaperUrl() const { | 663 GURL ServicesCustomizationDocument::GetDefaultWallpaperUrl() const { |
526 if (!IsReady()) | 664 if (!IsReady()) |
527 return GURL(); | 665 return GURL(); |
528 | 666 |
529 std::string url; | 667 std::string url; |
530 root_->GetString(kDefaultWallpaperAttr, &url); | 668 root_->GetString(kDefaultWallpaperAttr, &url); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 } | 779 } |
642 | 780 |
643 std::string ServicesCustomizationDocument::GetOemAppsFolderNameImpl( | 781 std::string ServicesCustomizationDocument::GetOemAppsFolderNameImpl( |
644 const std::string& locale, | 782 const std::string& locale, |
645 const base::DictionaryValue& root) const { | 783 const base::DictionaryValue& root) const { |
646 return GetLocaleSpecificStringImpl( | 784 return GetLocaleSpecificStringImpl( |
647 &root, locale, kLocalizedContent, kDefaultAppsFolderName); | 785 &root, locale, kLocalizedContent, kDefaultAppsFolderName); |
648 } | 786 } |
649 | 787 |
650 } // namespace chromeos | 788 } // namespace chromeos |
OLD | NEW |