| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ui/webui/welcome_win10_handler.h" | 5 #include "chrome/browser/ui/webui/welcome_win10_handler.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 10 #include "chrome/browser/shell_integration.h" | 9 #include "chrome/browser/shell_integration.h" |
| 11 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
| 11 #include "chrome/grit/generated_resources.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "url/gurl.h" |
| 13 | 16 |
| 14 WelcomeWin10Handler::WelcomeWin10Handler() = default; | 17 WelcomeWin10Handler::WelcomeWin10Handler() { |
| 18 // The check is started as early as possible because waiting for the page to |
| 19 // be fully loaded is unnecessarily wasting time. |
| 20 StartIsPinnedToTaskbarCheck(); |
| 21 } |
| 15 | 22 |
| 16 WelcomeWin10Handler::~WelcomeWin10Handler() = default; | 23 WelcomeWin10Handler::~WelcomeWin10Handler() = default; |
| 17 | 24 |
| 18 // Override from WebUIMessageHandler. | |
| 19 void WelcomeWin10Handler::RegisterMessages() { | 25 void WelcomeWin10Handler::RegisterMessages() { |
| 20 web_ui()->RegisterMessageCallback( | 26 web_ui()->RegisterMessageCallback( |
| 21 "handleSetDefaultBrowser", | 27 "handleSetDefaultBrowser", |
| 22 base::Bind(&WelcomeWin10Handler::HandleSetDefaultBrowser, | 28 base::Bind(&WelcomeWin10Handler::HandleSetDefaultBrowser, |
| 23 base::Unretained(this))); | 29 base::Unretained(this))); |
| 24 web_ui()->RegisterMessageCallback( | 30 web_ui()->RegisterMessageCallback( |
| 25 "handleContinue", | 31 "handleContinue", |
| 26 base::Bind(&WelcomeWin10Handler::HandleContinue, base::Unretained(this))); | 32 base::Bind(&WelcomeWin10Handler::HandleContinue, base::Unretained(this))); |
| 33 web_ui()->RegisterMessageCallback( |
| 34 "getPinnedToTaskbarState", |
| 35 base::Bind(&WelcomeWin10Handler::HandleGetPinnedToTaskbarState, |
| 36 base::Unretained(this))); |
| 37 } |
| 38 |
| 39 void WelcomeWin10Handler::HandleGetPinnedToTaskbarState( |
| 40 const base::ListValue* args) { |
| 41 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 42 AllowJavascript(); |
| 43 |
| 44 // Save the callback id so that the result can be sent back when it is |
| 45 // available. |
| 46 bool callback_id_found = args->GetString(0, &pinned_state_callback_id_); |
| 47 DCHECK(callback_id_found); |
| 48 |
| 49 // Send back the result if it is already available. |
| 50 if (pinned_state_result_) { |
| 51 SendPinnedToTaskbarStateResult(); |
| 52 return; |
| 53 } |
| 54 |
| 55 // Only wait for a small amount of time for the result. If the timer fires, |
| 56 // it will be assumed that Chrome is pinned to the taskbar. |
| 57 constexpr base::TimeDelta kPinnedToTaskbarTimeout = |
| 58 base::TimeDelta::FromMilliseconds(200); |
| 59 timer_.Start(FROM_HERE, kPinnedToTaskbarTimeout, |
| 60 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined, |
| 61 base::Unretained(this), true)); |
| 27 } | 62 } |
| 28 | 63 |
| 29 void WelcomeWin10Handler::HandleSetDefaultBrowser(const base::ListValue* args) { | 64 void WelcomeWin10Handler::HandleSetDefaultBrowser(const base::ListValue* args) { |
| 30 // The worker owns itself. | 65 // The worker owns itself. |
| 31 (new shell_integration::DefaultBrowserWorker( | 66 (new shell_integration::DefaultBrowserWorker( |
| 32 shell_integration::DefaultWebClientWorkerCallback())) | 67 shell_integration::DefaultWebClientWorkerCallback())) |
| 33 ->StartSetAsDefault(); | 68 ->StartSetAsDefault(); |
| 34 } | 69 } |
| 35 | 70 |
| 36 void WelcomeWin10Handler::HandleContinue(const base::ListValue* args) { | 71 void WelcomeWin10Handler::HandleContinue(const base::ListValue* args) { |
| 37 web_ui()->GetWebContents()->GetController().LoadURL( | 72 web_ui()->GetWebContents()->GetController().LoadURL( |
| 38 GURL(chrome::kChromeUINewTabURL), content::Referrer(), | 73 GURL(chrome::kChromeUINewTabURL), content::Referrer(), |
| 39 ui::PageTransition::PAGE_TRANSITION_LINK, std::string()); | 74 ui::PageTransition::PAGE_TRANSITION_LINK, std::string()); |
| 40 } | 75 } |
| 76 |
| 77 void WelcomeWin10Handler::StartIsPinnedToTaskbarCheck() { |
| 78 // Start the utility process that will handle the IsPinnedToTaskbar() call. |
| 79 client_ = |
| 80 base::MakeUnique<content::UtilityProcessMojoClient<mojom::ShellHandler>>( |
| 81 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_SHELL_HANDLER_NAME)); |
| 82 |
| 83 // Assume that Chrome is pinned to the taskbar if an error occurs. |
| 84 client_->set_error_callback( |
| 85 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined, |
| 86 base::Unretained(this), true)); |
| 87 client_->set_disable_sandbox(); |
| 88 client_->Start(); |
| 89 |
| 90 client_->service()->IsPinnedToTaskbar(base::Bind( |
| 91 &WelcomeWin10Handler::OnIsPinnedToTaskbarResult, base::Unretained(this))); |
| 92 } |
| 93 |
| 94 void WelcomeWin10Handler::OnIsPinnedToTaskbarResult(bool succeeded, |
| 95 bool is_pinned_to_taskbar) { |
| 96 OnIsPinnedToTaskbarDetermined(succeeded && is_pinned_to_taskbar); |
| 97 } |
| 98 |
| 99 void WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined( |
| 100 bool is_pinned_to_taskbar) { |
| 101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 102 |
| 103 // Reset the client and the timer to make sure this function only gets called |
| 104 // once. |
| 105 client_.reset(); |
| 106 timer_.Stop(); |
| 107 |
| 108 pinned_state_result_ = is_pinned_to_taskbar; |
| 109 |
| 110 // If the page already called getPinnedToTaskbarState(), the result can be |
| 111 // sent back. |
| 112 if (!pinned_state_callback_id_.empty()) |
| 113 SendPinnedToTaskbarStateResult(); |
| 114 } |
| 115 |
| 116 void WelcomeWin10Handler::SendPinnedToTaskbarStateResult() { |
| 117 ResolveJavascriptCallback( |
| 118 base::StringValue(pinned_state_callback_id_), |
| 119 base::FundamentalValue(pinned_state_result_.value())); |
| 120 } |
| OLD | NEW |