Chromium Code Reviews| Index: ios/web/web_state/js/crw_js_post_request_loader.mm |
| diff --git a/ios/web/web_state/js/crw_js_post_request_loader.mm b/ios/web/web_state/js/crw_js_post_request_loader.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2f6889aa7f5779dc44a9e9cc70e821e64588c6d7 |
| --- /dev/null |
| +++ b/ios/web/web_state/js/crw_js_post_request_loader.mm |
| @@ -0,0 +1,64 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/web/web_state/js/crw_js_post_request_loader.h" |
| + |
| +#include "base/json/string_escape.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#import "ios/web/web_state/js/page_script_util.h" |
| + |
| +namespace { |
| + |
| +// Escapes characters and encloses given string in quotes for use in JavaScript. |
| +NSString* EscapeAndQuoteStringForJavaScript(NSString* unescapedString) { |
| + std::string string = base::SysNSStringToUTF8(unescapedString); |
| + return base::SysUTF8ToNSString(base::GetQuotedJSONString(string)); |
| +} |
| + |
| +} // namespace |
| + |
| +@implementation CRWJSPOSTRequestLoader |
| + |
| +- (void)loadPOSTRequest:(NSURLRequest*)request inWebView:(WKWebView*)webView { |
| + NSString* js = web::GetPageScript(@"post_request"); |
|
stuartmorgan
2015/10/08 19:11:29
How long does this take on an older device? I susp
stkhapugin
2015/12/03 15:43:01
Done.
|
| + NSString* HTML = |
| + [NSString stringWithFormat:@"<html><script>%@%@</script></html>", js, |
| + [self stringToExecutePOSTRequest:request]]; |
| + [webView loadHTMLString:HTML baseURL:request.URL]; |
| +} |
| + |
| +- (NSString*)stringToExecutePOSTRequest:(NSURLRequest*)request { |
|
stuartmorgan
2015/10/08 19:11:29
None of your private methods are declared or docum
stkhapugin
2015/12/03 15:43:01
Done.
|
| + NSDictionary* headers = [request allHTTPHeaderFields]; |
| + NSString* headerString = [self stringForJavaScriptFromRequestHeaders:headers]; |
| + NSString* urlString = [[request URL] absoluteString]; |
|
Eugene But (OOO till 7-30)
2015/10/08 17:46:32
s/urlString/URLString
stkhapugin
2015/12/03 15:43:01
Done.
|
| + NSString* contentType = headers[@"Content-Type"]; |
| + NSString* base64Data = [[request HTTPBody] base64EncodedStringWithOptions:0]; |
| + |
| + return [NSString |
| + stringWithFormat:@"__gCrWeb.postRequest.runPOSTRequest(%@, %@, %@, %@)", |
| + EscapeAndQuoteStringForJavaScript(urlString), |
| + headerString, |
| + EscapeAndQuoteStringForJavaScript(base64Data), |
| + EscapeAndQuoteStringForJavaScript(contentType)]; |
| +} |
| + |
| +- (NSString*)stringForJavaScriptFromRequestHeaders:(NSDictionary*)headers { |
| + NSData* headerData = nil; |
| + if (headers) { |
| + headerData = |
| + [NSJSONSerialization dataWithJSONObject:headers options:0 error:nil]; |
| + } |
| + NSString* headerString = @"\"\""; |
| + if (headerData) { |
| + headerString = |
| + [[[NSString alloc] initWithData:headerData |
| + encoding:NSUTF8StringEncoding] autorelease]; |
|
stuartmorgan
2015/10/08 19:11:29
This doesn't need any quoting or escaping? If not,
stkhapugin
2015/12/03 15:43:01
Done.
|
| + } |
| + return headerString; |
| +} |
| + |
| +@end |
| + |
| +// Constructs an HTML page that executes the request through JavaScript and |
|
Eugene But (OOO till 7-30)
2015/10/08 17:46:32
Remove this?
stkhapugin
2015/12/03 15:43:01
Done.
|
| +// replaces document with the result. |