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

Unified 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: missing file Created 5 years 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 side-by-side diff with in-line comments
Download patch
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..735e76ab8e0a1014eac3a28d813697c53dea5dec
--- /dev/null
+++ b/ios/web/web_state/js/crw_js_post_request_loader.mm
@@ -0,0 +1,168 @@
+// 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/mac/objc_property_releaser.h"
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 s/include/import
stkhapugin 2015/12/03 15:43:02 Done.
+#include "base/strings/sys_string_conversions.h"
+#import "ios/web/web_state/js/page_script_util.h"
+#import "ios/web/web_state/ui/crw_wk_script_message_router.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));
+}
+
+NSString* const kErrorHandlerName = @"POSTErrorHandler";
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 These need comments
stkhapugin 2015/12/03 15:43:02 Done.
+NSString* const kSuccessHandlerName = @"POSTSuccess";
+
+} // namespace
+
+@interface CRWJSPOSTRequestLoader () {
+ base::mac::ObjCPropertyReleaser _propertyReleaser_CRWJSPOSTRequestLoader;
+}
+
+// JavaScript used to execute POST requests. Lazily instantiated.
+@property(nonatomic, retain) NSString* requestScript;
+
+// Handler for UIApplicationDidReceiveMemoryWarningNotification.
+- (void)handleMemoryWarning;
+
+// Forms a JavaScript method call to |requestScript| that executes given
+// |request|.
+- (NSString*)stringToExecutePOSTRequest:(NSURLRequest*)request;
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 s/stringToExecutePOSTRequest:/scriptToExecutePOSTR
stkhapugin 2015/12/03 15:43:02 Done.
+
+// Converts a dictionary of HTTP request headers to a JavaScript object.
+- (NSString*)stringForJavaScriptFromRequestHeaders:(NSDictionary*)headers;
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 s/stringForJavaScriptFromRequestHeaders:/JSONForJa
stkhapugin 2015/12/03 15:43:01 Done.
+
+@end
+
+@implementation CRWJSPOSTRequestLoader
+@synthesize requestScript = _requestScript;
+
+- (void)dealloc {
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
+ [super dealloc];
+}
+
+- (instancetype)init {
+ self = [super init];
+ if (self) {
+ _propertyReleaser_CRWJSPOSTRequestLoader.Init(
+ self, [CRWJSPOSTRequestLoader class]);
+ [[NSNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector(handleMemoryWarning)
+ name:UIApplicationDidReceiveMemoryWarningNotification
+ object:nil];
+ }
+ return self;
+}
+
+- (NSString*)requestScript {
+ if (!_requestScript) {
+ _requestScript = [web::GetPageScript(@"post_request") retain];
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 s/retain/copy Strings should be copied. https://g
stkhapugin 2015/12/03 15:43:02 Done.
+ }
+ return _requestScript;
+}
+
+- (void)loadPOSTRequest:(NSURLRequest*)request
+ inWebView:(WKWebView*)webView
+ messageRouter:(CRWWKScriptMessageRouter*)messageRouter
+ errorBlock:(void (^)(NSError*))errorBlock
+ successBlock:(void (^)())successBlock {
+ DCHECK([request.HTTPMethod isEqualToString:@"POST"]);
+ DCHECK(webView);
+ DCHECK(messageRouter);
+ DCHECK(errorBlock);
+
+ // Install error handling and success routers.
+ [messageRouter setScriptMessageHandler:^(WKScriptMessage* message) {
+ // Cleaning up script handlers.
+ // This needs to be done on next runloop because this block gets deallocated
+ // during its execution otherwise.
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 Which block is deallocated? one which is passed to
stkhapugin 2015/12/03 15:43:02 The message handler block (the one where this comm
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 Thank you for explanation. I think it will be clea
stkhapugin 2015/12/03 17:51:02 Done.
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [messageRouter removeScriptMessageHandlerForName:kErrorHandlerName
+ webView:webView];
+ [messageRouter removeScriptMessageHandlerForName:kSuccessHandlerName
+ webView:webView];
+ if (successBlock) {
+ successBlock();
+ }
+ });
+ }
+ name:kSuccessHandlerName
+ webView:webView];
+
+ [messageRouter setScriptMessageHandler:^(WKScriptMessage* message) {
+ NSNumber* statusCode = message.body;
+ NSError* error = [NSError errorWithDomain:@"http"
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 Please add a constant for error domain and put it
stkhapugin 2015/12/03 15:43:02 Since the only error that can happen here is HTTP,
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 NSURLErrorDomain is used by System libraries, so w
stkhapugin 2015/12/03 17:51:02 I agree that it's not necessary to emit an NSError
Eugene But (OOO till 7-30) 2015/12/03 18:13:21 Oh, I'm sorry, I'm blind. WC actually uses that NS
stkhapugin 2015/12/04 16:28:09 Done.
+ code:[statusCode integerValue]
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 Optional NIT: s/[statusCode integerValue]/statusCo
stkhapugin 2015/12/03 15:43:02 Done.
+ userInfo:nil];
+ // Cleaning up script handlers.
+ // This needs to be done on next runloop because this block gets deallocated
+ // during its execution otherwise.
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [messageRouter removeScriptMessageHandlerForName:kErrorHandlerName
+ webView:webView];
+ [messageRouter removeScriptMessageHandlerForName:kSuccessHandlerName
+ webView:webView];
+ errorBlock(error);
+ });
+ }
+ name:kErrorHandlerName
+ webView:webView];
+
+ NSString* js = [self requestScript];
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 Optional NIT: s/[self requestScript]/self.requestS
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 NIT: Please drop this variable and just inline the
stkhapugin 2015/12/03 15:43:02 Done.
stkhapugin 2015/12/03 15:43:02 Done.
+ NSString* HTML =
+ [NSString stringWithFormat:@"<html><script>%@%@</script></html>", js,
+ [self stringToExecutePOSTRequest:request]];
+ [webView loadHTMLString:HTML baseURL:request.URL];
+}
+
+#pragma mark - Private methods.
+
+- (void)handleMemoryWarning {
+ // Request script can be recreated from file at any moment.
+ self.requestScript = nil;
+}
+
+- (NSString*)stringToExecutePOSTRequest:(NSURLRequest*)request {
+ NSDictionary* headers = [request allHTTPHeaderFields];
+ NSString* headerString = [self stringForJavaScriptFromRequestHeaders:headers];
+ NSString* URLString = [[request URL] absoluteString];
+ NSString* contentType = headers[@"Content-Type"];
+ NSString* base64Data = [[request HTTPBody] base64EncodedStringWithOptions:0];
+
+ return [NSString
+ stringWithFormat:@"__crPostRequestHack.runPostRequest(%@, %@, %@, %@)",
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 s/Hack/Workaround :)
stkhapugin 2015/12/03 15:43:02 Done.
+ EscapeAndQuoteStringForJavaScript(URLString),
+ headerString,
+ EscapeAndQuoteStringForJavaScript(base64Data),
+ EscapeAndQuoteStringForJavaScript(contentType)];
+}
+
+- (NSString*)stringForJavaScriptFromRequestHeaders:(NSDictionary*)headers {
+ NSData* headerData = nil;
+ if (headers) {
Eugene But (OOO till 7-30) 2015/12/02 17:06:15 Optional NIT: I would restructure this: if (heade
stkhapugin 2015/12/03 15:43:02 Done.
+ headerData =
+ [NSJSONSerialization dataWithJSONObject:headers options:0 error:nil];
+ }
+ NSString* headerString = @"{}";
+ if (headerData) {
+ // This string is properly escaped by NSJSONSerialization. It needs to have
+ // no quotes since JavaScripts takes this parameter as an
+ // Object<string, string>.
+ headerString =
+ [[[NSString alloc] initWithData:headerData
+ encoding:NSUTF8StringEncoding] autorelease];
+ }
+ return headerString;
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698