| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "ios/web/web_state/ui/web_view_js_utils.h" | 5 #import "ios/web/web_state/ui/web_view_js_utils.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 #import <WebKit/WebKit.h> | 8 #import <WebKit/WebKit.h> |
| 9 | 9 |
| 10 #include "base/ios/weak_nsobject.h" | 10 #include "base/ios/weak_nsobject.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 [weak_web_view stringByEvaluatingJavaScriptFromString:script]; | 49 [weak_web_view stringByEvaluatingJavaScriptFromString:script]; |
| 50 if (completion_handler) | 50 if (completion_handler) |
| 51 completion_handler(result, nil); | 51 completion_handler(result, nil); |
| 52 }); | 52 }); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void EvaluateJavaScript(WKWebView* web_view, | 55 void EvaluateJavaScript(WKWebView* web_view, |
| 56 NSString* script, | 56 NSString* script, |
| 57 JavaScriptCompletion completion_handler) { | 57 JavaScriptCompletion completion_handler) { |
| 58 DCHECK([script length]); | 58 DCHECK([script length]); |
| 59 // __block qualifier ensures that web_view_completion_handler is correctly | 59 void (^web_view_completion_handler)(id, NSError*) = nil; |
| 60 // captured by itself. | 60 // Do not create a web_view_completion_handler if no |completion_handler| is |
| 61 __block void (^web_view_completion_handler)(id, NSError*) = nil; | 61 // passed to this function. WKWebView guarantees to call all completion |
| 62 // Do not create a web_view_completion_handler if no |handler| is passed to | 62 // handlers before deallocation. Passing nil as completion handler (when |
| 63 // this function. This results in no creation of an unnecessary block. | 63 // appropriate) may speed up web view deallocation, because there will be no |
| 64 // need to call those completion handlers. |
| 64 if (completion_handler) { | 65 if (completion_handler) { |
| 65 // WKWebView crashes on deallocation when it flushes scripts that did not | 66 web_view_completion_handler = ^(id result, NSError* error) { |
| 66 // finish the execution but have |completion_handler|. Passing | |
| 67 // |completion_handler| ownership to the block itself and keeping it alive | |
| 68 // until it's executed fixes the crash. No memory leak is expected since | |
| 69 // |completion_handler| is always executed even if WKWebView is deallocated. | |
| 70 // https://bugs.webkit.org/show_bug.cgi?id=140203 | |
| 71 web_view_completion_handler = [^(id result, NSError* error) { | |
| 72 completion_handler(UIResultFromWKResult(result), error); | 67 completion_handler(UIResultFromWKResult(result), error); |
| 73 [web_view_completion_handler autorelease]; | 68 }; |
| 74 } copy]; | |
| 75 } | 69 } |
| 76 [web_view evaluateJavaScript:script | 70 [web_view evaluateJavaScript:script |
| 77 completionHandler:web_view_completion_handler]; | 71 completionHandler:web_view_completion_handler]; |
| 78 } | 72 } |
| 79 | 73 |
| 80 } // namespace web | 74 } // namespace web |
| OLD | NEW |