Chromium Code Reviews| Index: chrome/browser/ui/webui/welcome_win10_handler.cc |
| diff --git a/chrome/browser/ui/webui/welcome_win10_handler.cc b/chrome/browser/ui/webui/welcome_win10_handler.cc |
| index de4167ad0b359e2850c9f2c8e7544b8e42322247..528758da2c2232ee43b697c6c967aa65dc185e15 100644 |
| --- a/chrome/browser/ui/webui/welcome_win10_handler.cc |
| +++ b/chrome/browser/ui/webui/welcome_win10_handler.cc |
| @@ -4,18 +4,23 @@ |
| #include "chrome/browser/ui/webui/welcome_win10_handler.h" |
| -#include <string> |
| - |
| #include "base/bind.h" |
| #include "chrome/browser/shell_integration.h" |
| #include "chrome/common/url_constants.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/web_contents.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "url/gurl.h" |
| -WelcomeWin10Handler::WelcomeWin10Handler() = default; |
| +WelcomeWin10Handler::WelcomeWin10Handler() { |
| + // The check is started as early as possible because waiting for the page to |
| + // be fully loaded is unnecessarily wasting time. |
| + StartIsPinnedToTaskbarCheck(); |
| +} |
| WelcomeWin10Handler::~WelcomeWin10Handler() = default; |
| -// Override from WebUIMessageHandler. |
| void WelcomeWin10Handler::RegisterMessages() { |
| web_ui()->RegisterMessageCallback( |
| "handleSetDefaultBrowser", |
| @@ -24,6 +29,35 @@ void WelcomeWin10Handler::RegisterMessages() { |
| web_ui()->RegisterMessageCallback( |
| "handleContinue", |
| base::Bind(&WelcomeWin10Handler::HandleContinue, base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + "getPinnedToTaskbarState", |
| + base::Bind(&WelcomeWin10Handler::HandleGetPinnedToTaskbarState, |
| + base::Unretained(this))); |
| +} |
| + |
| +void WelcomeWin10Handler::HandleGetPinnedToTaskbarState( |
| + const base::ListValue* args) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + AllowJavascript(); |
| + |
| + CHECK_EQ(1U, args->GetSize()); |
| + // Save the callback id so that the result can be sent back when it is |
| + // available. |
| + CHECK(args->GetString(0, &pinned_state_callback_id_)); |
| + |
| + // Send back the result if it is already available. |
| + if (pinned_state_result_) { |
| + SendPinnedToTaskbarStateResult(); |
| + return; |
| + } |
| + |
| + // Only wait for a small amount of time for the result. If the timer fires, |
| + // it will be assumed that Chrome is pinned to the taskbar. |
| + static constexpr base::TimeDelta kPinnedToTaskbarTimeout = |
|
michaelpg
2016/10/31 21:04:59
so it seems this is allowed because it's |constexp
Patrick Monette
2016/10/31 22:33:49
Removed static.
|
| + base::TimeDelta::FromMilliseconds(200); |
| + timer_.Start(FROM_HERE, kPinnedToTaskbarTimeout, |
| + base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined, |
| + base::Unretained(this), true)); |
| } |
| void WelcomeWin10Handler::HandleSetDefaultBrowser(const base::ListValue* args) { |
| @@ -38,3 +72,47 @@ void WelcomeWin10Handler::HandleContinue(const base::ListValue* args) { |
| GURL(chrome::kChromeUINewTabURL), content::Referrer(), |
| ui::PageTransition::PAGE_TRANSITION_LINK, std::string()); |
| } |
| + |
| +void WelcomeWin10Handler::StartIsPinnedToTaskbarCheck() { |
| + // Start the utility process that will handle the IsPinnedToTaskbar() call. |
| + client_.reset(new content::UtilityProcessMojoClient<mojom::ShellHandler>( |
| + l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_SHELL_HANDLER_NAME))); |
| + |
| + // Assume that Chrome is pinned to the taskbar if an error occurs. |
| + client_->set_error_callback( |
| + base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined, |
| + base::Unretained(this), true)); |
| + client_->set_disable_sandbox(); |
| + client_->Start(); |
| + |
| + client_->service()->IsPinnedToTaskbar(base::Bind( |
| + &WelcomeWin10Handler::OnIsPinnedToTaskbarResult, base::Unretained(this))); |
| +} |
| + |
| +void WelcomeWin10Handler::OnIsPinnedToTaskbarResult(bool succeeded, |
| + bool is_pinned_to_taskbar) { |
| + OnIsPinnedToTaskbarDetermined(succeeded && is_pinned_to_taskbar); |
| +} |
| + |
| +void WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined( |
| + bool is_pinned_to_taskbar) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + // Reset the client and the timer to make sure this function only gets called |
| + // once. |
| + client_.reset(); |
| + timer_.Stop(); |
| + |
| + pinned_state_result_ = is_pinned_to_taskbar; |
| + |
| + // If the page already called getPinnedToTaskbarState(), the result can be |
| + // sent back. |
| + if (!pinned_state_callback_id_.empty()) |
| + SendPinnedToTaskbarStateResult(); |
| +} |
| + |
| +void WelcomeWin10Handler::SendPinnedToTaskbarStateResult() { |
| + ResolveJavascriptCallback( |
| + base::StringValue(pinned_state_callback_id_), |
| + base::FundamentalValue(pinned_state_result_.value())); |
| +} |