Chromium Code Reviews| 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> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "chrome/browser/shell_integration.h" | 10 #include "chrome/browser/shell_integration.h" |
| 11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "chrome/grit/generated_resources.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() = default; |
| 15 | 18 |
| 16 WelcomeWin10Handler::~WelcomeWin10Handler() = default; | 19 WelcomeWin10Handler::~WelcomeWin10Handler() = default; |
| 17 | 20 |
| 18 // Override from WebUIMessageHandler. | |
| 19 void WelcomeWin10Handler::RegisterMessages() { | 21 void WelcomeWin10Handler::RegisterMessages() { |
| 20 web_ui()->RegisterMessageCallback( | 22 web_ui()->RegisterMessageCallback( |
| 21 "handleSetDefaultBrowser", | 23 "handleSetDefaultBrowser", |
| 22 base::Bind(&WelcomeWin10Handler::HandleSetDefaultBrowser, | 24 base::Bind(&WelcomeWin10Handler::HandleSetDefaultBrowser, |
| 23 base::Unretained(this))); | 25 base::Unretained(this))); |
| 24 web_ui()->RegisterMessageCallback( | 26 web_ui()->RegisterMessageCallback( |
| 25 "handleContinue", | 27 "handleContinue", |
| 26 base::Bind(&WelcomeWin10Handler::HandleContinue, base::Unretained(this))); | 28 base::Bind(&WelcomeWin10Handler::HandleContinue, base::Unretained(this))); |
| 29 web_ui()->RegisterMessageCallback( | |
| 30 "handleIsPinnedToTaskbar", | |
| 31 base::Bind(&WelcomeWin10Handler::HandleIsPinnedToTaskbar, | |
| 32 base::Unretained(this))); | |
| 33 } | |
| 34 | |
| 35 void WelcomeWin10Handler::HandleIsPinnedToTaskbar(const base::ListValue* args) { | |
| 36 AllowJavascript(); | |
| 37 | |
| 38 CHECK_EQ(1U, args->GetSize()); | |
| 39 std::string callback_id; | |
| 40 CHECK(args->GetString(0, &callback_id)); | |
| 41 | |
| 42 // Assume that Chrome is pinned to the taskbar if an error occured while | |
| 43 // checking or if the timeout is hit. | |
| 44 base::Closure on_error_callback = | |
| 45 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined, | |
| 46 base::Unretained(this), callback_id, true); | |
| 47 | |
| 48 constexpr base::TimeDelta kPinnedToTaskbarTimeout = | |
| 49 base::TimeDelta::FromMilliseconds(2000); | |
|
tmartino
2016/10/28 15:18:04
2s seems quite high.
Patrick Monette
2016/10/28 21:21:37
I've put 200ms. On my debug version of Chrome, the
| |
| 50 // Set a timeout. | |
| 51 timer_.Start(FROM_HERE, kPinnedToTaskbarTimeout, on_error_callback); | |
| 52 | |
| 53 // Start the utility process that will handle the IsPinnedToTaskbar() call. | |
|
tmartino
2016/10/28 15:18:04
Would it be possible to start the check in the cto
Patrick Monette
2016/10/28 21:21:37
Seems reasonable. Done.
| |
| 54 client_.reset(new content::UtilityProcessMojoClient<mojom::ShellHandler>( | |
| 55 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_SHELL_HANDLER_NAME))); | |
| 56 | |
| 57 client_->set_error_callback(on_error_callback); | |
| 58 client_->set_disable_sandbox(); | |
| 59 client_->Start(); | |
| 60 | |
| 61 client_->service()->IsPinnedToTaskbar( | |
| 62 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarResult, | |
| 63 base::Unretained(this), callback_id)); | |
| 27 } | 64 } |
| 28 | 65 |
| 29 void WelcomeWin10Handler::HandleSetDefaultBrowser(const base::ListValue* args) { | 66 void WelcomeWin10Handler::HandleSetDefaultBrowser(const base::ListValue* args) { |
| 30 // The worker owns itself. | 67 // The worker owns itself. |
| 31 (new shell_integration::DefaultBrowserWorker( | 68 (new shell_integration::DefaultBrowserWorker( |
| 32 shell_integration::DefaultWebClientWorkerCallback())) | 69 shell_integration::DefaultWebClientWorkerCallback())) |
| 33 ->StartSetAsDefault(); | 70 ->StartSetAsDefault(); |
| 34 } | 71 } |
| 35 | 72 |
| 36 void WelcomeWin10Handler::HandleContinue(const base::ListValue* args) { | 73 void WelcomeWin10Handler::HandleContinue(const base::ListValue* args) { |
| 37 web_ui()->GetWebContents()->GetController().LoadURL( | 74 web_ui()->GetWebContents()->GetController().LoadURL( |
| 38 GURL(chrome::kChromeUINewTabURL), content::Referrer(), | 75 GURL(chrome::kChromeUINewTabURL), content::Referrer(), |
| 39 ui::PageTransition::PAGE_TRANSITION_LINK, std::string()); | 76 ui::PageTransition::PAGE_TRANSITION_LINK, std::string()); |
| 40 } | 77 } |
| 78 | |
| 79 void WelcomeWin10Handler::OnIsPinnedToTaskbarResult( | |
| 80 const std::string& callback_id, | |
| 81 bool succeeded, | |
| 82 bool is_pinned_to_taskbar) { | |
| 83 OnIsPinnedToTaskbarDetermined(callback_id, succeeded && is_pinned_to_taskbar); | |
| 84 } | |
| 85 | |
| 86 void WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined( | |
| 87 const std::string& callback_id, | |
| 88 bool is_pinned_to_taskbar) { | |
| 89 // Reset the client and the timer to make sure this function only gets called | |
| 90 // once. | |
| 91 client_.reset(); | |
| 92 timer_.Stop(); | |
| 93 | |
| 94 ResolveJavascriptCallback(base::StringValue(callback_id), | |
| 95 base::FundamentalValue(is_pinned_to_taskbar)); | |
| 96 } | |
| OLD | NEW |