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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 } | 479 } |
462 | 480 |
463 LogManifestLoadResult(HISTOGRAM_LOAD_RESULT_PARSING_ERROR); | 481 LogManifestLoadResult(HISTOGRAM_LOAD_RESULT_PARSING_ERROR); |
464 return false; | 482 return false; |
465 } | 483 } |
466 | 484 |
467 void ServicesCustomizationDocument::OnManifestLoaded() { | 485 void ServicesCustomizationDocument::OnManifestLoaded() { |
468 if (!ServicesCustomizationDocument::WasOOBECustomizationApplied()) | 486 if (!ServicesCustomizationDocument::WasOOBECustomizationApplied()) |
469 ApplyOOBECustomization(); | 487 ApplyOOBECustomization(); |
470 | 488 |
489 CheckAndApplyWallpaper(); | |
490 | |
471 scoped_ptr<base::DictionaryValue> prefs = | 491 scoped_ptr<base::DictionaryValue> prefs = |
472 GetDefaultAppsInProviderFormat(*root_); | 492 GetDefaultAppsInProviderFormat(*root_); |
473 for (ExternalLoaders::iterator it = external_loaders_.begin(); | 493 for (ExternalLoaders::iterator it = external_loaders_.begin(); |
474 it != external_loaders_.end(); ++it) { | 494 it != external_loaders_.end(); ++it) { |
475 if (*it) { | 495 if (*it) { |
476 UpdateCachedManifest((*it)->profile()); | 496 UpdateCachedManifest((*it)->profile()); |
477 (*it)->SetCurrentApps( | 497 (*it)->SetCurrentApps( |
478 scoped_ptr<base::DictionaryValue>(prefs->DeepCopy())); | 498 scoped_ptr<base::DictionaryValue>(prefs->DeepCopy())); |
479 SetOemFolderName((*it)->profile(), *root_); | 499 SetOemFolderName((*it)->profile(), *root_); |
480 } | 500 } |
(...skipping 28 matching lines...) Expand all Loading... | |
509 // This doesn't stop fetching manifest on next restart. | 529 // This doesn't stop fetching manifest on next restart. |
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 |
539 void ServicesCustomizationDocument::StartOEMWallpaperDownload( | |
540 const GURL& wallpaper_url) { | |
541 DCHECK(wallpaper_url.is_valid()); | |
542 | |
543 const base::FilePath dir = GetCustomizedWallpaperCacheDir(); | |
544 const base::FilePath file = GetCustomizedWallpaperDownloadedFileName(); | |
545 if (!dir.empty() && !file.empty()) { | |
546 wallpaper_downloader_.reset(new CustomizationWallpaperDownloader( | |
547 g_browser_process->system_request_context(), wallpaper_url, dir, file)); | |
548 | |
549 wallpaper_downloader_->Start(); | |
550 } | |
551 } | |
552 | |
553 // static | |
554 base::FilePath ServicesCustomizationDocument::GetCustomizedWallpaperCacheDir() { | |
555 base::FilePath custom_wallpaper_dir; | |
556 const bool success = PathService::Get(chrome::DIR_CHROMEOS_CUSTOM_WALLPAPERS, | |
557 &custom_wallpaper_dir); | |
558 DCHECK(success); | |
559 | |
560 if (success) | |
561 return custom_wallpaper_dir.Append(kCustomizationDefaultWallpaperDir); | |
562 | |
563 return custom_wallpaper_dir; | |
564 } | |
565 | |
566 // static | |
567 base::FilePath | |
568 ServicesCustomizationDocument::GetCustomizedWallpaperDownloadedFileName() { | |
569 const base::FilePath dir = GetCustomizedWallpaperCacheDir(); | |
570 if (dir.empty()) | |
571 return dir; | |
572 return dir.Append(kCustomizationDefaultWallpaperDownloadedFile); | |
573 } | |
574 | |
575 // static | |
576 void ServicesCustomizationDocument::SetDefaultWallpaperPath( | |
577 const GURL& wallpaper_url) { | |
578 DCHECK(wallpaper_url.is_valid()); | |
579 | |
580 VLOG(1) << "Setting default wallpaper to '" | |
581 << GetCustomizedWallpaperDownloadedFileName().value() << "' ('" | |
582 << wallpaper_url.spec() << "')"; | |
583 WallpaperManager::Get()->SetCustomizedDefaultWallpaper( | |
584 wallpaper_url, | |
585 GetCustomizedWallpaperDownloadedFileName(), | |
586 GetCustomizedWallpaperCacheDir()); | |
587 } | |
588 | |
589 // static | |
590 void ServicesCustomizationDocument::DestroyWallpaperDownloader() { | |
591 ServicesCustomizationDocument* self = | |
592 ServicesCustomizationDocument::GetInstance(); | |
593 DCHECK(self); | |
594 self->wallpaper_downloader_.reset(); | |
595 } | |
596 | |
597 void ServicesCustomizationDocument::ApplyWallpaper( | |
598 bool default_wallpaper_file_exists) { | |
599 GURL wallpaper_url = GetDefaultWallpaperUrl(); | |
600 DCHECK(wallpaper_url.is_valid()); | |
601 | |
602 PrefService* prefService = g_browser_process->local_state(); | |
Daniel Erat
2014/03/25 21:58:29
s/prefService/pref_service/
Alexander Alekseev
2014/03/27 00:28:37
Done.
| |
603 | |
604 std::string current_url = | |
605 prefService->GetString(prefs::kCustomizationDefaultWallpaperURL); | |
606 if (current_url != wallpaper_url.spec()) { | |
607 VLOG(1) << "ServicesCustomizationDocument::ApplyWallpaper() : " | |
608 << "Wallpaper URL in customization document '" | |
609 << wallpaper_url.spec() << "' differs from current '" << current_url | |
610 << "'." | |
611 << (GURL(current_url).is_valid() && default_wallpaper_file_exists | |
612 ? " Ignored." | |
613 : " Will refetch."); | |
614 } | |
615 // Never update system-wide wallpaper (i.e. do not check | |
616 // current_url == wallpaper_url.spec() ) | |
617 if (GURL(current_url).is_valid() && default_wallpaper_file_exists) { | |
618 VLOG(1) | |
619 << "ServicesCustomizationDocument::ApplyWallpaper() : reuse existing"; | |
620 SetDefaultWallpaperPath(GURL(current_url)); | |
621 } else { | |
622 VLOG(1) | |
623 << "ServicesCustomizationDocument::ApplyWallpaper() : start download"; | |
624 StartOEMWallpaperDownload(wallpaper_url); | |
625 } | |
626 } | |
627 | |
628 void ServicesCustomizationDocument::OnCheckedWallpaperCacheExists( | |
629 scoped_ptr<bool> exists) { | |
630 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
631 DCHECK(exists.get()); | |
632 | |
633 ApplyWallpaper(*exists); | |
634 } | |
635 | |
636 void ServicesCustomizationDocument::CheckAndApplyWallpaper() { | |
637 if (wallpaper_downloader_.get()) { | |
638 VLOG(1) << "CheckAndApplyWallpaper(): download has already started."; | |
639 return; | |
640 } | |
641 GURL wallpaper_url = GetDefaultWallpaperUrl(); | |
642 // Should fail if this ever happens in tests. | |
643 DCHECK(wallpaper_url.is_valid() || wallpaper_url.is_empty()); | |
644 if (!wallpaper_url.is_valid()) { | |
645 if (!wallpaper_url.is_empty()) { | |
646 LOG(WARNING) << "Invalid Customized Wallpaper URL."; | |
647 } | |
648 return; | |
649 } | |
650 scoped_ptr<bool> exists(new bool(false)); | |
651 | |
652 base::Closure check_file_exists = | |
653 base::Bind(&CheckWallpaperCacheExists, | |
654 GetCustomizedWallpaperDownloadedFileName(), | |
655 base::Unretained(exists.get())); | |
656 base::Closure on_checked_closure = | |
657 base::Bind(&ServicesCustomizationDocument::OnCheckedWallpaperCacheExists, | |
658 weak_ptr_factory_.GetWeakPtr(), | |
659 base::Passed(exists.Pass())); | |
660 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( | |
661 FROM_HERE, check_file_exists, on_checked_closure)) { | |
662 LOG(WARNING) << "Failed to start check Wallpaper cache exists."; | |
663 } | |
664 } | |
665 | |
519 bool ServicesCustomizationDocument::ApplyOOBECustomization() { | 666 bool ServicesCustomizationDocument::ApplyOOBECustomization() { |
520 // TODO(dpolukhin): apply default wallpaper, crbug.com/348136. | |
521 SetApplied(true); | 667 SetApplied(true); |
522 return true; | 668 return true; |
523 } | 669 } |
524 | 670 |
525 GURL ServicesCustomizationDocument::GetDefaultWallpaperUrl() const { | 671 GURL ServicesCustomizationDocument::GetDefaultWallpaperUrl() const { |
526 if (!IsReady()) | 672 if (!IsReady()) |
527 return GURL(); | 673 return GURL(); |
528 | 674 |
529 std::string url; | 675 std::string url; |
530 root_->GetString(kDefaultWallpaperAttr, &url); | 676 root_->GetString(kDefaultWallpaperAttr, &url); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
641 } | 787 } |
642 | 788 |
643 std::string ServicesCustomizationDocument::GetOemAppsFolderNameImpl( | 789 std::string ServicesCustomizationDocument::GetOemAppsFolderNameImpl( |
644 const std::string& locale, | 790 const std::string& locale, |
645 const base::DictionaryValue& root) const { | 791 const base::DictionaryValue& root) const { |
646 return GetLocaleSpecificStringImpl( | 792 return GetLocaleSpecificStringImpl( |
647 &root, locale, kLocalizedContent, kDefaultAppsFolderName); | 793 &root, locale, kLocalizedContent, kDefaultAppsFolderName); |
648 } | 794 } |
649 | 795 |
650 } // namespace chromeos | 796 } // namespace chromeos |
OLD | NEW |