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