| 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 #include "base/mac/scoped_nsobject.h" | 12 #include "base/mac/scoped_nsobject.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 | 16 |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 18 #error "This file requires ARC support." |
| 19 #endif |
| 20 |
| 17 namespace { | 21 namespace { |
| 18 | 22 |
| 19 // Converts result of WKWebView script evaluation to base::Value, parsing | 23 // Converts result of WKWebView script evaluation to base::Value, parsing |
| 20 // |wk_result| up to a depth of |max_depth|. | 24 // |wk_result| up to a depth of |max_depth|. |
| 21 std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result, | 25 std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result, |
| 22 int max_depth) { | 26 int max_depth) { |
| 23 if (!wk_result) | 27 if (!wk_result) |
| 24 return nullptr; | 28 return nullptr; |
| 25 | 29 |
| 26 std::unique_ptr<base::Value> result; | 30 std::unique_ptr<base::Value> result; |
| 27 | 31 |
| 28 if (max_depth < 0) { | 32 if (max_depth < 0) { |
| 29 DLOG(WARNING) << "JS maximum recursion depth exceeded."; | 33 DLOG(WARNING) << "JS maximum recursion depth exceeded."; |
| 30 return result; | 34 return result; |
| 31 } | 35 } |
| 32 | 36 |
| 33 CFTypeID result_type = CFGetTypeID(wk_result); | 37 CFTypeID result_type = CFGetTypeID((__bridge CFTypeRef)wk_result); |
| 34 if (result_type == CFStringGetTypeID()) { | 38 if (result_type == CFStringGetTypeID()) { |
| 35 result.reset(new base::StringValue(base::SysNSStringToUTF16(wk_result))); | 39 result.reset(new base::StringValue(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::FundamentalValue([wk_result doubleValue])); | 42 result.reset(new base::FundamentalValue([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( | 45 result.reset( |
| 42 new base::FundamentalValue(static_cast<bool>([wk_result boolValue]))); | 46 new base::FundamentalValue(static_cast<bool>([wk_result boolValue]))); |
| 43 DCHECK(result->IsType(base::Value::TYPE_BOOLEAN)); | 47 DCHECK(result->IsType(base::Value::TYPE_BOOLEAN)); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 userInfo:@{NSLocalizedDescriptionKey : error_message}]); | 102 userInfo:@{NSLocalizedDescriptionKey : error_message}]); |
| 99 completion_handler(nil, error); | 103 completion_handler(nil, error); |
| 100 }); | 104 }); |
| 101 return; | 105 return; |
| 102 } | 106 } |
| 103 | 107 |
| 104 [web_view evaluateJavaScript:script completionHandler:completion_handler]; | 108 [web_view evaluateJavaScript:script completionHandler:completion_handler]; |
| 105 } | 109 } |
| 106 | 110 |
| 107 } // namespace web | 111 } // namespace web |
| OLD | NEW |