| 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/signin/signin_header_helper.h" | 5 #include "chrome/browser/signin/signin_header_helper.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "chrome/browser/google/google_util.h" | 10 #include "chrome/browser/google/google_util.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 // Dictionary of fields in a mirror response header. | 32 // Dictionary of fields in a mirror response header. |
| 33 typedef std::map<std::string, std::string> MirrorResponseHeaderDictionary; | 33 typedef std::map<std::string, std::string> MirrorResponseHeaderDictionary; |
| 34 | 34 |
| 35 const char kChromeConnectedHeader[] = "X-Chrome-Connected"; | 35 const char kChromeConnectedHeader[] = "X-Chrome-Connected"; |
| 36 const char kChromeManageAccountsHeader[] = "X-Chrome-Manage-Accounts"; | 36 const char kChromeManageAccountsHeader[] = "X-Chrome-Manage-Accounts"; |
| 37 const char kGaiaIdAttrName[] = "id"; | 37 const char kGaiaIdAttrName[] = "id"; |
| 38 const char kProfileModeAttrName[] = "mode"; | 38 const char kProfileModeAttrName[] = "mode"; |
| 39 const char kEnableAccountConsistencyAttrName[] = "enable_account_consistency"; | 39 const char kEnableAccountConsistencyAttrName[] = "enable_account_consistency"; |
| 40 | 40 |
| 41 const char kServiceTypeAttrName[] = "action"; |
| 42 const char kEmailAttrName[] = "email"; |
| 43 const char kIsSamlAttrName[] = "is_saml"; |
| 44 const char kContinueUrlAttrName[] = "continue_url"; |
| 45 const char kIsSameTabAttrName[] = "is_same_tab"; |
| 46 |
| 41 // Determines the service type that has been passed from GAIA in the header. | 47 // Determines the service type that has been passed from GAIA in the header. |
| 42 signin::GAIAServiceType GetGAIAServiceTypeFromHeader( | 48 signin::GAIAServiceType GetGAIAServiceTypeFromHeader( |
| 43 const std::string& header_value) { | 49 const std::string& header_value) { |
| 44 if (header_value == "SIGNOUT") | 50 if (header_value == "SIGNOUT") |
| 45 return signin::GAIA_SERVICE_TYPE_SIGNOUT; | 51 return signin::GAIA_SERVICE_TYPE_SIGNOUT; |
| 46 else if (header_value == "INCOGNITO") | 52 else if (header_value == "INCOGNITO") |
| 47 return signin::GAIA_SERVICE_TYPE_INCOGNITO; | 53 return signin::GAIA_SERVICE_TYPE_INCOGNITO; |
| 48 else if (header_value == "ADDSESSION") | 54 else if (header_value == "ADDSESSION") |
| 49 return signin::GAIA_SERVICE_TYPE_ADDSESSION; | 55 return signin::GAIA_SERVICE_TYPE_ADDSESSION; |
| 50 else if (header_value == "REAUTH") | 56 else if (header_value == "REAUTH") |
| (...skipping 11 matching lines...) Expand all Loading... |
| 62 std::vector<std::string> fields; | 68 std::vector<std::string> fields; |
| 63 if (!Tokenize(header_value, std::string(","), &fields)) | 69 if (!Tokenize(header_value, std::string(","), &fields)) |
| 64 return MirrorResponseHeaderDictionary(); | 70 return MirrorResponseHeaderDictionary(); |
| 65 | 71 |
| 66 MirrorResponseHeaderDictionary dictionary; | 72 MirrorResponseHeaderDictionary dictionary; |
| 67 for (std::vector<std::string>::iterator i = fields.begin(); | 73 for (std::vector<std::string>::iterator i = fields.begin(); |
| 68 i != fields.end(); ++i) { | 74 i != fields.end(); ++i) { |
| 69 std::string field(*i); | 75 std::string field(*i); |
| 70 std::vector<std::string> tokens; | 76 std::vector<std::string> tokens; |
| 71 if (Tokenize(field, "=", &tokens) != 2) { | 77 if (Tokenize(field, "=", &tokens) != 2) { |
| 72 DLOG(WARNING) << "Unexpected GAIA header filed '" << field << "'."; | 78 DLOG(WARNING) << "Unexpected GAIA header field '" << field << "'."; |
| 73 continue; | 79 continue; |
| 74 } | 80 } |
| 75 dictionary[tokens[0]] = tokens[1]; | 81 dictionary[tokens[0]] = tokens[1]; |
| 76 } | 82 } |
| 77 return dictionary; | 83 return dictionary; |
| 78 } | 84 } |
| 79 | 85 |
| 86 // Returns the parameters contained in the X-Chrome-Manage-Accounts response |
| 87 // header. |
| 88 signin::ManageAccountsParams BuildManageAccountsParams( |
| 89 const std::string& header_value) { |
| 90 signin::ManageAccountsParams params; |
| 91 MirrorResponseHeaderDictionary header_dictionary = |
| 92 ParseMirrorResponseHeader(header_value); |
| 93 MirrorResponseHeaderDictionary::const_iterator it = header_dictionary.begin(); |
| 94 for (; it != header_dictionary.end(); ++it) { |
| 95 const std::string key_name(it->first); |
| 96 if (key_name == kServiceTypeAttrName) { |
| 97 params.service_type = |
| 98 GetGAIAServiceTypeFromHeader(header_dictionary[kServiceTypeAttrName]); |
| 99 } else if (key_name == kEmailAttrName) { |
| 100 params.email = header_dictionary[kEmailAttrName]; |
| 101 } else if (key_name == kIsSamlAttrName) { |
| 102 params.is_saml = header_dictionary[kIsSamlAttrName] == "true"; |
| 103 } else if (key_name == kContinueUrlAttrName) { |
| 104 params.continue_url = header_dictionary[kContinueUrlAttrName]; |
| 105 } else if (key_name == kIsSameTabAttrName) { |
| 106 params.is_same_tab = header_dictionary[kIsSameTabAttrName] == "true"; |
| 107 } else { |
| 108 DLOG(WARNING) << "Unexpected GAIA header attribute '" << key_name << "'."; |
| 109 } |
| 110 } |
| 111 return params; |
| 112 } |
| 113 |
| 80 #if !defined(OS_IOS) | 114 #if !defined(OS_IOS) |
| 81 // Processes the mirror response header on the UI thread. Currently depending | 115 // Processes the mirror response header on the UI thread. Currently depending |
| 82 // on the value of |header_value|, it either shows the profile avatar menu, or | 116 // on the value of |header_value|, it either shows the profile avatar menu, or |
| 83 // opens an incognito window/tab. | 117 // opens an incognito window/tab. |
| 84 void ProcessMirrorHeaderUIThread( | 118 void ProcessMirrorHeaderUIThread( |
| 85 int child_id, int route_id, | 119 int child_id, int route_id, |
| 86 signin::ManageAccountsParams manage_accounts_params) { | 120 signin::ManageAccountsParams manage_accounts_params) { |
| 87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 88 | 122 |
| 89 signin::GAIAServiceType service_type = manage_accounts_params.service_type; | 123 signin::GAIAServiceType service_type = manage_accounts_params.service_type; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 105 case signin::GAIA_SERVICE_TYPE_ADDSESSION: | 139 case signin::GAIA_SERVICE_TYPE_ADDSESSION: |
| 106 bubble_mode = BrowserWindow::AVATAR_BUBBLE_MODE_SIGNIN; | 140 bubble_mode = BrowserWindow::AVATAR_BUBBLE_MODE_SIGNIN; |
| 107 break; | 141 break; |
| 108 case signin::GAIA_SERVICE_TYPE_REAUTH: | 142 case signin::GAIA_SERVICE_TYPE_REAUTH: |
| 109 bubble_mode = BrowserWindow::AVATAR_BUBBLE_MODE_REAUTH; | 143 bubble_mode = BrowserWindow::AVATAR_BUBBLE_MODE_REAUTH; |
| 110 break; | 144 break; |
| 111 default: | 145 default: |
| 112 bubble_mode = BrowserWindow::AVATAR_BUBBLE_MODE_ACCOUNT_MANAGEMENT; | 146 bubble_mode = BrowserWindow::AVATAR_BUBBLE_MODE_ACCOUNT_MANAGEMENT; |
| 113 } | 147 } |
| 114 browser->window()->ShowAvatarBubbleFromAvatarButton( | 148 browser->window()->ShowAvatarBubbleFromAvatarButton( |
| 115 bubble_mode, service_type); | 149 bubble_mode, manage_accounts_params); |
| 116 } | 150 } |
| 117 #else // defined(OS_ANDROID) | 151 #else // defined(OS_ANDROID) |
| 118 if (service_type == signin::GAIA_SERVICE_TYPE_INCOGNITO) { | 152 if (service_type == signin::GAIA_SERVICE_TYPE_INCOGNITO) { |
| 119 web_contents->OpenURL(content::OpenURLParams( | 153 web_contents->OpenURL(content::OpenURLParams( |
| 120 GURL(chrome::kChromeUINativeNewTabURL), content::Referrer(), | 154 GURL(chrome::kChromeUINativeNewTabURL), content::Referrer(), |
| 121 OFF_THE_RECORD, content::PAGE_TRANSITION_AUTO_TOPLEVEL, false)); | 155 OFF_THE_RECORD, content::PAGE_TRANSITION_AUTO_TOPLEVEL, false)); |
| 122 } else { | 156 } else { |
| 123 AccountManagementScreenHelper::OpenAccountManagementScreen( | 157 AccountManagementScreenHelper::OpenAccountManagementScreen( |
| 124 Profile::FromBrowserContext(web_contents->GetBrowserContext()), | 158 Profile::FromBrowserContext(web_contents->GetBrowserContext()), |
| 125 service_type); | 159 service_type); |
| 126 } | 160 } |
| 127 #endif // OS_ANDROID | 161 #endif // OS_ANDROID |
| 128 } | 162 } |
| 129 #endif // !defined(OS_IOS) | 163 #endif // !defined(OS_IOS) |
| 130 | 164 |
| 131 bool IsDriveOrigin(const GURL& url) { | 165 bool IsDriveOrigin(const GURL& url) { |
| 132 if (!url.SchemeIsSecure()) | 166 if (!url.SchemeIsSecure()) |
| 133 return false; | 167 return false; |
| 134 | 168 |
| 135 const GURL kGoogleDriveURL("https://drive.google.com"); | 169 const GURL kGoogleDriveURL("https://drive.google.com"); |
| 136 const GURL kGoogleDocsURL("https://docs.google.com"); | 170 const GURL kGoogleDocsURL("https://docs.google.com"); |
| 137 return url == kGoogleDriveURL || url == kGoogleDocsURL; | 171 return url == kGoogleDriveURL || url == kGoogleDocsURL; |
| 138 } | 172 } |
| 139 | 173 |
| 140 } // empty namespace | 174 } // empty namespace |
| 141 | 175 |
| 142 namespace signin { | 176 namespace signin { |
| 143 | 177 |
| 178 ManageAccountsParams::ManageAccountsParams() : |
| 179 service_type(GAIA_SERVICE_TYPE_NONE), |
| 180 email(""), |
| 181 is_saml(false), |
| 182 continue_url(""), |
| 183 is_same_tab(false), |
| 184 child_id(0), |
| 185 route_id(0) {} |
| 186 |
| 144 bool AppendMirrorRequestHeaderIfPossible( | 187 bool AppendMirrorRequestHeaderIfPossible( |
| 145 net::URLRequest* request, | 188 net::URLRequest* request, |
| 146 const GURL& redirect_url, | 189 const GURL& redirect_url, |
| 147 ProfileIOData* io_data, | 190 ProfileIOData* io_data, |
| 148 int child_id, | 191 int child_id, |
| 149 int route_id) { | 192 int route_id) { |
| 150 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 193 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 151 | 194 |
| 152 if (io_data->IsOffTheRecord() || | 195 if (io_data->IsOffTheRecord() || |
| 153 io_data->google_services_username()->GetValue().empty()) { | 196 io_data->google_services_username()->GetValue().empty()) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 std::string header_value(base::StringPrintf("%s=%s,%s=%s,%s=%s", | 234 std::string header_value(base::StringPrintf("%s=%s,%s=%s,%s=%s", |
| 192 kGaiaIdAttrName, account_id.c_str(), | 235 kGaiaIdAttrName, account_id.c_str(), |
| 193 kProfileModeAttrName, base::IntToString(profile_mode_mask).c_str(), | 236 kProfileModeAttrName, base::IntToString(profile_mode_mask).c_str(), |
| 194 kEnableAccountConsistencyAttrName, | 237 kEnableAccountConsistencyAttrName, |
| 195 is_new_profile_management ? "true" : "false")); | 238 is_new_profile_management ? "true" : "false")); |
| 196 request->SetExtraRequestHeaderByName( | 239 request->SetExtraRequestHeaderByName( |
| 197 kChromeConnectedHeader, header_value, false); | 240 kChromeConnectedHeader, header_value, false); |
| 198 return true; | 241 return true; |
| 199 } | 242 } |
| 200 | 243 |
| 201 ManageAccountsParams GetManageAccountsParams(net::URLRequest* request, | |
| 202 ProfileIOData* io_data) { | |
| 203 ManageAccountsParams params; | |
| 204 params.service_type = GAIA_SERVICE_TYPE_NONE; | |
| 205 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 206 if (!gaia::IsGaiaSignonRealm(request->url().GetOrigin())) | |
| 207 return params; | |
| 208 | |
| 209 std::string header_value; | |
| 210 if (!request->response_headers()->GetNormalizedHeader( | |
| 211 kChromeManageAccountsHeader, &header_value)) { | |
| 212 return params; | |
| 213 } | |
| 214 | |
| 215 DCHECK(switches::IsNewProfileManagement() && !io_data->IsOffTheRecord()); | |
| 216 | |
| 217 MirrorResponseHeaderDictionary header_dictionary = | |
| 218 ParseMirrorResponseHeader(header_value); | |
| 219 if (header_dictionary.count("action")) { | |
| 220 params.service_type = | |
| 221 GetGAIAServiceTypeFromHeader(header_dictionary["action"]); | |
| 222 } | |
| 223 return params; | |
| 224 } | |
| 225 | |
| 226 void ProcessMirrorResponseHeaderIfExists( | 244 void ProcessMirrorResponseHeaderIfExists( |
| 227 net::URLRequest* request, | 245 net::URLRequest* request, |
| 228 ProfileIOData* io_data, | 246 ProfileIOData* io_data, |
| 229 int child_id, | 247 int child_id, |
| 230 int route_id) { | 248 int route_id) { |
| 231 #if defined(OS_IOS) | 249 #if defined(OS_IOS) |
| 232 NOTREACHED(); | 250 NOTREACHED(); |
| 233 #else | 251 #else |
| 234 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 252 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 235 ManageAccountsParams params = GetManageAccountsParams(request, io_data); | 253 if (!gaia::IsGaiaSignonRealm(request->url().GetOrigin())) |
| 254 return; |
| 255 |
| 256 std::string header_value; |
| 257 if (!request->response_headers()->GetNormalizedHeader( |
| 258 kChromeManageAccountsHeader, &header_value)) { |
| 259 return; |
| 260 } |
| 261 |
| 262 DCHECK(switches::IsNewProfileManagement() && !io_data->IsOffTheRecord()); |
| 263 ManageAccountsParams params(BuildManageAccountsParams(header_value)); |
| 236 if (params.service_type == GAIA_SERVICE_TYPE_NONE) | 264 if (params.service_type == GAIA_SERVICE_TYPE_NONE) |
| 237 return; | 265 return; |
| 238 | 266 |
| 267 params.child_id = child_id; |
| 268 params.route_id = route_id; |
| 239 content::BrowserThread::PostTask( | 269 content::BrowserThread::PostTask( |
| 240 content::BrowserThread::UI, FROM_HERE, | 270 content::BrowserThread::UI, FROM_HERE, |
| 241 base::Bind(ProcessMirrorHeaderUIThread, child_id, route_id, params)); | 271 base::Bind(ProcessMirrorHeaderUIThread, child_id, route_id, params)); |
| 242 #endif // defined(OS_IOS) | 272 #endif // defined(OS_IOS) |
| 243 } | 273 } |
| 244 | 274 |
| 245 } // namespace signin | 275 } // namespace signin |
| OLD | NEW |