| 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/inline_login_ui.h" | |
| 6 | |
| 7 #include "base/atomic_sequence_num.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/browser_process.h" | |
| 15 #include "chrome/browser/extensions/tab_helper.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/signin/profile_oauth2_token_service.h" | |
| 18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 19 #include "chrome/browser/signin/signin_global_error.h" | |
| 20 #include "chrome/browser/signin/signin_names_io_thread.h" | |
| 21 #include "chrome/browser/signin/signin_oauth_helper.h" | |
| 22 #include "chrome/browser/signin/signin_promo.h" | |
| 23 #include "chrome/browser/sync/profile_sync_service.h" | |
| 24 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 25 #include "chrome/browser/ui/browser_finder.h" | |
| 26 #include "chrome/browser/ui/sync/one_click_signin_helper.h" | |
| 27 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" | |
| 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 29 #include "chrome/common/url_constants.h" | |
| 30 #include "content/public/browser/storage_partition.h" | |
| 31 #include "content/public/browser/web_contents.h" | |
| 32 #include "content/public/browser/web_ui.h" | |
| 33 #include "content/public/browser/web_ui_data_source.h" | |
| 34 #include "content/public/browser/web_ui_message_handler.h" | |
| 35 #include "google_apis/gaia/gaia_auth_consumer.h" | |
| 36 #include "google_apis/gaia/gaia_auth_fetcher.h" | |
| 37 #include "google_apis/gaia/gaia_constants.h" | |
| 38 #include "google_apis/gaia/gaia_switches.h" | |
| 39 #include "google_apis/gaia/gaia_urls.h" | |
| 40 #include "grit/browser_resources.h" | |
| 41 #include "net/base/escape.h" | |
| 42 #include "net/base/url_util.h" | |
| 43 | |
| 44 #if defined(OS_CHROMEOS) | |
| 45 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" | |
| 46 #endif | |
| 47 | |
| 48 namespace { | |
| 49 | |
| 50 content::WebUIDataSource* CreateWebUIDataSource() { | |
| 51 content::WebUIDataSource* source = | |
| 52 content::WebUIDataSource::Create(chrome::kChromeUIChromeSigninHost); | |
| 53 source->SetUseJsonJSFormatV2(); | |
| 54 source->SetJsonPath("strings.js"); | |
| 55 | |
| 56 source->SetDefaultResource(IDR_INLINE_LOGIN_HTML); | |
| 57 source->AddResourcePath("inline_login.css", IDR_INLINE_LOGIN_CSS); | |
| 58 source->AddResourcePath("inline_login.js", IDR_INLINE_LOGIN_JS); | |
| 59 return source; | |
| 60 }; | |
| 61 | |
| 62 #if defined(OS_CHROMEOS) | |
| 63 class InlineLoginUIOAuth2Delegate | |
| 64 : public chromeos::OAuth2TokenFetcher::Delegate { | |
| 65 public: | |
| 66 explicit InlineLoginUIOAuth2Delegate(content::WebUI* web_ui) | |
| 67 : web_ui_(web_ui) {} | |
| 68 virtual ~InlineLoginUIOAuth2Delegate() {} | |
| 69 | |
| 70 // OAuth2TokenFetcher::Delegate overrides: | |
| 71 virtual void OnOAuth2TokensAvailable( | |
| 72 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE { | |
| 73 // Closes sign-in dialog before update token service. Token service update | |
| 74 // might trigger a permission dialog and if this dialog does not close, | |
| 75 // a DCHECK would be triggered because attempting to activate a window | |
| 76 // while there is a modal dialog. | |
| 77 web_ui_->CallJavascriptFunction("inline.login.closeDialog"); | |
| 78 | |
| 79 Profile* profile = Profile::FromWebUI(web_ui_); | |
| 80 ProfileOAuth2TokenService* token_service = | |
| 81 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
| 82 token_service->UpdateCredentials(token_service->GetPrimaryAccountId(), | |
| 83 oauth2_tokens.refresh_token); | |
| 84 } | |
| 85 | |
| 86 virtual void OnOAuth2TokensFetchFailed() OVERRIDE { | |
| 87 LOG(ERROR) << "Failed to fetch oauth2 token with inline login."; | |
| 88 web_ui_->CallJavascriptFunction("inline.login.handleOAuth2TokenFailure"); | |
| 89 } | |
| 90 | |
| 91 private: | |
| 92 content::WebUI* web_ui_; | |
| 93 }; | |
| 94 #else | |
| 95 // Global SequenceNumber used for generating unique webview partition IDs. | |
| 96 base::StaticAtomicSequenceNumber next_partition_id; | |
| 97 #endif // OS_CHROMEOS | |
| 98 | |
| 99 class InlineLoginUIHandler : public GaiaAuthConsumer, | |
| 100 public content::WebUIMessageHandler { | |
| 101 public: | |
| 102 explicit InlineLoginUIHandler(Profile* profile) | |
| 103 : profile_(profile), weak_factory_(this), choose_what_to_sync_(false), | |
| 104 partition_id_("") {} | |
| 105 virtual ~InlineLoginUIHandler() {} | |
| 106 | |
| 107 // content::WebUIMessageHandler overrides: | |
| 108 virtual void RegisterMessages() OVERRIDE { | |
| 109 web_ui()->RegisterMessageCallback("initialize", | |
| 110 base::Bind(&InlineLoginUIHandler::HandleInitialize, | |
| 111 base::Unretained(this))); | |
| 112 web_ui()->RegisterMessageCallback("completeLogin", | |
| 113 base::Bind(&InlineLoginUIHandler::HandleCompleteLogin, | |
| 114 base::Unretained(this))); | |
| 115 web_ui()->RegisterMessageCallback("switchToFullTab", | |
| 116 base::Bind(&InlineLoginUIHandler::HandleSwitchToFullTab, | |
| 117 base::Unretained(this))); | |
| 118 } | |
| 119 | |
| 120 private: | |
| 121 // Enum for gaia auth mode, must match AuthMode defined in | |
| 122 // chrome/browser/resources/gaia_auth_host/gaia_auth_host.js. | |
| 123 enum AuthMode { | |
| 124 kDefaultAuthMode = 0, | |
| 125 kOfflineAuthMode = 1, | |
| 126 kInlineAuthMode = 2 | |
| 127 }; | |
| 128 | |
| 129 void LoadAuthExtension() { | |
| 130 base::DictionaryValue params; | |
| 131 | |
| 132 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | |
| 133 params.SetString("hl", app_locale); | |
| 134 | |
| 135 GaiaUrls* gaiaUrls = GaiaUrls::GetInstance(); | |
| 136 params.SetString("gaiaUrl", gaiaUrls->gaia_url().spec()); | |
| 137 | |
| 138 | |
| 139 #if defined(OS_CHROMEOS) | |
| 140 params.SetInteger("authMode", kDefaultAuthMode); | |
| 141 #else | |
| 142 params.SetInteger("authMode", kInlineAuthMode); | |
| 143 | |
| 144 const GURL& current_url = web_ui()->GetWebContents()->GetURL(); | |
| 145 signin::Source source = signin::GetSourceForPromoURL(current_url); | |
| 146 DCHECK(source != signin::SOURCE_UNKNOWN); | |
| 147 if (source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT || | |
| 148 source == signin::SOURCE_AVATAR_BUBBLE_SIGN_IN) { | |
| 149 // Drop the leading slash in the path. | |
| 150 params.SetString("gaiaPath", | |
| 151 gaiaUrls->embedded_signin_url().path().substr(1)); | |
| 152 } | |
| 153 | |
| 154 params.SetString("service", "chromiumsync"); | |
| 155 params.SetString("continueUrl", | |
| 156 signin::GetLandingURL("source", static_cast<int>(source)).spec()); | |
| 157 | |
| 158 std::string email; | |
| 159 net::GetValueForKeyInQuery(current_url, "Email", &email); | |
| 160 if (!email.empty()) | |
| 161 params.SetString("email", email); | |
| 162 | |
| 163 std::string frame_url; | |
| 164 net::GetValueForKeyInQuery(current_url, "frameUrl", &frame_url); | |
| 165 if (!frame_url.empty()) | |
| 166 params.SetString("frameUrl", frame_url); | |
| 167 | |
| 168 std::string is_constrained; | |
| 169 net::GetValueForKeyInQuery(current_url, "constrained", &is_constrained); | |
| 170 if (!is_constrained.empty()) | |
| 171 params.SetString("constrained", is_constrained); | |
| 172 | |
| 173 net::GetValueForKeyInQuery(current_url, "partitionId", &partition_id_); | |
| 174 if (partition_id_.empty()) { | |
| 175 partition_id_ = | |
| 176 "gaia-webview-" + base::IntToString(next_partition_id.GetNext()); | |
| 177 } | |
| 178 params.SetString("partitionId", partition_id_); | |
| 179 #endif // OS_CHROMEOS | |
| 180 | |
| 181 web_ui()->CallJavascriptFunction("inline.login.loadAuthExtension", params); | |
| 182 } | |
| 183 | |
| 184 // JS callback: | |
| 185 void HandleInitialize(const base::ListValue* args) { | |
| 186 LoadAuthExtension(); | |
| 187 } | |
| 188 | |
| 189 // JS callback: | |
| 190 void HandleSwitchToFullTab(const base::ListValue* args) { | |
| 191 base::string16 url_str; | |
| 192 CHECK(args->GetString(0, &url_str)); | |
| 193 | |
| 194 content::WebContents* web_contents = web_ui()->GetWebContents(); | |
| 195 GURL main_frame_url(web_contents->GetURL()); | |
| 196 main_frame_url = net::AppendOrReplaceQueryParameter( | |
| 197 main_frame_url, "frameUrl", UTF16ToASCII(url_str)); | |
| 198 main_frame_url = net::AppendOrReplaceQueryParameter( | |
| 199 main_frame_url, "partitionId", partition_id_); | |
| 200 chrome::NavigateParams params( | |
| 201 profile_, | |
| 202 net::AppendOrReplaceQueryParameter(main_frame_url, "constrained", "0"), | |
| 203 content::PAGE_TRANSITION_AUTO_TOPLEVEL); | |
| 204 chrome::Navigate(¶ms); | |
| 205 | |
| 206 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); | |
| 207 } | |
| 208 | |
| 209 void HandleCompleteLogin(const base::ListValue* args) { | |
| 210 // TODO(guohui, xiyuan): we should investigate if it is possible to unify | |
| 211 // the signin-with-cookies flow across ChromeOS and Chrome. | |
| 212 DCHECK(email_.empty() && password_.empty()); | |
| 213 | |
| 214 #if defined(OS_CHROMEOS) | |
| 215 oauth2_delegate_.reset(new InlineLoginUIOAuth2Delegate(web_ui())); | |
| 216 oauth2_token_fetcher_.reset(new chromeos::OAuth2TokenFetcher( | |
| 217 oauth2_delegate_.get(), profile_->GetRequestContext())); | |
| 218 oauth2_token_fetcher_->StartExchangeFromCookies(); | |
| 219 #else | |
| 220 const base::DictionaryValue* dict = NULL; | |
| 221 base::string16 email; | |
| 222 if (!args->GetDictionary(0, &dict) || !dict || | |
| 223 !dict->GetString("email", &email)) { | |
| 224 // User cancelled the signin by clicking 'skip for now'. | |
| 225 bool skip_for_now = false; | |
| 226 DCHECK(dict->GetBoolean("skipForNow", &skip_for_now) && skip_for_now); | |
| 227 | |
| 228 signin::SetUserSkippedPromo(profile_); | |
| 229 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); | |
| 230 return; | |
| 231 } | |
| 232 | |
| 233 email_ = UTF16ToASCII(email); | |
| 234 base::string16 password; | |
| 235 dict->GetString("password", &password); | |
| 236 password_ = UTF16ToASCII(password); | |
| 237 | |
| 238 dict->GetBoolean("chooseWhatToSync", &choose_what_to_sync_); | |
| 239 | |
| 240 content::WebContents* contents = web_ui()->GetWebContents(); | |
| 241 signin::Source source = signin::GetSourceForPromoURL(contents->GetURL()); | |
| 242 OneClickSigninHelper::CanOfferFor can_offer = | |
| 243 source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT ? | |
| 244 OneClickSigninHelper::CAN_OFFER_FOR_SECONDARY_ACCOUNT : | |
| 245 OneClickSigninHelper::CAN_OFFER_FOR_ALL; | |
| 246 std::string error_msg; | |
| 247 OneClickSigninHelper::CanOffer( | |
| 248 contents, can_offer, email_, &error_msg); | |
| 249 if (!error_msg.empty()) { | |
| 250 HandleLoginError(error_msg); | |
| 251 return; | |
| 252 } | |
| 253 | |
| 254 content::StoragePartition* partition = | |
| 255 content::BrowserContext::GetStoragePartitionForSite( | |
| 256 contents->GetBrowserContext(), | |
| 257 GURL("chrome-guest://mfffpogegjflfpflabcdkioaeobkgjik/?" + | |
| 258 partition_id_)); | |
| 259 | |
| 260 auth_fetcher_.reset(new GaiaAuthFetcher(this, | |
| 261 GaiaConstants::kChromeSource, | |
| 262 partition->GetURLRequestContext())); | |
| 263 auth_fetcher_->StartCookieForOAuthCodeExchange("0"); | |
| 264 #endif // OS_CHROMEOS | |
| 265 } | |
| 266 | |
| 267 // GaiaAuthConsumer override. | |
| 268 virtual void OnClientOAuthCodeSuccess( | |
| 269 const std::string& oauth_code) OVERRIDE { | |
| 270 #if !defined(OS_CHROMEOS) | |
| 271 DCHECK(!oauth_code.empty()); | |
| 272 | |
| 273 content::WebContents* contents = web_ui()->GetWebContents(); | |
| 274 ProfileSyncService* sync_service = | |
| 275 ProfileSyncServiceFactory::GetForProfile(profile_); | |
| 276 const GURL& current_url = contents->GetURL(); | |
| 277 signin::Source source = signin::GetSourceForPromoURL(current_url); | |
| 278 | |
| 279 if (source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT) { | |
| 280 // SigninOAuthHelper will delete itself. | |
| 281 SigninOAuthHelper* helper = new SigninOAuthHelper(profile_); | |
| 282 helper->StartAddingAccount(oauth_code); | |
| 283 } else { | |
| 284 OneClickSigninSyncStarter::StartSyncMode start_mode = | |
| 285 source == signin::SOURCE_SETTINGS || choose_what_to_sync_ ? | |
| 286 (SigninGlobalError::GetForProfile(profile_)->HasMenuItem() && | |
| 287 sync_service && sync_service->HasSyncSetupCompleted()) ? | |
| 288 OneClickSigninSyncStarter::SHOW_SETTINGS_WITHOUT_CONFIGURE : | |
| 289 OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST : | |
| 290 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS; | |
| 291 OneClickSigninSyncStarter::ConfirmationRequired confirmation_required = | |
| 292 source == signin::SOURCE_SETTINGS || | |
| 293 source == signin::SOURCE_WEBSTORE_INSTALL || | |
| 294 choose_what_to_sync_? | |
| 295 OneClickSigninSyncStarter::NO_CONFIRMATION : | |
| 296 OneClickSigninSyncStarter::CONFIRM_AFTER_SIGNIN; | |
| 297 OneClickSigninSyncStarter::Callback sync_callback = base::Bind( | |
| 298 &InlineLoginUIHandler::SyncStarterCallback, | |
| 299 weak_factory_.GetWeakPtr()); | |
| 300 | |
| 301 bool cross_account_error_handled = | |
| 302 OneClickSigninHelper::HandleCrossAccountError( | |
| 303 contents, "" /* session_index, not used */, | |
| 304 email_, password_, oauth_code, | |
| 305 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT, | |
| 306 source, start_mode, sync_callback); | |
| 307 | |
| 308 if (!cross_account_error_handled) { | |
| 309 // Call OneClickSigninSyncStarter to exchange oauth code for tokens. | |
| 310 // OneClickSigninSyncStarter will delete itself once the job is done. | |
| 311 new OneClickSigninSyncStarter( | |
| 312 profile_, NULL, "" /* session_index, not used */, | |
| 313 email_, password_, oauth_code, | |
| 314 start_mode, | |
| 315 contents, | |
| 316 confirmation_required, | |
| 317 sync_callback); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 email_.clear(); | |
| 322 password_.clear(); | |
| 323 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); | |
| 324 #endif // OS_CHROMEOS | |
| 325 } | |
| 326 | |
| 327 // GaiaAuthConsumer override. | |
| 328 virtual void OnClientOAuthCodeFailure(const GoogleServiceAuthError& error) | |
| 329 OVERRIDE { | |
| 330 #if !defined(OS_CHROMEOS) | |
| 331 LOG(ERROR) << "InlineLoginUI::OnClientOAuthCodeFailure"; | |
| 332 HandleLoginError(error.ToString()); | |
| 333 #endif // OS_CHROMEOS | |
| 334 } | |
| 335 | |
| 336 void HandleLoginError(const std::string& error_msg) { | |
| 337 SyncStarterCallback( | |
| 338 OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); | |
| 339 | |
| 340 Browser* browser = chrome::FindBrowserWithWebContents( | |
| 341 web_ui()->GetWebContents()); | |
| 342 if (!browser) { | |
| 343 browser = chrome::FindLastActiveWithProfile( | |
| 344 profile_, chrome::GetActiveDesktop()); | |
| 345 } | |
| 346 if (browser) | |
| 347 OneClickSigninHelper::ShowSigninErrorBubble(browser, error_msg); | |
| 348 | |
| 349 email_.clear(); | |
| 350 password_.clear(); | |
| 351 } | |
| 352 | |
| 353 void SyncStarterCallback(OneClickSigninSyncStarter::SyncSetupResult result) { | |
| 354 content::WebContents* contents = web_ui()->GetWebContents(); | |
| 355 const GURL& current_url = contents->GetURL(); | |
| 356 | |
| 357 if (signin::IsAutoCloseEnabledInURL(current_url)) { | |
| 358 base::MessageLoop::current()->PostTask( | |
| 359 FROM_HERE, | |
| 360 base::Bind( | |
| 361 &InlineLoginUIHandler::CloseTab, weak_factory_.GetWeakPtr())); | |
| 362 } else { | |
| 363 signin::Source source = signin::GetSourceForPromoURL(current_url); | |
| 364 DCHECK(source != signin::SOURCE_UNKNOWN); | |
| 365 OneClickSigninHelper::RedirectToNtpOrAppsPageIfNecessary( | |
| 366 contents, source); | |
| 367 } | |
| 368 } | |
| 369 | |
| 370 void CloseTab() { | |
| 371 content::WebContents* tab = web_ui()->GetWebContents(); | |
| 372 Browser* browser = chrome::FindBrowserWithWebContents(tab); | |
| 373 if (browser) { | |
| 374 TabStripModel* tab_strip_model = browser->tab_strip_model(); | |
| 375 if (tab_strip_model) { | |
| 376 int index = tab_strip_model->GetIndexOfWebContents(tab); | |
| 377 if (index != TabStripModel::kNoTab) { | |
| 378 tab_strip_model->ExecuteContextMenuCommand( | |
| 379 index, TabStripModel::CommandCloseTab); | |
| 380 } | |
| 381 } | |
| 382 } | |
| 383 } | |
| 384 | |
| 385 Profile* profile_; | |
| 386 base::WeakPtrFactory<InlineLoginUIHandler> weak_factory_; | |
| 387 scoped_ptr<GaiaAuthFetcher> auth_fetcher_; | |
| 388 std::string email_; | |
| 389 std::string password_; | |
| 390 bool choose_what_to_sync_; | |
| 391 // Partition id for the gaia webview; | |
| 392 std::string partition_id_; | |
| 393 | |
| 394 #if defined(OS_CHROMEOS) | |
| 395 scoped_ptr<chromeos::OAuth2TokenFetcher> oauth2_token_fetcher_; | |
| 396 scoped_ptr<InlineLoginUIOAuth2Delegate> oauth2_delegate_; | |
| 397 #endif | |
| 398 | |
| 399 DISALLOW_COPY_AND_ASSIGN(InlineLoginUIHandler); | |
| 400 }; | |
| 401 | |
| 402 } // namespace | |
| 403 | |
| 404 InlineLoginUI::InlineLoginUI(content::WebUI* web_ui) | |
| 405 : WebDialogUI(web_ui), | |
| 406 auth_extension_(Profile::FromWebUI(web_ui)) { | |
| 407 Profile* profile = Profile::FromWebUI(web_ui); | |
| 408 content::WebUIDataSource::Add(profile, CreateWebUIDataSource()); | |
| 409 | |
| 410 web_ui->AddMessageHandler(new InlineLoginUIHandler(profile)); | |
| 411 // Required for intercepting extension function calls when the page is loaded | |
| 412 // in a bubble (not a full tab, thus tab helpers are not registered | |
| 413 // automatically). | |
| 414 extensions::TabHelper::CreateForWebContents(web_ui->GetWebContents()); | |
| 415 } | |
| 416 | |
| 417 InlineLoginUI::~InlineLoginUI() {} | |
| OLD | NEW |