Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: ios/web/web_state/ui/web_view_js_utils.mm

Issue 2275513002: Support having javascript return dictionaries. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ios/web/web_state/ui/web_view_js_utils_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/strings/sys_string_conversions.h" 13 #include "base/strings/sys_string_conversions.h"
13 #include "base/values.h" 14 #include "base/values.h"
14 15
15 namespace { 16 namespace {
16 17
17 // Converts result of WKWebView script evaluation to a string. 18 // Converts result of WKWebView script evaluation to a string.
18 NSString* StringResultFromWKResult(id result) { 19 NSString* StringResultFromWKResult(id result) {
19 if (!result) 20 if (!result)
20 return @""; 21 return @"";
21 22
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 } else if (result_type == CFNumberGetTypeID()) { 56 } else if (result_type == CFNumberGetTypeID()) {
56 result.reset(new base::FundamentalValue([wk_result doubleValue])); 57 result.reset(new base::FundamentalValue([wk_result doubleValue]));
57 DCHECK(result->IsType(base::Value::TYPE_DOUBLE)); 58 DCHECK(result->IsType(base::Value::TYPE_DOUBLE));
58 } else if (result_type == CFBooleanGetTypeID()) { 59 } else if (result_type == CFBooleanGetTypeID()) {
59 result.reset( 60 result.reset(
60 new base::FundamentalValue(static_cast<bool>([wk_result boolValue]))); 61 new base::FundamentalValue(static_cast<bool>([wk_result boolValue])));
61 DCHECK(result->IsType(base::Value::TYPE_BOOLEAN)); 62 DCHECK(result->IsType(base::Value::TYPE_BOOLEAN));
62 } else if (result_type == CFNullGetTypeID()) { 63 } else if (result_type == CFNullGetTypeID()) {
63 result = base::Value::CreateNullValue(); 64 result = base::Value::CreateNullValue();
64 DCHECK(result->IsType(base::Value::TYPE_NULL)); 65 DCHECK(result->IsType(base::Value::TYPE_NULL));
66 } else if (result_type == CFDictionaryGetTypeID()) {
67 std::unique_ptr<base::DictionaryValue> dictionary =
68 base::MakeUnique<base::DictionaryValue>();
69 for (id key in wk_result) {
70 DCHECK([key respondsToSelector:@selector(UTF8String)]);
71 const std::string& path([key UTF8String]);
Eugene But (OOO till 7-30) 2016/09/09 15:26:21 Please use sys_string_conversions instead of |UTF8
jif-google 2016/09/09 17:54:45 Acknowledged.
Eugene But (OOO till 7-30) 2016/09/09 18:28:57 [key UTF8String] may return nil, which will crash
72 dictionary->Set(path,
73 ValueResultFromWKResult([wk_result objectForKey:key]));
Eugene But (OOO till 7-30) 2016/09/09 15:26:21 This JSON is coming from internet, which means tha
jif-google 2016/09/09 17:54:45 When does the JSON come from the internet?
Eugene But (OOO till 7-30) 2016/09/09 18:28:57 wk_result is the result of script evaluation, for
74 }
75 result = std::move(dictionary);
65 } else { 76 } else {
66 NOTREACHED(); // Convert other types as needed. 77 NOTREACHED(); // Convert other types as needed.
67 } 78 }
68 return result; 79 return result;
69 } 80 }
70 81
71 void EvaluateJavaScript(WKWebView* web_view, 82 void EvaluateJavaScript(WKWebView* web_view,
72 NSString* script, 83 NSString* script,
73 JavaScriptCompletion completion_handler) { 84 JavaScriptCompletion completion_handler) {
74 void (^web_view_completion_handler)(id, NSError*) = nil; 85 void (^web_view_completion_handler)(id, NSError*) = nil;
(...skipping 24 matching lines...) Expand all
99 userInfo:@{NSLocalizedDescriptionKey : error_message}]); 110 userInfo:@{NSLocalizedDescriptionKey : error_message}]);
100 completion_handler(nil, error); 111 completion_handler(nil, error);
101 }); 112 });
102 return; 113 return;
103 } 114 }
104 115
105 [web_view evaluateJavaScript:script completionHandler:completion_handler]; 116 [web_view evaluateJavaScript:script completionHandler:completion_handler];
106 } 117 }
107 118
108 } // namespace web 119 } // namespace web
OLDNEW
« no previous file with comments | « no previous file | ios/web/web_state/ui/web_view_js_utils_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698