| 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/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 | 15 |
| 16 namespace { | |
| 17 | |
| 18 // Converts result of WKWebView script evaluation to a string. | |
| 19 NSString* StringResultFromWKResult(id result) { | |
| 20 if (!result) | |
| 21 return @""; | |
| 22 | |
| 23 CFTypeID result_type = CFGetTypeID(result); | |
| 24 if (result_type == CFStringGetTypeID()) | |
| 25 return result; | |
| 26 | |
| 27 if (result_type == CFNumberGetTypeID()) | |
| 28 return [result stringValue]; | |
| 29 | |
| 30 if (result_type == CFBooleanGetTypeID()) | |
| 31 return [result boolValue] ? @"true" : @"false"; | |
| 32 | |
| 33 if (result_type == CFNullGetTypeID()) | |
| 34 return @""; | |
| 35 | |
| 36 // TODO(stuartmorgan): Stringify other types. | |
| 37 NOTREACHED(); | |
| 38 return nil; | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 namespace web { | 16 namespace web { |
| 44 | 17 |
| 45 NSString* const kJSEvaluationErrorDomain = @"JSEvaluationError"; | 18 NSString* const kJSEvaluationErrorDomain = @"JSEvaluationError"; |
| 46 | 19 |
| 47 std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result) { | 20 std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result) { |
| 48 if (!wk_result) | 21 if (!wk_result) |
| 49 return nullptr; | 22 return nullptr; |
| 50 | 23 |
| 51 std::unique_ptr<base::Value> result; | 24 std::unique_ptr<base::Value> result; |
| 52 CFTypeID result_type = CFGetTypeID(wk_result); | 25 CFTypeID result_type = CFGetTypeID(wk_result); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 72 dictionary->Set(path, | 45 dictionary->Set(path, |
| 73 ValueResultFromWKResult([wk_result objectForKey:key])); | 46 ValueResultFromWKResult([wk_result objectForKey:key])); |
| 74 } | 47 } |
| 75 result = std::move(dictionary); | 48 result = std::move(dictionary); |
| 76 } else { | 49 } else { |
| 77 NOTREACHED(); // Convert other types as needed. | 50 NOTREACHED(); // Convert other types as needed. |
| 78 } | 51 } |
| 79 return result; | 52 return result; |
| 80 } | 53 } |
| 81 | 54 |
| 82 void EvaluateJavaScript(WKWebView* web_view, | |
| 83 NSString* script, | |
| 84 JavaScriptCompletion completion_handler) { | |
| 85 void (^web_view_completion_handler)(id, NSError*) = nil; | |
| 86 // Do not create a web_view_completion_handler if no |completion_handler| is | |
| 87 // passed to this function. WKWebView guarantees to call all completion | |
| 88 // handlers before deallocation. Passing nil as completion handler (when | |
| 89 // appropriate) may speed up web view deallocation, because there will be no | |
| 90 // need to call those completion handlers. | |
| 91 if (completion_handler) { | |
| 92 web_view_completion_handler = ^(id result, NSError* error) { | |
| 93 completion_handler(StringResultFromWKResult(result), error); | |
| 94 }; | |
| 95 } | |
| 96 ExecuteJavaScript(web_view, script, web_view_completion_handler); | |
| 97 } | |
| 98 | |
| 99 void ExecuteJavaScript(WKWebView* web_view, | 55 void ExecuteJavaScript(WKWebView* web_view, |
| 100 NSString* script, | 56 NSString* script, |
| 101 JavaScriptResultBlock completion_handler) { | 57 JavaScriptResultBlock completion_handler) { |
| 102 DCHECK([script length]); | 58 DCHECK([script length]); |
| 103 if (!web_view && completion_handler) { | 59 if (!web_view && completion_handler) { |
| 104 dispatch_async(dispatch_get_main_queue(), ^{ | 60 dispatch_async(dispatch_get_main_queue(), ^{ |
| 105 NSString* error_message = | 61 NSString* error_message = |
| 106 @"JS evaluation failed because there is no web view."; | 62 @"JS evaluation failed because there is no web view."; |
| 107 base::scoped_nsobject<NSError> error([[NSError alloc] | 63 base::scoped_nsobject<NSError> error([[NSError alloc] |
| 108 initWithDomain:kJSEvaluationErrorDomain | 64 initWithDomain:kJSEvaluationErrorDomain |
| 109 code:JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW | 65 code:JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW |
| 110 userInfo:@{NSLocalizedDescriptionKey : error_message}]); | 66 userInfo:@{NSLocalizedDescriptionKey : error_message}]); |
| 111 completion_handler(nil, error); | 67 completion_handler(nil, error); |
| 112 }); | 68 }); |
| 113 return; | 69 return; |
| 114 } | 70 } |
| 115 | 71 |
| 116 [web_view evaluateJavaScript:script completionHandler:completion_handler]; | 72 [web_view evaluateJavaScript:script completionHandler:completion_handler]; |
| 117 } | 73 } |
| 118 | 74 |
| 119 } // namespace web | 75 } // namespace web |
| OLD | NEW |