• hansmuller's avatar
    Mojo JS bindings: simplify mojo.connectToService() usage - Part 2 · 76a748ae
    hansmuller authored
    This is the final step towards the goals listed in crbug.com/419160. It builds on https://codereview.chromium.org/628763002. The trivial NativeViewport application listed there can now be written relatively simply:
    
      define("test", [
        "mojo/services/public/interfaces/geometry/geometry.mojom",
        "mojo/services/public/interfaces/native_viewport/native_viewport.mojom",
        "mojo/apps/js/mojo",
        "console",
      ], function(geometry, viewport, mojo, console) {
    
        var client = {
          onDestroyed: mojo.quit,
          onEvent: function(event) {
            console.log("event.type=" + event.action);
            return Promise.resolve(); // This just gates the next event delivery
          },
        };
        var viewport = mojo.connectToService(
          "mojo:mojo_native_viewport_service", viewport.NativeViewport, client);
        viewport.create(new geometry.Size({width: 256, height: 256}));
        viewport.show();
      });
    
    The mojo connectToService() function now just returns a proxy to the remote interface. The 3rd (optional) connectToService() parameter is an object that implements the client interface for the remote interface.
    
    In this version the NativeViewportClient interface implementation is just an object with function properties whose names match the interface. Subclassing isn't needed and it's not necessary to implement all of the client functions.
    
    The Mojo JS bindings now generate a subclass of each FooStub class called DelegatingFooStub. The Delegating version just forwards the Stub methods to the value of |this.delegate$|. The ugly "$" suffix avoids collisions with mojom function names. The JS bindings' interface Foo object now includes a delegatingStubClass property whose value is FooDelegatingStub.
    
    |mojo.connectToService(url, Foo, client)| only uses Foo.client.delegatingStubClass if a client is specified.
    
    The mojo_module has been split into two: a native part, called MojoInternalsModule, which integrates with JSApp, and a JavaScript part that depends on the internals. Apps must now import "mojo/apps/js/mojo", rather than just "mojo". For now, quit and connectToService() are the only functions in the JS mojo module.
    
    BUG=419160
    
    Review URL: https://codereview.chromium.org/637373002
    
    Cr-Commit-Position: refs/heads/master@{#299197}
    76a748ae
js_app.cc 3.62 KB