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

Side by Side Diff: ios/web/web_state/ui/web_view_js_utils.mm

Issue 1808993002: [ios] Removed UIWebView JS evaluation code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
« no previous file with comments | « ios/web/web_state/ui/web_view_js_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <CoreFoundation/CoreFoundation.h>
8 #import <WebKit/WebKit.h> 8 #import <WebKit/WebKit.h>
9 9
10 #include "base/ios/weak_nsobject.h"
11 #include "base/logging.h" 10 #include "base/logging.h"
12 #include "base/mac/scoped_nsobject.h" 11 #include "base/mac/scoped_nsobject.h"
13 12
14 namespace { 13 namespace {
15 14
16 // Converts result of WKWebView script evaluation to UIWebView format. 15 // Converts result of WKWebView script evaluation to a string.
17 NSString* UIResultFromWKResult(id result) { 16 NSString* StringResultFromWKResult(id result) {
Jackie Quinn 2016/03/17 17:06:43 Are we planning on making it so that we don't conv
Eugene But (OOO till 7-30) 2016/03/17 17:19:14 Web Controller will not be used as public API. Web
18 if (!result) 17 if (!result)
19 return @""; 18 return @"";
20 19
21 CFTypeID result_type = CFGetTypeID(result); 20 CFTypeID result_type = CFGetTypeID(result);
22 if (result_type == CFStringGetTypeID()) 21 if (result_type == CFStringGetTypeID())
23 return result; 22 return result;
24 23
25 if (result_type == CFNumberGetTypeID()) 24 if (result_type == CFNumberGetTypeID())
26 return [result stringValue]; 25 return [result stringValue];
27 26
28 if (result_type == CFBooleanGetTypeID()) 27 if (result_type == CFBooleanGetTypeID())
29 return [result boolValue] ? @"true" : @"false"; 28 return [result boolValue] ? @"true" : @"false";
30 29
31 if (result_type == CFNullGetTypeID()) 30 if (result_type == CFNullGetTypeID())
32 return @""; 31 return @"";
33 32
34 // TODO(stuartmorgan): Stringify other types. 33 // TODO(stuartmorgan): Stringify other types.
35 NOTREACHED(); 34 NOTREACHED();
36 return nil; 35 return nil;
37 } 36 }
38 37
39 } // namespace 38 } // namespace
40 39
41 namespace web { 40 namespace web {
42 41
43 NSString* const kJSEvaluationErrorDomain = @"JSEvaluationError"; 42 NSString* const kJSEvaluationErrorDomain = @"JSEvaluationError";
44 43
45 void EvaluateJavaScript(UIWebView* web_view,
46 NSString* script,
47 JavaScriptCompletion completion_handler) {
48 base::WeakNSObject<UIWebView> weak_web_view(web_view);
49 dispatch_async(dispatch_get_main_queue(), ^{
50 NSString* result =
51 [weak_web_view stringByEvaluatingJavaScriptFromString:script];
52 if (completion_handler)
53 completion_handler(result, nil);
54 });
55 }
56
57 void EvaluateJavaScript(WKWebView* web_view, 44 void EvaluateJavaScript(WKWebView* web_view,
58 NSString* script, 45 NSString* script,
59 JavaScriptCompletion completion_handler) { 46 JavaScriptCompletion completion_handler) {
60 DCHECK([script length]); 47 DCHECK([script length]);
61 if (!web_view && completion_handler) { 48 if (!web_view && completion_handler) {
62 dispatch_async(dispatch_get_main_queue(), ^{ 49 dispatch_async(dispatch_get_main_queue(), ^{
63 NSString* error_message = 50 NSString* error_message =
64 @"JS evaluation failed because there is no web view."; 51 @"JS evaluation failed because there is no web view.";
65 base::scoped_nsobject<NSError> error([[NSError alloc] 52 base::scoped_nsobject<NSError> error([[NSError alloc]
66 initWithDomain:kJSEvaluationErrorDomain 53 initWithDomain:kJSEvaluationErrorDomain
67 code:JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW 54 code:JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW
68 userInfo:@{NSLocalizedDescriptionKey : error_message}]); 55 userInfo:@{NSLocalizedDescriptionKey : error_message}]);
69 completion_handler(nil, error); 56 completion_handler(nil, error);
70 }); 57 });
71 return; 58 return;
72 } 59 }
73 60
74 void (^web_view_completion_handler)(id, NSError*) = nil; 61 void (^web_view_completion_handler)(id, NSError*) = nil;
75 // Do not create a web_view_completion_handler if no |completion_handler| is 62 // Do not create a web_view_completion_handler if no |completion_handler| is
76 // passed to this function. WKWebView guarantees to call all completion 63 // passed to this function. WKWebView guarantees to call all completion
77 // handlers before deallocation. Passing nil as completion handler (when 64 // handlers before deallocation. Passing nil as completion handler (when
78 // appropriate) may speed up web view deallocation, because there will be no 65 // appropriate) may speed up web view deallocation, because there will be no
79 // need to call those completion handlers. 66 // need to call those completion handlers.
80 if (completion_handler) { 67 if (completion_handler) {
81 web_view_completion_handler = ^(id result, NSError* error) { 68 web_view_completion_handler = ^(id result, NSError* error) {
82 completion_handler(UIResultFromWKResult(result), error); 69 completion_handler(StringResultFromWKResult(result), error);
83 }; 70 };
84 } 71 }
85 [web_view evaluateJavaScript:script 72 [web_view evaluateJavaScript:script
86 completionHandler:web_view_completion_handler]; 73 completionHandler:web_view_completion_handler];
87 } 74 }
88 75
89 } // namespace web 76 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/web_state/ui/web_view_js_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698