Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/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.
| |
| 9 #include "base/strings/sys_string_conversions.h" | |
| 10 #import "ios/web/web_state/js/page_script_util.h" | |
| 11 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Escapes characters and encloses given string in quotes for use in JavaScript. | |
| 16 NSString* EscapeAndQuoteStringForJavaScript(NSString* unescapedString) { | |
| 17 std::string string = base::SysNSStringToUTF8(unescapedString); | |
| 18 return base::SysUTF8ToNSString(base::GetQuotedJSONString(string)); | |
| 19 } | |
| 20 | |
| 21 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.
| |
| 22 NSString* const kSuccessHandlerName = @"POSTSuccess"; | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 @interface CRWJSPOSTRequestLoader () { | |
| 27 base::mac::ObjCPropertyReleaser _propertyReleaser_CRWJSPOSTRequestLoader; | |
| 28 } | |
| 29 | |
| 30 // JavaScript used to execute POST requests. Lazily instantiated. | |
| 31 @property(nonatomic, retain) NSString* requestScript; | |
| 32 | |
| 33 // Handler for UIApplicationDidReceiveMemoryWarningNotification. | |
| 34 - (void)handleMemoryWarning; | |
| 35 | |
| 36 // Forms a JavaScript method call to |requestScript| that executes given | |
| 37 // |request|. | |
| 38 - (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.
| |
| 39 | |
| 40 // Converts a dictionary of HTTP request headers to a JavaScript object. | |
| 41 - (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.
| |
| 42 | |
| 43 @end | |
| 44 | |
| 45 @implementation CRWJSPOSTRequestLoader | |
| 46 @synthesize requestScript = _requestScript; | |
| 47 | |
| 48 - (void)dealloc { | |
| 49 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 50 [super dealloc]; | |
| 51 } | |
| 52 | |
| 53 - (instancetype)init { | |
| 54 self = [super init]; | |
| 55 if (self) { | |
| 56 _propertyReleaser_CRWJSPOSTRequestLoader.Init( | |
| 57 self, [CRWJSPOSTRequestLoader class]); | |
| 58 [[NSNotificationCenter defaultCenter] | |
| 59 addObserver:self | |
| 60 selector:@selector(handleMemoryWarning) | |
| 61 name:UIApplicationDidReceiveMemoryWarningNotification | |
| 62 object:nil]; | |
| 63 } | |
| 64 return self; | |
| 65 } | |
| 66 | |
| 67 - (NSString*)requestScript { | |
| 68 if (!_requestScript) { | |
| 69 _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.
| |
| 70 } | |
| 71 return _requestScript; | |
| 72 } | |
| 73 | |
| 74 - (void)loadPOSTRequest:(NSURLRequest*)request | |
| 75 inWebView:(WKWebView*)webView | |
| 76 messageRouter:(CRWWKScriptMessageRouter*)messageRouter | |
| 77 errorBlock:(void (^)(NSError*))errorBlock | |
| 78 successBlock:(void (^)())successBlock { | |
| 79 DCHECK([request.HTTPMethod isEqualToString:@"POST"]); | |
| 80 DCHECK(webView); | |
| 81 DCHECK(messageRouter); | |
| 82 DCHECK(errorBlock); | |
| 83 | |
| 84 // Install error handling and success routers. | |
| 85 [messageRouter setScriptMessageHandler:^(WKScriptMessage* message) { | |
| 86 // Cleaning up script handlers. | |
| 87 // This needs to be done on next runloop because this block gets deallocated | |
| 88 // 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.
| |
| 89 dispatch_async(dispatch_get_main_queue(), ^{ | |
| 90 [messageRouter removeScriptMessageHandlerForName:kErrorHandlerName | |
| 91 webView:webView]; | |
| 92 [messageRouter removeScriptMessageHandlerForName:kSuccessHandlerName | |
| 93 webView:webView]; | |
| 94 if (successBlock) { | |
| 95 successBlock(); | |
| 96 } | |
| 97 }); | |
| 98 } | |
| 99 name:kSuccessHandlerName | |
| 100 webView:webView]; | |
| 101 | |
| 102 [messageRouter setScriptMessageHandler:^(WKScriptMessage* message) { | |
| 103 NSNumber* statusCode = message.body; | |
| 104 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.
| |
| 105 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.
| |
| 106 userInfo:nil]; | |
| 107 // Cleaning up script handlers. | |
| 108 // This needs to be done on next runloop because this block gets deallocated | |
| 109 // during its execution otherwise. | |
| 110 dispatch_async(dispatch_get_main_queue(), ^{ | |
| 111 [messageRouter removeScriptMessageHandlerForName:kErrorHandlerName | |
| 112 webView:webView]; | |
| 113 [messageRouter removeScriptMessageHandlerForName:kSuccessHandlerName | |
| 114 webView:webView]; | |
| 115 errorBlock(error); | |
| 116 }); | |
| 117 } | |
| 118 name:kErrorHandlerName | |
| 119 webView:webView]; | |
| 120 | |
| 121 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.
| |
| 122 NSString* HTML = | |
| 123 [NSString stringWithFormat:@"<html><script>%@%@</script></html>", js, | |
| 124 [self stringToExecutePOSTRequest:request]]; | |
| 125 [webView loadHTMLString:HTML baseURL:request.URL]; | |
| 126 } | |
| 127 | |
| 128 #pragma mark - Private methods. | |
| 129 | |
| 130 - (void)handleMemoryWarning { | |
| 131 // Request script can be recreated from file at any moment. | |
| 132 self.requestScript = nil; | |
| 133 } | |
| 134 | |
| 135 - (NSString*)stringToExecutePOSTRequest:(NSURLRequest*)request { | |
| 136 NSDictionary* headers = [request allHTTPHeaderFields]; | |
| 137 NSString* headerString = [self stringForJavaScriptFromRequestHeaders:headers]; | |
| 138 NSString* URLString = [[request URL] absoluteString]; | |
| 139 NSString* contentType = headers[@"Content-Type"]; | |
| 140 NSString* base64Data = [[request HTTPBody] base64EncodedStringWithOptions:0]; | |
| 141 | |
| 142 return [NSString | |
| 143 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.
| |
| 144 EscapeAndQuoteStringForJavaScript(URLString), | |
| 145 headerString, | |
| 146 EscapeAndQuoteStringForJavaScript(base64Data), | |
| 147 EscapeAndQuoteStringForJavaScript(contentType)]; | |
| 148 } | |
| 149 | |
| 150 - (NSString*)stringForJavaScriptFromRequestHeaders:(NSDictionary*)headers { | |
| 151 NSData* headerData = nil; | |
| 152 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.
| |
| 153 headerData = | |
| 154 [NSJSONSerialization dataWithJSONObject:headers options:0 error:nil]; | |
| 155 } | |
| 156 NSString* headerString = @"{}"; | |
| 157 if (headerData) { | |
| 158 // This string is properly escaped by NSJSONSerialization. It needs to have | |
| 159 // no quotes since JavaScripts takes this parameter as an | |
| 160 // Object<string, string>. | |
| 161 headerString = | |
| 162 [[[NSString alloc] initWithData:headerData | |
| 163 encoding:NSUTF8StringEncoding] autorelease]; | |
| 164 } | |
| 165 return headerString; | |
| 166 } | |
| 167 | |
| 168 @end | |
| OLD | NEW |