Skip to content

Commit

Permalink
Mojo JS bindings: change module loading solution.
Browse files Browse the repository at this point in the history
This change takes place on the mojo/public/js/new_bindings copy so it doesn't
affect existing users.

- This change gets rid of AMD module loading. Now the bindings API is defined in
the "mojo" namespace. At build time, all bindings files are combined
into a single file "mojo_bindings.js". Users should use <script> tag to include
this file (as well as generated mojom.js files).

- Generated mojom.js files export their definitions under the same namespace as
the "module" statement in the corresponding mojom files.

- This change also adds a "use_new_js_bindings" option to the generator. It
  duplicates the control message mojom files in order to generate two
  different flavors of JS bindings.

- The new bindings use the Mojo system API defined by Web IDL.

BUG=699569

Review-Url: https://codereview.chromium.org/2759563004
Cr-Original-Commit-Position: refs/heads/master@{#459654}
Committed: https://chromium.googlesource.com/chromium/src/+/e6a5534bb3fe61b5224f1a22e43ba957190ad5d0
Review-Url: https://codereview.chromium.org/2759563004
Cr-Commit-Position: refs/heads/master@{#461349}
  • Loading branch information
yzshen authored and Commit bot committed Apr 2, 2017
1 parent 81f28ed commit a064699
Show file tree
Hide file tree
Showing 27 changed files with 556 additions and 301 deletions.
17 changes: 17 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ if (!is_ios) {
group("webkit_layout_tests") {
testonly = true
data_deps = [
":layout_test_data_mojo_bindings",
"//content/shell:content_shell",
"//mojo/public/interfaces/bindings/tests:test_interfaces",
"//third_party/WebKit/public:blink_devtools_frontend_resources_files",
Expand Down Expand Up @@ -952,6 +953,22 @@ if (!is_ios) {
]
}
}

copy("layout_test_data_mojo_bindings") {
testonly = true

sources = [
"$root_gen_dir/mojo/public/js/mojo_bindings.js",
]

outputs = [
"$root_gen_dir/layout_test_data/mojo/public/js/mojo_bindings.js",
]

deps = [
"//mojo/public/js:new_bindings",
]
}
}

# Add a dummy target for compatibility w/ GYP
Expand Down
12 changes: 12 additions & 0 deletions mojo/public/interfaces/bindings/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ mojom("bindings") {
export_define = "MOJO_CPP_BINDINGS_IMPLEMENTATION"
export_header = "mojo/public/cpp/bindings/bindings_export.h"
}

# TODO(yzshen): Remove this target and use the one above once
# |use_new_js_bindings| becomes true by default.
mojom("new_bindings") {
visibility = []
sources = [
"new_bindings/interface_control_messages.mojom",
"new_bindings/pipe_control_messages.mojom",
]

use_new_js_bindings = true
}
2 changes: 2 additions & 0 deletions mojo/public/interfaces/bindings/new_bindings/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
per-file *.mojom=set noparent
per-file *.mojom=file://ipc/SECURITY_OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

[JavaPackage="org.chromium.mojo.bindings.interfacecontrol"]
module mojo.interface_control2;

// For each user-defined interface, some control functions are provided by the
// interface endpoints at both sides.

////////////////////////////////////////////////////////////////////////////////
// Run@0xFFFFFFFF(RunInput input) => (RunOutput? output);
//
// This control function runs the input command. If the command is not
// supported, |output| is set to null; otherwise |output| stores the result,
// whose type depends on the input.

const uint32 kRunMessageId = 0xFFFFFFFF;

struct RunMessageParams {
RunInput input;
};
union RunInput {
QueryVersion query_version;
FlushForTesting flush_for_testing;
};

struct RunResponseMessageParams {
RunOutput? output;
};
union RunOutput {
QueryVersionResult query_version_result;
};

// Queries the max supported version of the user-defined interface.
// Sent by the interface client side.
struct QueryVersion {
};
struct QueryVersionResult {
uint32 version;
};

// Sent by either side of the interface.
struct FlushForTesting {
};

////////////////////////////////////////////////////////////////////////////////
// RunOrClosePipe@0xFFFFFFFE(RunOrClosePipeInput input);
//
// This control function runs the input command. If the operation fails or the
// command is not supported, the message pipe is closed.

const uint32 kRunOrClosePipeMessageId = 0xFFFFFFFE;

struct RunOrClosePipeMessageParams {
RunOrClosePipeInput input;
};
union RunOrClosePipeInput {
RequireVersion require_version;
};

// If the specified version of the user-defined interface is not supported, the
// function fails and the pipe is closed.
// Sent by the interface client side.
struct RequireVersion {
uint32 version;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

[JavaPackage="org.chromium.mojo.bindings.pipecontrol"]
module mojo.pipe_control2;

// For each message pipe running user-defined interfaces, some control
// functions are provided and used by the routers at both ends of the pipe, so
// that they can coordinate to manage interface endpoints.
// All these control messages will have the interface ID field in the message
// header set to invalid.

////////////////////////////////////////////////////////////////////////////////
// RunOrClosePipe@0xFFFFFFFE(RunOrClosePipeInput input);
//
// This control function runs the input command. If the operation fails or the
// command is not supported, the message pipe is closed.

const uint32 kRunOrClosePipeMessageId = 0xFFFFFFFE;

struct RunOrClosePipeMessageParams {
RunOrClosePipeInput input;
};

union RunOrClosePipeInput {
PeerAssociatedEndpointClosedEvent peer_associated_endpoint_closed_event;
};

// A user-defined reason about why the interface is disconnected.
struct DisconnectReason {
uint32 custom_reason;
string description;
};

// An event to notify that an interface endpoint set up at the message sender
// side has been closed.
//
// This event is omitted if the endpoint belongs to the master interface and
// there is no disconnect reason specified.
struct PeerAssociatedEndpointClosedEvent {
// The interface ID.
uint32 id;
DisconnectReason? disconnect_reason;
};

10 changes: 10 additions & 0 deletions mojo/public/interfaces/bindings/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mojom("test_interfaces") {
"validation_test_interfaces.mojom",
]
public_deps = [
":echo",
":test_mojom_import",
":test_mojom_import2",
]
Expand Down Expand Up @@ -192,3 +193,12 @@ mojom("test_no_sources") {
":test_interfaces",
]
}

mojom("echo") {
testonly = true
sources = [
"echo.mojom",
"echo_import.mojom",
]
use_new_js_bindings = true
}
12 changes: 12 additions & 0 deletions mojo/public/interfaces/bindings/tests/echo.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module test.echo.mojom;

import "echo_import.mojom";

interface Echo {
EchoPoint(test.echo_import.mojom.Point point)
=> (test.echo_import.mojom.Point result);
};
10 changes: 10 additions & 0 deletions mojo/public/interfaces/bindings/tests/echo_import.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module test.echo_import.mojom;

struct Point {
int32 x;
int32 y;
};
38 changes: 38 additions & 0 deletions mojo/public/js/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,48 @@ group("bindings") {
]

deps = [
":new_bindings",
"//mojo/public/interfaces/bindings:bindings__generator",
]
}

action("new_bindings") {
new_bindings_js_files = [
# This must be the first file in the list, because it initializes global
# variable |mojoBindings| that the others need to refer to.
"new_bindings/base.js",

"$interfaces_bindings_gen_dir/new_bindings/interface_control_messages.mojom.js",
"new_bindings/bindings.js",
"new_bindings/buffer.js",
"new_bindings/codec.js",
"new_bindings/connector.js",
"new_bindings/interface_types.js",
"new_bindings/lib/control_message_handler.js",
"new_bindings/lib/control_message_proxy.js",
"new_bindings/router.js",
"new_bindings/unicode.js",
"new_bindings/validator.js",
]
compiled_file = "$target_gen_dir/mojo_bindings.js"

# TODO(yzshen): Eventually we would like to use Closure Compiler to minify the
# bindings instead of simply concatenating the files.
script = "//v8/tools/concatenate-files.py"

sources = new_bindings_js_files
outputs = [
compiled_file,
]

args = rebase_path(new_bindings_js_files)
args += [ rebase_path(compiled_file) ]

deps = [
"//mojo/public/interfaces/bindings:new_bindings__generator",
]
}

group("tests") {
testonly = true

Expand Down
33 changes: 33 additions & 0 deletions mojo/public/js/new_bindings/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

if (mojo && mojo.internal) {
throw new Error('The Mojo bindings library has been initialized.');
}

var mojo = mojo || {};
mojo.internal = {};
mojo.internal.global = this;

(function() {
var internal = mojo.internal;

function exposeNamespace(namespace) {
var current = internal.global;
var parts = namespace.split('.');

for (var part; parts.length && (part = parts.shift());) {
if (!current[part]) {
current[part] = {};
}
current = current[part];
}

return current;
}

internal.exposeNamespace = exposeNamespace;
})();
Loading

0 comments on commit a064699

Please sign in to comment.