| 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/signin/inline_login_handler_impl.h" | 5 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h" |
| 6 | 6 |
| 7 #include "base/atomic_sequence_num.h" | |
| 8 #include "base/bind.h" | 7 #include "base/bind.h" |
| 9 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 12 #include "base/values.h" | 11 #include "base/values.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/signin/signin_global_error.h" | 13 #include "chrome/browser/signin/signin_global_error.h" |
| 15 #include "chrome/browser/signin/signin_oauth_helper.h" | 14 #include "chrome/browser/signin/signin_oauth_helper.h" |
| 16 #include "chrome/browser/signin/signin_promo.h" | 15 #include "chrome/browser/signin/signin_promo.h" |
| 17 #include "chrome/browser/sync/profile_sync_service.h" | 16 #include "chrome/browser/sync/profile_sync_service.h" |
| 18 #include "chrome/browser/sync/profile_sync_service_factory.h" | 17 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 19 #include "chrome/browser/ui/browser_finder.h" | 18 #include "chrome/browser/ui/browser_finder.h" |
| 20 #include "chrome/browser/ui/sync/one_click_signin_helper.h" | 19 #include "chrome/browser/ui/sync/one_click_signin_helper.h" |
| 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 22 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/common/url_constants.h" |
| 23 #include "content/public/browser/storage_partition.h" | 23 #include "content/public/browser/storage_partition.h" |
| 24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/browser/web_ui.h" | 25 #include "content/public/browser/web_ui.h" |
| 26 #include "google_apis/gaia/gaia_auth_fetcher.h" | 26 #include "google_apis/gaia/gaia_auth_fetcher.h" |
| 27 #include "google_apis/gaia/gaia_constants.h" | 27 #include "google_apis/gaia/gaia_constants.h" |
| 28 #include "google_apis/gaia/gaia_urls.h" | 28 #include "google_apis/gaia/gaia_urls.h" |
| 29 #include "net/base/url_util.h" | 29 #include "net/base/url_util.h" |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 // Global SequenceNumber used for generating unique webview partition IDs. | |
| 34 base::StaticAtomicSequenceNumber next_partition_id; | |
| 35 | |
| 36 } // empty namespace | 33 } // empty namespace |
| 37 | 34 |
| 38 InlineLoginHandlerImpl::InlineLoginHandlerImpl() | 35 InlineLoginHandlerImpl::InlineLoginHandlerImpl() |
| 39 : weak_factory_(this), choose_what_to_sync_(false), partition_id_("") { | 36 : weak_factory_(this), choose_what_to_sync_(false) { |
| 40 } | 37 } |
| 41 | 38 |
| 42 InlineLoginHandlerImpl::~InlineLoginHandlerImpl() {} | 39 InlineLoginHandlerImpl::~InlineLoginHandlerImpl() {} |
| 43 | 40 |
| 44 void InlineLoginHandlerImpl::RegisterMessages() { | 41 void InlineLoginHandlerImpl::RegisterMessages() { |
| 45 InlineLoginHandler::RegisterMessages(); | 42 InlineLoginHandler::RegisterMessages(); |
| 46 | 43 |
| 47 web_ui()->RegisterMessageCallback("switchToFullTab", | 44 web_ui()->RegisterMessageCallback("switchToFullTab", |
| 48 base::Bind(&InlineLoginHandlerImpl::HandleSwitchToFullTabMessage, | 45 base::Bind(&InlineLoginHandlerImpl::HandleSwitchToFullTabMessage, |
| 49 base::Unretained(this))); | 46 base::Unretained(this))); |
| 50 } | 47 } |
| 51 | 48 |
| 52 void InlineLoginHandlerImpl::SetExtraInitParams(base::DictionaryValue& params) { | 49 void InlineLoginHandlerImpl::SetExtraInitParams(base::DictionaryValue& params) { |
| 53 params.SetInteger("authMode", InlineLoginHandler::kInlineAuthMode); | 50 params.SetInteger("authMode", InlineLoginHandler::kDesktopAuthMode); |
| 54 | 51 |
| 55 const GURL& current_url = web_ui()->GetWebContents()->GetURL(); | 52 const GURL& current_url = web_ui()->GetWebContents()->GetURL(); |
| 56 signin::Source source = signin::GetSourceForPromoURL(current_url); | 53 signin::Source source = signin::GetSourceForPromoURL(current_url); |
| 57 DCHECK(source != signin::SOURCE_UNKNOWN); | 54 DCHECK(source != signin::SOURCE_UNKNOWN); |
| 58 if (source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT || | 55 if (source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT || |
| 59 source == signin::SOURCE_AVATAR_BUBBLE_SIGN_IN) { | 56 source == signin::SOURCE_AVATAR_BUBBLE_SIGN_IN) { |
| 60 // Drop the leading slash in the path. | 57 // Drop the leading slash in the path. |
| 61 params.SetString("gaiaPath", | 58 params.SetString("gaiaPath", |
| 62 GaiaUrls::GetInstance()->embedded_signin_url().path().substr(1)); | 59 GaiaUrls::GetInstance()->embedded_signin_url().path().substr(1)); |
| 63 } | 60 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 85 std::string is_constrained; | 82 std::string is_constrained; |
| 86 net::GetValueForKeyInQuery(current_url, "constrained", &is_constrained); | 83 net::GetValueForKeyInQuery(current_url, "constrained", &is_constrained); |
| 87 if (!is_constrained.empty()) | 84 if (!is_constrained.empty()) |
| 88 params.SetString("constrained", is_constrained); | 85 params.SetString("constrained", is_constrained); |
| 89 | 86 |
| 90 // TODO(rogerta): this needs to be passed on to gaia somehow. | 87 // TODO(rogerta): this needs to be passed on to gaia somehow. |
| 91 std::string read_only_email; | 88 std::string read_only_email; |
| 92 net::GetValueForKeyInQuery(current_url, "readOnlyEmail", &read_only_email); | 89 net::GetValueForKeyInQuery(current_url, "readOnlyEmail", &read_only_email); |
| 93 if (!read_only_email.empty()) | 90 if (!read_only_email.empty()) |
| 94 params.SetString("readOnlyEmail", read_only_email); | 91 params.SetString("readOnlyEmail", read_only_email); |
| 95 | |
| 96 net::GetValueForKeyInQuery(current_url, "partitionId", &partition_id_); | |
| 97 if (partition_id_.empty()) { | |
| 98 partition_id_ = | |
| 99 "gaia-webview-" + base::IntToString(next_partition_id.GetNext()); | |
| 100 } | |
| 101 params.SetString("partitionId", partition_id_); | |
| 102 } | 92 } |
| 103 | 93 |
| 104 | 94 |
| 105 void InlineLoginHandlerImpl::HandleSwitchToFullTabMessage( | 95 void InlineLoginHandlerImpl::HandleSwitchToFullTabMessage( |
| 106 const base::ListValue* args) { | 96 const base::ListValue* args) { |
| 107 base::string16 url_str; | 97 base::string16 url_str; |
| 108 CHECK(args->GetString(0, &url_str)); | 98 CHECK(args->GetString(0, &url_str)); |
| 109 | 99 |
| 110 content::WebContents* web_contents = web_ui()->GetWebContents(); | 100 content::WebContents* web_contents = web_ui()->GetWebContents(); |
| 111 GURL main_frame_url(web_contents->GetURL()); | 101 GURL main_frame_url(web_contents->GetURL()); |
| 112 main_frame_url = net::AppendOrReplaceQueryParameter( | 102 main_frame_url = net::AppendOrReplaceQueryParameter( |
| 113 main_frame_url, "frameUrl", UTF16ToASCII(url_str)); | 103 main_frame_url, "frameUrl", UTF16ToASCII(url_str)); |
| 114 main_frame_url = net::AppendOrReplaceQueryParameter( | |
| 115 main_frame_url, "partitionId", partition_id_); | |
| 116 chrome::NavigateParams params( | 104 chrome::NavigateParams params( |
| 117 Profile::FromWebUI(web_ui()), | 105 Profile::FromWebUI(web_ui()), |
| 118 net::AppendOrReplaceQueryParameter(main_frame_url, "constrained", "0"), | 106 net::AppendOrReplaceQueryParameter(main_frame_url, "constrained", "0"), |
| 119 content::PAGE_TRANSITION_AUTO_TOPLEVEL); | 107 content::PAGE_TRANSITION_AUTO_TOPLEVEL); |
| 120 chrome::Navigate(¶ms); | 108 chrome::Navigate(¶ms); |
| 121 | 109 |
| 122 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); | 110 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); |
| 123 } | 111 } |
| 124 | 112 |
| 125 void InlineLoginHandlerImpl::CompleteLogin(const base::ListValue* args) { | 113 void InlineLoginHandlerImpl::CompleteLogin(const base::ListValue* args) { |
| 126 DCHECK(email_.empty() && password_.empty()); | 114 DCHECK(email_.empty() && password_.empty() && session_index_.empty()); |
| 127 | 115 |
| 128 content::WebContents* contents = web_ui()->GetWebContents(); | 116 content::WebContents* contents = web_ui()->GetWebContents(); |
| 129 const GURL& current_url = contents->GetURL(); | 117 const GURL& current_url = contents->GetURL(); |
| 130 | 118 |
| 131 const base::DictionaryValue* dict = NULL; | 119 const base::DictionaryValue* dict = NULL; |
| 132 base::string16 email; | 120 base::string16 email; |
| 133 if (!args->GetDictionary(0, &dict) || !dict || | 121 if (!args->GetDictionary(0, &dict) || !dict || |
| 134 !dict->GetString("email", &email)) { | 122 !dict->GetString("email", &email)) { |
| 135 // User cancelled the signin by clicking 'skip for now'. | 123 // User cancelled the signin by clicking 'skip for now'. |
| 136 bool skip_for_now = false; | 124 bool skip_for_now = false; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 155 if (net::GetValueForKeyInQuery(current_url, "email", &default_email) && | 143 if (net::GetValueForKeyInQuery(current_url, "email", &default_email) && |
| 156 net::GetValueForKeyInQuery(current_url, "validateEmail", | 144 net::GetValueForKeyInQuery(current_url, "validateEmail", |
| 157 &validate_email) && | 145 &validate_email) && |
| 158 validate_email == "1") { | 146 validate_email == "1") { |
| 159 if (email_ != default_email) { | 147 if (email_ != default_email) { |
| 160 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); | 148 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); |
| 161 return; | 149 return; |
| 162 } | 150 } |
| 163 } | 151 } |
| 164 | 152 |
| 153 base::string16 session_index; |
| 154 dict->GetString("sessionIndex", &session_index); |
| 155 session_index_ = UTF16ToASCII(session_index); |
| 156 DCHECK(!session_index_.empty()); |
| 165 dict->GetBoolean("chooseWhatToSync", &choose_what_to_sync_); | 157 dict->GetBoolean("chooseWhatToSync", &choose_what_to_sync_); |
| 166 | 158 |
| 167 signin::Source source = signin::GetSourceForPromoURL(current_url); | 159 signin::Source source = signin::GetSourceForPromoURL(current_url); |
| 168 OneClickSigninHelper::CanOfferFor can_offer = | 160 OneClickSigninHelper::CanOfferFor can_offer = |
| 169 source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT ? | 161 source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT ? |
| 170 OneClickSigninHelper::CAN_OFFER_FOR_SECONDARY_ACCOUNT : | 162 OneClickSigninHelper::CAN_OFFER_FOR_SECONDARY_ACCOUNT : |
| 171 OneClickSigninHelper::CAN_OFFER_FOR_ALL; | 163 OneClickSigninHelper::CAN_OFFER_FOR_ALL; |
| 172 std::string error_msg; | 164 std::string error_msg; |
| 173 OneClickSigninHelper::CanOffer( | 165 OneClickSigninHelper::CanOffer( |
| 174 contents, can_offer, email_, &error_msg); | 166 contents, can_offer, email_, &error_msg); |
| 175 if (!error_msg.empty()) { | 167 if (!error_msg.empty()) { |
| 176 HandleLoginError(error_msg); | 168 HandleLoginError(error_msg); |
| 177 return; | 169 return; |
| 178 } | 170 } |
| 179 | 171 |
| 180 content::StoragePartition* partition = | 172 content::StoragePartition* partition = |
| 181 content::BrowserContext::GetStoragePartitionForSite( | 173 content::BrowserContext::GetStoragePartitionForSite( |
| 182 contents->GetBrowserContext(), | 174 contents->GetBrowserContext(), |
| 183 GURL("chrome-guest://mfffpogegjflfpflabcdkioaeobkgjik/?" + | 175 GURL(chrome::kChromeUIChromeSigninURL)); |
| 184 partition_id_)); | |
| 185 | 176 |
| 186 auth_fetcher_.reset(new GaiaAuthFetcher(this, | 177 auth_fetcher_.reset(new GaiaAuthFetcher(this, |
| 187 GaiaConstants::kChromeSource, | 178 GaiaConstants::kChromeSource, |
| 188 partition->GetURLRequestContext())); | 179 partition->GetURLRequestContext())); |
| 189 auth_fetcher_->StartCookieForOAuthCodeExchange("0"); | 180 auth_fetcher_->StartCookieForOAuthCodeExchange(session_index_); |
| 190 } | 181 } |
| 191 | 182 |
| 192 void InlineLoginHandlerImpl::OnClientOAuthCodeSuccess( | 183 void InlineLoginHandlerImpl::OnClientOAuthCodeSuccess( |
| 193 const std::string& oauth_code) { | 184 const std::string& oauth_code) { |
| 194 DCHECK(!oauth_code.empty()); | 185 DCHECK(!oauth_code.empty()); |
| 195 | 186 |
| 196 content::WebContents* contents = web_ui()->GetWebContents(); | 187 content::WebContents* contents = web_ui()->GetWebContents(); |
| 197 Profile* profile = Profile::FromWebUI(web_ui()); | 188 Profile* profile = Profile::FromWebUI(web_ui()); |
| 198 ProfileSyncService* sync_service = | 189 ProfileSyncService* sync_service = |
| 199 ProfileSyncServiceFactory::GetForProfile(profile); | 190 ProfileSyncServiceFactory::GetForProfile(profile); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 email_, password_, oauth_code, | 237 email_, password_, oauth_code, |
| 247 start_mode, | 238 start_mode, |
| 248 contents, | 239 contents, |
| 249 confirmation_required, | 240 confirmation_required, |
| 250 sync_callback); | 241 sync_callback); |
| 251 } | 242 } |
| 252 } | 243 } |
| 253 | 244 |
| 254 email_.clear(); | 245 email_.clear(); |
| 255 password_.clear(); | 246 password_.clear(); |
| 247 session_index_.clear(); |
| 256 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); | 248 web_ui()->CallJavascriptFunction("inline.login.closeDialog"); |
| 257 } | 249 } |
| 258 | 250 |
| 259 void InlineLoginHandlerImpl::OnClientOAuthCodeFailure( | 251 void InlineLoginHandlerImpl::OnClientOAuthCodeFailure( |
| 260 const GoogleServiceAuthError& error) { | 252 const GoogleServiceAuthError& error) { |
| 261 LOG(ERROR) << "InlineLoginUI::OnClientOAuthCodeFailure"; | 253 LOG(ERROR) << "InlineLoginUI::OnClientOAuthCodeFailure"; |
| 262 HandleLoginError(error.ToString()); | 254 HandleLoginError(error.ToString()); |
| 263 } | 255 } |
| 264 | 256 |
| 265 void InlineLoginHandlerImpl::HandleLoginError(const std::string& error_msg) { | 257 void InlineLoginHandlerImpl::HandleLoginError(const std::string& error_msg) { |
| 266 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); | 258 SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE); |
| 267 | 259 |
| 268 Browser* browser = chrome::FindBrowserWithWebContents( | 260 Browser* browser = chrome::FindBrowserWithWebContents( |
| 269 web_ui()->GetWebContents()); | 261 web_ui()->GetWebContents()); |
| 270 if (!browser) { | 262 if (!browser) { |
| 271 browser = chrome::FindLastActiveWithProfile( | 263 browser = chrome::FindLastActiveWithProfile( |
| 272 Profile::FromWebUI(web_ui()), chrome::GetActiveDesktop()); | 264 Profile::FromWebUI(web_ui()), chrome::GetActiveDesktop()); |
| 273 } | 265 } |
| 274 if (browser) | 266 if (browser) |
| 275 OneClickSigninHelper::ShowSigninErrorBubble(browser, error_msg); | 267 OneClickSigninHelper::ShowSigninErrorBubble(browser, error_msg); |
| 276 | 268 |
| 277 email_.clear(); | 269 email_.clear(); |
| 278 password_.clear(); | 270 password_.clear(); |
| 271 session_index_.clear(); |
| 279 } | 272 } |
| 280 | 273 |
| 281 void InlineLoginHandlerImpl::SyncStarterCallback( | 274 void InlineLoginHandlerImpl::SyncStarterCallback( |
| 282 OneClickSigninSyncStarter::SyncSetupResult result) { | 275 OneClickSigninSyncStarter::SyncSetupResult result) { |
| 283 content::WebContents* contents = web_ui()->GetWebContents(); | 276 content::WebContents* contents = web_ui()->GetWebContents(); |
| 284 const GURL& current_url = contents->GetURL(); | 277 const GURL& current_url = contents->GetURL(); |
| 285 bool auto_close = signin::IsAutoCloseEnabledInURL(current_url); | 278 bool auto_close = signin::IsAutoCloseEnabledInURL(current_url); |
| 286 if (auto_close) { | 279 if (auto_close) { |
| 287 base::MessageLoop::current()->PostTask( | 280 base::MessageLoop::current()->PostTask( |
| 288 FROM_HERE, | 281 FROM_HERE, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 302 TabStripModel* tab_strip_model = browser->tab_strip_model(); | 295 TabStripModel* tab_strip_model = browser->tab_strip_model(); |
| 303 if (tab_strip_model) { | 296 if (tab_strip_model) { |
| 304 int index = tab_strip_model->GetIndexOfWebContents(tab); | 297 int index = tab_strip_model->GetIndexOfWebContents(tab); |
| 305 if (index != TabStripModel::kNoTab) { | 298 if (index != TabStripModel::kNoTab) { |
| 306 tab_strip_model->ExecuteContextMenuCommand( | 299 tab_strip_model->ExecuteContextMenuCommand( |
| 307 index, TabStripModel::CommandCloseTab); | 300 index, TabStripModel::CommandCloseTab); |
| 308 } | 301 } |
| 309 } | 302 } |
| 310 } | 303 } |
| 311 } | 304 } |
| OLD | NEW |