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

Side by Side Diff: ios/web/webui/mojo_facade.mm

Issue 2946383002: Support new-style Mojo JS core API on IOS. (Closed)
Patch Set: . Created 3 years, 5 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 | « ios/web/webui/mojo_facade.h ('k') | ios/web/webui/mojo_facade_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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/webui/mojo_facade.h" 5 #import "ios/web/webui/mojo_facade.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 16 matching lines...) Expand all
27 #error "This file requires ARC support." 27 #error "This file requires ARC support."
28 #endif 28 #endif
29 29
30 namespace web { 30 namespace web {
31 31
32 namespace { 32 namespace {
33 33
34 // Wraps an integer into |base::Value| as |Type::INTEGER|. 34 // Wraps an integer into |base::Value| as |Type::INTEGER|.
35 template <typename IntegerT> 35 template <typename IntegerT>
36 std::unique_ptr<base::Value> ValueFromInteger(IntegerT handle) { 36 std::unique_ptr<base::Value> ValueFromInteger(IntegerT handle) {
37 return std::unique_ptr<base::Value>( 37 return base::MakeUnique<base::Value>(static_cast<int>(handle));
38 new base::Value(static_cast<int>(handle)));
39 } 38 }
40 39
41 } // namespace 40 } // namespace
42 41
43 MojoFacade::MojoFacade( 42 MojoFacade::MojoFacade(
44 service_manager::mojom::InterfaceProvider* interface_provider, 43 service_manager::mojom::InterfaceProvider* interface_provider,
45 id<CRWJSInjectionEvaluator> script_evaluator) 44 id<CRWJSInjectionEvaluator> script_evaluator)
46 : interface_provider_(interface_provider), 45 : interface_provider_(interface_provider),
47 script_evaluator_(script_evaluator) { 46 script_evaluator_(script_evaluator) {
48 DCHECK_CURRENTLY_ON(WebThread::UI); 47 DCHECK_CURRENTLY_ON(WebThread::UI);
49 DCHECK(interface_provider_); 48 DCHECK(interface_provider_);
50 DCHECK(script_evaluator_); 49 DCHECK(script_evaluator_);
51 } 50 }
52 51
53 MojoFacade::~MojoFacade() { 52 MojoFacade::~MojoFacade() {
54 DCHECK_CURRENTLY_ON(WebThread::UI); 53 DCHECK_CURRENTLY_ON(WebThread::UI);
55 } 54 }
56 55
57 std::string MojoFacade::HandleMojoMessage( 56 std::string MojoFacade::HandleMojoMessage(
58 const std::string& mojo_message_as_json) { 57 const std::string& mojo_message_as_json) {
59 DCHECK_CURRENTLY_ON(WebThread::UI); 58 DCHECK_CURRENTLY_ON(WebThread::UI);
60 std::string name; 59 std::string name;
61 std::unique_ptr<base::DictionaryValue> args; 60 std::unique_ptr<base::DictionaryValue> args;
62 GetMessageNameAndArguments(mojo_message_as_json, &name, &args); 61 GetMessageNameAndArguments(mojo_message_as_json, &name, &args);
63 62
64 std::unique_ptr<base::Value> result; 63 std::unique_ptr<base::Value> result;
65 if (name == "interface_provider.getInterface") { 64 if (name == "Mojo.bindInterface") {
66 result = HandleInterfaceProviderGetInterface(args.get()); 65 result = HandleMojoBindInterface(args.get());
67 } else if (name == "core.close") { 66 } else if (name == "MojoHandle.close") {
68 result = HandleCoreClose(args.get()); 67 result = HandleMojoHandleClose(args.get());
69 } else if (name == "core.createMessagePipe") { 68 } else if (name == "Mojo.createMessagePipe") {
70 result = HandleCoreCreateMessagePipe(args.get()); 69 result = HandleMojoCreateMessagePipe(args.get());
71 } else if (name == "core.writeMessage") { 70 } else if (name == "MojoHandle.writeMessage") {
72 result = HandleCoreWriteMessage(args.get()); 71 result = HandleMojoHandleWriteMessage(args.get());
73 } else if (name == "core.readMessage") { 72 } else if (name == "MojoHandle.readMessage") {
74 result = HandleCoreReadMessage(args.get()); 73 result = HandleMojoHandleReadMessage(args.get());
75 } else if (name == "support.watch") { 74 } else if (name == "MojoHandle.watch") {
76 result = HandleSupportWatch(args.get()); 75 result = HandleMojoHandleWatch(args.get());
77 } else if (name == "support.cancelWatch") { 76 } else if (name == "MojoWatcher.cancel") {
78 result = HandleSupportCancelWatch(args.get()); 77 result = HandleMojoWatcherCancel(args.get());
79 } 78 }
80 79
81 if (!result) { 80 if (!result) {
82 return ""; 81 return "";
83 } 82 }
84 83
85 std::string json_result; 84 std::string json_result;
86 base::JSONWriter::Write(*result, &json_result); 85 base::JSONWriter::Write(*result, &json_result);
87 return json_result; 86 return json_result;
88 } 87 }
(...skipping 14 matching lines...) Expand all
103 std::string name; 102 std::string name;
104 CHECK(mojo_message->GetString("name", &name)); 103 CHECK(mojo_message->GetString("name", &name));
105 104
106 base::DictionaryValue* args = nullptr; 105 base::DictionaryValue* args = nullptr;
107 CHECK(mojo_message->GetDictionary("args", &args)); 106 CHECK(mojo_message->GetDictionary("args", &args));
108 107
109 *out_name = name; 108 *out_name = name;
110 *out_args = args->CreateDeepCopy(); 109 *out_args = args->CreateDeepCopy();
111 } 110 }
112 111
113 std::unique_ptr<base::Value> MojoFacade::HandleInterfaceProviderGetInterface( 112 std::unique_ptr<base::Value> MojoFacade::HandleMojoBindInterface(
114 const base::DictionaryValue* args) { 113 const base::DictionaryValue* args) {
115 const base::Value* interface_name_as_value = nullptr; 114 const base::Value* interface_name_as_value = nullptr;
116 CHECK(args->Get("interfaceName", &interface_name_as_value)); 115 CHECK(args->Get("interfaceName", &interface_name_as_value));
116 int raw_handle = 0;
117 CHECK(args->GetInteger("requestHandle", &raw_handle));
118
119 mojo::ScopedMessagePipeHandle handle(
120 static_cast<mojo::MessagePipeHandle>(raw_handle));
117 121
118 // By design interface_provider.getInterface either succeeds or crashes, so 122 // By design interface_provider.getInterface either succeeds or crashes, so
119 // check if interface name is a valid string is intentionally omitted. 123 // check if interface name is a valid string is intentionally omitted.
120 std::string interface_name_as_string; 124 std::string interface_name_as_string;
121 interface_name_as_value->GetAsString(&interface_name_as_string); 125 interface_name_as_value->GetAsString(&interface_name_as_string);
122 126
123 mojo::MessagePipe pipe;
124 interface_provider_->GetInterface(interface_name_as_string, 127 interface_provider_->GetInterface(interface_name_as_string,
125 std::move(pipe.handle0)); 128 std::move(handle));
126 129 return nullptr;
127 return ValueFromInteger(pipe.handle1.release().value());
128 } 130 }
129 131
130 std::unique_ptr<base::Value> MojoFacade::HandleCoreClose( 132 std::unique_ptr<base::Value> MojoFacade::HandleMojoHandleClose(
131 const base::DictionaryValue* args) { 133 const base::DictionaryValue* args) {
132 int handle = 0; 134 int handle = 0;
133 CHECK(args->GetInteger("handle", &handle)); 135 CHECK(args->GetInteger("handle", &handle));
134 136
135 mojo::Handle(handle).Close(); 137 mojo::Handle(handle).Close();
136 138 return nullptr;
137 return ValueFromInteger(MOJO_RESULT_OK);
138 } 139 }
139 140
140 std::unique_ptr<base::Value> MojoFacade::HandleCoreCreateMessagePipe( 141 std::unique_ptr<base::Value> MojoFacade::HandleMojoCreateMessagePipe(
141 base::DictionaryValue* args) { 142 base::DictionaryValue* args) {
142 const base::Value* options_as_value = nullptr; 143 mojo::ScopedMessagePipeHandle handle0, handle1;
143 CHECK(args->Get("optionsDict", &options_as_value)); 144 MojoResult mojo_result = mojo::CreateMessagePipe(nullptr, &handle0, &handle1);
144 145 auto result = base::MakeUnique<base::DictionaryValue>();
145 if (options_as_value->IsType(base::Value::Type::DICTIONARY)) { 146 result->SetInteger("result", mojo_result);
146 // There are no options defined for CreateMessagePipe yet. 147 if (mojo_result == MOJO_RESULT_OK) {
147 const base::DictionaryValue* options_as_dict; 148 result->SetInteger("handle0", handle0.release().value());
148 options_as_value->GetAsDictionary(&options_as_dict); 149 result->SetInteger("handle1", handle1.release().value());
149 CHECK(options_as_dict->empty());
150 } 150 }
151
152 CHECK(options_as_value->IsType(base::Value::Type::NONE));
153
154 mojo::MessagePipe message_pipe;
155 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
156 result->SetInteger("handle0", message_pipe.handle0.release().value());
157 result->SetInteger("handle1", message_pipe.handle1.release().value());
158 return std::unique_ptr<base::Value>(result.release()); 151 return std::unique_ptr<base::Value>(result.release());
159 } 152 }
160 153
161 std::unique_ptr<base::Value> MojoFacade::HandleCoreWriteMessage( 154 std::unique_ptr<base::Value> MojoFacade::HandleMojoHandleWriteMessage(
162 base::DictionaryValue* args) { 155 base::DictionaryValue* args) {
163 int handle = 0; 156 int handle = 0;
164 CHECK(args->GetInteger("handle", &handle)); 157 CHECK(args->GetInteger("handle", &handle));
165 158
166 base::ListValue* handles_list = nullptr; 159 base::ListValue* handles_list = nullptr;
167 CHECK(args->GetList("handles", &handles_list)); 160 CHECK(args->GetList("handles", &handles_list));
168 161
169 base::DictionaryValue* buffer = nullptr; 162 base::DictionaryValue* buffer = nullptr;
170 CHECK(args->GetDictionary("buffer", &buffer)); 163 CHECK(args->GetDictionary("buffer", &buffer));
171 164
172 const base::Value* flags_as_value = nullptr;
173 CHECK(args->Get("flags", &flags_as_value));
174
175 int flags = MOJO_WRITE_MESSAGE_FLAG_NONE; 165 int flags = MOJO_WRITE_MESSAGE_FLAG_NONE;
176 if (!flags_as_value->GetAsInteger(&flags)) {
177 flags = MOJO_WRITE_MESSAGE_FLAG_NONE;
178 }
179 166
180 std::vector<MojoHandle> handles(handles_list->GetSize()); 167 std::vector<MojoHandle> handles(handles_list->GetSize());
181 for (size_t i = 0; i < handles_list->GetSize(); i++) { 168 for (size_t i = 0; i < handles_list->GetSize(); i++) {
182 int one_handle = 0; 169 int one_handle = 0;
183 handles_list->GetInteger(i, &one_handle); 170 handles_list->GetInteger(i, &one_handle);
184 handles[i] = one_handle; 171 handles[i] = one_handle;
185 } 172 }
186 173
187 std::vector<uint8_t> bytes(buffer->size()); 174 std::vector<uint8_t> bytes(buffer->size());
188 for (size_t i = 0; i < buffer->size(); i++) { 175 for (size_t i = 0; i < buffer->size(); i++) {
189 int one_byte = 0; 176 int one_byte = 0;
190 buffer->GetInteger(base::IntToString(i), &one_byte); 177 buffer->GetInteger(base::IntToString(i), &one_byte);
191 bytes[i] = one_byte; 178 bytes[i] = one_byte;
192 } 179 }
193 180
194 mojo::MessagePipeHandle message_pipe(static_cast<MojoHandle>(handle)); 181 mojo::MessagePipeHandle message_pipe(static_cast<MojoHandle>(handle));
195 MojoResult result = 182 MojoResult result =
196 mojo::WriteMessageRaw(message_pipe, bytes.data(), bytes.size(), 183 mojo::WriteMessageRaw(message_pipe, bytes.data(), bytes.size(),
197 handles.data(), handles.size(), flags); 184 handles.data(), handles.size(), flags);
198 185
199 return ValueFromInteger(result); 186 return ValueFromInteger(result);
200 } 187 }
201 188
202 std::unique_ptr<base::Value> MojoFacade::HandleCoreReadMessage( 189 std::unique_ptr<base::Value> MojoFacade::HandleMojoHandleReadMessage(
203 const base::DictionaryValue* args) { 190 const base::DictionaryValue* args) {
204 const base::Value* handle_as_value = nullptr; 191 const base::Value* handle_as_value = nullptr;
205 CHECK(args->Get("handle", &handle_as_value)); 192 CHECK(args->Get("handle", &handle_as_value));
206 int handle_as_int = 0; 193 int handle_as_int = 0;
207 if (!handle_as_value->GetAsInteger(&handle_as_int)) { 194 if (!handle_as_value->GetAsInteger(&handle_as_int)) {
208 handle_as_int = 0; 195 handle_as_int = 0;
209 } 196 }
210 197
211 const base::Value* flags_as_value = nullptr;
212 CHECK(args->Get("flags", &flags_as_value));
213
214 int flags = MOJO_READ_MESSAGE_FLAG_NONE; 198 int flags = MOJO_READ_MESSAGE_FLAG_NONE;
215 if (!flags_as_value->GetAsInteger(&flags)) {
216 flags = MOJO_READ_MESSAGE_FLAG_NONE;
217 }
218 199
219 std::vector<uint8_t> bytes; 200 std::vector<uint8_t> bytes;
220 std::vector<mojo::ScopedHandle> handles; 201 std::vector<mojo::ScopedHandle> handles;
221 mojo::MessagePipeHandle handle(static_cast<MojoHandle>(handle_as_int)); 202 mojo::MessagePipeHandle handle(static_cast<MojoHandle>(handle_as_int));
222 MojoResult mojo_result = 203 MojoResult mojo_result =
223 mojo::ReadMessageRaw(handle, &bytes, &handles, flags); 204 mojo::ReadMessageRaw(handle, &bytes, &handles, flags);
224 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); 205 auto result = base::MakeUnique<base::DictionaryValue>();
225 if (mojo_result == MOJO_RESULT_OK) { 206 if (mojo_result == MOJO_RESULT_OK) {
226 base::ListValue* handles_list = new base::ListValue; 207 auto handles_list = base::MakeUnique<base::ListValue>();
227 for (uint32_t i = 0; i < handles.size(); i++) { 208 for (uint32_t i = 0; i < handles.size(); i++) {
228 handles_list->AppendInteger(handles[i].release().value()); 209 handles_list->AppendInteger(handles[i].release().value());
229 } 210 }
230 result->Set("handles", std::unique_ptr<base::Value>(handles_list)); 211 result->Set("handles", std::move(handles_list));
231 212
232 base::ListValue* buffer = new base::ListValue; 213 auto buffer = base::MakeUnique<base::ListValue>();
233 for (uint32_t i = 0; i < bytes.size(); i++) { 214 for (uint32_t i = 0; i < bytes.size(); i++) {
234 buffer->AppendInteger(bytes[i]); 215 buffer->AppendInteger(bytes[i]);
235 } 216 }
236 result->Set("buffer", std::unique_ptr<base::Value>(buffer)); 217 result->Set("buffer", std::move(buffer));
237 } 218 }
238 result->SetInteger("result", mojo_result); 219 result->SetInteger("result", mojo_result);
239 220
240 return std::unique_ptr<base::Value>(result.release()); 221 return std::unique_ptr<base::Value>(result.release());
241 } 222 }
242 223
243 std::unique_ptr<base::Value> MojoFacade::HandleSupportWatch( 224 std::unique_ptr<base::Value> MojoFacade::HandleMojoHandleWatch(
244 const base::DictionaryValue* args) { 225 const base::DictionaryValue* args) {
245 int handle = 0; 226 int handle = 0;
246 CHECK(args->GetInteger("handle", &handle)); 227 CHECK(args->GetInteger("handle", &handle));
247 int signals = 0; 228 int signals = 0;
248 CHECK(args->GetInteger("signals", &signals)); 229 CHECK(args->GetInteger("signals", &signals));
249 int callback_id; 230 int callback_id;
250 CHECK(args->GetInteger("callbackId", &callback_id)); 231 CHECK(args->GetInteger("callbackId", &callback_id));
251 232
252 mojo::SimpleWatcher::ReadyCallback callback = base::BindBlockArc(^( 233 mojo::SimpleWatcher::ReadyCallback callback =
253 MojoResult result) { 234 base::BindBlockArc(^(MojoResult result) {
254 NSString* script = 235 NSString* script = [NSString
255 [NSString stringWithFormat:@"__crWeb.mojo.signalWatch(%d, %d)", 236 stringWithFormat:
256 callback_id, result]; 237 @"Mojo.internal.watchCallbacksHolder.callCallback(%d, %d)",
257 [script_evaluator_ executeJavaScript:script completionHandler:nil]; 238 callback_id, result];
258 }); 239 [script_evaluator_ executeJavaScript:script completionHandler:nil];
259 mojo::SimpleWatcher* watcher = new mojo::SimpleWatcher( 240 });
241 auto watcher = base::MakeUnique<mojo::SimpleWatcher>(
260 FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::AUTOMATIC); 242 FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::AUTOMATIC);
261 watchers_.insert(std::make_pair(++last_watch_id_, base::WrapUnique(watcher)));
262 watcher->Watch(static_cast<mojo::Handle>(handle), signals, callback); 243 watcher->Watch(static_cast<mojo::Handle>(handle), signals, callback);
244 watchers_.insert(std::make_pair(++last_watch_id_, std::move(watcher)));
263 return ValueFromInteger(last_watch_id_); 245 return ValueFromInteger(last_watch_id_);
264 } 246 }
265 247
266 std::unique_ptr<base::Value> MojoFacade::HandleSupportCancelWatch( 248 std::unique_ptr<base::Value> MojoFacade::HandleMojoWatcherCancel(
267 const base::DictionaryValue* args) { 249 const base::DictionaryValue* args) {
268 int watch_id = 0; 250 int watch_id = 0;
269 CHECK(args->GetInteger("watchId", &watch_id)); 251 CHECK(args->GetInteger("watchId", &watch_id));
270 watchers_.erase(watch_id); 252 watchers_.erase(watch_id);
271 return nullptr; 253 return nullptr;
272 } 254 }
273 255
274 } // namespace web 256 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/webui/mojo_facade.h ('k') | ios/web/webui/mojo_facade_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698