| 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/scoped_nsobject.h" | 12 #include "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 namespace web { | 17 namespace { |
| 17 | 18 |
| 18 NSString* const kJSEvaluationErrorDomain = @"JSEvaluationError"; | 19 // Converts result of WKWebView script evaluation to base::Value, parsing |
| 19 | 20 // |wk_result| up to a depth of |max_depth|. |
| 20 std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result) { | 21 std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result, |
| 22 int max_depth) { |
| 21 if (!wk_result) | 23 if (!wk_result) |
| 22 return nullptr; | 24 return nullptr; |
| 23 | 25 |
| 24 std::unique_ptr<base::Value> result; | 26 std::unique_ptr<base::Value> result; |
| 27 |
| 28 if (max_depth < 0) { |
| 29 DLOG(WARNING) << "JS maximum recursion depth exceeded."; |
| 30 return result; |
| 31 } |
| 32 |
| 25 CFTypeID result_type = CFGetTypeID(wk_result); | 33 CFTypeID result_type = CFGetTypeID(wk_result); |
| 26 if (result_type == CFStringGetTypeID()) { | 34 if (result_type == CFStringGetTypeID()) { |
| 27 result.reset(new base::StringValue(base::SysNSStringToUTF16(wk_result))); | 35 result.reset(new base::StringValue(base::SysNSStringToUTF16(wk_result))); |
| 28 DCHECK(result->IsType(base::Value::TYPE_STRING)); | 36 DCHECK(result->IsType(base::Value::TYPE_STRING)); |
| 29 } else if (result_type == CFNumberGetTypeID()) { | 37 } else if (result_type == CFNumberGetTypeID()) { |
| 30 result.reset(new base::FundamentalValue([wk_result doubleValue])); | 38 result.reset(new base::FundamentalValue([wk_result doubleValue])); |
| 31 DCHECK(result->IsType(base::Value::TYPE_DOUBLE)); | 39 DCHECK(result->IsType(base::Value::TYPE_DOUBLE)); |
| 32 } else if (result_type == CFBooleanGetTypeID()) { | 40 } else if (result_type == CFBooleanGetTypeID()) { |
| 33 result.reset( | 41 result.reset( |
| 34 new base::FundamentalValue(static_cast<bool>([wk_result boolValue]))); | 42 new base::FundamentalValue(static_cast<bool>([wk_result boolValue]))); |
| 35 DCHECK(result->IsType(base::Value::TYPE_BOOLEAN)); | 43 DCHECK(result->IsType(base::Value::TYPE_BOOLEAN)); |
| 36 } else if (result_type == CFNullGetTypeID()) { | 44 } else if (result_type == CFNullGetTypeID()) { |
| 37 result = base::Value::CreateNullValue(); | 45 result = base::Value::CreateNullValue(); |
| 38 DCHECK(result->IsType(base::Value::TYPE_NULL)); | 46 DCHECK(result->IsType(base::Value::TYPE_NULL)); |
| 39 } else if (result_type == CFDictionaryGetTypeID()) { | 47 } else if (result_type == CFDictionaryGetTypeID()) { |
| 40 std::unique_ptr<base::DictionaryValue> dictionary = | 48 std::unique_ptr<base::DictionaryValue> dictionary = |
| 41 base::MakeUnique<base::DictionaryValue>(); | 49 base::MakeUnique<base::DictionaryValue>(); |
| 42 for (id key in wk_result) { | 50 for (id key in wk_result) { |
| 43 DCHECK([key respondsToSelector:@selector(UTF8String)]); | 51 NSString* obj_c_string = base::mac::ObjCCast<NSString>(key); |
| 44 const std::string& path([key UTF8String]); | 52 const std::string path = base::SysNSStringToUTF8(obj_c_string); |
| 45 dictionary->Set(path, | 53 std::unique_ptr<base::Value> value = ValueResultFromWKResult( |
| 46 ValueResultFromWKResult([wk_result objectForKey:key])); | 54 [wk_result objectForKey:obj_c_string], max_depth - 1); |
| 55 if (value) { |
| 56 dictionary->Set(path, std::move(value)); |
| 57 } |
| 47 } | 58 } |
| 48 result = std::move(dictionary); | 59 result = std::move(dictionary); |
| 49 } else { | 60 } else { |
| 50 NOTREACHED(); // Convert other types as needed. | 61 NOTREACHED(); // Convert other types as needed. |
| 51 } | 62 } |
| 52 return result; | 63 return result; |
| 53 } | 64 } |
| 54 | 65 |
| 66 } // namespace |
| 67 |
| 68 namespace web { |
| 69 |
| 70 NSString* const kJSEvaluationErrorDomain = @"JSEvaluationError"; |
| 71 int const kMaximumParsingRecursionDepth = 6; |
| 72 |
| 73 std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result) { |
| 74 return ::ValueResultFromWKResult(wk_result, kMaximumParsingRecursionDepth); |
| 75 } |
| 76 |
| 55 void ExecuteJavaScript(WKWebView* web_view, | 77 void ExecuteJavaScript(WKWebView* web_view, |
| 56 NSString* script, | 78 NSString* script, |
| 57 JavaScriptResultBlock completion_handler) { | 79 JavaScriptResultBlock completion_handler) { |
| 58 DCHECK([script length]); | 80 DCHECK([script length]); |
| 59 if (!web_view && completion_handler) { | 81 if (!web_view && completion_handler) { |
| 60 dispatch_async(dispatch_get_main_queue(), ^{ | 82 dispatch_async(dispatch_get_main_queue(), ^{ |
| 61 NSString* error_message = | 83 NSString* error_message = |
| 62 @"JS evaluation failed because there is no web view."; | 84 @"JS evaluation failed because there is no web view."; |
| 63 base::scoped_nsobject<NSError> error([[NSError alloc] | 85 base::scoped_nsobject<NSError> error([[NSError alloc] |
| 64 initWithDomain:kJSEvaluationErrorDomain | 86 initWithDomain:kJSEvaluationErrorDomain |
| 65 code:JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW | 87 code:JS_EVALUATION_ERROR_CODE_NO_WEB_VIEW |
| 66 userInfo:@{NSLocalizedDescriptionKey : error_message}]); | 88 userInfo:@{NSLocalizedDescriptionKey : error_message}]); |
| 67 completion_handler(nil, error); | 89 completion_handler(nil, error); |
| 68 }); | 90 }); |
| 69 return; | 91 return; |
| 70 } | 92 } |
| 71 | 93 |
| 72 [web_view evaluateJavaScript:script completionHandler:completion_handler]; | 94 [web_view evaluateJavaScript:script completionHandler:completion_handler]; |
| 73 } | 95 } |
| 74 | 96 |
| 75 } // namespace web | 97 } // namespace web |
| OLD | NEW |