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 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #import <WebKit/WebKit.h> | 8 #import <WebKit/WebKit.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
| 12 #import "base/mac/scoped_nsobject.h" |
12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
13 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 | 16 |
16 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
17 #error "This file requires ARC support." | |
18 #endif | |
19 | |
20 namespace { | 17 namespace { |
21 | 18 |
22 // Converts result of WKWebView script evaluation to base::Value, parsing | 19 // Converts result of WKWebView script evaluation to base::Value, parsing |
23 // |wk_result| up to a depth of |max_depth|. | 20 // |wk_result| up to a depth of |max_depth|. |
24 std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result, | 21 std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result, |
25 int max_depth) { | 22 int max_depth) { |
26 if (!wk_result) | 23 if (!wk_result) |
27 return nullptr; | 24 return nullptr; |
28 | 25 |
29 std::unique_ptr<base::Value> result; | 26 std::unique_ptr<base::Value> result; |
30 | 27 |
31 if (max_depth < 0) { | 28 if (max_depth < 0) { |
32 DLOG(WARNING) << "JS maximum recursion depth exceeded."; | 29 DLOG(WARNING) << "JS maximum recursion depth exceeded."; |
33 return result; | 30 return result; |
34 } | 31 } |
35 | 32 |
36 CFTypeRef typeRef = (__bridge CFTypeRef)wk_result; | 33 CFTypeID result_type = CFGetTypeID(wk_result); |
37 CFTypeID result_type = CFGetTypeID(typeRef); | |
38 if (result_type == CFStringGetTypeID()) { | 34 if (result_type == CFStringGetTypeID()) { |
39 result.reset(new base::Value(base::SysNSStringToUTF16(wk_result))); | 35 result.reset(new base::Value(base::SysNSStringToUTF16(wk_result))); |
40 DCHECK(result->IsType(base::Value::Type::STRING)); | 36 DCHECK(result->IsType(base::Value::Type::STRING)); |
41 } else if (result_type == CFNumberGetTypeID()) { | 37 } else if (result_type == CFNumberGetTypeID()) { |
42 result.reset(new base::Value([wk_result doubleValue])); | 38 result.reset(new base::Value([wk_result doubleValue])); |
43 DCHECK(result->IsType(base::Value::Type::DOUBLE)); | 39 DCHECK(result->IsType(base::Value::Type::DOUBLE)); |
44 } else if (result_type == CFBooleanGetTypeID()) { | 40 } else if (result_type == CFBooleanGetTypeID()) { |
45 result.reset(new base::Value(static_cast<bool>([wk_result boolValue]))); | 41 result.reset(new base::Value(static_cast<bool>([wk_result boolValue]))); |
46 DCHECK(result->IsType(base::Value::Type::BOOLEAN)); | 42 DCHECK(result->IsType(base::Value::Type::BOOLEAN)); |
47 } else if (result_type == CFNullGetTypeID()) { | 43 } else if (result_type == CFNullGetTypeID()) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } | 84 } |
89 | 85 |
90 void ExecuteJavaScript(WKWebView* web_view, | 86 void ExecuteJavaScript(WKWebView* web_view, |
91 NSString* script, | 87 NSString* script, |
92 JavaScriptResultBlock completion_handler) { | 88 JavaScriptResultBlock completion_handler) { |
93 DCHECK([script length]); | 89 DCHECK([script length]); |
94 if (!web_view && completion_handler) { | 90 if (!web_view && completion_handler) { |
95 dispatch_async(dispatch_get_main_queue(), ^{ | 91 dispatch_async(dispatch_get_main_queue(), ^{ |
96 NSString* error_message = | 92 NSString* error_message = |
97 @"JS evaluation failed because there is no web view."; | 93 @"JS evaluation failed because there is no web view."; |
98 NSError* error = [[NSError alloc] | 94 base::scoped_nsobject<NSError> error([[NSError alloc] |
99 initWithDomain:kJSEvaluationErrorDomain | 95 initWithDomain:kJSEvaluationErrorDomain |
100 code:JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW | 96 code:JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW |
101 userInfo:@{NSLocalizedDescriptionKey : error_message}]; | 97 userInfo:@{NSLocalizedDescriptionKey : error_message}]); |
102 completion_handler(nil, error); | 98 completion_handler(nil, error); |
103 }); | 99 }); |
104 return; | 100 return; |
105 } | 101 } |
106 | 102 |
107 [web_view evaluateJavaScript:script completionHandler:completion_handler]; | 103 [web_view evaluateJavaScript:script completionHandler:completion_handler]; |
108 } | 104 } |
109 | 105 |
110 } // namespace web | 106 } // namespace web |
OLD | NEW |