OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/inline_login_ui.h" | 5 #include "chrome/browser/ui/webui/inline_login_ui.h" |
6 | 6 |
7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/extensions/tab_helper.h" | 16 #include "chrome/browser/extensions/tab_helper.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 18 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
20 #include "chrome/browser/signin/signin_global_error.h" | 20 #include "chrome/browser/signin/signin_global_error.h" |
21 #include "chrome/browser/signin/signin_manager_cookie_helper.h" | 21 #include "chrome/browser/signin/signin_manager_cookie_helper.h" |
22 #include "chrome/browser/signin/signin_names_io_thread.h" | 22 #include "chrome/browser/signin/signin_names_io_thread.h" |
23 #include "chrome/browser/signin/signin_oauth_helper.h" | 23 #include "chrome/browser/signin/signin_oauth_helper.h" |
24 #include "chrome/browser/signin/signin_promo.h" | 24 #include "chrome/browser/signin/signin_promo.h" |
25 #include "chrome/browser/sync/profile_sync_service.h" | 25 #include "chrome/browser/sync/profile_sync_service.h" |
26 #include "chrome/browser/sync/profile_sync_service_factory.h" | 26 #include "chrome/browser/sync/profile_sync_service_factory.h" |
27 #include "chrome/browser/ui/browser_finder.h" | 27 #include "chrome/browser/ui/browser_finder.h" |
| 28 #include "chrome/browser/ui/sync/one_click_signin_helper.h" |
28 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" | 29 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" |
29 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 30 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
30 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
31 #include "chrome/common/url_constants.h" | 32 #include "chrome/common/url_constants.h" |
32 #include "content/public/browser/storage_partition.h" | 33 #include "content/public/browser/storage_partition.h" |
33 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
34 #include "content/public/browser/web_ui.h" | 35 #include "content/public/browser/web_ui.h" |
35 #include "content/public/browser/web_ui_data_source.h" | 36 #include "content/public/browser/web_ui_data_source.h" |
36 #include "content/public/browser/web_ui_message_handler.h" | 37 #include "content/public/browser/web_ui_message_handler.h" |
37 #include "google_apis/gaia/gaia_switches.h" | 38 #include "google_apis/gaia/gaia_switches.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 } | 84 } |
84 | 85 |
85 virtual void OnOAuth2TokensFetchFailed() OVERRIDE { | 86 virtual void OnOAuth2TokensFetchFailed() OVERRIDE { |
86 LOG(ERROR) << "Failed to fetch oauth2 token with inline login."; | 87 LOG(ERROR) << "Failed to fetch oauth2 token with inline login."; |
87 web_ui_->CallJavascriptFunction("inline.login.handleOAuth2TokenFailure"); | 88 web_ui_->CallJavascriptFunction("inline.login.handleOAuth2TokenFailure"); |
88 } | 89 } |
89 | 90 |
90 private: | 91 private: |
91 content::WebUI* web_ui_; | 92 content::WebUI* web_ui_; |
92 }; | 93 }; |
93 #elif !defined(OS_ANDROID) | 94 #else |
94 // Global SequenceNumber used for generating unique webview partition IDs. | 95 // Global SequenceNumber used for generating unique webview partition IDs. |
95 base::StaticAtomicSequenceNumber next_partition_id; | 96 base::StaticAtomicSequenceNumber next_partition_id; |
96 #endif | 97 #endif // OS_CHROMEOS |
97 | 98 |
98 class InlineLoginUIHandler : public content::WebUIMessageHandler { | 99 class InlineLoginUIHandler : public content::WebUIMessageHandler { |
99 public: | 100 public: |
100 explicit InlineLoginUIHandler(Profile* profile) | 101 explicit InlineLoginUIHandler(Profile* profile) |
101 : profile_(profile), weak_factory_(this), choose_what_to_sync_(false), | 102 : profile_(profile), weak_factory_(this), choose_what_to_sync_(false), |
102 partition_id_("") {} | 103 partition_id_("") {} |
103 virtual ~InlineLoginUIHandler() {} | 104 virtual ~InlineLoginUIHandler() {} |
104 | 105 |
105 // content::WebUIMessageHandler overrides: | 106 // content::WebUIMessageHandler overrides: |
106 virtual void RegisterMessages() OVERRIDE { | 107 virtual void RegisterMessages() OVERRIDE { |
(...skipping 22 matching lines...) Expand all Loading... |
129 | 130 |
130 GaiaUrls* gaiaUrls = GaiaUrls::GetInstance(); | 131 GaiaUrls* gaiaUrls = GaiaUrls::GetInstance(); |
131 params.SetString("gaiaUrl", gaiaUrls->gaia_url().spec()); | 132 params.SetString("gaiaUrl", gaiaUrls->gaia_url().spec()); |
132 | 133 |
133 bool enable_inline = CommandLine::ForCurrentProcess()->HasSwitch( | 134 bool enable_inline = CommandLine::ForCurrentProcess()->HasSwitch( |
134 switches::kEnableInlineSignin); | 135 switches::kEnableInlineSignin); |
135 params.SetInteger("authMode", | 136 params.SetInteger("authMode", |
136 enable_inline ? kInlineAuthMode : kDefaultAuthMode); | 137 enable_inline ? kInlineAuthMode : kDefaultAuthMode); |
137 | 138 |
138 // Set parameters specific for inline signin flow. | 139 // Set parameters specific for inline signin flow. |
139 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) | 140 #if !defined(OS_CHROMEOS) |
140 if (enable_inline) { | 141 if (enable_inline) { |
141 // Set continueUrl param for the inline sign in flow. It should point to | 142 // Set continueUrl param for the inline sign in flow. It should point to |
142 // the oauth2 auth code URL so that later we can grab the auth code from | 143 // the oauth2 auth code URL so that later we can grab the auth code from |
143 // the cookie jar of the embedded webview. | 144 // the cookie jar of the embedded webview. |
144 std::string scope = net::EscapeUrlEncodedData( | 145 std::string scope = net::EscapeUrlEncodedData( |
145 gaiaUrls->oauth1_login_scope(), true); | 146 gaiaUrls->oauth1_login_scope(), true); |
146 std::string client_id = net::EscapeUrlEncodedData( | 147 std::string client_id = net::EscapeUrlEncodedData( |
147 gaiaUrls->oauth2_chrome_client_id(), true); | 148 gaiaUrls->oauth2_chrome_client_id(), true); |
148 std::string encoded_continue_params = base::StringPrintf( | 149 std::string encoded_continue_params = base::StringPrintf( |
149 "?scope=%s&client_id=%s", scope.c_str(), client_id.c_str()); | 150 "?scope=%s&client_id=%s", scope.c_str(), client_id.c_str()); |
(...skipping 19 matching lines...) Expand all Loading... |
169 | 170 |
170 std::string email; | 171 std::string email; |
171 net::GetValueForKeyInQuery(current_url, "Email", &email); | 172 net::GetValueForKeyInQuery(current_url, "Email", &email); |
172 if (!email.empty()) | 173 if (!email.empty()) |
173 params.SetString("email", email); | 174 params.SetString("email", email); |
174 | 175 |
175 partition_id_ = | 176 partition_id_ = |
176 "gaia-webview-" + base::IntToString(next_partition_id.GetNext()); | 177 "gaia-webview-" + base::IntToString(next_partition_id.GetNext()); |
177 params.SetString("partitionId", partition_id_); | 178 params.SetString("partitionId", partition_id_); |
178 } | 179 } |
179 #endif | 180 #endif // OS_CHROMEOS |
180 | 181 |
181 web_ui()->CallJavascriptFunction("inline.login.loadAuthExtension", params); | 182 web_ui()->CallJavascriptFunction("inline.login.loadAuthExtension", params); |
182 } | 183 } |
183 | 184 |
184 // JS callback: | 185 // JS callback: |
185 void HandleInitialize(const base::ListValue* args) { | 186 void HandleInitialize(const base::ListValue* args) { |
186 LoadAuthExtension(); | 187 LoadAuthExtension(); |
187 } | 188 } |
188 | 189 |
189 void HandleCompleteLogin(const base::ListValue* args) { | 190 void HandleCompleteLogin(const base::ListValue* args) { |
190 // TODO(guohui, xiyuan): we should investigate if it is possible to unify | 191 // TODO(guohui, xiyuan): we should investigate if it is possible to unify |
191 // the signin-with-cookies flow across ChromeOS and Chrome. | 192 // the signin-with-cookies flow across ChromeOS and Chrome. |
192 #if defined(OS_CHROMEOS) | 193 #if defined(OS_CHROMEOS) |
193 oauth2_delegate_.reset(new InlineLoginUIOAuth2Delegate(web_ui())); | 194 oauth2_delegate_.reset(new InlineLoginUIOAuth2Delegate(web_ui())); |
194 oauth2_token_fetcher_.reset(new chromeos::OAuth2TokenFetcher( | 195 oauth2_token_fetcher_.reset(new chromeos::OAuth2TokenFetcher( |
195 oauth2_delegate_.get(), profile_->GetRequestContext())); | 196 oauth2_delegate_.get(), profile_->GetRequestContext())); |
196 oauth2_token_fetcher_->StartExchangeFromCookies(); | 197 oauth2_token_fetcher_->StartExchangeFromCookies(); |
197 #elif !defined(OS_ANDROID) | 198 #else |
198 const base::DictionaryValue* dict = NULL; | 199 const base::DictionaryValue* dict = NULL; |
199 string16 email; | 200 string16 email; |
200 string16 password; | 201 string16 password; |
201 if (!args->GetDictionary(0, &dict) || !dict || | 202 if (!args->GetDictionary(0, &dict) || !dict || |
202 !dict->GetString("email", &email)) { | 203 !dict->GetString("email", &email)) { |
203 NOTREACHED(); | 204 NOTREACHED(); |
204 return; | 205 return; |
205 } | 206 } |
206 dict->GetString("password", &password); | 207 dict->GetString("password", &password); |
207 dict->GetBoolean("chooseWhatToSync", &choose_what_to_sync_); | 208 dict->GetBoolean("chooseWhatToSync", &choose_what_to_sync_); |
208 | 209 |
209 content::WebContents* web_contents = web_ui()->GetWebContents(); | 210 content::WebContents* web_contents = web_ui()->GetWebContents(); |
| 211 std::string error_msg; |
| 212 OneClickSigninHelper::CanOffer( |
| 213 web_contents, OneClickSigninHelper::CAN_OFFER_FOR_ALL, |
| 214 UTF16ToASCII(email), &error_msg); |
| 215 if (!error_msg.empty()) { |
| 216 SyncStarterCallback( |
| 217 OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); |
| 218 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 219 OneClickSigninHelper::ShowSigninErrorBubble(browser, error_msg); |
| 220 return; |
| 221 } |
| 222 |
210 content::StoragePartition* partition = | 223 content::StoragePartition* partition = |
211 content::BrowserContext::GetStoragePartitionForSite( | 224 content::BrowserContext::GetStoragePartitionForSite( |
212 web_contents->GetBrowserContext(), | 225 web_contents->GetBrowserContext(), |
213 GURL("chrome-guest://mfffpogegjflfpflabcdkioaeobkgjik/?" + | 226 GURL("chrome-guest://mfffpogegjflfpflabcdkioaeobkgjik/?" + |
214 partition_id_)); | 227 partition_id_)); |
215 | 228 |
216 scoped_refptr<SigninManagerCookieHelper> cookie_helper( | 229 scoped_refptr<SigninManagerCookieHelper> cookie_helper( |
217 new SigninManagerCookieHelper(partition->GetURLRequestContext())); | 230 new SigninManagerCookieHelper(partition->GetURLRequestContext())); |
218 cookie_helper->StartFetchingCookiesOnUIThread( | 231 cookie_helper->StartFetchingCookiesOnUIThread( |
219 GURL(GaiaUrls::GetInstance()->client_login_to_oauth2_url()), | 232 GURL(GaiaUrls::GetInstance()->client_login_to_oauth2_url()), |
220 base::Bind(&InlineLoginUIHandler::OnGaiaCookiesFetched, | 233 base::Bind(&InlineLoginUIHandler::OnGaiaCookiesFetched, |
221 weak_factory_.GetWeakPtr(), email, password)); | 234 weak_factory_.GetWeakPtr(), email, password)); |
222 #endif | 235 #endif // OS_CHROMEOS |
223 } | 236 } |
224 | 237 |
225 void OnGaiaCookiesFetched( | 238 void OnGaiaCookiesFetched( |
226 const string16 email, | 239 const string16 email, |
227 const string16 password, | 240 const string16 password, |
228 const net::CookieList& cookie_list) { | 241 const net::CookieList& cookie_list) { |
229 net::CookieList::const_iterator it; | 242 net::CookieList::const_iterator it; |
230 std::string oauth_code; | 243 std::string oauth_code; |
231 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { | 244 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
232 if (it->Name() == "oauth_code") { | 245 if (it->Name() == "oauth_code") { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 void SyncStarterCallback(OneClickSigninSyncStarter::SyncSetupResult result) { | 291 void SyncStarterCallback(OneClickSigninSyncStarter::SyncSetupResult result) { |
279 content::WebContents* contents = web_ui()->GetWebContents(); | 292 content::WebContents* contents = web_ui()->GetWebContents(); |
280 const GURL& current_url = contents->GetURL(); | 293 const GURL& current_url = contents->GetURL(); |
281 bool auto_close = signin::IsAutoCloseEnabledInURL(current_url); | 294 bool auto_close = signin::IsAutoCloseEnabledInURL(current_url); |
282 signin::Source source = signin::GetSourceForPromoURL(current_url); | 295 signin::Source source = signin::GetSourceForPromoURL(current_url); |
283 if (auto_close) { | 296 if (auto_close) { |
284 base::MessageLoop::current()->PostTask( | 297 base::MessageLoop::current()->PostTask( |
285 FROM_HERE, | 298 FROM_HERE, |
286 base::Bind( | 299 base::Bind( |
287 &InlineLoginUIHandler::CloseTab, weak_factory_.GetWeakPtr())); | 300 &InlineLoginUIHandler::CloseTab, weak_factory_.GetWeakPtr())); |
288 } else if (source != signin::SOURCE_UNKNOWN && | 301 return; |
289 source != signin::SOURCE_SETTINGS && | |
290 source != signin::SOURCE_WEBSTORE_INSTALL) { | |
291 // Redirect to NTP/Apps page and display a confirmation bubble. | |
292 // TODO(guohui): should redirect to the given continue url for webstore | |
293 // install flows. | |
294 GURL url(source == signin::SOURCE_APPS_PAGE_LINK ? | |
295 chrome::kChromeUIAppsURL : chrome::kChromeUINewTabURL); | |
296 content::OpenURLParams params(url, | |
297 content::Referrer(), | |
298 CURRENT_TAB, | |
299 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
300 false); | |
301 contents->OpenURL(params); | |
302 } | 302 } |
| 303 |
| 304 OneClickSigninHelper::RedirectToNtpOrAppsPageIfNecessary(contents, source); |
303 } | 305 } |
304 | 306 |
305 void CloseTab() { | 307 void CloseTab() { |
306 content::WebContents* tab = web_ui()->GetWebContents(); | 308 content::WebContents* tab = web_ui()->GetWebContents(); |
307 Browser* browser = chrome::FindBrowserWithWebContents(tab); | 309 Browser* browser = chrome::FindBrowserWithWebContents(tab); |
308 if (browser) { | 310 if (browser) { |
309 TabStripModel* tab_strip_model = browser->tab_strip_model(); | 311 TabStripModel* tab_strip_model = browser->tab_strip_model(); |
310 if (tab_strip_model) { | 312 if (tab_strip_model) { |
311 int index = tab_strip_model->GetIndexOfWebContents(tab); | 313 int index = tab_strip_model->GetIndexOfWebContents(tab); |
312 if (index != TabStripModel::kNoTab) { | 314 if (index != TabStripModel::kNoTab) { |
(...skipping 27 matching lines...) Expand all Loading... |
340 content::WebUIDataSource::Add(profile, CreateWebUIDataSource()); | 342 content::WebUIDataSource::Add(profile, CreateWebUIDataSource()); |
341 | 343 |
342 web_ui->AddMessageHandler(new InlineLoginUIHandler(profile)); | 344 web_ui->AddMessageHandler(new InlineLoginUIHandler(profile)); |
343 // Required for intercepting extension function calls when the page is loaded | 345 // Required for intercepting extension function calls when the page is loaded |
344 // in a bubble (not a full tab, thus tab helpers are not registered | 346 // in a bubble (not a full tab, thus tab helpers are not registered |
345 // automatically). | 347 // automatically). |
346 extensions::TabHelper::CreateForWebContents(web_ui->GetWebContents()); | 348 extensions::TabHelper::CreateForWebContents(web_ui->GetWebContents()); |
347 } | 349 } |
348 | 350 |
349 InlineLoginUI::~InlineLoginUI() {} | 351 InlineLoginUI::~InlineLoginUI() {} |
OLD | NEW |