Skip to content

Commit

Permalink
Revert "[ios]Fix mojo for iOS webui."
Browse files Browse the repository at this point in the history
This reverts commit 50903c5.

Reason for revert: refers to generated file mojom-webui/url/mojom/url.mojom-webui.js without adding a dependency in BUILD.gn

Original change's description:
> [ios]Fix mojo for iOS webui.
>
> Fit new style of applying mojo to iOS webui.
> (1) Allow using new style of using xxx.mojom-webui.js to apply mojo
> to iOS webui.
> (2) Modify 'MojoHandle.writeMessage' to reduce traffic size. Change
> from indexed array to base64 encoded data.
> (3) Modify unittest file accordingly to pass the test.
>
> Bug: 1346504
> Change-Id: I2994e0a375acc83302aae4e40260f5aaaddd7d8b
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3780332
> Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
> Commit-Queue: Yuanqing Zhu <yuanqingzhu@microsoft.com>
> Cr-Commit-Position: refs/heads/main@{#1034427}

Bug: 1346504
Change-Id: I801861427904ec2a961ff8e3d6c330c44eac978a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3829189
Commit-Queue: Harry Souders <harrysouders@google.com>
Owners-Override: Harry Souders <harrysouders@google.com>
Reviewed-by: Chris Lu <thegreenfrog@chromium.org>
Cr-Commit-Position: refs/branch-heads/5242@{#3}
Cr-Branched-From: 032ef13-refs/heads/main@{#1035017}
  • Loading branch information
Chris Lu authored and Chromium LUCI CQ committed Aug 15, 2022
1 parent ebaafb0 commit acab981
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 101 deletions.
2 changes: 0 additions & 2 deletions ios/web/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,6 @@ grit("resources") {

outputs = [
"grit/ios_web_resources.h",
"grit/ios_web_resources_map.cc",
"grit/ios_web_resources_map.h",
"ios_web_resources.pak",
]
deps = [ "//mojo/public/js:bindings" ]
Expand Down
8 changes: 1 addition & 7 deletions ios/web/ios_web_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
<output filename="grit/ios_web_resources.h" type="rc_header">
<emit emit_type='prepend'></emit>
</output>
<output filename="grit/ios_web_resources_map.h" type="resource_map_header" />
<output filename="grit/ios_web_resources_map.cc" type="resource_file_map_source" />
<output filename="ios_web_resources.pak" type="data_package" />
</outputs>
<translations />
<release seq="1">
<includes>
<include name="IDR_IOS_MOJO_BINDINGS_JS" file="${root_gen_dir}/mojo/public/js/mojo_bindings.js" type="BINDATA" use_base_dir="false" skip_in_resource_map="true" />
<include name="IDR_IOS_URL_MOJOM_WEBUI_JS" file="${root_gen_dir}/mojom-webui/url/mojom/url.mojom-webui.js" resource_path="mojo/url/mojom/url.mojom-webui.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_IOS_ORIGIN_MOJO_WEBUI_JS" file="${root_gen_dir}/mojom-webui/url/mojom/origin.mojom-webui.js" resource_path="mojo/url/mojom/origin.mojom-webui.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_IOS_UI_WINDOW_OPEN_DISPOSITION_MOJO_WEBUI_JS" file="${root_gen_dir}/mojom-webui/ui/base/mojom/window_open_disposition.mojom-webui.js" resource_path="mojo/ui/base/mojom/window_open_disposition.mojom-webui.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_IOS_UI_BASE_TYPES_MOJO_WEBUI_JS" file="${root_gen_dir}/mojom-webui/ui/base/mojom/ui_base_types.mojom-webui.js" resource_path="mojo/ui/base/mojom/ui_base_types.mojom-webui.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_IOS_MOJO_BINDINGS_JS" file="${root_gen_dir}/mojo/public/js/mojo_bindings.js" type="BINDATA" use_base_dir="false" />
</includes>
</release>
</grit>
2 changes: 0 additions & 2 deletions ios/web/public/web_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ class WebState : public base::SupportsUserData {
base::RepeatingCallback<void(mojo::GenericPendingReceiver*)>;
void AddInterface(base::StringPiece interface_name, Callback callback);

void RemoveInterface(base::StringPiece interface_name);

// Attempts to bind |receiver| by matching its interface name against the
// callbacks registered on this InterfaceBinder.
void BindInterface(mojo::GenericPendingReceiver receiver);
Expand Down
5 changes: 0 additions & 5 deletions ios/web/web_state/web_state.mm
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@
callbacks_.emplace(std::string(interface_name), std::move(callback));
}

void WebState::InterfaceBinder::RemoveInterface(
base::StringPiece interface_name) {
callbacks_.erase(std::string(interface_name));
}

void WebState::InterfaceBinder::BindInterface(
mojo::GenericPendingReceiver receiver) {
DCHECK(receiver.is_valid());
Expand Down
1 change: 0 additions & 1 deletion ios/web/webui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ source_set("webui") {
"//ios/web/public/webui",
"//ios/web/web_state:web_state_impl_header",
"//mojo/public/cpp/system",
"//mojo/public/js:resources",
"//net",
"//ui/base",
"//ui/resources",
Expand Down
45 changes: 24 additions & 21 deletions ios/web/webui/mojo_facade.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@

#import "ios/web/webui/mojo_facade.h"

#import <stdint.h>
#include <stdint.h>

#import <limits>
#import <utility>
#import <vector>
#include <limits>
#include <utility>
#include <vector>

#import <Foundation/Foundation.h>

#import "base/base64.h"
#import "base/bind.h"
#include "base/bind.h"
#import "base/ios/block_types.h"
#import "base/json/json_reader.h"
#import "base/json/json_writer.h"
#import "base/strings/string_number_conversions.h"
#import "base/strings/sys_string_conversions.h"
#import "base/values.h"
#import "ios/web/public/js_messaging/web_frame.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
#include "ios/web/public/js_messaging/web_frame.h"
#import "ios/web/public/js_messaging/web_frame_util.h"
#import "ios/web/public/thread/web_thread.h"
#include "ios/web/public/thread/web_thread.h"
#import "ios/web/public/web_state.h"
#import "mojo/public/cpp/bindings/generic_pending_receiver.h"
#import "mojo/public/cpp/system/core.h"
#include "mojo/public/cpp/bindings/generic_pending_receiver.h"
#include "mojo/public/cpp/system/core.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
Expand Down Expand Up @@ -137,7 +136,7 @@
CHECK(handles_list);

const base::Value* buffer =
args.FindKeyOfType("buffer", base::Value::Type::STRING);
args.FindKeyOfType("buffer", base::Value::Type::DICTIONARY);
CHECK(buffer);

int flags = MOJO_WRITE_MESSAGE_FLAG_NONE;
Expand All @@ -148,15 +147,19 @@
int one_handle = handles_list_storage[i].GetInt();
handles[i] = one_handle;
}
absl::optional<std::vector<uint8_t>> bytes =
base::Base64Decode(buffer->GetString());
if (!bytes) {
return base::Value(static_cast<int>(MOJO_RESULT_INVALID_ARGUMENT));

std::vector<uint8_t> bytes(buffer->DictSize());
for (const auto item : buffer->DictItems()) {
size_t index = std::numeric_limits<size_t>::max();
CHECK(base::StringToSizeT(item.first, &index));
CHECK(index < bytes.size());
int one_byte = item.second.GetInt();
bytes[index] = one_byte;
}

mojo::MessagePipeHandle message_pipe(static_cast<MojoHandle>(*handle));
MojoResult result =
mojo::WriteMessageRaw(message_pipe, bytes->data(), bytes->size(),
mojo::WriteMessageRaw(message_pipe, bytes.data(), bytes.size(),
handles.data(), handles.size(), flags);

return base::Value(static_cast<int>(result));
Expand Down
11 changes: 4 additions & 7 deletions ios/web/webui/mojo_facade_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,8 @@ void CloseHandle(uint32_t handle) {
// Write to the other end of the pipe.
NSDictionary* write = @{
@"name" : @"MojoHandle.writeMessage",
@"args" : @{
@"handle" : @(handle1),
@"handles" : @[],
@"buffer" : @"QUJDRA==" // "ABCD"
},
@"args" :
@{@"handle" : @(handle1), @"handles" : @[], @"buffer" : @{@"0" : @0}},
};
std::string result_as_string = facade()->HandleMojoMessage(GetJson(write));
EXPECT_FALSE(result_as_string.empty());
Expand Down Expand Up @@ -241,7 +238,7 @@ void CloseHandle(uint32_t handle) {
@"args" : @{
@"handle" : @(handle1),
@"handles" : @[],
@"buffer" : @"QUJDRA==" // "ABCD"
@"buffer" : @{@"0" : @9, @"1" : @2, @"2" : @2008}
},
};
std::string result_as_string = facade()->HandleMojoMessage(GetJson(write));
Expand All @@ -260,7 +257,7 @@ void CloseHandle(uint32_t handle) {
NSDictionary* message = GetObject(facade()->HandleMojoMessage(GetJson(read)));
EXPECT_TRUE([message isKindOfClass:[NSDictionary class]]);
EXPECT_TRUE(message);
NSArray* expected_message = @[ @65, @66, @67, @68 ];
NSArray* expected_message = @[ @9, @2, @216 ]; // 2008 does not fit 8-bit.
EXPECT_NSEQ(expected_message, message[@"buffer"]);
EXPECT_FALSE([message[@"handles"] count]);
EXPECT_EQ(MOJO_RESULT_OK, [message[@"result"] unsignedIntValue]);
Expand Down
33 changes: 10 additions & 23 deletions ios/web/webui/shared_resources_data_source_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/web/webui/shared_resources_data_source_ios.h"
#include "ios/web/webui/shared_resources_data_source_ios.h"

#import <stddef.h>
#include <stddef.h>

#import "base/check.h"
#import "base/memory/ref_counted_memory.h"
#import "base/strings/string_util.h"
#import "ios/web/grit/ios_web_resources.h"
#import "ios/web/grit/ios_web_resources_map.h"
#include "base/check.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_util.h"
#import "ios/web/public/web_client.h"
#import "mojo/public/js/grit/mojo_bindings_resources.h"
#import "mojo/public/js/grit/mojo_bindings_resources_map.h"
#import "net/base/mime_util.h"
#import "ui/base/webui/resource_path.h"
#import "ui/base/webui/web_ui_util.h"
#import "ui/resources/grit/webui_generated_resources.h"
#import "ui/resources/grit/webui_generated_resources_map.h"
#include "net/base/mime_util.h"
#include "ui/base/webui/resource_path.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/resources/grit/webui_generated_resources.h"
#include "ui/resources/grit/webui_generated_resources_map.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
Expand All @@ -39,15 +35,6 @@
if (path == kWebuiGeneratedResources[i].path)
return &kWebuiGeneratedResources[i];
}
for (size_t i = 0; i < kMojoBindingsResourcesSize; ++i) {
if (path == kMojoBindingsResources[i].path)
return &kMojoBindingsResources[i];
}
for (size_t i = 0; i < kIosWebResourcesSize; ++i) {
if (path == kIosWebResources[i].path)
return &kIosWebResources[i];
}

return nullptr;
}

Expand Down
35 changes: 2 additions & 33 deletions ui/webui/resources/js/ios/mojo_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,14 @@ MojoHandle.prototype.watch = function(signals, callback) {
* @return {!MojoResult} Result code.
*/
MojoHandle.prototype.writeMessage = function(buffer, handles) {
let base64EncodedBuffer;
if (buffer instanceof Uint8Array) {
// calls from mojo_bindings.js
base64EncodedBuffer = _Uint8ArrayToBase64(buffer);
} else if (buffer instanceof ArrayBuffer) {
// calls from mojo/public/js/bindings.js
base64EncodedBuffer = _arrayBufferToBase64(buffer);
}
const nativeHandles = handles.map(function(handle) {
return handle.takeNativeHandle_();
});
return Mojo.internal.sendMessage({
name: 'MojoHandle.writeMessage',
args: {
handle: this.nativeHandle_,
buffer: base64EncodedBuffer,
buffer: buffer,
handles: nativeHandles,
},
});
Expand Down Expand Up @@ -210,7 +202,7 @@ const MojoWatcher = function(watchId) {
MojoWatcher.prototype.cancel = function() {
Mojo.internal.sendMessage(
{name: 'MojoWatcher.cancel', args: {watchId: this.watchId_}});
Mojo.internal.watchCallbacksHolder.removeWatchCallback(this.watchId_);
Mojo.internal.watchCallbacksHolder.removeWatchCallback(watchId);
};

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -307,26 +299,3 @@ Mojo.internal.watchCallbacksHolder = (function() {
removeWatchCallback: removeWatchCallback,
};
})();

/**
* Base64-encode an ArrayBuffer
* @param {ArrayBuffer} buffer
* @return {String}
*/
function _arrayBufferToBase64(buffer) {
return _Uint8ArrayToBase64(new Uint8Array(buffer));
}

/**
* Base64-encode an Uint8Array
* @param {Uint8Array} buffer
* @return {String}
*/
function _Uint8ArrayToBase64(bytes) {
let binary = '';
const numBytes = bytes.byteLength;
for (let i = 0; i < numBytes; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}

0 comments on commit acab981

Please sign in to comment.