1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/signin/inline_login_handler.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/signin/gaia_auth_extension_loader.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/signin/signin_promo.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_navigator.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/common/pref_names.h"
20 #include "components/signin/core/common/profile_management_switches.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_ui.h"
23 #include "google_apis/gaia/gaia_urls.h"
24 #include "net/base/url_util.h"
25 #include "ui/aura/window.h"
27 InlineLoginHandler::InlineLoginHandler() {}
29 InlineLoginHandler::~InlineLoginHandler() {}
31 void InlineLoginHandler::RegisterMessages() {
32 web_ui()->RegisterMessageCallback("initialize",
33 base::Bind(&InlineLoginHandler::HandleInitializeMessage
,
34 base::Unretained(this)));
35 web_ui()->RegisterMessageCallback("completeLogin",
36 base::Bind(&InlineLoginHandler::HandleCompleteLoginMessage
,
37 base::Unretained(this)));
38 web_ui()->RegisterMessageCallback(
40 base::Bind(&InlineLoginHandler::HandleSwitchToFullTabMessage
,
41 base::Unretained(this)));
44 void InlineLoginHandler::HandleInitializeMessage(const base::ListValue
* args
) {
45 base::DictionaryValue params
;
47 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
48 params
.SetString("hl", app_locale
);
49 GaiaUrls
* gaiaUrls
= GaiaUrls::GetInstance();
50 params
.SetString("gaiaUrl", gaiaUrls
->gaia_url().spec());
51 params
.SetInteger("authMode", InlineLoginHandler::kDesktopAuthMode
);
53 const GURL
& current_url
= web_ui()->GetWebContents()->GetURL();
54 signin_metrics::Source source
= signin::GetSourceForPromoURL(current_url
);
55 if (source
== signin_metrics::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT
||
56 source
== signin_metrics::SOURCE_AVATAR_BUBBLE_SIGN_IN
||
57 source
== signin_metrics::SOURCE_REAUTH
) {
58 // Drop the leading slash in the path.
61 GaiaUrls::GetInstance()->embedded_signin_url().path().substr(1));
66 signin::GetLandingURL(signin::kSignInPromoQueryKeySource
,
67 static_cast<int>(source
)).spec());
69 Profile
* profile
= Profile::FromWebUI(web_ui());
70 std::string default_email
;
71 if (source
!= signin_metrics::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT
&&
72 source
!= signin_metrics::SOURCE_REAUTH
) {
74 profile
->GetPrefs()->GetString(prefs::kGoogleServicesLastUsername
);
76 if (!net::GetValueForKeyInQuery(current_url
, "email", &default_email
))
77 default_email
.clear();
79 if (!default_email
.empty())
80 params
.SetString("email", default_email
);
82 std::string frame_url_id_str
;
83 net::GetValueForKeyInQuery(current_url
, "frameUrlId", &frame_url_id_str
);
85 std::string frame_url
;
86 if (!frame_url_id_str
.empty() &&
87 base::StringToInt(frame_url_id_str
, &frame_url_id
) &&
88 extensions::GaiaAuthExtensionLoader::Get(profile
)
89 ->GetData(frame_url_id
, &frame_url
)) {
90 params
.SetString("frameUrl", frame_url
);
93 std::string is_constrained
;
94 net::GetValueForKeyInQuery(
95 current_url
, signin::kSignInPromoQueryKeyConstrained
, &is_constrained
);
96 if (!is_constrained
.empty())
97 params
.SetString(signin::kSignInPromoQueryKeyConstrained
, is_constrained
);
99 // TODO(rogerta): this needs to be passed on to gaia somehow.
100 std::string read_only_email
;
101 net::GetValueForKeyInQuery(current_url
, "readOnlyEmail", &read_only_email
);
102 if (!read_only_email
.empty())
103 params
.SetString("readOnlyEmail", read_only_email
);
105 SetExtraInitParams(params
);
107 web_ui()->CallJavascriptFunction("inline.login.loadAuthExtension", params
);
110 void InlineLoginHandler::HandleCompleteLoginMessage(
111 const base::ListValue
* args
) {
115 void InlineLoginHandler::HandleSwitchToFullTabMessage(
116 const base::ListValue
* args
) {
118 CHECK(args
->GetString(0, &url_str
));
120 Profile
* profile
= Profile::FromWebUI(web_ui());
121 const int frame_url_id
=
122 extensions::GaiaAuthExtensionLoader::Get(profile
)->AddData(url_str
);
124 content::WebContents
* web_contents
= web_ui()->GetWebContents();
125 GURL
main_frame_url(web_contents
->GetURL());
126 main_frame_url
= net::AppendOrReplaceQueryParameter(
127 main_frame_url
, "frameUrlId", base::IntToString(frame_url_id
));
129 // Adds extra parameters to the signin URL so that Chrome will close the tab
130 // and show the account management view of the avatar menu upon completion.
131 main_frame_url
= net::AppendOrReplaceQueryParameter(
132 main_frame_url
, signin::kSignInPromoQueryKeyAutoClose
, "1");
133 main_frame_url
= net::AppendOrReplaceQueryParameter(
134 main_frame_url
, signin::kSignInPromoQueryKeyShowAccountManagement
, "1");
136 chrome::NavigateParams
params(
138 net::AppendOrReplaceQueryParameter(
139 main_frame_url
, signin::kSignInPromoQueryKeyConstrained
, "0"),
140 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
);
141 chrome::Navigate(¶ms
);
143 signin_metrics::Source source
= signin::GetSourceForPromoURL(main_frame_url
);
144 if (source
== signin_metrics::SOURCE_AVATAR_BUBBLE_SIGN_IN
||
145 source
== signin_metrics::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT
) {
146 // Close the singleton avatar bubble to reveal the full tab UI.
147 Browser
* browser
= chrome::FindAnyBrowser(
149 chrome::GetHostDesktopTypeForNativeView(web_contents
->GetNativeView()));
150 if (browser
!= nullptr)
151 browser
->window()->CloseAvatarBubbleFromAvatarButton();
154 web_ui()->CallJavascriptFunction("inline.login.closeDialog");