OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h" | |
6 | |
7 #include "base/atomic_sequence_num.h" | |
8 #include "base/bind.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/signin/signin_global_error.h" | |
14 #include "chrome/browser/signin/signin_oauth_helper.h" | |
15 #include "chrome/browser/signin/signin_promo.h" | |
16 #include "chrome/browser/sync/profile_sync_service.h" | |
17 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
18 #include "chrome/browser/ui/browser_finder.h" | |
19 #include "chrome/browser/ui/sync/one_click_signin_helper.h" | |
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
21 #include "content/public/browser/storage_partition.h" | |
22 #include "content/public/browser/web_contents.h" | |
23 #include "content/public/browser/web_ui.h" | |
24 #include "google_apis/gaia/gaia_auth_fetcher.h" | |
25 #include "google_apis/gaia/gaia_constants.h" | |
26 #include "google_apis/gaia/gaia_urls.h" | |
27 #include "net/base/url_util.h" | |
28 | |
29 namespace { | |
30 | |
31 // Global SequenceNumber used for generating unique webview partition IDs. | |
32 base::StaticAtomicSequenceNumber next_partition_id; | |
33 | |
34 } // empty namespace | |
35 | |
36 InlineLoginHandlerImpl::InlineLoginHandlerImpl() | |
37 : weak_factory_(this), choose_what_to_sync_(false), partition_id_("") { | |
38 } | |
39 | |
40 InlineLoginHandlerImpl::~InlineLoginHandlerImpl() {} | |
41 | |
42 void InlineLoginHandlerImpl::RegisterMessages() { | |
43 InlineLoginHandler::RegisterMessages(); | |
44 | |
45 web_ui()->RegisterMessageCallback("switchToFullTab", | |
46 base::Bind(&InlineLoginHandlerImpl::HandleSwitchToFullTabMessage, | |
47 base::Unretained(this))); | |
48 } | |
49 | |
50 void InlineLoginHandlerImpl::SetExtraInitParams(base::DictionaryValue& params) { | |
51 params.SetInteger("authMode", InlineLoginHandler::kInlineAuthMode); | |
52 | |
53 const GURL& current_url = web_ui()->GetWebContents()->GetURL(); | |
54 signin::Source source = signin::GetSourceForPromoURL(current_url); | |
55 DCHECK(source != signin::SOURCE_UNKNOWN); | |
56 if (source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT || | |
57 source == signin::SOURCE_AVATAR_BUBBLE_SIGN_IN) { | |
58 // Drop the leading slash in the path. | |
59 params.SetString("gaiaPath", | |
60 GaiaUrls::GetInstance()->embedded_signin_url().path().substr(1)); | |
61 } | |
62 | |
63 params.SetString("service", "chromiumsync"); | |
64 params.SetString("continueUrl", | |
65 signin::GetLandingURL("source", static_cast<int>(source)).spec()); | |
66 | |
67 std::string email; | |
68 net::GetValueForKeyInQuery(current_url, "Email", &email); | |
69 if (!email.empty()) | |
70 params.SetString("email", email); | |
71 | |
72 std::string frame_url; | |
73 net::GetValueForKeyInQuery(current_url, "frameUrl", &frame_url); | |
74 if (!frame_url.empty()) | |
75 params.SetString("frameUrl", frame_url); | |
76 | |
77 std::string is_constrained; | |
78 net::GetValueForKeyInQuery(current_url, "constrained", &is_constrained); | |
79 if (!is_constrained.empty()) | |
80 params.SetString("constrained", is_constrained); | |
81 | |
82 net::GetValueForKeyInQuery(current_url, "partitionId", &partition_id_); | |
83 if (partition_id_.empty()) { | |
84 partition_id_ = | |
85 "gaia-webview-" + base::IntToString(next_partition_id.GetNext()); | |
86 } | |
87 params.SetString("partitionId", partition_id_); | |
88 } | |
89 | |
90 | |
91 void InlineLoginHandlerImpl::HandleSwitchToFullTabMessage( | |
92 const base::ListValue* args) { | |
93 base::string16 url_str; | |
94 CHECK(args->GetString(0, &url_str)); | |
95 | |
96 content::WebContents* web_contents = web_ui()->GetWebContents(); | |
97 GURL main_frame_url(web_contents->GetURL()); | |
98 main_frame_url = net::AppendOrReplaceQueryParameter( | |
99 main_frame_url, "frameUrl", UTF16ToASCII(url_str)); | |
100 main_frame_url = net::AppendOrReplaceQueryParameter( | |
101 main_frame_url, "partitionId", partition_id_); | |
102 chrome::NavigateParams params( | |
103 Profile::FromWebUI(web_ui()), | |
104 net::AppendOrReplaceQueryParameter(main_frame_url, "constrained", "0"), | |
105 content::PAGE_TRANSITION_AUTO_TOPLEVEL); | |
106 chrome::Navigate(¶ms); | |
107 | |
108 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); | |
109 } | |
110 | |
111 void InlineLoginHandlerImpl::CompleteLogin(const base::ListValue* args) { | |
112 DCHECK(email_.empty() && password_.empty()); | |
113 | |
114 const base::DictionaryValue* dict = NULL; | |
115 base::string16 email; | |
116 if (!args->GetDictionary(0, &dict) || !dict || | |
117 !dict->GetString("email", &email)) { | |
118 // User cancelled the signin by clicking 'skip for now'. | |
119 bool skip_for_now = false; | |
120 DCHECK(dict->GetBoolean("skipForNow", &skip_for_now) && skip_for_now); | |
121 | |
122 signin::SetUserSkippedPromo(Profile::FromWebUI(web_ui())); | |
123 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); | |
124 return; | |
125 } | |
126 | |
127 email_ = UTF16ToASCII(email); | |
128 base::string16 password; | |
129 dict->GetString("password", &password); | |
130 password_ = UTF16ToASCII(password); | |
131 | |
132 dict->GetBoolean("chooseWhatToSync", &choose_what_to_sync_); | |
133 | |
134 content::WebContents* contents = web_ui()->GetWebContents(); | |
135 signin::Source source = signin::GetSourceForPromoURL(contents->GetURL()); | |
136 OneClickSigninHelper::CanOfferFor can_offer = | |
137 source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT ? | |
138 OneClickSigninHelper::CAN_OFFER_FOR_SECONDARY_ACCOUNT : | |
139 OneClickSigninHelper::CAN_OFFER_FOR_ALL; | |
140 std::string error_msg; | |
141 OneClickSigninHelper::CanOffer( | |
142 contents, can_offer, email_, &error_msg); | |
143 if (!error_msg.empty()) { | |
144 HandleLoginError(error_msg); | |
145 return; | |
146 } | |
147 | |
148 content::StoragePartition* partition = | |
149 content::BrowserContext::GetStoragePartitionForSite( | |
150 contents->GetBrowserContext(), | |
151 GURL("chrome-guest://mfffpogegjflfpflabcdkioaeobkgjik/?" + | |
152 partition_id_)); | |
153 | |
154 auth_fetcher_.reset(new GaiaAuthFetcher(this, | |
155 GaiaConstants::kChromeSource, | |
156 partition->GetURLRequestContext())); | |
157 auth_fetcher_->StartCookieForOAuthCodeExchange("0"); | |
Roger Tawa OOO till Jul 10th
2013/12/18 20:49:35
Instead of using GaiaAuthFetcher::StartCookieForOA
guohui
2013/12/18 20:51:47
yup, will do it in a followup CL. Thanks!
| |
158 } | |
159 | |
160 void InlineLoginHandlerImpl::OnClientOAuthCodeSuccess( | |
161 const std::string& oauth_code) { | |
162 DCHECK(!oauth_code.empty()); | |
163 | |
164 content::WebContents* contents = web_ui()->GetWebContents(); | |
165 Profile* profile = Profile::FromWebUI(web_ui()); | |
166 ProfileSyncService* sync_service = | |
167 ProfileSyncServiceFactory::GetForProfile(profile); | |
168 const GURL& current_url = contents->GetURL(); | |
169 signin::Source source = signin::GetSourceForPromoURL(current_url); | |
170 | |
171 if (source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT) { | |
172 // SigninOAuthHelper will delete itself. | |
173 SigninOAuthHelper* helper = new SigninOAuthHelper(profile); | |
174 helper->StartAddingAccount(oauth_code); | |
175 } else { | |
176 OneClickSigninSyncStarter::StartSyncMode start_mode = | |
177 source == signin::SOURCE_SETTINGS || choose_what_to_sync_ ? | |
178 (SigninGlobalError::GetForProfile(profile)->HasMenuItem() && | |
179 sync_service && sync_service->HasSyncSetupCompleted()) ? | |
180 OneClickSigninSyncStarter::SHOW_SETTINGS_WITHOUT_CONFIGURE : | |
181 OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST : | |
182 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS; | |
183 OneClickSigninSyncStarter::ConfirmationRequired confirmation_required = | |
184 source == signin::SOURCE_SETTINGS || | |
185 source == signin::SOURCE_WEBSTORE_INSTALL || | |
186 choose_what_to_sync_? | |
187 OneClickSigninSyncStarter::NO_CONFIRMATION : | |
188 OneClickSigninSyncStarter::CONFIRM_AFTER_SIGNIN; | |
189 // Call OneClickSigninSyncStarter to exchange oauth code for tokens. | |
190 // OneClickSigninSyncStarter will delete itself once the job is done. | |
191 new OneClickSigninSyncStarter( | |
192 profile, NULL, "" /* session_index, not used */, | |
193 email_, password_, oauth_code, | |
194 start_mode, | |
195 contents, | |
196 confirmation_required, | |
197 base::Bind(&InlineLoginHandlerImpl::SyncStarterCallback, | |
198 weak_factory_.GetWeakPtr())); | |
199 } | |
200 | |
201 email_.clear(); | |
202 password_.clear(); | |
203 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); | |
204 } | |
205 | |
206 void InlineLoginHandlerImpl::OnClientOAuthCodeFailure( | |
207 const GoogleServiceAuthError& error) { | |
208 LOG(ERROR) << "InlineLoginUI::OnClientOAuthCodeFailure"; | |
209 HandleLoginError(error.ToString()); | |
210 } | |
211 | |
212 void InlineLoginHandlerImpl::HandleLoginError(const std::string& error_msg) { | |
213 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); | |
214 | |
215 Browser* browser = chrome::FindBrowserWithWebContents( | |
216 web_ui()->GetWebContents()); | |
217 if (!browser) { | |
218 browser = chrome::FindLastActiveWithProfile( | |
219 Profile::FromWebUI(web_ui()), chrome::GetActiveDesktop()); | |
220 } | |
221 if (browser) | |
222 OneClickSigninHelper::ShowSigninErrorBubble(browser, error_msg); | |
223 | |
224 email_.clear(); | |
225 password_.clear(); | |
226 } | |
227 | |
228 void InlineLoginHandlerImpl::SyncStarterCallback( | |
229 OneClickSigninSyncStarter::SyncSetupResult result) { | |
230 content::WebContents* contents = web_ui()->GetWebContents(); | |
231 const GURL& current_url = contents->GetURL(); | |
232 bool auto_close = signin::IsAutoCloseEnabledInURL(current_url); | |
233 signin::Source source = signin::GetSourceForPromoURL(current_url); | |
234 if (auto_close) { | |
235 base::MessageLoop::current()->PostTask( | |
236 FROM_HERE, | |
237 base::Bind(&InlineLoginHandlerImpl::CloseTab, | |
238 weak_factory_.GetWeakPtr())); | |
239 } else { | |
240 OneClickSigninHelper::RedirectToNtpOrAppsPageIfNecessary(contents, source); | |
241 } | |
242 } | |
243 | |
244 void InlineLoginHandlerImpl::CloseTab() { | |
245 content::WebContents* tab = web_ui()->GetWebContents(); | |
246 Browser* browser = chrome::FindBrowserWithWebContents(tab); | |
247 if (browser) { | |
248 TabStripModel* tab_strip_model = browser->tab_strip_model(); | |
249 if (tab_strip_model) { | |
250 int index = tab_strip_model->GetIndexOfWebContents(tab); | |
251 if (index != TabStripModel::kNoTab) { | |
252 tab_strip_model->ExecuteContextMenuCommand( | |
253 index, TabStripModel::CommandCloseTab); | |
254 } | |
255 } | |
256 } | |
257 } | |
OLD | NEW |