Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: ios/web/web_state/js/crw_js_post_request_loader.mm

Issue 1375023002: Adds support for POST request with bodies on WKWebView. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rest of the commetns Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "ios/web/web_state/js/crw_js_post_request_loader.h"
6
7 #include "base/json/string_escape.h"
8 #include "base/strings/sys_string_conversions.h"
9 #import "ios/web/web_state/js/page_script_util.h"
10
11 namespace {
12
13 // Escapes characters and encloses given string in quotes for use in JavaScript.
14 NSString* EscapeAndQuoteStringForJavaScript(NSString* unescapedString) {
15 std::string string = base::SysNSStringToUTF8(unescapedString);
16 return base::SysUTF8ToNSString(base::GetQuotedJSONString(string));
17 }
18
19 } // namespace
20
21 @implementation CRWJSPOSTRequestLoader
22
23 - (void)loadPOSTRequest:(NSURLRequest*)request inWebView:(WKWebView*)webView {
24 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.
25 NSString* HTML =
26 [NSString stringWithFormat:@"<html><script>%@%@</script></html>", js,
27 [self stringToExecutePOSTRequest:request]];
28 [webView loadHTMLString:HTML baseURL:request.URL];
29 }
30
31 - (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.
32 NSDictionary* headers = [request allHTTPHeaderFields];
33 NSString* headerString = [self stringForJavaScriptFromRequestHeaders:headers];
34 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.
35 NSString* contentType = headers[@"Content-Type"];
36 NSString* base64Data = [[request HTTPBody] base64EncodedStringWithOptions:0];
37
38 return [NSString
39 stringWithFormat:@"__gCrWeb.postRequest.runPOSTRequest(%@, %@, %@, %@)",
40 EscapeAndQuoteStringForJavaScript(urlString),
41 headerString,
42 EscapeAndQuoteStringForJavaScript(base64Data),
43 EscapeAndQuoteStringForJavaScript(contentType)];
44 }
45
46 - (NSString*)stringForJavaScriptFromRequestHeaders:(NSDictionary*)headers {
47 NSData* headerData = nil;
48 if (headers) {
49 headerData =
50 [NSJSONSerialization dataWithJSONObject:headers options:0 error:nil];
51 }
52 NSString* headerString = @"\"\"";
53 if (headerData) {
54 headerString =
55 [[[NSString alloc] initWithData:headerData
56 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.
57 }
58 return headerString;
59 }
60
61 @end
62
63 // 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.
64 // replaces document with the result.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698