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/path_service.h" |
14 #include "base/prefs/pref_registry_simple.h" | 15 #include "base/prefs/pref_registry_simple.h" |
15 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
16 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 23 #include "chrome/browser/chromeos/customization_wallpaper_downloader.h" |
| 24 #include "chrome/browser/chromeos/login/wallpaper_manager.h" |
22 #include "chrome/browser/chromeos/login/wizard_controller.h" | 25 #include "chrome/browser/chromeos/login/wizard_controller.h" |
23 #include "chrome/browser/extensions/external_loader.h" | 26 #include "chrome/browser/extensions/external_loader.h" |
24 #include "chrome/browser/extensions/external_provider_impl.h" | 27 #include "chrome/browser/extensions/external_provider_impl.h" |
25 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 29 #include "chrome/common/chrome_paths.h" |
26 #include "chrome/common/extensions/extension_constants.h" | 30 #include "chrome/common/extensions/extension_constants.h" |
| 31 #include "chrome/common/pref_names.h" |
27 #include "chromeos/network/network_state.h" | 32 #include "chromeos/network/network_state.h" |
28 #include "chromeos/network/network_state_handler.h" | 33 #include "chromeos/network/network_state_handler.h" |
29 #include "chromeos/system/statistics_provider.h" | 34 #include "chromeos/system/statistics_provider.h" |
30 #include "components/user_prefs/pref_registry_syncable.h" | 35 #include "components/user_prefs/pref_registry_syncable.h" |
31 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
32 #include "net/base/load_flags.h" | 37 #include "net/base/load_flags.h" |
33 #include "net/http/http_response_headers.h" | 38 #include "net/http/http_response_headers.h" |
34 #include "net/url_request/url_fetcher.h" | 39 #include "net/url_request/url_fetcher.h" |
35 | 40 |
36 using content::BrowserThread; | 41 using content::BrowserThread; |
(...skipping 13 matching lines...) Expand all Loading... |
50 const char kEulaPageAttr[] = "eula_page"; | 55 const char kEulaPageAttr[] = "eula_page"; |
51 const char kDefaultWallpaperAttr[] = "default_wallpaper"; | 56 const char kDefaultWallpaperAttr[] = "default_wallpaper"; |
52 const char kDefaultAppsAttr[] = "default_apps"; | 57 const char kDefaultAppsAttr[] = "default_apps"; |
53 | 58 |
54 const char kAcceptedManifestVersion[] = "1.0"; | 59 const char kAcceptedManifestVersion[] = "1.0"; |
55 | 60 |
56 // Path to OEM partner startup customization manifest. | 61 // Path to OEM partner startup customization manifest. |
57 const char kStartupCustomizationManifestPath[] = | 62 const char kStartupCustomizationManifestPath[] = |
58 "/opt/oem/etc/startup_manifest.json"; | 63 "/opt/oem/etc/startup_manifest.json"; |
59 | 64 |
| 65 static const char kCustomizationDefaultWallpaperDir[] = "customization/"; |
| 66 |
| 67 // The original downloaded image file is stored under this name. |
| 68 static const char kCustomizationDefaultWallpaperDownloadedFile[] = |
| 69 "default.downloaded"; |
| 70 |
60 // Name of local state option that tracks if services customization has been | 71 // Name of local state option that tracks if services customization has been |
61 // applied. | 72 // applied. |
62 const char kServicesCustomizationAppliedPref[] = "ServicesCustomizationApplied"; | 73 const char kServicesCustomizationAppliedPref[] = "ServicesCustomizationApplied"; |
63 | 74 |
64 // Maximum number of retries to fetch file if network is not available. | 75 // Maximum number of retries to fetch file if network is not available. |
65 const int kMaxFetchRetries = 3; | 76 const int kMaxFetchRetries = 3; |
66 | 77 |
67 // Delay between file fetch retries if network is not available. | 78 // Delay between file fetch retries if network is not available. |
68 const int kRetriesDelayInSec = 2; | 79 const int kRetriesDelayInSec = 2; |
69 | 80 |
70 // Name of profile option that tracks cached version of service customization. | 81 // Name of profile option that tracks cached version of service customization. |
71 const char kServicesCustomizationKey[] = "customization.manifest_cache"; | 82 const char kServicesCustomizationKey[] = "customization.manifest_cache"; |
72 | 83 |
| 84 void CheckWallpaperCacheExists(const base::FilePath& path, bool* exists) { |
| 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 86 DCHECK(exists); |
| 87 *exists = base::PathExists(path); |
| 88 } |
| 89 |
73 } // anonymous namespace | 90 } // anonymous namespace |
74 | 91 |
75 namespace chromeos { | 92 namespace chromeos { |
76 | 93 |
77 // Template URL where to fetch OEM services customization manifest from. | 94 // Template URL where to fetch OEM services customization manifest from. |
78 const char ServicesCustomizationDocument::kManifestUrl[] = | 95 const char ServicesCustomizationDocument::kManifestUrl[] = |
79 "https://ssl.gstatic.com/chrome/chromeos-customization/%s.json"; | 96 "https://ssl.gstatic.com/chrome/chromeos-customization/%s.json"; |
80 | 97 |
81 // A custom extensions::ExternalLoader that the ServicesCustomizationDocument | 98 // A custom extensions::ExternalLoader that the ServicesCustomizationDocument |
82 // creates and uses to publish OEM default apps to the extensions system. | 99 // creates and uses to publish OEM default apps to the extensions system. |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 std::string StartupCustomizationDocument::GetEULAPage( | 304 std::string StartupCustomizationDocument::GetEULAPage( |
288 const std::string& locale) const { | 305 const std::string& locale) const { |
289 return GetLocaleSpecificString(locale, kSetupContentAttr, kEulaPageAttr); | 306 return GetLocaleSpecificString(locale, kSetupContentAttr, kEulaPageAttr); |
290 } | 307 } |
291 | 308 |
292 // ServicesCustomizationDocument implementation. ------------------------------- | 309 // ServicesCustomizationDocument implementation. ------------------------------- |
293 | 310 |
294 ServicesCustomizationDocument::ServicesCustomizationDocument() | 311 ServicesCustomizationDocument::ServicesCustomizationDocument() |
295 : CustomizationDocument(kAcceptedManifestVersion), | 312 : CustomizationDocument(kAcceptedManifestVersion), |
296 num_retries_(0), | 313 num_retries_(0), |
297 fetch_started_(false) { | 314 fetch_started_(false), |
| 315 weak_factory_(this) { |
298 } | 316 } |
299 | 317 |
300 ServicesCustomizationDocument::ServicesCustomizationDocument( | 318 ServicesCustomizationDocument::ServicesCustomizationDocument( |
301 const std::string& manifest) | 319 const std::string& manifest) |
302 : CustomizationDocument(kAcceptedManifestVersion) { | 320 : CustomizationDocument(kAcceptedManifestVersion), weak_factory_(this) { |
303 LoadManifestFromString(manifest); | 321 LoadManifestFromString(manifest); |
304 } | 322 } |
305 | 323 |
306 ServicesCustomizationDocument::~ServicesCustomizationDocument() {} | 324 ServicesCustomizationDocument::~ServicesCustomizationDocument() {} |
307 | 325 |
308 // static | 326 // static |
309 ServicesCustomizationDocument* ServicesCustomizationDocument::GetInstance() { | 327 ServicesCustomizationDocument* ServicesCustomizationDocument::GetInstance() { |
310 return Singleton<ServicesCustomizationDocument, | 328 return Singleton<ServicesCustomizationDocument, |
311 DefaultSingletonTraits<ServicesCustomizationDocument> >::get(); | 329 DefaultSingletonTraits<ServicesCustomizationDocument> >::get(); |
312 } | 330 } |
313 | 331 |
314 // static | 332 // static |
315 void ServicesCustomizationDocument::RegisterPrefs( | 333 void ServicesCustomizationDocument::RegisterPrefs( |
316 PrefRegistrySimple* registry) { | 334 PrefRegistrySimple* registry) { |
317 registry->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); | 335 registry->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); |
| 336 registry->RegisterStringPref(prefs::kCustomizationDefaultWallpaperURL, ""); |
318 } | 337 } |
319 | 338 |
320 // static | 339 // static |
321 void ServicesCustomizationDocument::RegisterProfilePrefs( | 340 void ServicesCustomizationDocument::RegisterProfilePrefs( |
322 user_prefs::PrefRegistrySyncable* registry) { | 341 user_prefs::PrefRegistrySyncable* registry) { |
323 registry->RegisterDictionaryPref( | 342 registry->RegisterDictionaryPref( |
324 kServicesCustomizationKey, | 343 kServicesCustomizationKey, |
325 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 344 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
326 } | 345 } |
327 | 346 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 this, &ServicesCustomizationDocument::StartFileFetch); | 469 this, &ServicesCustomizationDocument::StartFileFetch); |
451 return; | 470 return; |
452 } | 471 } |
453 LOG(ERROR) << "URL fetch for services customization failed:" | 472 LOG(ERROR) << "URL fetch for services customization failed:" |
454 << " response code = " << source->GetResponseCode() | 473 << " response code = " << source->GetResponseCode() |
455 << " URL = " << source->GetURL().spec(); | 474 << " URL = " << source->GetURL().spec(); |
456 fetch_started_ = false; | 475 fetch_started_ = false; |
457 } | 476 } |
458 } | 477 } |
459 | 478 |
| 479 void ServicesCustomizationDocument::StartOEMWallpaperDownload( |
| 480 const GURL& wallpaper_url) { |
| 481 DCHECK(wallpaper_url.is_valid()); |
| 482 |
| 483 const base::FilePath dir = GetCustomizedWallpaperCacheDir(); |
| 484 const base::FilePath file = GetCustomizedWallpaperDownloadedFileName(); |
| 485 if (!dir.empty() && !file.empty()) { |
| 486 wallpaper_downloader_.reset(new CustomizationWallpaperDownloader( |
| 487 g_browser_process->system_request_context(), wallpaper_url, dir, file)); |
| 488 |
| 489 wallpaper_downloader_->Start(); |
| 490 } |
| 491 } |
| 492 |
| 493 // static |
| 494 base::FilePath ServicesCustomizationDocument::GetCustomizedWallpaperCacheDir() { |
| 495 base::FilePath custom_wallpaper_dir; |
| 496 const bool success = PathService::Get(chrome::DIR_CHROMEOS_CUSTOM_WALLPAPERS, |
| 497 &custom_wallpaper_dir); |
| 498 DCHECK(success); |
| 499 |
| 500 if (success) |
| 501 return custom_wallpaper_dir.Append(kCustomizationDefaultWallpaperDir); |
| 502 |
| 503 return custom_wallpaper_dir; |
| 504 } |
| 505 |
| 506 // static |
| 507 base::FilePath |
| 508 ServicesCustomizationDocument::GetCustomizedWallpaperDownloadedFileName() { |
| 509 const base::FilePath dir = GetCustomizedWallpaperCacheDir(); |
| 510 if (dir.empty()) |
| 511 return dir; |
| 512 return dir.Append(kCustomizationDefaultWallpaperDownloadedFile); |
| 513 } |
| 514 |
| 515 // static |
| 516 void ServicesCustomizationDocument::SetDefaultWallpaperPath( |
| 517 const GURL& wallpaper_url) { |
| 518 DCHECK(wallpaper_url.is_valid()); |
| 519 |
| 520 VLOG(1) << "Setting default wallpaper to '" |
| 521 << GetCustomizedWallpaperDownloadedFileName().value() << "' ('" |
| 522 << wallpaper_url.spec() << "')"; |
| 523 WallpaperManager::Get()->SetCustomizedDefaultWallpaper( |
| 524 wallpaper_url, |
| 525 GetCustomizedWallpaperDownloadedFileName(), |
| 526 GetCustomizedWallpaperCacheDir()); |
| 527 } |
| 528 |
| 529 // static |
| 530 void ServicesCustomizationDocument::DestroyWallpaperDownloader() { |
| 531 ServicesCustomizationDocument* self = |
| 532 ServicesCustomizationDocument::GetInstance(); |
| 533 DCHECK(self); |
| 534 self->wallpaper_downloader_.reset(); |
| 535 } |
| 536 |
| 537 void ServicesCustomizationDocument::ApplyWallpaper( |
| 538 bool default_wallpaper_file_exists) { |
| 539 GURL wallpaper_url = GetDefaultWallpaperUrl(); |
| 540 DCHECK(wallpaper_url.is_valid()); |
| 541 |
| 542 PrefService* prefService = g_browser_process->local_state(); |
| 543 DCHECK(prefService); |
| 544 |
| 545 std::string current_url = |
| 546 prefService->GetString(prefs::kCustomizationDefaultWallpaperURL); |
| 547 if ((current_url != wallpaper_url.spec()) || |
| 548 (!default_wallpaper_file_exists)) { |
| 549 VLOG(1) |
| 550 << "ServicesCustomizationDocument::ApplyWallpaper() : start download"; |
| 551 StartOEMWallpaperDownload(wallpaper_url); |
| 552 } else { |
| 553 VLOG(1) |
| 554 << "ServicesCustomizationDocument::ApplyWallpaper() : reuse existing"; |
| 555 SetDefaultWallpaperPath(wallpaper_url); |
| 556 } |
| 557 } |
| 558 |
| 559 void ServicesCustomizationDocument::OnCheckedWallpaperCacheExists( |
| 560 scoped_ptr<bool> exists) { |
| 561 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 562 DCHECK(exists.get()); |
| 563 |
| 564 ApplyWallpaper(*exists); |
| 565 } |
| 566 |
| 567 void ServicesCustomizationDocument::CheckAndApplyWallpaper() { |
| 568 GURL wallpaper_url = GetDefaultWallpaperUrl(); |
| 569 // Should fail if this ever happens in tests. |
| 570 DCHECK(wallpaper_url.is_valid() || wallpaper_url.is_empty()); |
| 571 if (!wallpaper_url.is_valid()) { |
| 572 if (!wallpaper_url.is_empty()) { |
| 573 LOG(WARNING) << "Invalid Customized Wallpaper URL."; |
| 574 } |
| 575 return; |
| 576 } |
| 577 scoped_ptr<bool> exists(new bool(false)); |
| 578 |
| 579 base::Closure check_file_exists = |
| 580 base::Bind(&CheckWallpaperCacheExists, |
| 581 GetCustomizedWallpaperDownloadedFileName(), |
| 582 base::Unretained(exists.get())); |
| 583 base::Closure on_checked_closure = |
| 584 base::Bind(&ServicesCustomizationDocument::OnCheckedWallpaperCacheExists, |
| 585 weak_factory_.GetWeakPtr(), |
| 586 base::Passed(exists.Pass())); |
| 587 if (!content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 588 FROM_HERE, check_file_exists, on_checked_closure)) { |
| 589 LOG(WARNING) << "Failed to start check Wallpaper cache exists."; |
| 590 } |
| 591 } |
| 592 |
460 bool ServicesCustomizationDocument::ApplyOOBECustomization() { | 593 bool ServicesCustomizationDocument::ApplyOOBECustomization() { |
461 // TODO(dpolukhin): apply default wallpaper. | 594 CheckAndApplyWallpaper(); |
462 SetApplied(true); | 595 SetApplied(true); |
463 return true; | 596 return true; |
464 } | 597 } |
465 | 598 |
466 GURL ServicesCustomizationDocument::GetDefaultWallpaperUrl() const { | 599 GURL ServicesCustomizationDocument::GetDefaultWallpaperUrl() const { |
467 if (!IsReady()) | 600 if (!IsReady()) |
468 return GURL(); | 601 return GURL(); |
469 | 602 |
470 std::string url; | 603 std::string url; |
471 root_->GetString(kDefaultWallpaperAttr, &url); | 604 root_->GetString(kDefaultWallpaperAttr, &url); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 // StartFetching will be called from ServicesCustomizationExternalLoader | 677 // StartFetching will be called from ServicesCustomizationExternalLoader |
545 // when StartLoading is called. We can't initiate manifest fetch here | 678 // when StartLoading is called. We can't initiate manifest fetch here |
546 // because caller may never call StartLoading for the provider. | 679 // because caller may never call StartLoading for the provider. |
547 } | 680 } |
548 } | 681 } |
549 | 682 |
550 return loader; | 683 return loader; |
551 } | 684 } |
552 | 685 |
553 } // namespace chromeos | 686 } // namespace chromeos |
OLD | NEW |