| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/chrome/browser/signin/gaia_auth_fetcher_ios.h" | 5 #include "ios/chrome/browser/signin/gaia_auth_fetcher_ios.h" |
| 6 | 6 |
| 7 #import <WebKit/WebKit.h> | 7 #import <WebKit/WebKit.h> |
| 8 | 8 |
| 9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #import "base/mac/foundation_util.h" | 11 #import "base/mac/foundation_util.h" |
| 12 #include "base/mac/scoped_block.h" | 12 #include "base/mac/scoped_block.h" |
| 13 #import "base/mac/scoped_nsobject.h" | |
| 14 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 15 #include "ios/chrome/browser/signin/gaia_auth_fetcher_ios_private.h" | 14 #include "ios/chrome/browser/signin/gaia_auth_fetcher_ios_private.h" |
| 16 #include "ios/web/public/browser_state.h" | 15 #include "ios/web/public/browser_state.h" |
| 17 #import "ios/web/public/web_view_creation_util.h" | 16 #import "ios/web/public/web_view_creation_util.h" |
| 18 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 19 #import "net/base/mac/url_conversions.h" | 18 #import "net/base/mac/url_conversions.h" |
| 20 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 21 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
| 22 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
| 23 | 22 |
| 23 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 24 #error "This file requires ARC support." |
| 25 #endif |
| 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 // Whether the iOS specialization of the GaiaAuthFetcher should be used. | 29 // Whether the iOS specialization of the GaiaAuthFetcher should be used. |
| 27 bool g_should_use_gaia_auth_fetcher_ios = true; | 30 bool g_should_use_gaia_auth_fetcher_ios = true; |
| 28 | 31 |
| 29 // JavaScript template to do a POST request using an XMLHttpRequest. | 32 // JavaScript template to do a POST request using an XMLHttpRequest. |
| 30 // The request is retried once on failure, as it can be marked as failing to | 33 // The request is retried once on failure, as it can be marked as failing to |
| 31 // load the resource because of 302s on POST request (the cookies of the first | 34 // load the resource because of 302s on POST request (the cookies of the first |
| 32 // response are correctly set). | 35 // response are correctly set). |
| 33 // | 36 // |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 "} else {" | 77 "} else {" |
| 75 " document.body.innerText;" | 78 " document.body.innerText;" |
| 76 "}"; | 79 "}"; |
| 77 | 80 |
| 78 // Creates an NSURLRequest to |url| that can be loaded by a WebView from |body| | 81 // Creates an NSURLRequest to |url| that can be loaded by a WebView from |body| |
| 79 // and |headers|. | 82 // and |headers|. |
| 80 // The request is a GET if |body| is empty and a POST otherwise. | 83 // The request is a GET if |body| is empty and a POST otherwise. |
| 81 NSURLRequest* GetRequest(const std::string& body, | 84 NSURLRequest* GetRequest(const std::string& body, |
| 82 const std::string& headers, | 85 const std::string& headers, |
| 83 const GURL& url) { | 86 const GURL& url) { |
| 84 base::scoped_nsobject<NSMutableURLRequest> request( | 87 NSMutableURLRequest* request = |
| 85 [[NSMutableURLRequest alloc] initWithURL:net::NSURLWithGURL(url)]); | 88 [[NSMutableURLRequest alloc] initWithURL:net::NSURLWithGURL(url)]; |
| 86 net::HttpRequestHeaders request_headers; | 89 net::HttpRequestHeaders request_headers; |
| 87 request_headers.AddHeadersFromString(headers); | 90 request_headers.AddHeadersFromString(headers); |
| 88 for (net::HttpRequestHeaders::Iterator it(request_headers); it.GetNext();) { | 91 for (net::HttpRequestHeaders::Iterator it(request_headers); it.GetNext();) { |
| 89 [request setValue:base::SysUTF8ToNSString(it.value()) | 92 [request setValue:base::SysUTF8ToNSString(it.value()) |
| 90 forHTTPHeaderField:base::SysUTF8ToNSString(it.name())]; | 93 forHTTPHeaderField:base::SysUTF8ToNSString(it.name())]; |
| 91 } | 94 } |
| 92 if (!body.empty()) { | 95 if (!body.empty()) { |
| 93 NSData* post_data = | 96 NSData* post_data = |
| 94 [base::SysUTF8ToNSString(body) dataUsingEncoding:NSUTF8StringEncoding]; | 97 [base::SysUTF8ToNSString(body) dataUsingEncoding:NSUTF8StringEncoding]; |
| 95 [request setHTTPBody:post_data]; | 98 [request setHTTPBody:post_data]; |
| 96 [request setHTTPMethod:@"POST"]; | 99 [request setHTTPMethod:@"POST"]; |
| 97 DCHECK(![[request allHTTPHeaderFields] objectForKey:@"Content-Type"]); | 100 DCHECK(![[request allHTTPHeaderFields] objectForKey:@"Content-Type"]); |
| 98 [request setValue:@"application/x-www-form-urlencoded" | 101 [request setValue:@"application/x-www-form-urlencoded" |
| 99 forHTTPHeaderField:@"Content-Type"]; | 102 forHTTPHeaderField:@"Content-Type"]; |
| 100 } | 103 } |
| 101 return request.autorelease(); | 104 return request; |
| 102 } | 105 } |
| 103 | 106 |
| 104 // Escapes and quotes |value| and converts the result to an NSString. | 107 // Escapes and quotes |value| and converts the result to an NSString. |
| 105 NSString* EscapeAndQuoteToNSString(const std::string& value) { | 108 NSString* EscapeAndQuoteToNSString(const std::string& value) { |
| 106 return base::SysUTF8ToNSString(base::GetQuotedJSONString(value)); | 109 return base::SysUTF8ToNSString(base::GetQuotedJSONString(value)); |
| 107 } | 110 } |
| 108 | 111 |
| 109 // Simulates a POST request on |web_view| using a XMLHttpRequest in | 112 // Simulates a POST request on |web_view| using a XMLHttpRequest in |
| 110 // JavaScript. | 113 // JavaScript. |
| 111 // This is needed because WKWebView ignores the HTTPBody in a POST request. | 114 // This is needed because WKWebView ignores the HTTPBody in a POST request. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 return url; | 277 return url; |
| 275 } | 278 } |
| 276 | 279 |
| 277 WKWebView* GaiaAuthFetcherIOSBridge::GetWKWebView() { | 280 WKWebView* GaiaAuthFetcherIOSBridge::GetWKWebView() { |
| 278 if (!web::BrowserState::GetActiveStateManager(browser_state_)->IsActive()) { | 281 if (!web::BrowserState::GetActiveStateManager(browser_state_)->IsActive()) { |
| 279 // |browser_state_| is not active, WKWebView linked to this browser state | 282 // |browser_state_| is not active, WKWebView linked to this browser state |
| 280 // should not exist or be created. | 283 // should not exist or be created. |
| 281 return nil; | 284 return nil; |
| 282 } | 285 } |
| 283 if (!web_view_) { | 286 if (!web_view_) { |
| 284 web_view_.reset([BuildWKWebView() retain]); | 287 web_view_.reset(BuildWKWebView()); |
| 285 navigation_delegate_.reset( | 288 navigation_delegate_.reset( |
| 286 [[GaiaAuthFetcherNavigationDelegate alloc] initWithBridge:this]); | 289 [[GaiaAuthFetcherNavigationDelegate alloc] initWithBridge:this]); |
| 287 [web_view_ setNavigationDelegate:navigation_delegate_]; | 290 [web_view_ setNavigationDelegate:navigation_delegate_]; |
| 288 } | 291 } |
| 289 return web_view_.get(); | 292 return web_view_.get(); |
| 290 } | 293 } |
| 291 | 294 |
| 292 void GaiaAuthFetcherIOSBridge::ResetWKWebView() { | 295 void GaiaAuthFetcherIOSBridge::ResetWKWebView() { |
| 293 [web_view_ setNavigationDelegate:nil]; | 296 [web_view_ setNavigationDelegate:nil]; |
| 294 [web_view_ stopLoading]; | 297 [web_view_ stopLoading]; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 } | 377 } |
| 375 | 378 |
| 376 void GaiaAuthFetcherIOS::SetShouldUseGaiaAuthFetcherIOSForTesting( | 379 void GaiaAuthFetcherIOS::SetShouldUseGaiaAuthFetcherIOSForTesting( |
| 377 bool use_gaia_fetcher_ios) { | 380 bool use_gaia_fetcher_ios) { |
| 378 g_should_use_gaia_auth_fetcher_ios = use_gaia_fetcher_ios; | 381 g_should_use_gaia_auth_fetcher_ios = use_gaia_fetcher_ios; |
| 379 } | 382 } |
| 380 | 383 |
| 381 bool GaiaAuthFetcherIOS::ShouldUseGaiaAuthFetcherIOS() { | 384 bool GaiaAuthFetcherIOS::ShouldUseGaiaAuthFetcherIOS() { |
| 382 return g_should_use_gaia_auth_fetcher_ios; | 385 return g_should_use_gaia_auth_fetcher_ios; |
| 383 } | 386 } |
| OLD | NEW |