Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: chrome/browser/ui/webui/welcome_win10_handler.cc

Issue 2449853008: Determine the Win10-specific Welcome page variant to display (Closed)
Patch Set: Responding to comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
10 #include "chrome/browser/shell_integration.h" 8 #include "chrome/browser/shell_integration.h"
11 #include "chrome/common/url_constants.h" 9 #include "chrome/common/url_constants.h"
10 #include "chrome/grit/generated_resources.h"
12 #include "content/public/browser/web_contents.h" 11 #include "content/public/browser/web_contents.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "url/gurl.h"
13 14
14 WelcomeWin10Handler::WelcomeWin10Handler() = default; 15 WelcomeWin10Handler::WelcomeWin10Handler() {
16 // The check is started as early as possible becasue waiting for the page to
tmartino 2016/10/28 21:44:15 becasue -> because
Patrick Monette 2016/10/29 00:59:12 Done.
17 // be fully loaded is unnecessarily wasiting time.
tmartino 2016/10/28 21:44:15 wasiting -> wasting
Patrick Monette 2016/10/29 00:59:12 Done.
18 StartIsPinnedToTaskbarCheck();
19 }
15 20
16 WelcomeWin10Handler::~WelcomeWin10Handler() = default; 21 WelcomeWin10Handler::~WelcomeWin10Handler() = default;
17 22
18 // Override from WebUIMessageHandler.
19 void WelcomeWin10Handler::RegisterMessages() { 23 void WelcomeWin10Handler::RegisterMessages() {
20 web_ui()->RegisterMessageCallback( 24 web_ui()->RegisterMessageCallback(
21 "handleSetDefaultBrowser", 25 "handleSetDefaultBrowser",
22 base::Bind(&WelcomeWin10Handler::HandleSetDefaultBrowser, 26 base::Bind(&WelcomeWin10Handler::HandleSetDefaultBrowser,
23 base::Unretained(this))); 27 base::Unretained(this)));
24 web_ui()->RegisterMessageCallback( 28 web_ui()->RegisterMessageCallback(
25 "handleContinue", 29 "handleContinue",
26 base::Bind(&WelcomeWin10Handler::HandleContinue, base::Unretained(this))); 30 base::Bind(&WelcomeWin10Handler::HandleContinue, base::Unretained(this)));
31 web_ui()->RegisterMessageCallback(
32 "getPinnedToTaskbarState",
33 base::Bind(&WelcomeWin10Handler::HandleGetPinnedToTaskbarState,
34 base::Unretained(this)));
35 }
36
37 void WelcomeWin10Handler::HandleGetPinnedToTaskbarState(
38 const base::ListValue* args) {
39 AllowJavascript();
40
41 CHECK_EQ(1U, args->GetSize());
42 // Save the callback id so that the result can be sent back when it is
43 // available.
44 CHECK(args->GetString(0, &pinned_state_callback_id_));
45
46 // Send back the result if it is already available.
47 if (pinned_state_result_) {
48 SendPinnedToTaskbarStateResult();
49 return;
50 }
51
52 // Only wait for a small amount of time for the result. If the timer fires,
53 // it will be assumed that Chrome is pinned to the taskbar.
54 static constexpr base::TimeDelta kPinnedToTaskbarTimeout =
55 base::TimeDelta::FromMilliseconds(200);
56 timer_.Start(FROM_HERE, kPinnedToTaskbarTimeout,
57 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined,
58 base::Unretained(this), true));
27 } 59 }
28 60
29 void WelcomeWin10Handler::HandleSetDefaultBrowser(const base::ListValue* args) { 61 void WelcomeWin10Handler::HandleSetDefaultBrowser(const base::ListValue* args) {
30 // The worker owns itself. 62 // The worker owns itself.
31 (new shell_integration::DefaultBrowserWorker( 63 (new shell_integration::DefaultBrowserWorker(
32 shell_integration::DefaultWebClientWorkerCallback())) 64 shell_integration::DefaultWebClientWorkerCallback()))
33 ->StartSetAsDefault(); 65 ->StartSetAsDefault();
34 } 66 }
35 67
36 void WelcomeWin10Handler::HandleContinue(const base::ListValue* args) { 68 void WelcomeWin10Handler::HandleContinue(const base::ListValue* args) {
37 web_ui()->GetWebContents()->GetController().LoadURL( 69 web_ui()->GetWebContents()->GetController().LoadURL(
38 GURL(chrome::kChromeUINewTabURL), content::Referrer(), 70 GURL(chrome::kChromeUINewTabURL), content::Referrer(),
39 ui::PageTransition::PAGE_TRANSITION_LINK, std::string()); 71 ui::PageTransition::PAGE_TRANSITION_LINK, std::string());
40 } 72 }
73
74 void WelcomeWin10Handler::StartIsPinnedToTaskbarCheck() {
75 // Start the utility process that will handle the IsPinnedToTaskbar() call.
76 client_.reset(new content::UtilityProcessMojoClient<mojom::ShellHandler>(
77 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_SHELL_HANDLER_NAME)));
78
79 // Assume that Chrome is pinned to the taskbar if an error occurs.
80 client_->set_error_callback(
81 base::Bind(&WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined,
82 base::Unretained(this), true));
83 client_->set_disable_sandbox();
84 client_->Start();
85
86 client_->service()->IsPinnedToTaskbar(base::Bind(
87 &WelcomeWin10Handler::OnIsPinnedToTaskbarResult, base::Unretained(this)));
88 }
89
90 void WelcomeWin10Handler::OnIsPinnedToTaskbarResult(bool succeeded,
91 bool is_pinned_to_taskbar) {
92 OnIsPinnedToTaskbarDetermined(succeeded && is_pinned_to_taskbar);
93 }
94
95 void WelcomeWin10Handler::OnIsPinnedToTaskbarDetermined(
96 bool is_pinned_to_taskbar) {
97 // Reset the client and the timer to make sure this function only gets called
98 // once.
99 client_.reset();
100 timer_.Stop();
101
102 pinned_state_result_ = is_pinned_to_taskbar;
103
104 // If the page already called getPinnedToTaskbarState(), the result can be
105 // sent back.
106 if (!pinned_state_callback_id_.empty())
107 SendPinnedToTaskbarStateResult();
108 }
109
110 void WelcomeWin10Handler::SendPinnedToTaskbarStateResult() {
111 ResolveJavascriptCallback(
112 base::StringValue(pinned_state_callback_id_),
113 base::FundamentalValue(pinned_state_result_.value()));
114 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698