OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/login/string_fetcher.h" | 5 #include "chrome/browser/chromeos/login/apply_services_customization.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
7 #include "base/file_path.h" | 8 #include "base/file_path.h" |
8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 14 #include "chrome/browser/chromeos/cros/network_library.h" |
| 15 #include "chrome/browser/chromeos/customization_document.h" |
| 16 #include "chrome/browser/pref_service.h" |
10 #include "chrome/browser/profile_manager.h" | 17 #include "chrome/browser/profile_manager.h" |
11 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
12 | 19 |
13 StringFetcher::StringFetcher(const std::string& url_str) { | 20 namespace { |
14 GURL url(url_str); | |
15 | 21 |
16 DCHECK(url.is_valid()); | 22 // URL where to fetch OEM services customization manifest from. |
17 if (!url.is_valid()) { | 23 // TODO(denisromanov): Change this to real URL when it becomes available. |
18 response_code_ = 404; | 24 const char kServicesCustomizationManifestUrl[] = |
19 return; | 25 "file:///mnt/partner_partition/etc/chromeos/services_manifest.json"; |
| 26 |
| 27 // Name of local state option that tracks if services customization has been |
| 28 // applied. |
| 29 const wchar_t kServicesCustomizationAppliedPref[] = |
| 30 L"ServicesCustomizationApplied"; |
| 31 |
| 32 // Maximum number of retries to fetch file if network is not available. |
| 33 const int kMaxFetchRetries = 3; |
| 34 |
| 35 // Delay between file fetch retries if network is not available. |
| 36 const int kRetriesDelayInSec = 2; |
| 37 |
| 38 } // namespace |
| 39 |
| 40 namespace chromeos { |
| 41 |
| 42 |
| 43 // static |
| 44 void ApplyServicesCustomization::StartIfNeeded() { |
| 45 if (!IsApplied()) { |
| 46 ApplyServicesCustomization* object = |
| 47 new ApplyServicesCustomization(kServicesCustomizationManifestUrl); |
| 48 if (!object->Init()) { |
| 49 delete object; |
| 50 } |
| 51 // |object| will be deleted on download complete. |
| 52 } |
| 53 } |
| 54 |
| 55 // static |
| 56 void ApplyServicesCustomization::RegisterPrefs(PrefService* local_state) { |
| 57 local_state->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); |
| 58 } |
| 59 |
| 60 // static |
| 61 bool ApplyServicesCustomization::IsApplied() { |
| 62 PrefService* prefs = g_browser_process->local_state(); |
| 63 return prefs->GetBoolean(kServicesCustomizationAppliedPref); |
| 64 } |
| 65 |
| 66 // static |
| 67 void ApplyServicesCustomization::SetApplied(bool val) { |
| 68 PrefService* prefs = g_browser_process->local_state(); |
| 69 prefs->SetBoolean(kServicesCustomizationAppliedPref, val); |
| 70 } |
| 71 |
| 72 ApplyServicesCustomization::ApplyServicesCustomization( |
| 73 const std::string& url_str) : url_(url_str), num_retries_(0) { |
| 74 } |
| 75 |
| 76 bool ApplyServicesCustomization::Init() { |
| 77 DCHECK(url_.is_valid()); |
| 78 if (!url_.is_valid()) { |
| 79 return false; |
20 } | 80 } |
21 | 81 |
22 if (url.SchemeIsFile()) { | 82 if (url_.SchemeIsFile()) { |
23 LOG(INFO) << url.path(); | 83 std::string manifest; |
24 if (file_util::ReadFileToString(FilePath(url.path()), &result_)) { | 84 if (file_util::ReadFileToString(FilePath(url_.path()), &manifest)) { |
25 response_code_ = 200; | 85 Apply(manifest); |
26 } else { | 86 } else { |
27 response_code_ = 404; | 87 LOG(ERROR) << "Failed to load services customization manifest from: " |
| 88 << url_.path(); |
28 } | 89 } |
29 return; | 90 |
| 91 return false; |
30 } | 92 } |
31 url_fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this)); | 93 |
| 94 StartFileFetch(); |
| 95 return true; |
| 96 } |
| 97 |
| 98 void ApplyServicesCustomization::StartFileFetch() { |
| 99 url_fetcher_.reset(new URLFetcher(url_, URLFetcher::GET, this)); |
32 url_fetcher_->set_request_context( | 100 url_fetcher_->set_request_context( |
33 ProfileManager::GetDefaultProfile()->GetRequestContext()); | 101 ProfileManager::GetDefaultProfile()->GetRequestContext()); |
34 url_fetcher_->Start(); | 102 url_fetcher_->Start(); |
35 } | 103 } |
36 | 104 |
37 void StringFetcher::OnURLFetchComplete(const URLFetcher* source, | 105 void ApplyServicesCustomization::OnURLFetchComplete( |
38 const GURL& url, | 106 const URLFetcher* source, |
39 const URLRequestStatus& status, | 107 const GURL& url, |
40 int response_code, | 108 const URLRequestStatus& status, |
41 const ResponseCookies& cookies, | 109 int response_code, |
42 const std::string& data) { | 110 const ResponseCookies& cookies, |
43 response_code_ = response_code; | 111 const std::string& data) { |
44 if (response_code != 200) { | 112 if (response_code == 200) { |
45 LOG(ERROR) << "Response code is " << response_code; | 113 Apply(data); |
46 LOG(ERROR) << "Url is " << url.spec(); | 114 } else { |
47 LOG(ERROR) << "Data is " << data; | 115 NetworkLibrary* network = CrosLibrary::Get()->GetNetworkLibrary(); |
| 116 if (!network->Connected() && num_retries_ < kMaxFetchRetries) { |
| 117 num_retries_++; |
| 118 retry_timer_.Start(base::TimeDelta::FromSeconds(kRetriesDelayInSec), |
| 119 this, &ApplyServicesCustomization::StartFileFetch); |
| 120 return; |
| 121 } |
| 122 LOG(ERROR) << "URL fetch for services customization failed:" |
| 123 << " response code = " << response_code |
| 124 << " URL = " << url.spec(); |
| 125 } |
| 126 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 127 } |
| 128 |
| 129 void ApplyServicesCustomization::Apply(const std::string& manifest) { |
| 130 chromeos::ServicesCustomizationDocument customization; |
| 131 if (!customization.LoadManifestFromString(manifest)) { |
| 132 LOG(ERROR) << "Failed to partner parse services customizations manifest"; |
48 return; | 133 return; |
49 } | 134 } |
50 result_ = data; | 135 |
| 136 LOG(INFO) << "Partner services customizations manifest loaded successfully"; |
| 137 if (!customization.initial_start_page_url().empty()) { |
| 138 // Append partner's start page url to command line so it gets opened |
| 139 // on browser startup. |
| 140 CommandLine::ForCurrentProcess()->AppendLooseValue( |
| 141 UTF8ToWide(customization.initial_start_page_url())); |
| 142 LOG(INFO) << "initial_start_page_url: " |
| 143 << customization.initial_start_page_url(); |
| 144 } |
| 145 // TODO(dpolukhin): apply customized apps, exts and support page. |
| 146 |
| 147 SetApplied(true); |
51 } | 148 } |
| 149 |
| 150 } |
OLD | NEW |