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

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

Issue 2431543003: WelcomeHandler sometimes has browser_ null in ctor (Closed)
Patch Set: Fix WelcomeHandler crash when signin link is clicked. Created 4 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_handler.h" 5 #include "chrome/browser/ui/webui/welcome_handler.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" 8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
9 #include "chrome/browser/signin/signin_manager_factory.h" 9 #include "chrome/browser/signin/signin_manager_factory.h"
10 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_finder.h" 11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/browser_navigator.h" 12 #include "chrome/browser/ui/browser_navigator.h"
13 #include "chrome/browser/ui/profile_chooser_constants.h" 13 #include "chrome/browser/ui/profile_chooser_constants.h"
14 #include "chrome/common/url_constants.h" 14 #include "chrome/common/url_constants.h"
15 #include "components/signin/core/browser/signin_manager.h" 15 #include "components/signin/core/browser/signin_manager.h"
16 #include "components/signin/core/browser/signin_metrics.h" 16 #include "components/signin/core/browser/signin_metrics.h"
17 #include "ui/base/page_transition_types.h" 17 #include "ui/base/page_transition_types.h"
18 18
19 WelcomeHandler::WelcomeHandler(content::WebUI* web_ui) 19 WelcomeHandler::WelcomeHandler(content::WebUI* web_ui)
20 : profile_(Profile::FromWebUI(web_ui)), 20 : profile_(Profile::FromWebUI(web_ui)),
21 browser_(chrome::FindBrowserWithWebContents(web_ui->GetWebContents())), 21 browser_(chrome::FindBrowserWithWebContents(web_ui->GetWebContents())),
Dan Beam 2016/10/18 21:01:54 can we just make a Browser* WelcomeHandler::GetBr
22 oauth2_token_service_( 22 oauth2_token_service_(
23 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)), 23 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)),
24 result_(WelcomeResult::DEFAULT) { 24 result_(WelcomeResult::DEFAULT) {
25 oauth2_token_service_->AddObserver(this); 25 oauth2_token_service_->AddObserver(this);
26 } 26 }
27 27
28 WelcomeHandler::~WelcomeHandler() { 28 WelcomeHandler::~WelcomeHandler() {
29 oauth2_token_service_->RemoveObserver(this); 29 oauth2_token_service_->RemoveObserver(this);
30 // TODO(tmartino): Log to UMA according to WelcomeResult. 30 // TODO(tmartino): Log to UMA according to WelcomeResult.
31 } 31 }
32 32
33 // Override from OAuth2TokenService::Observer. Occurs when a new auth token is 33 // Override from OAuth2TokenService::Observer. Occurs when a new auth token is
34 // available. 34 // available.
35 void WelcomeHandler::OnRefreshTokenAvailable(const std::string& account_id) { 35 void WelcomeHandler::OnRefreshTokenAvailable(const std::string& account_id) {
36 result_ = WelcomeResult::SIGNED_IN; 36 result_ = WelcomeResult::SIGNED_IN;
37 GoToNewTabPage(); 37 GoToNewTabPage();
38 } 38 }
39 39
40 // Handles backend events necessary when user clicks "Sign in." 40 // Handles backend events necessary when user clicks "Sign in."
41 void WelcomeHandler::HandleActivateSignIn(const base::ListValue* args) { 41 void WelcomeHandler::HandleActivateSignIn(const base::ListValue* args) {
42 if (SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated()) { 42 if (SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated()) {
43 // In general, this page isn't shown to signed-in users; however, if one 43 // In general, this page isn't shown to signed-in users; however, if one
44 // should arrive here, then opening the sign-in dialog will likely lead 44 // should arrive here, then opening the sign-in dialog will likely lead
45 // to a crash. Thus, we just act like sign-in was "successful" and whisk 45 // to a crash. Thus, we just act like sign-in was "successful" and whisk
46 // them away to the NTP instead. 46 // them away to the NTP instead.
47 GoToNewTabPage(); 47 GoToNewTabPage();
48 } else { 48 } else {
49 // browser_ does not always properly initialize in the constructor.
50 // https://crbug.com/656153
dpapad 2016/10/18 18:17:02 +dbeam who might know more about WebUI lifetime re
51 if (browser_ == nullptr) {
52 browser_ = chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
53 }
49 browser_->ShowModalSigninWindow( 54 browser_->ShowModalSigninWindow(
50 profiles::BubbleViewMode::BUBBLE_VIEW_MODE_GAIA_SIGNIN, 55 profiles::BubbleViewMode::BUBBLE_VIEW_MODE_GAIA_SIGNIN,
51 signin_metrics::AccessPoint::ACCESS_POINT_START_PAGE); 56 signin_metrics::AccessPoint::ACCESS_POINT_START_PAGE);
52 } 57 }
53 } 58 }
54 59
55 // Handles backend events necessary when user clicks "No thanks." 60 // Handles backend events necessary when user clicks "No thanks."
56 void WelcomeHandler::HandleUserDecline(const base::ListValue* args) { 61 void WelcomeHandler::HandleUserDecline(const base::ListValue* args) {
57 result_ = WelcomeResult::DECLINED; 62 result_ = WelcomeResult::DECLINED;
58 GoToNewTabPage(); 63 GoToNewTabPage();
59 } 64 }
60 65
61 // Override from WebUIMessageHandler. 66 // Override from WebUIMessageHandler.
62 void WelcomeHandler::RegisterMessages() { 67 void WelcomeHandler::RegisterMessages() {
63 web_ui()->RegisterMessageCallback( 68 web_ui()->RegisterMessageCallback(
64 "handleActivateSignIn", base::Bind(&WelcomeHandler::HandleActivateSignIn, 69 "handleActivateSignIn", base::Bind(&WelcomeHandler::HandleActivateSignIn,
65 base::Unretained(this))); 70 base::Unretained(this)));
66 web_ui()->RegisterMessageCallback( 71 web_ui()->RegisterMessageCallback(
67 "handleUserDecline", 72 "handleUserDecline",
68 base::Bind(&WelcomeHandler::HandleUserDecline, base::Unretained(this))); 73 base::Bind(&WelcomeHandler::HandleUserDecline, base::Unretained(this)));
69 } 74 }
70 75
71 void WelcomeHandler::GoToNewTabPage() { 76 void WelcomeHandler::GoToNewTabPage() {
72 chrome::NavigateParams params(browser_, GURL(chrome::kChromeUINewTabURL), 77 chrome::NavigateParams params(browser_, GURL(chrome::kChromeUINewTabURL),
73 ui::PageTransition::PAGE_TRANSITION_LINK); 78 ui::PageTransition::PAGE_TRANSITION_LINK);
74 chrome::Navigate(&params); 79 chrome::Navigate(&params);
75 } 80 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698