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"); |
| 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 OneClickSigninSyncStarter::Callback sync_callback = base::Bind( |
| 190 &InlineLoginHandlerImpl::SyncStarterCallback, |
| 191 weak_factory_.GetWeakPtr()); |
| 192 |
| 193 bool cross_account_error_handled = |
| 194 OneClickSigninHelper::HandleCrossAccountError( |
| 195 contents, "" /* session_index, not used */, |
| 196 email_, password_, oauth_code, |
| 197 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT, |
| 198 source, start_mode, sync_callback); |
| 199 |
| 200 if (!cross_account_error_handled) { |
| 201 // Call OneClickSigninSyncStarter to exchange oauth code for tokens. |
| 202 // OneClickSigninSyncStarter will delete itself once the job is done. |
| 203 new OneClickSigninSyncStarter( |
| 204 profile, NULL, "" /* session_index, not used */, |
| 205 email_, password_, oauth_code, |
| 206 start_mode, |
| 207 contents, |
| 208 confirmation_required, |
| 209 sync_callback); |
| 210 } |
| 211 } |
| 212 |
| 213 email_.clear(); |
| 214 password_.clear(); |
| 215 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); |
| 216 } |
| 217 |
| 218 void InlineLoginHandlerImpl::OnClientOAuthCodeFailure( |
| 219 const GoogleServiceAuthError& error) { |
| 220 LOG(ERROR) << "InlineLoginUI::OnClientOAuthCodeFailure"; |
| 221 HandleLoginError(error.ToString()); |
| 222 } |
| 223 |
| 224 void InlineLoginHandlerImpl::HandleLoginError(const std::string& error_msg) { |
| 225 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); |
| 226 |
| 227 Browser* browser = chrome::FindBrowserWithWebContents( |
| 228 web_ui()->GetWebContents()); |
| 229 if (!browser) { |
| 230 browser = chrome::FindLastActiveWithProfile( |
| 231 Profile::FromWebUI(web_ui()), chrome::GetActiveDesktop()); |
| 232 } |
| 233 if (browser) |
| 234 OneClickSigninHelper::ShowSigninErrorBubble(browser, error_msg); |
| 235 |
| 236 email_.clear(); |
| 237 password_.clear(); |
| 238 } |
| 239 |
| 240 void InlineLoginHandlerImpl::SyncStarterCallback( |
| 241 OneClickSigninSyncStarter::SyncSetupResult result) { |
| 242 content::WebContents* contents = web_ui()->GetWebContents(); |
| 243 const GURL& current_url = contents->GetURL(); |
| 244 bool auto_close = signin::IsAutoCloseEnabledInURL(current_url); |
| 245 if (auto_close) { |
| 246 base::MessageLoop::current()->PostTask( |
| 247 FROM_HERE, |
| 248 base::Bind(&InlineLoginHandlerImpl::CloseTab, |
| 249 weak_factory_.GetWeakPtr())); |
| 250 } else { |
| 251 signin::Source source = signin::GetSourceForPromoURL(current_url); |
| 252 DCHECK(source != signin::SOURCE_UNKNOWN); |
| 253 OneClickSigninHelper::RedirectToNtpOrAppsPageIfNecessary(contents, source); |
| 254 } |
| 255 } |
| 256 |
| 257 void InlineLoginHandlerImpl::CloseTab() { |
| 258 content::WebContents* tab = web_ui()->GetWebContents(); |
| 259 Browser* browser = chrome::FindBrowserWithWebContents(tab); |
| 260 if (browser) { |
| 261 TabStripModel* tab_strip_model = browser->tab_strip_model(); |
| 262 if (tab_strip_model) { |
| 263 int index = tab_strip_model->GetIndexOfWebContents(tab); |
| 264 if (index != TabStripModel::kNoTab) { |
| 265 tab_strip_model->ExecuteContextMenuCommand( |
| 266 index, TabStripModel::CommandCloseTab); |
| 267 } |
| 268 } |
| 269 } |
| 270 } |
OLD | NEW |