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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/welcome_ui.cc
diff --git a/chrome/browser/ui/webui/welcome_ui.cc b/chrome/browser/ui/webui/welcome_ui.cc
index 7c5ffe117b60636bc418bbce20ecbc3c2a720865..b09b6d33de7c84837da16d1672f2d3ac82878ce3 100644
--- a/chrome/browser/ui/webui/welcome_ui.cc
+++ b/chrome/browser/ui/webui/welcome_ui.cc
@@ -5,26 +5,124 @@
#include "chrome/browser/ui/webui/welcome_ui.h"
#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
+#include "chrome/browser/signin/signin_manager_factory.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/browser/ui/browser_finder.h"
+#include "chrome/browser/ui/browser_navigator.h"
+#include "chrome/browser/ui/profile_chooser_constants.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/grit/chrome_unscaled_resources.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
+#include "components/signin/core/browser/profile_oauth2_token_service.h"
+#include "components/signin/core/browser/signin_manager.h"
+#include "components/signin/core/browser/signin_metrics.h"
#include "content/public/browser/web_ui_data_source.h"
+#include "content/public/browser/web_ui_message_handler.h"
+#include "google_apis/gaia/oauth2_token_service.h"
#include "ui/base/l10n/l10n_util.h"
+#include "ui/base/page_transition_types.h"
#include "ui/resources/grit/webui_resources.h"
namespace {
+enum WelcomeResult {
+ DEFAULT = 0,
+ SIGNED_IN, // User clicked the "Sign In" button and completed sign-in.
+ DECLINED // User clicked the "No Thanks" button.
+};
+
// A URL ending in /1 indicates that we should modify the page to use the
// "Take Chrome Everywhere" text.
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.
+const char* kHandleActivateSignIn = "handleActivateSignIn";
anthonyvd 2016/09/19 17:12:15 Message name strings tend to be just inlined when
+const char* kHandleUserDecline = "handleUserDecline";
+
+class WelcomeHandler : public content::WebUIMessageHandler,
anthonyvd 2016/09/19 17:12:15 Your handler should be in a separate file for cons
+ public OAuth2TokenService::Observer {
+ public:
+ explicit WelcomeHandler(content::WebUI* web_ui) {
+ profile_ = Profile::FromWebUI(web_ui);
anthonyvd 2016/09/19 17:12:16 Prefer initializer lists over initializing in the
+ oauth2_token_service_ =
+ ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
+ if (oauth2_token_service_)
anthonyvd 2016/09/19 17:12:16 The Service Factories initialize services in GetFo
+ oauth2_token_service_->AddObserver(this);
+
+ browser_ = chrome::FindBrowserWithWebContents(web_ui->GetWebContents());
+ result_ = WelcomeResult::DEFAULT;
+ user_has_clicked_signin_ = false;
+ }
+
+ ~WelcomeHandler() override {
+ if (oauth2_token_service_)
+ oauth2_token_service_->RemoveObserver(this);
+ // TODO(tmartino): Log to UMA according to WelcomeResult.
+ }
+
+ // Override from OAuth2TokenService::Observer. Occurs when a new auth token is
+ // available.
+ void OnRefreshTokenAvailable(const std::string& account_id) override {
+ 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 (
+ result_ = WelcomeResult::SIGNED_IN;
+ GoToNewTabPage();
+ }
+ }
+
+ // Handles backend events necessary when user clicks "Sign in."
+ void HandleActivateSignIn(const base::ListValue* args) {
+ user_has_clicked_signin_ = true;
+
+ if (SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated()) {
+ // In general, this page isn't shown to signed-in users; however, if one
+ // should arrive here, then opening the sign-in dialog will likely lead
+ // to a crash. Thus, we just act like sign-in was "successful" and whisk
+ // them away to the NTP instead,
+ GoToNewTabPage();
+ } else {
+ browser_->ShowModalSigninWindow(
+ profiles::BubbleViewMode::BUBBLE_VIEW_MODE_GAIA_SIGNIN,
+ signin_metrics::AccessPoint::ACCESS_POINT_START_PAGE);
+ }
+ }
+
+ // Handles backend events necessary when user clicks "No thanks."
+ void HandleUserDecline(const base::ListValue* args) {
+ result_ = WelcomeResult::DECLINED;
+ GoToNewTabPage();
+ }
+
+ // Override from WebUIMessageHandler.
+ void RegisterMessages() override {
+ web_ui()->RegisterMessageCallback(
+ kHandleActivateSignIn, base::Bind(&WelcomeHandler::HandleActivateSignIn,
+ base::Unretained(this)));
+ web_ui()->RegisterMessageCallback(
+ kHandleUserDecline,
+ base::Bind(&WelcomeHandler::HandleUserDecline, base::Unretained(this)));
+ }
+
+ void GoToNewTabPage() {
+ chrome::NavigateParams params(browser_, GURL(chrome::kChromeUINewTabURL),
+ ui::PageTransition::PAGE_TRANSITION_LINK);
+ chrome::Navigate(&params);
+ }
+
+ private:
+ Profile* profile_;
+ Browser* browser_;
+ ProfileOAuth2TokenService* oauth2_token_service_;
+ WelcomeResult result_;
+ bool user_has_clicked_signin_;
+};
+
} // namespace
WelcomeUI::WelcomeUI(content::WebUI* web_ui, const GURL& url)
: content::WebUIController(web_ui) {
- // TODO(tmartino): Create WelcomeHandler, and add here.
+ web_ui->AddMessageHandler(new WelcomeHandler(web_ui));
content::WebUIDataSource* html_source =
content::WebUIDataSource::Create(url.host());
« 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