| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/login/apply_services_customization.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/file_path.h" | |
| 9 #include "base/file_util.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/chromeos/login/existing_user_controller.h" | |
| 17 #include "chrome/browser/prefs/pref_service.h" | |
| 18 #include "chrome/browser/profiles/profile_manager.h" | |
| 19 #include "content/browser/browser_thread.h" | |
| 20 #include "googleurl/src/gurl.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // URL where to fetch OEM services customization manifest from. | |
| 25 const char kServicesCustomizationManifestUrl[] = | |
| 26 "file:///opt/oem/etc/services_manifest.json"; | |
| 27 | |
| 28 // Name of local state option that tracks if services customization has been | |
| 29 // applied. | |
| 30 const char kServicesCustomizationAppliedPref[] = "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 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::ApplyServicesCustomization); | |
| 41 | |
| 42 namespace chromeos { | |
| 43 | |
| 44 // static | |
| 45 void ApplyServicesCustomization::StartIfNeeded() { | |
| 46 if (!IsApplied()) { | |
| 47 ApplyServicesCustomization* object = | |
| 48 new ApplyServicesCustomization(kServicesCustomizationManifestUrl); | |
| 49 if (!object->Init()) { | |
| 50 delete object; | |
| 51 } | |
| 52 // |object| will be deleted on download complete. | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 void ApplyServicesCustomization::RegisterPrefs(PrefService* local_state) { | |
| 58 local_state->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); | |
| 59 } | |
| 60 | |
| 61 // static | |
| 62 bool ApplyServicesCustomization::IsApplied() { | |
| 63 PrefService* prefs = g_browser_process->local_state(); | |
| 64 return prefs->GetBoolean(kServicesCustomizationAppliedPref); | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 void ApplyServicesCustomization::SetApplied(bool val) { | |
| 69 PrefService* prefs = g_browser_process->local_state(); | |
| 70 prefs->SetBoolean(kServicesCustomizationAppliedPref, val); | |
| 71 } | |
| 72 | |
| 73 ApplyServicesCustomization::ApplyServicesCustomization( | |
| 74 const std::string& url_str) : url_(url_str), num_retries_(0) { | |
| 75 } | |
| 76 | |
| 77 bool ApplyServicesCustomization::Init() { | |
| 78 DCHECK(url_.is_valid()); | |
| 79 if (!url_.is_valid()) { | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 if (url_.SchemeIsFile()) { | |
| 84 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 85 NewRunnableMethod(this, | |
| 86 &ApplyServicesCustomization::ReadFileInBackground, | |
| 87 FilePath(url_.path()))); | |
| 88 } else { | |
| 89 StartFileFetch(); | |
| 90 } | |
| 91 | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 void ApplyServicesCustomization::StartFileFetch() { | |
| 96 url_fetcher_.reset(new URLFetcher(url_, URLFetcher::GET, this)); | |
| 97 url_fetcher_->set_request_context( | |
| 98 ProfileManager::GetDefaultProfile()->GetRequestContext()); | |
| 99 url_fetcher_->Start(); | |
| 100 } | |
| 101 | |
| 102 void ApplyServicesCustomization::OnURLFetchComplete( | |
| 103 const URLFetcher* source, | |
| 104 const GURL& url, | |
| 105 const net::URLRequestStatus& status, | |
| 106 int response_code, | |
| 107 const ResponseCookies& cookies, | |
| 108 const std::string& data) { | |
| 109 if (response_code == 200) { | |
| 110 Apply(data); | |
| 111 } else { | |
| 112 NetworkLibrary* network = CrosLibrary::Get()->GetNetworkLibrary(); | |
| 113 if (!network->Connected() && num_retries_ < kMaxFetchRetries) { | |
| 114 num_retries_++; | |
| 115 retry_timer_.Start(base::TimeDelta::FromSeconds(kRetriesDelayInSec), | |
| 116 this, &ApplyServicesCustomization::StartFileFetch); | |
| 117 return; | |
| 118 } | |
| 119 LOG(ERROR) << "URL fetch for services customization failed:" | |
| 120 << " response code = " << response_code | |
| 121 << " URL = " << url.spec(); | |
| 122 } | |
| 123 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 124 } | |
| 125 | |
| 126 void ApplyServicesCustomization::ReadFileInBackground(const FilePath& file) { | |
| 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 128 | |
| 129 std::string manifest; | |
| 130 if (file_util::ReadFileToString(file, &manifest)) { | |
| 131 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 132 NewRunnableMethod( | |
| 133 this, &ApplyServicesCustomization::ApplyAndDelete, manifest)); | |
| 134 } else { | |
| 135 VLOG(1) << "Failed to load services customization manifest from: " | |
| 136 << file.value(); | |
| 137 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 void ApplyServicesCustomization::ApplyAndDelete(const std::string& manifest) { | |
| 142 Apply(manifest); | |
| 143 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 144 } | |
| 145 | |
| 146 void ApplyServicesCustomization::Apply(const std::string& manifest) { | |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 148 | |
| 149 chromeos::ServicesCustomizationDocument customization; | |
| 150 if (!customization.LoadManifestFromString(manifest)) { | |
| 151 LOG(ERROR) << "Failed to partner parse services customizations manifest"; | |
| 152 return; | |
| 153 } | |
| 154 | |
| 155 VLOG(1) << "Partner services customizations manifest loaded successfully"; | |
| 156 std::string locale = g_browser_process->GetApplicationLocale(); | |
| 157 std::string initial_start_page = customization.GetInitialStartPage(locale); | |
| 158 if (!initial_start_page.empty()) { | |
| 159 VLOG(1) << "initial_start_page_url: " << initial_start_page; | |
| 160 ExistingUserController* current_controller = | |
| 161 ExistingUserController::current_controller(); | |
| 162 if (current_controller) { | |
| 163 current_controller->set_initial_start_page(initial_start_page); | |
| 164 } else { | |
| 165 // Exit here to don't safe that manifest was applied. | |
| 166 return; | |
| 167 } | |
| 168 } | |
| 169 // TODO(dpolukhin): apply customized apps, exts and support page. | |
| 170 | |
| 171 SetApplied(true); | |
| 172 } | |
| 173 | |
| 174 } // namespace chromeos | |
| OLD | NEW |