OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import "ios/web/web_state/ui/web_view_js_utils.h" | |
6 | |
7 #import <UIKit/UIKit.h> | |
8 #import <WebKit/WebKit.h> | |
9 | |
10 #include "base/ios/weak_nsobject.h" | |
11 #include "base/logging.h" | |
12 #include "base/mac/scoped_nsobject.h" | |
13 | |
14 namespace { | |
15 | |
16 // Converts result of WKWebView script evaluation to UIWebView format. | |
17 NSString* UIResultFromWKResult(id result) { | |
18 if (!result) | |
19 return @""; | |
20 | |
21 CFTypeID result_type = CFGetTypeID(result); | |
22 if (result_type == CFStringGetTypeID()) | |
23 return result; | |
24 | |
25 if (result_type == CFNumberGetTypeID()) | |
26 return [result stringValue]; | |
27 | |
28 if (result_type == CFBooleanGetTypeID()) | |
29 return [result boolValue] ? @"true" : @"false"; | |
30 | |
31 // TODO(stuartmorgan): Stringify other types. | |
32 NOTREACHED(); | |
33 return nil; | |
34 } | |
35 | |
36 } // namespace | |
37 | |
38 namespace web { | |
39 | |
40 void EvaluateJavaScript(UIWebView* web_view, | |
41 NSString* script, | |
42 JavaScriptCompletion completion_handler) { | |
43 base::WeakNSObject<UIWebView> weak_web_view(web_view); | |
44 dispatch_async(dispatch_get_main_queue(), ^{ | |
45 NSString* result = | |
46 [weak_web_view stringByEvaluatingJavaScriptFromString:script]; | |
47 if (completion_handler) | |
48 completion_handler(result, nil); | |
49 }); | |
50 } | |
51 | |
52 void EvaluateJavaScript(WKWebView* web_view, | |
53 NSString* script, | |
54 JavaScriptCompletion completion_handler) { | |
55 DCHECK([script length]); | |
56 id web_view_completion_handler = nil; | |
droger
2015/02/19 10:51:04
Why id?
sdefresne
2015/02/19 13:39:08
Good catch.
| |
57 // Do not create a web_view_completion_handler if no |handler| is passed to | |
58 // this function. This results in no creation of an unnecessary block. | |
59 if (completion_handler) { | |
60 // WKWebView crashes on deallocation when it flushes scripts that did not | |
61 // finish the execution but have |completion_handler|. Passing | |
62 // |completion_handler| ownership to the block itself and keeping it alive | |
63 // until it's executed fixes the crash. No memory leak is expected since | |
64 // |completion_handler| is always executed even if WKWebView is deallocated. | |
65 // https://bugs.webkit.org/show_bug.cgi?id=140203 | |
66 web_view_completion_handler = [^(id result, NSError* error) { | |
67 completion_handler(UIResultFromWKResult(result), error); | |
68 [web_view_completion_handler autorelease]; | |
droger
2015/02/19 10:51:04
This looks like a bug.
I think that the block capt
sdefresne
2015/02/19 13:39:08
I no longer require this file, so the question app
stuartmorgan
2015/02/19 14:23:54
Yep, this does look worrying. I'll investigate.
Eugene But (OOO till 7-30)
2015/02/23 03:20:11
What exactly looks wrong to you? Functionality or
| |
69 } copy]; | |
70 } | |
71 [web_view evaluateJavaScript:script | |
72 completionHandler:web_view_completion_handler]; | |
73 } | |
74 | |
75 } // namespace web | |
OLD | NEW |