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

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

Issue 2338213007: Adding JS and C++ handlers for events on new Welcome page. (Closed)
Patch Set: Created 4 years, 3 months 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_ui.h" 5 #include "chrome/browser/ui/webui/welcome_ui.h"
6 6
7 #include "chrome/browser/profiles/profile.h" 7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
9 #include "chrome/browser/signin/signin_manager_factory.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/browser_navigator.h"
13 #include "chrome/browser/ui/profile_chooser_constants.h"
8 #include "chrome/common/url_constants.h" 14 #include "chrome/common/url_constants.h"
9 #include "chrome/grit/browser_resources.h" 15 #include "chrome/grit/browser_resources.h"
10 #include "chrome/grit/chrome_unscaled_resources.h" 16 #include "chrome/grit/chrome_unscaled_resources.h"
11 #include "chrome/grit/chromium_strings.h" 17 #include "chrome/grit/chromium_strings.h"
12 #include "chrome/grit/generated_resources.h" 18 #include "chrome/grit/generated_resources.h"
19 #include "components/signin/core/browser/profile_oauth2_token_service.h"
20 #include "components/signin/core/browser/signin_manager.h"
21 #include "components/signin/core/browser/signin_metrics.h"
13 #include "content/public/browser/web_ui_data_source.h" 22 #include "content/public/browser/web_ui_data_source.h"
23 #include "content/public/browser/web_ui_message_handler.h"
24 #include "google_apis/gaia/oauth2_token_service.h"
14 #include "ui/base/l10n/l10n_util.h" 25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/page_transition_types.h"
15 #include "ui/resources/grit/webui_resources.h" 27 #include "ui/resources/grit/webui_resources.h"
16 28
17 namespace { 29 namespace {
18 30
31 enum WelcomeResult {
32 DEFAULT = 0,
33 SIGNED_IN, // User clicked the "Sign In" button and completed sign-in.
34 DECLINED // User clicked the "No Thanks" button.
35 };
36
19 // A URL ending in /1 indicates that we should modify the page to use the 37 // A URL ending in /1 indicates that we should modify the page to use the
20 // "Take Chrome Everywhere" text. 38 // "Take Chrome Everywhere" text.
21 const char* kEverywhereVariantPath = "/1"; 39 const char* kEverywhereVariantPath = "/1";
anthonyvd 2016/09/19 17:12:16 Why not a descriptive GET style param?
tmartino 2016/09/19 22:37:04 If you don't mind me asking, what would the gain b
anthonyvd 2016/09/21 16:17:56 It's just more self-documenting that way. You can
tmartino 2016/09/22 20:32:09 Opted for the latter.
22 40
41 const char* kHandleActivateSignIn = "handleActivateSignIn";
anthonyvd 2016/09/19 17:12:15 Message name strings tend to be just inlined when
42 const char* kHandleUserDecline = "handleUserDecline";
43
44 class WelcomeHandler : public content::WebUIMessageHandler,
anthonyvd 2016/09/19 17:12:15 Your handler should be in a separate file for cons
45 public OAuth2TokenService::Observer {
46 public:
47 explicit WelcomeHandler(content::WebUI* web_ui) {
48 profile_ = Profile::FromWebUI(web_ui);
anthonyvd 2016/09/19 17:12:16 Prefer initializer lists over initializing in the
49 oauth2_token_service_ =
50 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
51 if (oauth2_token_service_)
anthonyvd 2016/09/19 17:12:16 The Service Factories initialize services in GetFo
52 oauth2_token_service_->AddObserver(this);
53
54 browser_ = chrome::FindBrowserWithWebContents(web_ui->GetWebContents());
55 result_ = WelcomeResult::DEFAULT;
56 user_has_clicked_signin_ = false;
57 }
58
59 ~WelcomeHandler() override {
60 if (oauth2_token_service_)
61 oauth2_token_service_->RemoveObserver(this);
62 // TODO(tmartino): Log to UMA according to WelcomeResult.
63 }
64
65 // Override from OAuth2TokenService::Observer. Occurs when a new auth token is
66 // available.
67 void OnRefreshTokenAvailable(const std::string& account_id) override {
68 if (user_has_clicked_signin_) {
anthonyvd 2016/09/19 17:12:15 What happens if the user has this page up and sign
tmartino 2016/09/19 22:37:04 I was torn on this. Either way it's kind of jarrin
anthonyvd 2016/09/21 16:17:56 Good point, I personally prefer (1) but I could se
tmartino 2016/09/22 20:32:09 Brought this up and the consensus was to go with (
69 result_ = WelcomeResult::SIGNED_IN;
70 GoToNewTabPage();
71 }
72 }
73
74 // Handles backend events necessary when user clicks "Sign in."
75 void HandleActivateSignIn(const base::ListValue* args) {
76 user_has_clicked_signin_ = true;
77
78 if (SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated()) {
79 // In general, this page isn't shown to signed-in users; however, if one
80 // should arrive here, then opening the sign-in dialog will likely lead
81 // to a crash. Thus, we just act like sign-in was "successful" and whisk
82 // them away to the NTP instead,
83 GoToNewTabPage();
84 } else {
85 browser_->ShowModalSigninWindow(
86 profiles::BubbleViewMode::BUBBLE_VIEW_MODE_GAIA_SIGNIN,
87 signin_metrics::AccessPoint::ACCESS_POINT_START_PAGE);
88 }
89 }
90
91 // Handles backend events necessary when user clicks "No thanks."
92 void HandleUserDecline(const base::ListValue* args) {
93 result_ = WelcomeResult::DECLINED;
94 GoToNewTabPage();
95 }
96
97 // Override from WebUIMessageHandler.
98 void RegisterMessages() override {
99 web_ui()->RegisterMessageCallback(
100 kHandleActivateSignIn, base::Bind(&WelcomeHandler::HandleActivateSignIn,
101 base::Unretained(this)));
102 web_ui()->RegisterMessageCallback(
103 kHandleUserDecline,
104 base::Bind(&WelcomeHandler::HandleUserDecline, base::Unretained(this)));
105 }
106
107 void GoToNewTabPage() {
108 chrome::NavigateParams params(browser_, GURL(chrome::kChromeUINewTabURL),
109 ui::PageTransition::PAGE_TRANSITION_LINK);
110 chrome::Navigate(&params);
111 }
112
113 private:
114 Profile* profile_;
115 Browser* browser_;
116 ProfileOAuth2TokenService* oauth2_token_service_;
117 WelcomeResult result_;
118 bool user_has_clicked_signin_;
119 };
120
23 } // namespace 121 } // namespace
24 122
25 WelcomeUI::WelcomeUI(content::WebUI* web_ui, const GURL& url) 123 WelcomeUI::WelcomeUI(content::WebUI* web_ui, const GURL& url)
26 : content::WebUIController(web_ui) { 124 : content::WebUIController(web_ui) {
27 // TODO(tmartino): Create WelcomeHandler, and add here. 125 web_ui->AddMessageHandler(new WelcomeHandler(web_ui));
28 126
29 content::WebUIDataSource* html_source = 127 content::WebUIDataSource* html_source =
30 content::WebUIDataSource::Create(url.host()); 128 content::WebUIDataSource::Create(url.host());
31 129
32 // Check URL for variations. 130 // Check URL for variations.
33 bool is_everywhere_variant = url.path() == kEverywhereVariantPath; 131 bool is_everywhere_variant = url.path() == kEverywhereVariantPath;
34 132
35 int header_id = is_everywhere_variant ? IDS_WELCOME_HEADER_AFTER_FIRST_RUN 133 int header_id = is_everywhere_variant ? IDS_WELCOME_HEADER_AFTER_FIRST_RUN
36 : IDS_WELCOME_HEADER; 134 : IDS_WELCOME_HEADER;
37 html_source->AddString("headerText", l10n_util::GetStringUTF16(header_id)); 135 html_source->AddString("headerText", l10n_util::GetStringUTF16(header_id));
(...skipping 16 matching lines...) Expand all
54 html_source->AddResourcePath("logo.png", IDR_PRODUCT_LOGO_128); 152 html_source->AddResourcePath("logo.png", IDR_PRODUCT_LOGO_128);
55 html_source->AddResourcePath("logo2x.png", IDR_PRODUCT_LOGO_256); 153 html_source->AddResourcePath("logo2x.png", IDR_PRODUCT_LOGO_256);
56 html_source->AddResourcePath("watermark.svg", IDR_WEBUI_IMAGES_GOOGLE_LOGO); 154 html_source->AddResourcePath("watermark.svg", IDR_WEBUI_IMAGES_GOOGLE_LOGO);
57 html_source->SetDefaultResource(IDR_WELCOME_HTML); 155 html_source->SetDefaultResource(IDR_WELCOME_HTML);
58 156
59 Profile* profile = Profile::FromWebUI(web_ui); 157 Profile* profile = Profile::FromWebUI(web_ui);
60 content::WebUIDataSource::Add(profile, html_source); 158 content::WebUIDataSource::Add(profile, html_source);
61 } 159 }
62 160
63 WelcomeUI::~WelcomeUI() {} 161 WelcomeUI::~WelcomeUI() {}
OLDNEW
« chrome/browser/resources/welcome/welcome.js ('K') | « chrome/browser/resources/welcome/welcome.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698