| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/web/webui/mojo_facade.h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 |
| 9 #include "base/ios/block_types.h" |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" |
| 12 #include "base/mac/bind_objc_block.h" |
| 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/values.h" |
| 15 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h" |
| 16 #include "ios/web/public/web_thread.h" |
| 17 #include "mojo/public/cpp/system/core.h" |
| 18 #include "services/shell/public/interfaces/interface_provider.mojom.h" |
| 19 |
| 20 namespace web { |
| 21 |
| 22 namespace { |
| 23 |
| 24 // Wraps an integer into |base::Value| as |TYPE_INTEGER|. |
| 25 template <typename IntegerT> |
| 26 std::unique_ptr<base::Value> ValueFromInteger(IntegerT handle) { |
| 27 return std::unique_ptr<base::Value>( |
| 28 new base::FundamentalValue(static_cast<int>(handle))); |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 MojoFacade::MojoFacade(shell::mojom::InterfaceProvider* interface_provider, |
| 34 id<CRWJSInjectionEvaluator> script_evaluator) |
| 35 : interface_provider_(interface_provider), |
| 36 script_evaluator_(script_evaluator) { |
| 37 DCHECK_CURRENTLY_ON(WebThread::UI); |
| 38 DCHECK(interface_provider_); |
| 39 DCHECK(script_evaluator_); |
| 40 } |
| 41 |
| 42 MojoFacade::~MojoFacade() { |
| 43 DCHECK_CURRENTLY_ON(WebThread::UI); |
| 44 } |
| 45 |
| 46 std::string MojoFacade::HandleMojoMessage( |
| 47 const std::string& mojo_message_as_json) { |
| 48 DCHECK_CURRENTLY_ON(WebThread::UI); |
| 49 std::string name; |
| 50 std::unique_ptr<base::DictionaryValue> args; |
| 51 GetMessageNameAndArguments(mojo_message_as_json, &name, &args); |
| 52 |
| 53 std::unique_ptr<base::Value> result; |
| 54 if (name == "service_provider.connectToService") { |
| 55 result = HandleServiceProviderConnectToService(args.get()); |
| 56 } else if (name == "core.close") { |
| 57 result = HandleCoreClose(args.get()); |
| 58 } else if (name == "core.createMessagePipe") { |
| 59 result = HandleCoreCreateMessagePipe(args.get()); |
| 60 } else if (name == "core.writeMessage") { |
| 61 result = HandleCoreWriteMessage(args.get()); |
| 62 } else if (name == "core.readMessage") { |
| 63 result = HandleCoreReadMessage(args.get()); |
| 64 } else if (name == "support.watch") { |
| 65 result = HandleSupportWatch(args.get()); |
| 66 } else if (name == "support.cancelWatch") { |
| 67 result = HandleSupportCancelWatch(args.get()); |
| 68 } |
| 69 |
| 70 if (!result) { |
| 71 return ""; |
| 72 } |
| 73 |
| 74 std::string json_result; |
| 75 base::JSONWriter::Write(*result, &json_result); |
| 76 return json_result; |
| 77 } |
| 78 |
| 79 void MojoFacade::GetMessageNameAndArguments( |
| 80 const std::string& mojo_message_as_json, |
| 81 std::string* out_name, |
| 82 std::unique_ptr<base::DictionaryValue>* out_args) { |
| 83 int error_code = 0; |
| 84 std::string error_message; |
| 85 std::unique_ptr<base::Value> mojo_message_as_value( |
| 86 base::JSONReader::ReadAndReturnError(mojo_message_as_json, false, |
| 87 &error_code, &error_message)); |
| 88 CHECK(!error_code); |
| 89 base::DictionaryValue* mojo_message = nullptr; |
| 90 CHECK(mojo_message_as_value->GetAsDictionary(&mojo_message)); |
| 91 |
| 92 std::string name; |
| 93 CHECK(mojo_message->GetString("name", &name)); |
| 94 |
| 95 base::DictionaryValue* args = nullptr; |
| 96 CHECK(mojo_message->GetDictionary("args", &args)); |
| 97 |
| 98 *out_name = name; |
| 99 *out_args = args->CreateDeepCopy(); |
| 100 } |
| 101 |
| 102 std::unique_ptr<base::Value> MojoFacade::HandleServiceProviderConnectToService( |
| 103 const base::DictionaryValue* args) { |
| 104 const base::Value* service_name_as_value = nullptr; |
| 105 CHECK(args->Get("serviceName", &service_name_as_value)); |
| 106 |
| 107 // By design service_provider.connectToService either succeeds or crashes, so |
| 108 // check if service name is a valid string is intentionally omitted. |
| 109 std::string service_name_as_string; |
| 110 service_name_as_value->GetAsString(&service_name_as_string); |
| 111 |
| 112 mojo::MessagePipe pipe; |
| 113 interface_provider_->GetInterface(mojo::String::From(service_name_as_string), |
| 114 std::move(pipe.handle0)); |
| 115 |
| 116 return ValueFromInteger(pipe.handle1.release().value()); |
| 117 } |
| 118 |
| 119 std::unique_ptr<base::Value> MojoFacade::HandleCoreClose( |
| 120 const base::DictionaryValue* args) { |
| 121 int handle = 0; |
| 122 CHECK(args->GetInteger("handle", &handle)); |
| 123 |
| 124 mojo::Handle(handle).Close(); |
| 125 |
| 126 return ValueFromInteger(MOJO_RESULT_OK); |
| 127 } |
| 128 |
| 129 std::unique_ptr<base::Value> MojoFacade::HandleCoreCreateMessagePipe( |
| 130 base::DictionaryValue* args) { |
| 131 const base::Value* options_as_value = nullptr; |
| 132 CHECK(args->Get("optionsDict", &options_as_value)); |
| 133 |
| 134 if (options_as_value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 135 // There are no options defined for CreateMessagePipe yet. |
| 136 const base::DictionaryValue* options_as_dict; |
| 137 options_as_value->GetAsDictionary(&options_as_dict); |
| 138 CHECK(options_as_dict->empty()); |
| 139 } |
| 140 |
| 141 CHECK(options_as_value->IsType(base::Value::TYPE_NULL)); |
| 142 |
| 143 mojo::MessagePipe message_pipe; |
| 144 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
| 145 result->SetInteger("handle0", message_pipe.handle0.release().value()); |
| 146 result->SetInteger("handle1", message_pipe.handle1.release().value()); |
| 147 return std::unique_ptr<base::Value>(result.release()); |
| 148 } |
| 149 |
| 150 std::unique_ptr<base::Value> MojoFacade::HandleCoreWriteMessage( |
| 151 base::DictionaryValue* args) { |
| 152 int handle = 0; |
| 153 CHECK(args->GetInteger("handle", &handle)); |
| 154 |
| 155 base::ListValue* handles_list = nullptr; |
| 156 CHECK(args->GetList("handles", &handles_list)); |
| 157 |
| 158 base::DictionaryValue* buffer = nullptr; |
| 159 CHECK(args->GetDictionary("buffer", &buffer)); |
| 160 |
| 161 const base::Value* flags_as_value = nullptr; |
| 162 CHECK(args->Get("flags", &flags_as_value)); |
| 163 |
| 164 int flags = MOJO_WRITE_MESSAGE_FLAG_NONE; |
| 165 if (!flags_as_value->GetAsInteger(&flags)) { |
| 166 flags = MOJO_WRITE_MESSAGE_FLAG_NONE; |
| 167 } |
| 168 |
| 169 std::vector<MojoHandle> handles(handles_list->GetSize()); |
| 170 for (size_t i = 0; i < handles_list->GetSize(); i++) { |
| 171 int one_handle = 0; |
| 172 handles_list->GetInteger(i, &one_handle); |
| 173 handles[i] = one_handle; |
| 174 } |
| 175 |
| 176 std::vector<uint8_t> bytes(buffer->size()); |
| 177 for (size_t i = 0; i < buffer->size(); i++) { |
| 178 int one_byte = 0; |
| 179 buffer->GetInteger(base::IntToString(i), &one_byte); |
| 180 bytes[i] = one_byte; |
| 181 } |
| 182 |
| 183 mojo::MessagePipeHandle message_pipe(static_cast<MojoHandle>(handle)); |
| 184 MojoResult result = |
| 185 mojo::WriteMessageRaw(message_pipe, bytes.data(), bytes.size(), |
| 186 handles.data(), handles.size(), flags); |
| 187 |
| 188 return ValueFromInteger(result); |
| 189 } |
| 190 |
| 191 std::unique_ptr<base::Value> MojoFacade::HandleCoreReadMessage( |
| 192 const base::DictionaryValue* args) { |
| 193 const base::Value* handle_as_value = nullptr; |
| 194 CHECK(args->Get("handle", &handle_as_value)); |
| 195 int handle_as_int = 0; |
| 196 if (!handle_as_value->GetAsInteger(&handle_as_int)) { |
| 197 handle_as_int = 0; |
| 198 } |
| 199 |
| 200 const base::Value* flags_as_value = nullptr; |
| 201 CHECK(args->Get("flags", &flags_as_value)); |
| 202 |
| 203 int flags = MOJO_READ_MESSAGE_FLAG_NONE; |
| 204 if (!flags_as_value->GetAsInteger(&flags)) { |
| 205 flags = MOJO_READ_MESSAGE_FLAG_NONE; |
| 206 } |
| 207 |
| 208 uint32_t num_bytes = 0; |
| 209 uint32_t num_handles = 0; |
| 210 mojo::MessagePipeHandle handle(static_cast<MojoHandle>(handle_as_int)); |
| 211 MojoResult mojo_result = mojo::ReadMessageRaw(handle, nullptr, &num_bytes, |
| 212 nullptr, &num_handles, flags); |
| 213 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
| 214 |
| 215 if (mojo_result == MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| 216 std::vector<uint8_t> bytes(num_bytes); |
| 217 std::vector<MojoHandle> handles(num_handles); |
| 218 mojo_result = mojo::ReadMessageRaw(handle, bytes.data(), &num_bytes, |
| 219 handles.data(), &num_handles, flags); |
| 220 |
| 221 base::ListValue* handles_list = new base::ListValue; |
| 222 for (uint32_t i = 0; i < num_handles; i++) { |
| 223 handles_list->AppendInteger(handles[i]); |
| 224 } |
| 225 result->Set("handles", std::unique_ptr<base::Value>(handles_list)); |
| 226 |
| 227 base::ListValue* buffer = new base::ListValue; |
| 228 for (uint32_t i = 0; i < num_bytes; i++) { |
| 229 buffer->AppendInteger(bytes[i]); |
| 230 } |
| 231 result->Set("buffer", std::unique_ptr<base::Value>(buffer)); |
| 232 } |
| 233 result->SetInteger("result", mojo_result); |
| 234 |
| 235 return std::unique_ptr<base::Value>(result.release()); |
| 236 } |
| 237 |
| 238 std::unique_ptr<base::Value> MojoFacade::HandleSupportWatch( |
| 239 const base::DictionaryValue* args) { |
| 240 int handle = 0; |
| 241 CHECK(args->GetInteger("handle", &handle)); |
| 242 int signals = 0; |
| 243 CHECK(args->GetInteger("signals", &signals)); |
| 244 int callback_id; |
| 245 CHECK(args->GetInteger("callbackId", &callback_id)); |
| 246 |
| 247 mojo::Watcher::ReadyCallback callback = base::BindBlock(^(MojoResult result) { |
| 248 NSString* script = |
| 249 [NSString stringWithFormat:@"__crWeb.mojo.signalWatch(%d, %d)", |
| 250 callback_id, result]; |
| 251 [script_evaluator_ executeJavaScript:script completionHandler:nil]; |
| 252 }); |
| 253 |
| 254 mojo::Watcher& watcher = watchers_[++last_watch_id_]; |
| 255 watcher.Start(static_cast<mojo::Handle>(handle), signals, callback); |
| 256 return ValueFromInteger(last_watch_id_); |
| 257 } |
| 258 |
| 259 std::unique_ptr<base::Value> MojoFacade::HandleSupportCancelWatch( |
| 260 const base::DictionaryValue* args) { |
| 261 int watch_id = 0; |
| 262 CHECK(args->GetInteger("watchId", &watch_id)); |
| 263 watchers_.erase(watch_id); |
| 264 return nullptr; |
| 265 } |
| 266 |
| 267 } // namespace web |
| OLD | NEW |