Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / signin / inline_login_handler.cc
blob11190a7af90fdb88ae6e0a0979c5e7ee9100a050
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"
7 #include "base/bind.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_navigator.h"
17 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_ui.h"
20 #include "google_apis/gaia/gaia_urls.h"
21 #include "net/base/url_util.h"
23 InlineLoginHandler::InlineLoginHandler() {}
25 InlineLoginHandler::~InlineLoginHandler() {}
27 void InlineLoginHandler::RegisterMessages() {
28 web_ui()->RegisterMessageCallback("initialize",
29 base::Bind(&InlineLoginHandler::HandleInitializeMessage,
30 base::Unretained(this)));
31 web_ui()->RegisterMessageCallback("completeLogin",
32 base::Bind(&InlineLoginHandler::HandleCompleteLoginMessage,
33 base::Unretained(this)));
34 web_ui()->RegisterMessageCallback(
35 "switchToFullTab",
36 base::Bind(&InlineLoginHandler::HandleSwitchToFullTabMessage,
37 base::Unretained(this)));
40 void InlineLoginHandler::HandleInitializeMessage(const base::ListValue* args) {
41 base::DictionaryValue params;
43 const std::string& app_locale = g_browser_process->GetApplicationLocale();
44 params.SetString("hl", app_locale);
45 GaiaUrls* gaiaUrls = GaiaUrls::GetInstance();
46 params.SetString("gaiaUrl", gaiaUrls->gaia_url().spec());
47 params.SetInteger("authMode", InlineLoginHandler::kDesktopAuthMode);
49 const GURL& current_url = web_ui()->GetWebContents()->GetURL();
50 signin_metrics::Source source = signin::GetSourceForPromoURL(current_url);
51 if (source == signin_metrics::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT ||
52 source == signin_metrics::SOURCE_AVATAR_BUBBLE_SIGN_IN ||
53 source == signin_metrics::SOURCE_REAUTH) {
54 // Drop the leading slash in the path.
55 params.SetString(
56 "gaiaPath",
57 GaiaUrls::GetInstance()->embedded_signin_url().path().substr(1));
60 params.SetString(
61 "continueUrl",
62 signin::GetLandingURL(signin::kSignInPromoQueryKeySource,
63 static_cast<int>(source)).spec());
65 Profile* profile = Profile::FromWebUI(web_ui());
66 std::string default_email;
67 if (source != signin_metrics::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT &&
68 source != signin_metrics::SOURCE_REAUTH) {
69 default_email =
70 profile->GetPrefs()->GetString(prefs::kGoogleServicesLastUsername);
71 } else {
72 if (!net::GetValueForKeyInQuery(current_url, "email", &default_email))
73 default_email.clear();
75 if (!default_email.empty())
76 params.SetString("email", default_email);
78 std::string frame_url_id_str;
79 net::GetValueForKeyInQuery(current_url, "frameUrlId", &frame_url_id_str);
80 int frame_url_id;
81 std::string frame_url;
82 if (!frame_url_id_str.empty() &&
83 base::StringToInt(frame_url_id_str, &frame_url_id) &&
84 extensions::GaiaAuthExtensionLoader::Get(profile)
85 ->GetData(frame_url_id, &frame_url)) {
86 params.SetString("frameUrl", frame_url);
89 std::string is_constrained;
90 net::GetValueForKeyInQuery(
91 current_url, signin::kSignInPromoQueryKeyConstrained, &is_constrained);
92 if (!is_constrained.empty())
93 params.SetString(signin::kSignInPromoQueryKeyConstrained, is_constrained);
95 // TODO(rogerta): this needs to be passed on to gaia somehow.
96 std::string read_only_email;
97 net::GetValueForKeyInQuery(current_url, "readOnlyEmail", &read_only_email);
98 if (!read_only_email.empty())
99 params.SetString("readOnlyEmail", read_only_email);
101 SetExtraInitParams(params);
103 web_ui()->CallJavascriptFunction("inline.login.loadAuthExtension", params);
106 void InlineLoginHandler::HandleCompleteLoginMessage(
107 const base::ListValue* args) {
108 CompleteLogin(args);
111 void InlineLoginHandler::HandleSwitchToFullTabMessage(
112 const base::ListValue* args) {
113 std::string url_str;
114 CHECK(args->GetString(0, &url_str));
116 Profile* profile = Profile::FromWebUI(web_ui());
117 const int frame_url_id =
118 extensions::GaiaAuthExtensionLoader::Get(profile)->AddData(url_str);
120 content::WebContents* web_contents = web_ui()->GetWebContents();
121 GURL main_frame_url(web_contents->GetURL());
122 main_frame_url = net::AppendOrReplaceQueryParameter(
123 main_frame_url, "frameUrlId", base::IntToString(frame_url_id));
125 // Adds extra parameters to the signin URL so that Chrome will close the tab
126 // and show the account management view of the avatar menu upon completion.
127 main_frame_url = net::AppendOrReplaceQueryParameter(
128 main_frame_url, signin::kSignInPromoQueryKeyAutoClose, "1");
129 main_frame_url = net::AppendOrReplaceQueryParameter(
130 main_frame_url, signin::kSignInPromoQueryKeyShowAccountManagement, "1");
132 chrome::NavigateParams params(
133 profile,
134 net::AppendOrReplaceQueryParameter(
135 main_frame_url, signin::kSignInPromoQueryKeyConstrained, "0"),
136 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
137 chrome::Navigate(&params);
139 web_ui()->CallJavascriptFunction("inline.login.closeDialog");