| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/shell_integration.h" | 9 #include "chrome/browser/shell_integration.h" |
| 10 #include "chrome/browser/shell_integration_win.h" |
| 10 #include "chrome/common/url_constants.h" | 11 #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/browser_thread.h" |
| 13 #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" | 14 #include "url/gurl.h" |
| 16 | 15 |
| 17 WelcomeWin10Handler::WelcomeWin10Handler() { | 16 WelcomeWin10Handler::WelcomeWin10Handler() : weak_ptr_factory_(this) { |
| 18 // The check is started as early as possible because waiting for the page to | 17 // The check is started as early as possible because waiting for the page to |
| 19 // be fully loaded is unnecessarily wasting time. | 18 // be fully loaded is unnecessarily wasting time. |
| 20 StartIsPinnedToTaskbarCheck(); | 19 StartIsPinnedToTaskbarCheck(); |
| 21 } | 20 } |
| 22 | 21 |
| 23 WelcomeWin10Handler::~WelcomeWin10Handler() = default; | 22 WelcomeWin10Handler::~WelcomeWin10Handler() = default; |
| 24 | 23 |
| 25 void WelcomeWin10Handler::RegisterMessages() { | 24 void WelcomeWin10Handler::RegisterMessages() { |
| 26 web_ui()->RegisterMessageCallback( | 25 web_ui()->RegisterMessageCallback( |
| 27 "handleSetDefaultBrowser", | 26 "handleSetDefaultBrowser", |
| (...skipping 18 matching lines...) Expand all Loading... |
| 46 bool callback_id_found = args->GetString(0, &pinned_state_callback_id_); | 45 bool callback_id_found = args->GetString(0, &pinned_state_callback_id_); |
| 47 DCHECK(callback_id_found); | 46 DCHECK(callback_id_found); |
| 48 | 47 |
| 49 // Send back the result if it is already available. | 48 // Send back the result if it is already available. |
| 50 if (pinned_state_result_) { | 49 if (pinned_state_result_) { |
| 51 SendPinnedToTaskbarStateResult(); | 50 SendPinnedToTaskbarStateResult(); |
| 52 return; | 51 return; |
| 53 } | 52 } |
| 54 | 53 |
| 55 // Only wait for a small amount of time for the result. If the timer fires, | 54 // 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. | 55 // it will be assumed that Chrome is pinned to the taskbar. This is to make |
| 56 // sure the instructions are never displayed in case it was impossible to |
| 57 // determine the pinned state. |
| 57 constexpr base::TimeDelta kPinnedToTaskbarTimeout = | 58 constexpr base::TimeDelta kPinnedToTaskbarTimeout = |
| 58 base::TimeDelta::FromMilliseconds(200); | 59 base::TimeDelta::FromMilliseconds(200); |
| 59 timer_.Start(FROM_HERE, kPinnedToTaskbarTimeout, | 60 timer_.Start(FROM_HERE, kPinnedToTaskbarTimeout, |
| 60 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined, | 61 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined, |
| 61 base::Unretained(this), true)); | 62 base::Unretained(this), true)); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void WelcomeWin10Handler::HandleSetDefaultBrowser(const base::ListValue* args) { | 65 void WelcomeWin10Handler::HandleSetDefaultBrowser(const base::ListValue* args) { |
| 65 // The worker owns itself. | 66 // The worker owns itself. |
| 66 (new shell_integration::DefaultBrowserWorker( | 67 (new shell_integration::DefaultBrowserWorker( |
| 67 shell_integration::DefaultWebClientWorkerCallback())) | 68 shell_integration::DefaultWebClientWorkerCallback())) |
| 68 ->StartSetAsDefault(); | 69 ->StartSetAsDefault(); |
| 69 } | 70 } |
| 70 | 71 |
| 71 void WelcomeWin10Handler::HandleContinue(const base::ListValue* args) { | 72 void WelcomeWin10Handler::HandleContinue(const base::ListValue* args) { |
| 72 web_ui()->GetWebContents()->GetController().LoadURL( | 73 web_ui()->GetWebContents()->GetController().LoadURL( |
| 73 GURL(chrome::kChromeUINewTabURL), content::Referrer(), | 74 GURL(chrome::kChromeUINewTabURL), content::Referrer(), |
| 74 ui::PageTransition::PAGE_TRANSITION_LINK, std::string()); | 75 ui::PageTransition::PAGE_TRANSITION_LINK, std::string()); |
| 75 } | 76 } |
| 76 | 77 |
| 77 void WelcomeWin10Handler::StartIsPinnedToTaskbarCheck() { | 78 void WelcomeWin10Handler::StartIsPinnedToTaskbarCheck() { |
| 78 // Start the utility process that will handle the IsPinnedToTaskbar() call. | 79 // Assume that Chrome is pinned to the taskbar if an error occurs. |
| 79 client_ = base::MakeUnique< | 80 base::Closure error_callback = |
| 80 content::UtilityProcessMojoClient<chrome::mojom::ShellHandler>>( | 81 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined, |
| 81 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_SHELL_HANDLER_NAME)); | 82 weak_ptr_factory_.GetWeakPtr(), true); |
| 82 | 83 |
| 83 // Assume that Chrome is pinned to the taskbar if an error occurs. | 84 shell_integration::win::GetIsPinnedToTaskbarState( |
| 84 client_->set_error_callback( | 85 error_callback, |
| 85 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined, | 86 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarResult, |
| 86 base::Unretained(this), true)); | 87 weak_ptr_factory_.GetWeakPtr())); |
| 87 client_->set_disable_sandbox(); | |
| 88 client_->Start(); | |
| 89 | |
| 90 client_->service()->IsPinnedToTaskbar(base::Bind( | |
| 91 &WelcomeWin10Handler::OnIsPinnedToTaskbarResult, base::Unretained(this))); | |
| 92 } | 88 } |
| 93 | 89 |
| 94 void WelcomeWin10Handler::OnIsPinnedToTaskbarResult(bool succeeded, | 90 void WelcomeWin10Handler::OnIsPinnedToTaskbarResult(bool succeeded, |
| 95 bool is_pinned_to_taskbar) { | 91 bool is_pinned_to_taskbar) { |
| 96 OnIsPinnedToTaskbarDetermined(succeeded && is_pinned_to_taskbar); | 92 // Assume that Chrome is pinned to the taskbar if an error occured. |
| 93 OnIsPinnedToTaskbarDetermined(!succeeded || is_pinned_to_taskbar); |
| 97 } | 94 } |
| 98 | 95 |
| 99 void WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined( | 96 void WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined( |
| 100 bool is_pinned_to_taskbar) { | 97 bool is_pinned_to_taskbar) { |
| 101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 102 | 99 |
| 103 // Reset the client and the timer to make sure this function only gets called | 100 // Early exit if the pinned_state was already determined. |
| 104 // once. | 101 if (pinned_state_result_) |
| 105 client_.reset(); | 102 return; |
| 103 |
| 104 // Stop the timer if it's still running. |
| 106 timer_.Stop(); | 105 timer_.Stop(); |
| 107 | 106 |
| 108 pinned_state_result_ = is_pinned_to_taskbar; | 107 pinned_state_result_ = is_pinned_to_taskbar; |
| 109 | 108 |
| 110 // If the page already called getPinnedToTaskbarState(), the result can be | 109 // If the page already called getPinnedToTaskbarState(), the result can be |
| 111 // sent back. | 110 // sent back. |
| 112 if (!pinned_state_callback_id_.empty()) | 111 if (!pinned_state_callback_id_.empty()) |
| 113 SendPinnedToTaskbarStateResult(); | 112 SendPinnedToTaskbarStateResult(); |
| 114 } | 113 } |
| 115 | 114 |
| 116 void WelcomeWin10Handler::SendPinnedToTaskbarStateResult() { | 115 void WelcomeWin10Handler::SendPinnedToTaskbarStateResult() { |
| 117 ResolveJavascriptCallback( | 116 ResolveJavascriptCallback( |
| 118 base::StringValue(pinned_state_callback_id_), | 117 base::StringValue(pinned_state_callback_id_), |
| 119 base::FundamentalValue(pinned_state_result_.value())); | 118 base::FundamentalValue(pinned_state_result_.value())); |
| 120 } | 119 } |
| OLD | NEW |