Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / help / help_handler.cc
blob6acf4ff913d9000c986389e2f638906fca2aea2e
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/help/help_handler.h"
7 #include <string>
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/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/location.h"
16 #include "base/strings/string16.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/task_runner_util.h"
21 #include "base/values.h"
22 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/chrome_notification_types.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_commands.h"
26 #include "chrome/browser/ui/browser_finder.h"
27 #include "chrome/browser/ui/chrome_pages.h"
28 #include "chrome/common/channel_info.h"
29 #include "chrome/common/chrome_content_client.h"
30 #include "chrome/common/pref_names.h"
31 #include "chrome/common/url_constants.h"
32 #include "chrome/grit/chromium_strings.h"
33 #include "chrome/grit/generated_resources.h"
34 #include "components/google/core/browser/google_util.h"
35 #include "components/version_info/version_info.h"
36 #include "content/public/browser/browser_thread.h"
37 #include "content/public/browser/notification_service.h"
38 #include "content/public/browser/web_contents.h"
39 #include "content/public/browser/web_ui.h"
40 #include "content/public/common/user_agent.h"
41 #include "grit/components_strings.h"
42 #include "ui/base/l10n/l10n_util.h"
43 #include "v8/include/v8.h"
45 #if defined(OS_MACOSX)
46 #include "chrome/browser/mac/obsolete_system.h"
47 #endif
49 #if defined(OS_CHROMEOS)
50 #include "base/files/file_util_proxy.h"
51 #include "base/i18n/time_formatting.h"
52 #include "base/prefs/pref_service.h"
53 #include "base/sys_info.h"
54 #include "base/task_runner_util.h"
55 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
56 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
57 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
58 #include "chrome/browser/chromeos/profiles/profile_helper.h"
59 #include "chrome/browser/chromeos/settings/cros_settings.h"
60 #include "chrome/browser/profiles/profile.h"
61 #include "chrome/browser/ui/webui/chromeos/image_source.h"
62 #include "chrome/browser/ui/webui/help/help_utils_chromeos.h"
63 #include "chrome/browser/ui/webui/help/version_updater_chromeos.h"
64 #include "chromeos/chromeos_switches.h"
65 #include "chromeos/dbus/dbus_thread_manager.h"
66 #include "chromeos/dbus/power_manager_client.h"
67 #include "chromeos/system/statistics_provider.h"
68 #include "components/user_manager/user_manager.h"
69 #endif
71 using base::ListValue;
72 using content::BrowserThread;
74 namespace {
76 #if defined(OS_CHROMEOS)
78 // Directory containing the regulatory labels for supported regions.
79 const char kRegulatoryLabelsDirectory[] = "regulatory_labels";
81 // File names of the image file and the file containing alt text for the label.
82 const char kRegulatoryLabelImageFilename[] = "label.png";
83 const char kRegulatoryLabelTextFilename[] = "label.txt";
85 // Default region code to use if there's no label for the VPD region code.
86 const char kDefaultRegionCode[] = "us";
88 struct RegulatoryLabel {
89 const std::string label_text;
90 const std::string image_url;
93 // Returns message that informs user that for update it's better to
94 // connect to a network of one of the allowed types.
95 base::string16 GetAllowedConnectionTypesMessage() {
96 if (help_utils_chromeos::IsUpdateOverCellularAllowed()) {
97 return l10n_util::GetStringUTF16(IDS_UPGRADE_NETWORK_LIST_CELLULAR_ALLOWED);
98 } else {
99 return l10n_util::GetStringUTF16(
100 IDS_UPGRADE_NETWORK_LIST_CELLULAR_DISALLOWED);
104 // Returns true if the device is enterprise managed, false otherwise.
105 bool IsEnterpriseManaged() {
106 policy::BrowserPolicyConnectorChromeOS* connector =
107 g_browser_process->platform_part()->browser_policy_connector_chromeos();
108 return connector->IsEnterpriseManaged();
111 // Returns true if current user can change channel, false otherwise.
112 bool CanChangeChannel(Profile* profile) {
113 bool value = false;
114 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kReleaseChannelDelegated,
115 &value);
117 // On a managed machine we delegate this setting to the users of the same
118 // domain only if the policy value is "domain".
119 if (IsEnterpriseManaged()) {
120 if (!value)
121 return false;
122 // Get the currently logged in user and strip the domain part only.
123 std::string domain = "";
124 const user_manager::User* user =
125 profile ? chromeos::ProfileHelper::Get()->GetUserByProfile(profile)
126 : nullptr;
127 std::string email = user ? user->email() : std::string();
128 size_t at_pos = email.find('@');
129 if (at_pos != std::string::npos && at_pos + 1 < email.length())
130 domain = email.substr(email.find('@') + 1);
131 policy::BrowserPolicyConnectorChromeOS* connector =
132 g_browser_process->platform_part()->browser_policy_connector_chromeos();
133 return domain == connector->GetEnterpriseDomain();
134 } else {
135 chromeos::OwnerSettingsServiceChromeOS* service =
136 chromeos::OwnerSettingsServiceChromeOSFactory::GetInstance()
137 ->GetForBrowserContext(profile);
138 // On non managed machines we have local owner who is the only one to change
139 // anything. Ensure that ReleaseChannelDelegated is false.
140 if (service && service->IsOwner())
141 return !value;
143 return false;
146 // Returns the path of the regulatory labels directory for a given region, if
147 // found. Must be called from the blocking pool.
148 base::FilePath GetRegulatoryLabelDirForRegion(const std::string& region) {
149 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
151 // Generate the path under the asset dir or URL host to the regulatory files
152 // for the region, e.g., "regulatory_labels/us/".
153 const base::FilePath region_path =
154 base::FilePath(kRegulatoryLabelsDirectory).AppendASCII(region);
156 // Check for file existence starting in /usr/share/chromeos-assets/, e.g.,
157 // "/usr/share/chromeos-assets/regulatory_labels/us/label.png".
158 const base::FilePath asset_dir(chrome::kChromeOSAssetPath);
159 if (base::PathExists(asset_dir.Append(region_path)
160 .AppendASCII(kRegulatoryLabelImageFilename))) {
161 return region_path;
164 return base::FilePath();
167 // Finds the directory for the regulatory label, using the VPD region code.
168 // Also tries "us" as a fallback region. Must be called from the blocking pool.
169 base::FilePath FindRegulatoryLabelDir() {
170 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
172 std::string region;
173 base::FilePath region_path;
174 // Use the VPD region code to find the label dir.
175 if (chromeos::system::StatisticsProvider::GetInstance()->GetMachineStatistic(
176 "region", &region) && !region.empty()) {
177 region_path = GetRegulatoryLabelDirForRegion(region);
180 // Try the fallback region code if no directory was found.
181 if (region_path.empty() && region != kDefaultRegionCode)
182 region_path = GetRegulatoryLabelDirForRegion(kDefaultRegionCode);
184 return region_path;
187 // Reads the file containing the regulatory label text, if found, relative to
188 // the asset directory. Must be called from the blocking pool.
189 std::string ReadRegulatoryLabelText(const base::FilePath& path) {
190 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
191 base::FilePath text_path(chrome::kChromeOSAssetPath);
192 text_path = text_path.Append(path);
193 text_path = text_path.AppendASCII(kRegulatoryLabelTextFilename);
195 std::string contents;
196 if (base::ReadFileToString(text_path, &contents))
197 return contents;
198 return std::string();
201 #endif // defined(OS_CHROMEOS)
203 } // namespace
205 HelpHandler::HelpHandler()
206 : weak_factory_(this) {
209 HelpHandler::~HelpHandler() {
212 void HelpHandler::GetLocalizedValues(base::DictionaryValue* localized_strings) {
213 struct L10nResources {
214 const char* name;
215 int ids;
218 static L10nResources resources[] = {
219 { "aboutTitle", IDS_ABOUT_TITLE },
220 #if defined(OS_CHROMEOS)
221 { "aboutProductTitle", IDS_PRODUCT_OS_NAME },
222 #else
223 { "aboutProductTitle", IDS_PRODUCT_NAME },
224 #endif
225 { "aboutProductDescription", IDS_ABOUT_PRODUCT_DESCRIPTION },
226 { "relaunch", IDS_RELAUNCH_BUTTON },
227 #if defined(OS_CHROMEOS)
228 { "relaunchAndPowerwash", IDS_RELAUNCH_AND_POWERWASH_BUTTON },
229 #endif
230 { "productName", IDS_PRODUCT_NAME },
231 { "updateCheckStarted", IDS_UPGRADE_CHECK_STARTED },
232 { "upToDate", IDS_UPGRADE_UP_TO_DATE },
233 { "updating", IDS_UPGRADE_UPDATING },
234 #if defined(OS_CHROMEOS)
235 { "updateButton", IDS_UPGRADE_BUTTON },
236 { "updatingChannelSwitch", IDS_UPGRADE_UPDATING_CHANNEL_SWITCH },
237 #endif
238 { "updateAlmostDone", IDS_UPGRADE_SUCCESSFUL_RELAUNCH },
239 #if defined(OS_CHROMEOS)
240 { "successfulChannelSwitch", IDS_UPGRADE_SUCCESSFUL_CHANNEL_SWITCH },
241 #endif
242 { "getHelpWithChrome", IDS_GET_HELP_USING_CHROME },
243 { "reportAnIssue", IDS_REPORT_AN_ISSUE },
244 #if defined(OS_CHROMEOS)
245 { "platform", IDS_PLATFORM_LABEL },
246 { "firmware", IDS_ABOUT_PAGE_FIRMWARE },
247 { "showMoreInfo", IDS_SHOW_MORE_INFO },
248 { "hideMoreInfo", IDS_HIDE_MORE_INFO },
249 { "channel", IDS_ABOUT_PAGE_CHANNEL },
250 { "stable", IDS_ABOUT_PAGE_CHANNEL_STABLE },
251 { "beta", IDS_ABOUT_PAGE_CHANNEL_BETA },
252 { "dev", IDS_ABOUT_PAGE_CHANNEL_DEVELOPMENT },
253 { "channel-changed", IDS_ABOUT_PAGE_CHANNEL_CHANGED },
254 { "currentChannelStable", IDS_ABOUT_PAGE_CURRENT_CHANNEL_STABLE },
255 { "currentChannelBeta", IDS_ABOUT_PAGE_CURRENT_CHANNEL_BETA },
256 { "currentChannelDev", IDS_ABOUT_PAGE_CURRENT_CHANNEL_DEV },
257 { "currentChannel", IDS_ABOUT_PAGE_CURRENT_CHANNEL },
258 { "channelChangeButton", IDS_ABOUT_PAGE_CHANNEL_CHANGE_BUTTON },
259 { "channelChangeDisallowedMessage",
260 IDS_ABOUT_PAGE_CHANNEL_CHANGE_DISALLOWED_MESSAGE },
261 { "channelChangePageTitle", IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_TITLE },
262 { "channelChangePagePowerwashTitle",
263 IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_POWERWASH_TITLE },
264 { "channelChangePagePowerwashMessage",
265 IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_POWERWASH_MESSAGE },
266 { "channelChangePageDelayedChangeTitle",
267 IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_DELAYED_CHANGE_TITLE },
268 { "channelChangePageUnstableTitle",
269 IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_UNSTABLE_TITLE },
270 { "channelChangePagePowerwashButton",
271 IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_POWERWASH_BUTTON },
272 { "channelChangePageChangeButton",
273 IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_CHANGE_BUTTON },
274 { "channelChangePageCancelButton",
275 IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_CANCEL_BUTTON },
276 { "webkit", IDS_WEBKIT },
277 { "userAgent", IDS_ABOUT_VERSION_USER_AGENT },
278 { "commandLine", IDS_ABOUT_VERSION_COMMAND_LINE },
279 { "buildDate", IDS_ABOUT_VERSION_BUILD_DATE },
280 #endif
281 #if defined(OS_MACOSX)
282 { "promote", IDS_ABOUT_CHROME_PROMOTE_UPDATER },
283 { "learnMore", IDS_LEARN_MORE },
284 #endif
287 for (size_t i = 0; i < arraysize(resources); ++i) {
288 localized_strings->SetString(resources[i].name,
289 l10n_util::GetStringUTF16(resources[i].ids));
292 #if defined(OS_MACOSX)
293 localized_strings->SetString(
294 "updateObsoleteSystem",
295 ObsoleteSystemMac::LocalizedObsoleteSystemString());
296 localized_strings->SetString(
297 "updateObsoleteSystemURL",
298 chrome::kMac32BitDeprecationURL);
299 #endif
301 localized_strings->SetString(
302 "browserVersion",
303 l10n_util::GetStringFUTF16(IDS_ABOUT_PRODUCT_VERSION,
304 BuildBrowserVersionString()));
306 base::Time::Exploded exploded_time;
307 base::Time::Now().LocalExplode(&exploded_time);
308 localized_strings->SetString(
309 "productCopyright",
310 l10n_util::GetStringFUTF16(IDS_ABOUT_VERSION_COPYRIGHT,
311 base::IntToString16(exploded_time.year)));
313 base::string16 license = l10n_util::GetStringFUTF16(
314 IDS_ABOUT_VERSION_LICENSE,
315 base::ASCIIToUTF16(chrome::kChromiumProjectURL),
316 base::ASCIIToUTF16(chrome::kChromeUICreditsURL));
317 localized_strings->SetString("productLicense", license);
319 #if defined(OS_CHROMEOS)
320 base::string16 os_license = l10n_util::GetStringFUTF16(
321 IDS_ABOUT_CROS_VERSION_LICENSE,
322 base::ASCIIToUTF16(chrome::kChromeUIOSCreditsURL));
323 localized_strings->SetString("productOsLicense", os_license);
325 base::string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_OS_NAME);
326 localized_strings->SetString(
327 "channelChangePageDelayedChangeMessage",
328 l10n_util::GetStringFUTF16(
329 IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_DELAYED_CHANGE_MESSAGE,
330 product_name));
331 localized_strings->SetString(
332 "channelChangePageUnstableMessage",
333 l10n_util::GetStringFUTF16(
334 IDS_ABOUT_PAGE_CHANNEL_CHANGE_PAGE_UNSTABLE_MESSAGE,
335 product_name));
337 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
338 chromeos::switches::kDisableNewChannelSwitcherUI)) {
339 localized_strings->SetBoolean("disableNewChannelSwitcherUI", true);
341 #endif
343 base::string16 tos = l10n_util::GetStringFUTF16(
344 IDS_ABOUT_TERMS_OF_SERVICE, base::UTF8ToUTF16(chrome::kChromeUITermsURL));
345 localized_strings->SetString("productTOS", tos);
347 localized_strings->SetString("webkitVersion", content::GetWebKitVersion());
349 localized_strings->SetString("jsEngine", "V8");
350 localized_strings->SetString("jsEngineVersion", v8::V8::GetVersion());
352 localized_strings->SetString("userAgentInfo", GetUserAgent());
354 base::CommandLine::StringType command_line =
355 base::CommandLine::ForCurrentProcess()->GetCommandLineString();
356 localized_strings->SetString("commandLineInfo", command_line);
359 void HelpHandler::RegisterMessages() {
360 version_updater_.reset(VersionUpdater::Create(web_ui()->GetWebContents()));
361 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
362 content::NotificationService::AllSources());
364 web_ui()->RegisterMessageCallback("onPageLoaded",
365 base::Bind(&HelpHandler::OnPageLoaded, base::Unretained(this)));
366 web_ui()->RegisterMessageCallback("relaunchNow",
367 base::Bind(&HelpHandler::RelaunchNow, base::Unretained(this)));
368 web_ui()->RegisterMessageCallback("openFeedbackDialog",
369 base::Bind(&HelpHandler::OpenFeedbackDialog, base::Unretained(this)));
370 web_ui()->RegisterMessageCallback("openHelpPage",
371 base::Bind(&HelpHandler::OpenHelpPage, base::Unretained(this)));
372 #if defined(OS_CHROMEOS)
373 web_ui()->RegisterMessageCallback("setChannel",
374 base::Bind(&HelpHandler::SetChannel, base::Unretained(this)));
375 web_ui()->RegisterMessageCallback("relaunchAndPowerwash",
376 base::Bind(&HelpHandler::RelaunchAndPowerwash, base::Unretained(this)));
377 web_ui()->RegisterMessageCallback("requestUpdate",
378 base::Bind(&HelpHandler::RequestUpdate, base::Unretained(this)));
379 #endif
380 #if defined(OS_MACOSX)
381 web_ui()->RegisterMessageCallback("promoteUpdater",
382 base::Bind(&HelpHandler::PromoteUpdater, base::Unretained(this)));
383 #endif
385 #if defined(OS_CHROMEOS)
386 // Handler for the product label image, which will be shown if available.
387 content::URLDataSource::Add(Profile::FromWebUI(web_ui()),
388 new chromeos::ImageSource());
389 #endif
392 void HelpHandler::Observe(int type, const content::NotificationSource& source,
393 const content::NotificationDetails& details) {
394 switch (type) {
395 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
396 // A version update is installed and ready to go. Refresh the UI so the
397 // correct state will be shown.
398 RequestUpdate(NULL);
399 break;
401 default:
402 NOTREACHED();
406 // static
407 base::string16 HelpHandler::BuildBrowserVersionString() {
408 std::string version = version_info::GetVersionNumber();
410 std::string modifier = chrome::GetChannelString();
411 if (!modifier.empty())
412 version += " " + modifier;
414 #if defined(ARCH_CPU_64_BITS)
415 version += " (64-bit)";
416 #endif
418 return base::UTF8ToUTF16(version);
421 void HelpHandler::OnPageLoaded(const base::ListValue* args) {
422 #if defined(OS_CHROMEOS)
423 base::PostTaskAndReplyWithResult(
424 content::BrowserThread::GetBlockingPool(),
425 FROM_HERE,
426 base::Bind(&chromeos::version_loader::GetVersion,
427 chromeos::version_loader::VERSION_FULL),
428 base::Bind(&HelpHandler::OnOSVersion,
429 weak_factory_.GetWeakPtr()));
430 base::PostTaskAndReplyWithResult(
431 content::BrowserThread::GetBlockingPool(),
432 FROM_HERE,
433 base::Bind(&chromeos::version_loader::GetFirmware),
434 base::Bind(&HelpHandler::OnOSFirmware,
435 weak_factory_.GetWeakPtr()));
437 web_ui()->CallJavascriptFunction(
438 "help.HelpPage.updateEnableReleaseChannel",
439 base::FundamentalValue(CanChangeChannel(Profile::FromWebUI(web_ui()))));
441 base::Time build_time = base::SysInfo::GetLsbReleaseTime();
442 base::string16 build_date = base::TimeFormatFriendlyDate(build_time);
443 web_ui()->CallJavascriptFunction("help.HelpPage.setBuildDate",
444 base::StringValue(build_date));
445 #endif // defined(OS_CHROMEOS)
447 // On Chrome OS, do not check for an update automatically.
448 #if defined(OS_CHROMEOS)
449 static_cast<VersionUpdaterCros*>(version_updater_.get())->GetUpdateStatus(
450 base::Bind(&HelpHandler::SetUpdateStatus, base::Unretained(this)));
451 #else
452 RequestUpdate(NULL);
453 #endif
455 #if defined(OS_MACOSX)
456 web_ui()->CallJavascriptFunction(
457 "help.HelpPage.setObsoleteSystem",
458 base::FundamentalValue(ObsoleteSystemMac::Is32BitObsoleteNowOrSoon() &&
459 ObsoleteSystemMac::Has32BitOnlyCPU()));
460 web_ui()->CallJavascriptFunction(
461 "help.HelpPage.setObsoleteSystemEndOfTheLine",
462 base::FundamentalValue(ObsoleteSystemMac::Is32BitObsoleteNowOrSoon() &&
463 ObsoleteSystemMac::Is32BitEndOfTheLine()));
464 #endif
466 #if defined(OS_CHROMEOS)
467 web_ui()->CallJavascriptFunction(
468 "help.HelpPage.updateIsEnterpriseManaged",
469 base::FundamentalValue(IsEnterpriseManaged()));
470 // First argument to GetChannel() is a flag that indicates whether
471 // current channel should be returned (if true) or target channel
472 // (otherwise).
473 version_updater_->GetChannel(true,
474 base::Bind(&HelpHandler::OnCurrentChannel, weak_factory_.GetWeakPtr()));
475 version_updater_->GetChannel(false,
476 base::Bind(&HelpHandler::OnTargetChannel, weak_factory_.GetWeakPtr()));
478 base::PostTaskAndReplyWithResult(
479 content::BrowserThread::GetBlockingPool(),
480 FROM_HERE,
481 base::Bind(&FindRegulatoryLabelDir),
482 base::Bind(&HelpHandler::OnRegulatoryLabelDirFound,
483 weak_factory_.GetWeakPtr()));
484 #endif
487 #if defined(OS_MACOSX)
488 void HelpHandler::PromoteUpdater(const base::ListValue* args) {
489 version_updater_->PromoteUpdater();
491 #endif
493 void HelpHandler::RelaunchNow(const base::ListValue* args) {
494 DCHECK(args->empty());
495 version_updater_->RelaunchBrowser();
498 void HelpHandler::OpenFeedbackDialog(const base::ListValue* args) {
499 DCHECK(args->empty());
500 Browser* browser = chrome::FindBrowserWithWebContents(
501 web_ui()->GetWebContents());
502 chrome::OpenFeedbackDialog(browser);
505 void HelpHandler::OpenHelpPage(const base::ListValue* args) {
506 DCHECK(args->empty());
507 Browser* browser = chrome::FindBrowserWithWebContents(
508 web_ui()->GetWebContents());
509 chrome::ShowHelp(browser, chrome::HELP_SOURCE_WEBUI);
512 #if defined(OS_CHROMEOS)
514 void HelpHandler::SetChannel(const base::ListValue* args) {
515 DCHECK(args->GetSize() == 2);
517 if (!CanChangeChannel(Profile::FromWebUI(web_ui()))) {
518 LOG(WARNING) << "Non-owner tried to change release track.";
519 return;
522 base::string16 channel;
523 bool is_powerwash_allowed;
524 if (!args->GetString(0, &channel) ||
525 !args->GetBoolean(1, &is_powerwash_allowed)) {
526 LOG(ERROR) << "Can't parse SetChannel() args";
527 return;
530 version_updater_->SetChannel(base::UTF16ToUTF8(channel),
531 is_powerwash_allowed);
532 if (user_manager::UserManager::Get()->IsCurrentUserOwner()) {
533 // Check for update after switching release channel.
534 version_updater_->CheckForUpdate(base::Bind(&HelpHandler::SetUpdateStatus,
535 base::Unretained(this)));
539 void HelpHandler::RelaunchAndPowerwash(const base::ListValue* args) {
540 DCHECK(args->empty());
542 if (IsEnterpriseManaged())
543 return;
545 PrefService* prefs = g_browser_process->local_state();
546 prefs->SetBoolean(prefs::kFactoryResetRequested, true);
547 prefs->CommitPendingWrite();
549 // Perform sign out. Current chrome process will then terminate, new one will
550 // be launched (as if it was a restart).
551 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart();
554 #endif // defined(OS_CHROMEOS)
556 void HelpHandler::RequestUpdate(const base::ListValue* args) {
557 version_updater_->CheckForUpdate(
558 base::Bind(&HelpHandler::SetUpdateStatus, base::Unretained(this))
559 #if defined(OS_MACOSX)
560 , base::Bind(&HelpHandler::SetPromotionState, base::Unretained(this))
561 #endif
565 void HelpHandler::SetUpdateStatus(VersionUpdater::Status status,
566 int progress, const base::string16& message) {
567 // Only UPDATING state should have progress set.
568 DCHECK(status == VersionUpdater::UPDATING || progress == 0);
570 std::string status_str;
571 switch (status) {
572 case VersionUpdater::CHECKING:
573 status_str = "checking";
574 break;
575 case VersionUpdater::UPDATING:
576 status_str = "updating";
577 break;
578 case VersionUpdater::NEARLY_UPDATED:
579 status_str = "nearly_updated";
580 break;
581 case VersionUpdater::UPDATED:
582 status_str = "updated";
583 break;
584 case VersionUpdater::FAILED:
585 case VersionUpdater::FAILED_OFFLINE:
586 case VersionUpdater::FAILED_CONNECTION_TYPE_DISALLOWED:
587 status_str = "failed";
588 break;
589 case VersionUpdater::DISABLED:
590 status_str = "disabled";
591 break;
594 web_ui()->CallJavascriptFunction("help.HelpPage.setUpdateStatus",
595 base::StringValue(status_str),
596 base::StringValue(message));
598 if (status == VersionUpdater::UPDATING) {
599 web_ui()->CallJavascriptFunction("help.HelpPage.setProgress",
600 base::FundamentalValue(progress));
603 #if defined(OS_CHROMEOS)
604 if (status == VersionUpdater::FAILED_OFFLINE ||
605 status == VersionUpdater::FAILED_CONNECTION_TYPE_DISALLOWED) {
606 base::string16 types_msg = GetAllowedConnectionTypesMessage();
607 if (!types_msg.empty()) {
608 web_ui()->CallJavascriptFunction(
609 "help.HelpPage.setAndShowAllowedConnectionTypesMsg",
610 base::StringValue(types_msg));
611 } else {
612 web_ui()->CallJavascriptFunction(
613 "help.HelpPage.showAllowedConnectionTypesMsg",
614 base::FundamentalValue(false));
616 } else {
617 web_ui()->CallJavascriptFunction(
618 "help.HelpPage.showAllowedConnectionTypesMsg",
619 base::FundamentalValue(false));
621 #endif // defined(OS_CHROMEOS)
624 #if defined(OS_MACOSX)
625 void HelpHandler::SetPromotionState(VersionUpdater::PromotionState state) {
626 std::string state_str;
627 switch (state) {
628 case VersionUpdater::PROMOTE_HIDDEN:
629 state_str = "hidden";
630 break;
631 case VersionUpdater::PROMOTE_ENABLED:
632 state_str = "enabled";
633 break;
634 case VersionUpdater::PROMOTE_DISABLED:
635 state_str = "disabled";
636 break;
639 web_ui()->CallJavascriptFunction("help.HelpPage.setPromotionState",
640 base::StringValue(state_str));
642 #endif // defined(OS_MACOSX)
644 #if defined(OS_CHROMEOS)
645 void HelpHandler::OnOSVersion(const std::string& version) {
646 web_ui()->CallJavascriptFunction("help.HelpPage.setOSVersion",
647 base::StringValue(version));
650 void HelpHandler::OnOSFirmware(const std::string& firmware) {
651 web_ui()->CallJavascriptFunction("help.HelpPage.setOSFirmware",
652 base::StringValue(firmware));
655 void HelpHandler::OnCurrentChannel(const std::string& channel) {
656 web_ui()->CallJavascriptFunction(
657 "help.HelpPage.updateCurrentChannel", base::StringValue(channel));
660 void HelpHandler::OnTargetChannel(const std::string& channel) {
661 web_ui()->CallJavascriptFunction(
662 "help.HelpPage.updateTargetChannel", base::StringValue(channel));
665 void HelpHandler::OnRegulatoryLabelDirFound(const base::FilePath& path) {
666 if (path.empty())
667 return;
669 base::PostTaskAndReplyWithResult(
670 content::BrowserThread::GetBlockingPool(), FROM_HERE,
671 base::Bind(&ReadRegulatoryLabelText, path),
672 base::Bind(&HelpHandler::OnRegulatoryLabelTextRead,
673 weak_factory_.GetWeakPtr()));
675 // Send the image path to the WebUI.
676 OnRegulatoryLabelImageFound(path.AppendASCII(kRegulatoryLabelImageFilename));
679 void HelpHandler::OnRegulatoryLabelImageFound(const base::FilePath& path) {
680 std::string url = std::string("chrome://") + chrome::kChromeOSAssetHost +
681 "/" + path.MaybeAsASCII();
682 web_ui()->CallJavascriptFunction("help.HelpPage.setRegulatoryLabelPath",
683 base::StringValue(url));
686 void HelpHandler::OnRegulatoryLabelTextRead(const std::string& text) {
687 // Remove unnecessary whitespace.
688 web_ui()->CallJavascriptFunction(
689 "help.HelpPage.setRegulatoryLabelText",
690 base::StringValue(base::CollapseWhitespaceASCII(text, true)));
693 #endif // defined(OS_CHROMEOS)