DescriptionMojo JS Bindings: Simplify sharing services for content-provided JS applications
JS Mojo apps must now use mojo.shell() to access an object that represents the Mojo shell
The shell() object can be used to connect to applications and services:
mojo.shell().connectToApplication(url)
mojo.shell().connectToService(url, interface, client)
The connectToService() method is just shorthand, it's the same as:
shell().connectToApplication(url).connectToService(interface, client);
The connectToApplication() method returns an object that represents a
Mojo application. A JS Mojo app can also provide services to another
Mojo application with provideService():
myApp.provideService(interface, service)
Closing a mojo app causes all of its incoming and outgoing connections to be closed.
myApp.close();
JS Mojo apps launched with a content handler can access an object that represents
the requesting application, with mojo.requestor().
mojo.requestor().connectToApplication(url);
mojo.requestor().connectToService(url, interface, client);
mojo.requestor().provideService(interface, service);
Here's a simple example based on a pair of JS applications, one.js and
two.js. The one.js application connects to a second, two.js, which
provides an implementation of the ExampleService interface.
Both applications are launched by the Mojo JS content handler. The paths for
each JS application must be specified as URLs, so we launch an HTTP server in
the directory that contains one.js and two.js, and then launch
js_content_handler and one.js using mojo_shell.
( cd $ONE_TWO_DIR; python -m SimpleHTTPServer ) &
mojo_shell --content-handlers=application/javascript,mojo://js_content_handler http://localhost:8000/one.js
The one.js application connects to the ExampleService provided by
two.js and then calls its ping() method.
define("one", [
"mojo/apps/js/mojo",
"mojo/examples/apptest/example_service.mojom",
"console",
], function(mojo, example, console) {
var exampleClient = {
pong: function(value) {
console.log("one.js: exampleClient.pong(" + value + ")");
}
};
var exampleService = mojo.shell().connectToService(
"http://localhost:8000/two.js", example.ExampleService, exampleClient);
exampleService.ping(100);
exampleService.ping(200);
// ...
});
The connectTorService() interface parameter, example.ExampleService,
is an object generated by the Mojo JS bindings. The ExampleService
has a client interface, so one.js provides a client implementation
when it connects to two.js.
The two.js application is also launched by the content handler. It
provides the ExampleService to its requestor, which is one.js in this
case.
The ExampleService implementation is a factory method or a constructor
that will be called each time another Mojo application connects to the
ExampleService provided by two.js. The constructor parameter is a
proxy for the interface's client.
define("two", [
"mojo/apps/js/mojo",
"mojo/examples/apptest/example_service.mojom",
"console",
], function(mojo, example, console) {
function ExampleServiceImpl(client) {
this.ping = function(value) {
console.log("two.js: exampleServiceImpl.ping(" + value + ")");
client.pong(value + 1);
}
}
mojo.requestor().provideService(example.ExampleService, ExampleServiceImpl);
});
Each time one.js calls ping(), the two.js ExampleServiceImpl.ping()
method runs. Then two.js calls the client's pong() method which causes
exampleClient.pong(), defined by one.js, to run.
BUG=425684
Patch Set 1 #Patch Set 2 : Complete #
Total comments: 19
Patch Set 3 : Changes per review feedback #Patch Set 4 : #
Total comments: 4
Messages
Total messages: 6 (1 generated)
|