| 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/ui/webui/options2/chromeos/about_page_handler2.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 12 #include "base/command_line.h" | |
| 13 #include "base/i18n/time_formatting.h" | |
| 14 #include "base/string16.h" | |
| 15 #include "base/string_number_conversions.h" | |
| 16 #include "base/time.h" | |
| 17 #include "base/utf_string_conversions.h" | |
| 18 #include "base/values.h" | |
| 19 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" | |
| 20 #include "chrome/browser/chromeos/dbus/power_manager_client.h" | |
| 21 #include "chrome/browser/chromeos/dbus/update_engine_client.h" | |
| 22 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 23 #include "chrome/browser/chromeos/login/wizard_controller.h" | |
| 24 #include "chrome/browser/google/google_util.h" | |
| 25 #include "chrome/common/chrome_version_info.h" | |
| 26 #include "chrome/common/url_constants.h" | |
| 27 #include "content/public/common/content_client.h" | |
| 28 #include "googleurl/src/gurl.h" | |
| 29 #include "grit/chromium_strings.h" | |
| 30 #include "grit/generated_resources.h" | |
| 31 #include "grit/locale_settings.h" | |
| 32 #include "grit/theme_resources.h" | |
| 33 #include "ui/base/l10n/l10n_util.h" | |
| 34 #include "ui/base/resource/resource_bundle.h" | |
| 35 #include "v8/include/v8.h" | |
| 36 #include "webkit/glue/user_agent.h" | |
| 37 #include "webkit/glue/webkit_glue.h" | |
| 38 #include "webkit/glue/user_agent.h" | |
| 39 | |
| 40 namespace { | |
| 41 | |
| 42 // These are used as placeholder text around the links in the text in the | |
| 43 // license. | |
| 44 const char kBeginLink[] = "BEGIN_LINK"; | |
| 45 const char kEndLink[] = "END_LINK"; | |
| 46 const char kBeginLinkChr[] = "BEGIN_LINK_CHR"; | |
| 47 const char kBeginLinkOss[] = "BEGIN_LINK_OSS"; | |
| 48 const char kEndLinkChr[] = "END_LINK_CHR"; | |
| 49 const char kEndLinkOss[] = "END_LINK_OSS"; | |
| 50 const char kBeginLinkCrosOss[] = "BEGIN_LINK_CROS_OSS"; | |
| 51 const char kEndLinkCrosOss[] = "END_LINK_CROS_OSS"; | |
| 52 | |
| 53 // Returns a substring [start, end) from |text|. | |
| 54 std::string StringSubRange(const std::string& text, size_t start, | |
| 55 size_t end) { | |
| 56 DCHECK(end > start); | |
| 57 return text.substr(start, end - start); | |
| 58 } | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 namespace chromeos { | |
| 63 namespace options2 { | |
| 64 | |
| 65 class AboutPageHandler::UpdateObserver | |
| 66 : public UpdateEngineClient::Observer { | |
| 67 public: | |
| 68 explicit UpdateObserver(AboutPageHandler* handler) : page_handler_(handler) {} | |
| 69 virtual ~UpdateObserver() {} | |
| 70 | |
| 71 AboutPageHandler* page_handler() const { return page_handler_; } | |
| 72 | |
| 73 private: | |
| 74 virtual void UpdateStatusChanged( | |
| 75 const UpdateEngineClient::Status& status) OVERRIDE { | |
| 76 page_handler_->UpdateStatus(status); | |
| 77 } | |
| 78 | |
| 79 AboutPageHandler* page_handler_; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(UpdateObserver); | |
| 82 }; | |
| 83 | |
| 84 AboutPageHandler::AboutPageHandler() | |
| 85 : progress_(-1), | |
| 86 sticky_(false), | |
| 87 started_(false) | |
| 88 {} | |
| 89 | |
| 90 AboutPageHandler::~AboutPageHandler() { | |
| 91 if (update_observer_.get()) { | |
| 92 DBusThreadManager::Get()->GetUpdateEngineClient()-> | |
| 93 RemoveObserver(update_observer_.get()); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void AboutPageHandler::GetLocalizedValues(DictionaryValue* localized_strings) { | |
| 98 DCHECK(localized_strings); | |
| 99 | |
| 100 static OptionsStringResource resources[] = { | |
| 101 { "firmware", IDS_ABOUT_PAGE_FIRMWARE }, | |
| 102 { "product", IDS_PRODUCT_OS_NAME }, | |
| 103 { "os", IDS_PRODUCT_OS_NAME }, | |
| 104 { "platform", IDS_PLATFORM_LABEL }, | |
| 105 { "loading", IDS_ABOUT_PAGE_LOADING }, | |
| 106 { "check_now", IDS_ABOUT_PAGE_CHECK_NOW }, | |
| 107 { "update_status", IDS_UPGRADE_CHECK_STARTED }, | |
| 108 { "restart_now", IDS_RELAUNCH_AND_UPDATE }, | |
| 109 { "browser", IDS_PRODUCT_NAME }, | |
| 110 { "more_info", IDS_ABOUT_PAGE_MORE_INFO }, | |
| 111 { "copyright", IDS_ABOUT_VERSION_COPYRIGHT }, | |
| 112 { "channel", IDS_ABOUT_PAGE_CHANNEL }, | |
| 113 { "stable", IDS_ABOUT_PAGE_CHANNEL_STABLE }, | |
| 114 { "beta", IDS_ABOUT_PAGE_CHANNEL_BETA }, | |
| 115 { "dev", IDS_ABOUT_PAGE_CHANNEL_DEVELOPMENT }, | |
| 116 { "canary", IDS_ABOUT_PAGE_CHANNEL_CANARY }, | |
| 117 { "channel_warning_header", IDS_ABOUT_PAGE_CHANNEL_WARNING_HEADER }, | |
| 118 { "channel_warning_text", IDS_ABOUT_PAGE_CHANNEL_WARNING_TEXT }, | |
| 119 { "user_agent", IDS_ABOUT_VERSION_USER_AGENT }, | |
| 120 { "command_line", IDS_ABOUT_VERSION_COMMAND_LINE }, | |
| 121 }; | |
| 122 | |
| 123 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 124 RegisterTitle(localized_strings, "aboutPage", IDS_ABOUT_TAB_TITLE); | |
| 125 | |
| 126 // browser version | |
| 127 | |
| 128 chrome::VersionInfo version_info; | |
| 129 DCHECK(version_info.is_valid()); | |
| 130 | |
| 131 std::string browser_version = version_info.Version(); | |
| 132 std::string version_modifier = | |
| 133 chrome::VersionInfo::GetVersionStringModifier(); | |
| 134 if (!version_modifier.empty()) | |
| 135 browser_version += " " + version_modifier; | |
| 136 | |
| 137 #if !defined(GOOGLE_CHROME_BUILD) | |
| 138 browser_version += " ("; | |
| 139 browser_version += version_info.LastChange(); | |
| 140 browser_version += ")"; | |
| 141 #endif | |
| 142 | |
| 143 localized_strings->SetString("browser_version", browser_version); | |
| 144 | |
| 145 // license | |
| 146 | |
| 147 std::string text = l10n_util::GetStringUTF8(IDS_ABOUT_VERSION_LICENSE); | |
| 148 | |
| 149 bool chromium_url_appears_first = | |
| 150 text.find(kBeginLinkChr) < text.find(kBeginLinkOss); | |
| 151 | |
| 152 size_t link1 = text.find(kBeginLink); | |
| 153 DCHECK(link1 != std::string::npos); | |
| 154 size_t link1_end = text.find(kEndLink, link1); | |
| 155 DCHECK(link1_end != std::string::npos); | |
| 156 size_t link2 = text.find(kBeginLink, link1_end); | |
| 157 DCHECK(link2 != std::string::npos); | |
| 158 size_t link2_end = text.find(kEndLink, link2); | |
| 159 DCHECK(link2_end != std::string::npos); | |
| 160 | |
| 161 localized_strings->SetString("license_content_0", text.substr(0, link1)); | |
| 162 localized_strings->SetString("license_content_1", | |
| 163 StringSubRange(text, link1_end + strlen(kEndLinkOss), link2)); | |
| 164 localized_strings->SetString("license_content_2", | |
| 165 text.substr(link2_end + strlen(kEndLinkOss))); | |
| 166 | |
| 167 // The Chromium link within the main text of the dialog. | |
| 168 localized_strings->SetString(chromium_url_appears_first ? | |
| 169 "license_link_content_0" : "license_link_content_1", | |
| 170 StringSubRange(text, | |
| 171 text.find(kBeginLinkChr) + strlen(kBeginLinkChr), | |
| 172 text.find(kEndLinkChr))); | |
| 173 GURL url = google_util::AppendGoogleLocaleParam( | |
| 174 GURL(chrome::kChromiumProjectURL)); | |
| 175 localized_strings->SetString(chromium_url_appears_first ? | |
| 176 "license_link_0" : "license_link_1", url.spec()); | |
| 177 | |
| 178 // The Open Source link within the main text of the dialog. | |
| 179 localized_strings->SetString(chromium_url_appears_first ? | |
| 180 "license_link_content_1" : "license_link_content_0", | |
| 181 StringSubRange(text, | |
| 182 text.find(kBeginLinkOss) + strlen(kBeginLinkOss), | |
| 183 text.find(kEndLinkOss))); | |
| 184 localized_strings->SetString(chromium_url_appears_first ? | |
| 185 "license_link_1" : "license_link_0", chrome::kChromeUICreditsURL); | |
| 186 | |
| 187 std::string cros_text = | |
| 188 l10n_util::GetStringUTF8(IDS_ABOUT_CROS_VERSION_LICENSE); | |
| 189 | |
| 190 size_t cros_link = cros_text.find(kBeginLinkCrosOss); | |
| 191 DCHECK(cros_link != std::string::npos); | |
| 192 size_t cros_link_end = cros_text.find(kEndLinkCrosOss, cros_link); | |
| 193 DCHECK(cros_link_end != std::string::npos); | |
| 194 | |
| 195 localized_strings->SetString("cros_license_content_0", | |
| 196 cros_text.substr(0, cros_link)); | |
| 197 localized_strings->SetString("cros_license_content_1", | |
| 198 cros_text.substr(cros_link_end + strlen(kEndLinkCrosOss))); | |
| 199 localized_strings->SetString("cros_license_link_content_0", | |
| 200 StringSubRange(cros_text, cros_link + strlen(kBeginLinkCrosOss), | |
| 201 cros_link_end)); | |
| 202 localized_strings->SetString("cros_license_link_0", | |
| 203 chrome::kChromeUIOSCreditsURL); | |
| 204 | |
| 205 // webkit | |
| 206 | |
| 207 localized_strings->SetString("webkit_version", | |
| 208 webkit_glue::GetWebKitVersion()); | |
| 209 | |
| 210 // javascript | |
| 211 | |
| 212 localized_strings->SetString("js_engine", "V8"); | |
| 213 localized_strings->SetString("js_engine_version", v8::V8::GetVersion()); | |
| 214 | |
| 215 // user agent | |
| 216 | |
| 217 localized_strings->SetString("user_agent_info", | |
| 218 content::GetUserAgent(GURL())); | |
| 219 | |
| 220 // command line | |
| 221 | |
| 222 #if defined(OS_WIN) | |
| 223 localized_strings->SetString("command_line_info", | |
| 224 WideToUTF16(CommandLine::ForCurrentProcess()->GetCommandLineString())); | |
| 225 #elif defined(OS_POSIX) | |
| 226 // TODO(viettrungluu): something horrible might happen if there are non-UTF-8 | |
| 227 // arguments (since |SetString()| requires Unicode). | |
| 228 std::string command_line = ""; | |
| 229 typedef std::vector<std::string> ArgvList; | |
| 230 const ArgvList& argv = CommandLine::ForCurrentProcess()->argv(); | |
| 231 for (ArgvList::const_iterator iter = argv.begin(); iter != argv.end(); iter++) | |
| 232 command_line += " " + *iter; | |
| 233 localized_strings->SetString("command_line_info", command_line); | |
| 234 #endif | |
| 235 } | |
| 236 | |
| 237 void AboutPageHandler::RegisterMessages() { | |
| 238 web_ui()->RegisterMessageCallback("PageReady", | |
| 239 base::Bind(&AboutPageHandler::PageReady, base::Unretained(this))); | |
| 240 web_ui()->RegisterMessageCallback("SetReleaseTrack", | |
| 241 base::Bind(&AboutPageHandler::SetReleaseTrack, base::Unretained(this))); | |
| 242 | |
| 243 web_ui()->RegisterMessageCallback("CheckNow", | |
| 244 base::Bind(&AboutPageHandler::CheckNow, base::Unretained(this))); | |
| 245 web_ui()->RegisterMessageCallback("RestartNow", | |
| 246 base::Bind(&AboutPageHandler::RestartNow, base::Unretained(this))); | |
| 247 } | |
| 248 | |
| 249 void AboutPageHandler::PageReady(const ListValue* args) { | |
| 250 // Version information is loaded from a callback | |
| 251 loader_.GetVersion(&consumer_, | |
| 252 base::Bind(&AboutPageHandler::OnOSVersion, | |
| 253 base::Unretained(this)), | |
| 254 VersionLoader::VERSION_FULL); | |
| 255 loader_.GetFirmware(&consumer_, | |
| 256 base::Bind(&AboutPageHandler::OnOSFirmware, | |
| 257 base::Unretained(this))); | |
| 258 | |
| 259 UpdateEngineClient* update_engine_client = | |
| 260 DBusThreadManager::Get()->GetUpdateEngineClient(); | |
| 261 | |
| 262 update_observer_.reset(new UpdateObserver(this)); | |
| 263 update_engine_client->AddObserver(update_observer_.get()); | |
| 264 | |
| 265 // Update the WebUI page with the current status. See comments below. | |
| 266 UpdateStatus(update_engine_client->GetLastStatus()); | |
| 267 | |
| 268 // Initiate update check. UpdateStatus() below will be called when we | |
| 269 // get update status via update_observer_. If the update has been | |
| 270 // already complete, update_observer_ won't receive a notification. | |
| 271 // This is why we manually update the WebUI page above. | |
| 272 CheckNow(NULL); | |
| 273 | |
| 274 // Request the channel information. Use the observer to track the about | |
| 275 // page handler and ensure it does not get deleted before the callback. | |
| 276 update_engine_client->GetReleaseTrack( | |
| 277 base::Bind(UpdateSelectedChannel, update_observer_.get())); | |
| 278 } | |
| 279 | |
| 280 void AboutPageHandler::SetReleaseTrack(const ListValue* args) { | |
| 281 if (!UserManager::Get()->current_user_is_owner()) { | |
| 282 LOG(WARNING) << "Non-owner tried to change release track."; | |
| 283 return; | |
| 284 } | |
| 285 const std::string channel = UTF16ToUTF8(ExtractStringValue(args)); | |
| 286 DBusThreadManager::Get()->GetUpdateEngineClient()->SetReleaseTrack(channel); | |
| 287 } | |
| 288 | |
| 289 void AboutPageHandler::CheckNow(const ListValue* args) { | |
| 290 // Make sure that libcros is loaded and OOBE is complete. | |
| 291 if (!WizardController::default_controller() || | |
| 292 WizardController::IsDeviceRegistered()) { | |
| 293 DBusThreadManager::Get()->GetUpdateEngineClient()-> | |
| 294 RequestUpdateCheck(UpdateEngineClient::EmptyUpdateCheckCallback()); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 void AboutPageHandler::RestartNow(const ListValue* args) { | |
| 299 DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart(); | |
| 300 } | |
| 301 | |
| 302 void AboutPageHandler::UpdateStatus( | |
| 303 const UpdateEngineClient::Status& status) { | |
| 304 string16 message; | |
| 305 std::string image = "up-to-date"; | |
| 306 bool enabled = false; | |
| 307 | |
| 308 switch (status.status) { | |
| 309 case UpdateEngineClient::UPDATE_STATUS_IDLE: | |
| 310 if (!sticky_) { | |
| 311 message = l10n_util::GetStringFUTF16(IDS_UPGRADE_ALREADY_UP_TO_DATE, | |
| 312 l10n_util::GetStringUTF16(IDS_PRODUCT_OS_NAME)); | |
| 313 enabled = true; | |
| 314 } | |
| 315 break; | |
| 316 case UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE: | |
| 317 message = l10n_util::GetStringUTF16(IDS_UPGRADE_CHECK_STARTED); | |
| 318 sticky_ = false; | |
| 319 break; | |
| 320 case UpdateEngineClient::UPDATE_STATUS_UPDATE_AVAILABLE: | |
| 321 message = l10n_util::GetStringUTF16(IDS_UPDATE_AVAILABLE); | |
| 322 started_ = true; | |
| 323 break; | |
| 324 case UpdateEngineClient::UPDATE_STATUS_DOWNLOADING: | |
| 325 { | |
| 326 int progress = static_cast<int>(status.download_progress * 100.0); | |
| 327 if (progress != progress_) { | |
| 328 progress_ = progress; | |
| 329 message = l10n_util::GetStringFUTF16Int(IDS_UPDATE_DOWNLOADING, | |
| 330 progress_); | |
| 331 } | |
| 332 started_ = true; | |
| 333 } | |
| 334 break; | |
| 335 case UpdateEngineClient::UPDATE_STATUS_VERIFYING: | |
| 336 message = l10n_util::GetStringUTF16(IDS_UPDATE_VERIFYING); | |
| 337 started_ = true; | |
| 338 break; | |
| 339 case UpdateEngineClient::UPDATE_STATUS_FINALIZING: | |
| 340 message = l10n_util::GetStringUTF16(IDS_UPDATE_FINALIZING); | |
| 341 started_ = true; | |
| 342 break; | |
| 343 case UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT: | |
| 344 message = l10n_util::GetStringUTF16(IDS_UPDATE_COMPLETED); | |
| 345 image = "available"; | |
| 346 sticky_ = true; | |
| 347 break; | |
| 348 default: | |
| 349 // case UpdateEngineClient::UPDATE_STATUS_ERROR: | |
| 350 // case UpdateEngineClient::UPDATE_STATUS_REPORTING_ERROR_EVENT: | |
| 351 | |
| 352 // The error is only displayed if we were able to determine an | |
| 353 // update was available. | |
| 354 if (started_) { | |
| 355 message = l10n_util::GetStringUTF16(IDS_UPDATE_ERROR); | |
| 356 image = "fail"; | |
| 357 enabled = true; | |
| 358 sticky_ = true; | |
| 359 started_ = false; | |
| 360 } | |
| 361 break; | |
| 362 } | |
| 363 if (message.size()) { | |
| 364 scoped_ptr<Value> update_message(Value::CreateStringValue(message)); | |
| 365 // "Checking for update..." needs to be shown for a while, so users | |
| 366 // can read it, hence insert delay for this. | |
| 367 scoped_ptr<Value> insert_delay(Value::CreateBooleanValue( | |
| 368 status.status == | |
| 369 UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE)); | |
| 370 web_ui()->CallJavascriptFunction("AboutPage.updateStatusCallback", | |
| 371 *update_message, *insert_delay); | |
| 372 | |
| 373 scoped_ptr<Value> enabled_value(Value::CreateBooleanValue(enabled)); | |
| 374 web_ui()->CallJavascriptFunction("AboutPage.updateEnableCallback", | |
| 375 *enabled_value); | |
| 376 | |
| 377 scoped_ptr<Value> image_string(Value::CreateStringValue(image)); | |
| 378 web_ui()->CallJavascriptFunction("AboutPage.setUpdateImage", | |
| 379 *image_string); | |
| 380 } | |
| 381 // We'll change the "Check For Update" button to "Restart" button. | |
| 382 if (status.status == UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT) { | |
| 383 web_ui()->CallJavascriptFunction("AboutPage.changeToRestartButton"); | |
| 384 } | |
| 385 } | |
| 386 | |
| 387 void AboutPageHandler::OnOSVersion(VersionLoader::Handle handle, | |
| 388 std::string version) { | |
| 389 if (version.size()) { | |
| 390 scoped_ptr<Value> version_string(Value::CreateStringValue(version)); | |
| 391 web_ui()->CallJavascriptFunction("AboutPage.updateOSVersionCallback", | |
| 392 *version_string); | |
| 393 } | |
| 394 } | |
| 395 | |
| 396 void AboutPageHandler::OnOSFirmware(VersionLoader::Handle handle, | |
| 397 std::string firmware) { | |
| 398 if (firmware.size()) { | |
| 399 scoped_ptr<Value> firmware_string(Value::CreateStringValue(firmware)); | |
| 400 web_ui()->CallJavascriptFunction("AboutPage.updateOSFirmwareCallback", | |
| 401 *firmware_string); | |
| 402 } | |
| 403 } | |
| 404 | |
| 405 // Callback from UpdateEngine with channel information. | |
| 406 // static | |
| 407 void AboutPageHandler::UpdateSelectedChannel(UpdateObserver* observer, | |
| 408 const std::string& channel) { | |
| 409 if (DBusThreadManager::Get()->GetUpdateEngineClient() | |
| 410 ->HasObserver(observer)) { | |
| 411 // If UpdateEngineClient still has the observer, then the page handler | |
| 412 // is valid. | |
| 413 AboutPageHandler* handler = observer->page_handler(); | |
| 414 scoped_ptr<Value> channel_string(Value::CreateStringValue(channel)); | |
| 415 handler->web_ui()->CallJavascriptFunction( | |
| 416 "AboutPage.updateSelectedOptionCallback", *channel_string); | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 } // namespace options2 | |
| 421 } // namespace chromeos | |
| OLD | NEW |