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

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, 6 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
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 } 55 }
56 56
57 std::string MojoFacade::HandleMojoMessage( 57 std::string MojoFacade::HandleMojoMessage(
58 const std::string& mojo_message_as_json) { 58 const std::string& mojo_message_as_json) {
59 DCHECK_CURRENTLY_ON(WebThread::UI); 59 DCHECK_CURRENTLY_ON(WebThread::UI);
60 std::string name; 60 std::string name;
61 std::unique_ptr<base::DictionaryValue> args; 61 std::unique_ptr<base::DictionaryValue> args;
62 GetMessageNameAndArguments(mojo_message_as_json, &name, &args); 62 GetMessageNameAndArguments(mojo_message_as_json, &name, &args);
63 63
64 std::unique_ptr<base::Value> result; 64 std::unique_ptr<base::Value> result;
65 if (name == "interface_provider.getInterface") { 65 if (name == "Mojo.bindInterface") {
66 result = HandleInterfaceProviderGetInterface(args.get()); 66 result = HandleMojoBindInterface(args.get());
67 } else if (name == "core.close") { 67 } else if (name == "MojoHandle.close") {
68 result = HandleCoreClose(args.get()); 68 result = HandleMojoHandleClose(args.get());
69 } else if (name == "core.createMessagePipe") { 69 } else if (name == "Mojo.createMessagePipe") {
70 result = HandleCoreCreateMessagePipe(args.get()); 70 result = HandleMojoCreateMessagePipe(args.get());
71 } else if (name == "core.writeMessage") { 71 } else if (name == "MojoHandle.writeMessage") {
72 result = HandleCoreWriteMessage(args.get()); 72 result = HandleMojoHandleWriteMessage(args.get());
73 } else if (name == "core.readMessage") { 73 } else if (name == "MojoHandle.readMessage") {
74 result = HandleCoreReadMessage(args.get()); 74 result = HandleMojoHandleReadMessage(args.get());
75 } else if (name == "support.watch") { 75 } else if (name == "MojoHandle.watch") {
76 result = HandleSupportWatch(args.get()); 76 result = HandleMojoHandleWatch(args.get());
77 } else if (name == "support.cancelWatch") { 77 } else if (name == "MojoWatcher.cancel") {
78 result = HandleSupportCancelWatch(args.get()); 78 result = HandleMojoWatcherCancel(args.get());
79 } 79 }
80 80
81 if (!result) { 81 if (!result) {
82 return ""; 82 return "";
83 } 83 }
84 84
85 std::string json_result; 85 std::string json_result;
86 base::JSONWriter::Write(*result, &json_result); 86 base::JSONWriter::Write(*result, &json_result);
87 return json_result; 87 return json_result;
88 } 88 }
(...skipping 14 matching lines...) Expand all
103 std::string name; 103 std::string name;
104 CHECK(mojo_message->GetString("name", &name)); 104 CHECK(mojo_message->GetString("name", &name));
105 105
106 base::DictionaryValue* args = nullptr; 106 base::DictionaryValue* args = nullptr;
107 CHECK(mojo_message->GetDictionary("args", &args)); 107 CHECK(mojo_message->GetDictionary("args", &args));
108 108
109 *out_name = name; 109 *out_name = name;
110 *out_args = args->CreateDeepCopy(); 110 *out_args = args->CreateDeepCopy();
111 } 111 }
112 112
113 std::unique_ptr<base::Value> MojoFacade::HandleInterfaceProviderGetInterface( 113 std::unique_ptr<base::Value> MojoFacade::HandleMojoBindInterface(
114 const base::DictionaryValue* args) { 114 const base::DictionaryValue* args) {
115 const base::Value* interface_name_as_value = nullptr; 115 const base::Value* interface_name_as_value = nullptr;
116 CHECK(args->Get("interfaceName", &interface_name_as_value)); 116 CHECK(args->Get("interfaceName", &interface_name_as_value));
117 int raw_handle = 0;
118 CHECK(args->GetInteger("requestHandle", &raw_handle));
119
120 mojo::ScopedMessagePipeHandle handle(
121 static_cast<mojo::MessagePipeHandle>(raw_handle));
117 122
118 // By design interface_provider.getInterface either succeeds or crashes, so 123 // By design interface_provider.getInterface either succeeds or crashes, so
119 // check if interface name is a valid string is intentionally omitted. 124 // check if interface name is a valid string is intentionally omitted.
120 std::string interface_name_as_string; 125 std::string interface_name_as_string;
121 interface_name_as_value->GetAsString(&interface_name_as_string); 126 interface_name_as_value->GetAsString(&interface_name_as_string);
122 127
123 mojo::MessagePipe pipe;
124 interface_provider_->GetInterface(interface_name_as_string, 128 interface_provider_->GetInterface(interface_name_as_string,
125 std::move(pipe.handle0)); 129 std::move(handle));
126 130 return nullptr;
127 return ValueFromInteger(pipe.handle1.release().value());
128 } 131 }
129 132
130 std::unique_ptr<base::Value> MojoFacade::HandleCoreClose( 133 std::unique_ptr<base::Value> MojoFacade::HandleMojoHandleClose(
131 const base::DictionaryValue* args) { 134 const base::DictionaryValue* args) {
132 int handle = 0; 135 int handle = 0;
133 CHECK(args->GetInteger("handle", &handle)); 136 CHECK(args->GetInteger("handle", &handle));
134 137
135 mojo::Handle(handle).Close(); 138 mojo::Handle(handle).Close();
136 139 return nullptr;
137 return ValueFromInteger(MOJO_RESULT_OK);
138 } 140 }
139 141
140 std::unique_ptr<base::Value> MojoFacade::HandleCoreCreateMessagePipe( 142 std::unique_ptr<base::Value> MojoFacade::HandleMojoCreateMessagePipe(
141 base::DictionaryValue* args) { 143 base::DictionaryValue* args) {
142 const base::Value* options_as_value = nullptr; 144 mojo::ScopedMessagePipeHandle handle0, handle1;
143 CHECK(args->Get("optionsDict", &options_as_value)); 145 MojoResult mojo_result = mojo::CreateMessagePipe(nullptr, &handle0, &handle1);
144 146 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
Eugene But (OOO till 7-30) 2017/06/24 01:09:51 nit: s/new base::DictionaryValue/base::MakeUnique<
yzshen1 2017/06/26 20:09:59 Done.
yzshen1 2017/06/26 20:09:59 Done.
145 if (options_as_value->IsType(base::Value::Type::DICTIONARY)) { 147 result->SetInteger("result", mojo_result);
146 // There are no options defined for CreateMessagePipe yet. 148 if (mojo_result == MOJO_RESULT_OK) {
147 const base::DictionaryValue* options_as_dict; 149 result->SetInteger("handle0", handle0.release().value());
148 options_as_value->GetAsDictionary(&options_as_dict); 150 result->SetInteger("handle1", handle1.release().value());
149 CHECK(options_as_dict->empty());
150 } 151 }
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()); 152 return std::unique_ptr<base::Value>(result.release());
159 } 153 }
160 154
161 std::unique_ptr<base::Value> MojoFacade::HandleCoreWriteMessage( 155 std::unique_ptr<base::Value> MojoFacade::HandleMojoHandleWriteMessage(
162 base::DictionaryValue* args) { 156 base::DictionaryValue* args) {
163 int handle = 0; 157 int handle = 0;
164 CHECK(args->GetInteger("handle", &handle)); 158 CHECK(args->GetInteger("handle", &handle));
165 159
166 base::ListValue* handles_list = nullptr; 160 base::ListValue* handles_list = nullptr;
167 CHECK(args->GetList("handles", &handles_list)); 161 CHECK(args->GetList("handles", &handles_list));
168 162
169 base::DictionaryValue* buffer = nullptr; 163 base::DictionaryValue* buffer = nullptr;
170 CHECK(args->GetDictionary("buffer", &buffer)); 164 CHECK(args->GetDictionary("buffer", &buffer));
171 165
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; 166 int flags = MOJO_WRITE_MESSAGE_FLAG_NONE;
176 if (!flags_as_value->GetAsInteger(&flags)) {
177 flags = MOJO_WRITE_MESSAGE_FLAG_NONE;
178 }
179 167
180 std::vector<MojoHandle> handles(handles_list->GetSize()); 168 std::vector<MojoHandle> handles(handles_list->GetSize());
181 for (size_t i = 0; i < handles_list->GetSize(); i++) { 169 for (size_t i = 0; i < handles_list->GetSize(); i++) {
182 int one_handle = 0; 170 int one_handle = 0;
183 handles_list->GetInteger(i, &one_handle); 171 handles_list->GetInteger(i, &one_handle);
184 handles[i] = one_handle; 172 handles[i] = one_handle;
185 } 173 }
186 174
187 std::vector<uint8_t> bytes(buffer->size()); 175 std::vector<uint8_t> bytes(buffer->size());
188 for (size_t i = 0; i < buffer->size(); i++) { 176 for (size_t i = 0; i < buffer->size(); i++) {
189 int one_byte = 0; 177 int one_byte = 0;
190 buffer->GetInteger(base::IntToString(i), &one_byte); 178 buffer->GetInteger(base::IntToString(i), &one_byte);
191 bytes[i] = one_byte; 179 bytes[i] = one_byte;
192 } 180 }
193 181
194 mojo::MessagePipeHandle message_pipe(static_cast<MojoHandle>(handle)); 182 mojo::MessagePipeHandle message_pipe(static_cast<MojoHandle>(handle));
195 MojoResult result = 183 MojoResult result =
196 mojo::WriteMessageRaw(message_pipe, bytes.data(), bytes.size(), 184 mojo::WriteMessageRaw(message_pipe, bytes.data(), bytes.size(),
197 handles.data(), handles.size(), flags); 185 handles.data(), handles.size(), flags);
198 186
199 return ValueFromInteger(result); 187 return ValueFromInteger(result);
200 } 188 }
201 189
202 std::unique_ptr<base::Value> MojoFacade::HandleCoreReadMessage( 190 std::unique_ptr<base::Value> MojoFacade::HandleMojoHandleReadMessage(
203 const base::DictionaryValue* args) { 191 const base::DictionaryValue* args) {
204 const base::Value* handle_as_value = nullptr; 192 const base::Value* handle_as_value = nullptr;
205 CHECK(args->Get("handle", &handle_as_value)); 193 CHECK(args->Get("handle", &handle_as_value));
206 int handle_as_int = 0; 194 int handle_as_int = 0;
207 if (!handle_as_value->GetAsInteger(&handle_as_int)) { 195 if (!handle_as_value->GetAsInteger(&handle_as_int)) {
208 handle_as_int = 0; 196 handle_as_int = 0;
209 } 197 }
210 198
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; 199 int flags = MOJO_READ_MESSAGE_FLAG_NONE;
215 if (!flags_as_value->GetAsInteger(&flags)) {
216 flags = MOJO_READ_MESSAGE_FLAG_NONE;
217 }
218 200
219 std::vector<uint8_t> bytes; 201 std::vector<uint8_t> bytes;
220 std::vector<mojo::ScopedHandle> handles; 202 std::vector<mojo::ScopedHandle> handles;
221 mojo::MessagePipeHandle handle(static_cast<MojoHandle>(handle_as_int)); 203 mojo::MessagePipeHandle handle(static_cast<MojoHandle>(handle_as_int));
222 MojoResult mojo_result = 204 MojoResult mojo_result =
223 mojo::ReadMessageRaw(handle, &bytes, &handles, flags); 205 mojo::ReadMessageRaw(handle, &bytes, &handles, flags);
224 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); 206 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
225 if (mojo_result == MOJO_RESULT_OK) { 207 if (mojo_result == MOJO_RESULT_OK) {
226 base::ListValue* handles_list = new base::ListValue; 208 base::ListValue* handles_list = new base::ListValue;
227 for (uint32_t i = 0; i < handles.size(); i++) { 209 for (uint32_t i = 0; i < handles.size(); i++) {
228 handles_list->AppendInteger(handles[i].release().value()); 210 handles_list->AppendInteger(handles[i].release().value());
229 } 211 }
230 result->Set("handles", std::unique_ptr<base::Value>(handles_list)); 212 result->Set("handles", std::unique_ptr<base::Value>(handles_list));
231 213
232 base::ListValue* buffer = new base::ListValue; 214 base::ListValue* buffer = new base::ListValue;
233 for (uint32_t i = 0; i < bytes.size(); i++) { 215 for (uint32_t i = 0; i < bytes.size(); i++) {
234 buffer->AppendInteger(bytes[i]); 216 buffer->AppendInteger(bytes[i]);
235 } 217 }
236 result->Set("buffer", std::unique_ptr<base::Value>(buffer)); 218 result->Set("buffer", std::unique_ptr<base::Value>(buffer));
237 } 219 }
238 result->SetInteger("result", mojo_result); 220 result->SetInteger("result", mojo_result);
239 221
240 return std::unique_ptr<base::Value>(result.release()); 222 return std::unique_ptr<base::Value>(result.release());
241 } 223 }
242 224
243 std::unique_ptr<base::Value> MojoFacade::HandleSupportWatch( 225 std::unique_ptr<base::Value> MojoFacade::HandleMojoHandleWatch(
244 const base::DictionaryValue* args) { 226 const base::DictionaryValue* args) {
245 int handle = 0; 227 int handle = 0;
246 CHECK(args->GetInteger("handle", &handle)); 228 CHECK(args->GetInteger("handle", &handle));
247 int signals = 0; 229 int signals = 0;
248 CHECK(args->GetInteger("signals", &signals)); 230 CHECK(args->GetInteger("signals", &signals));
249 int callback_id; 231 int callback_id;
250 CHECK(args->GetInteger("callbackId", &callback_id)); 232 CHECK(args->GetInteger("callbackId", &callback_id));
251 233
252 mojo::SimpleWatcher::ReadyCallback callback = base::BindBlockArc(^( 234 mojo::SimpleWatcher::ReadyCallback callback =
253 MojoResult result) { 235 base::BindBlockArc(^(MojoResult result) {
254 NSString* script = 236 NSString* script = [NSString
255 [NSString stringWithFormat:@"__crWeb.mojo.signalWatch(%d, %d)", 237 stringWithFormat:
256 callback_id, result]; 238 @"Mojo.internal.watchCallbacksHolder.callCallback(%d, %d)",
257 [script_evaluator_ executeJavaScript:script completionHandler:nil]; 239 callback_id, result];
258 }); 240 [script_evaluator_ executeJavaScript:script completionHandler:nil];
241 });
259 mojo::SimpleWatcher* watcher = new mojo::SimpleWatcher( 242 mojo::SimpleWatcher* watcher = new mojo::SimpleWatcher(
260 FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::AUTOMATIC); 243 FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::AUTOMATIC);
261 watchers_.insert(std::make_pair(++last_watch_id_, base::WrapUnique(watcher))); 244 watchers_.insert(std::make_pair(++last_watch_id_, base::WrapUnique(watcher)));
262 watcher->Watch(static_cast<mojo::Handle>(handle), signals, callback); 245 watcher->Watch(static_cast<mojo::Handle>(handle), signals, callback);
263 return ValueFromInteger(last_watch_id_); 246 return ValueFromInteger(last_watch_id_);
264 } 247 }
265 248
266 std::unique_ptr<base::Value> MojoFacade::HandleSupportCancelWatch( 249 std::unique_ptr<base::Value> MojoFacade::HandleMojoWatcherCancel(
267 const base::DictionaryValue* args) { 250 const base::DictionaryValue* args) {
268 int watch_id = 0; 251 int watch_id = 0;
269 CHECK(args->GetInteger("watchId", &watch_id)); 252 CHECK(args->GetInteger("watchId", &watch_id));
270 watchers_.erase(watch_id); 253 watchers_.erase(watch_id);
271 return nullptr; 254 return nullptr;
272 } 255 }
273 256
274 } // namespace web 257 } // namespace web
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698