summaryrefslogtreecommitdiffstats
path: root/mojo/services/public/js/application.js
blob: d4aa1e674236691fa1643ee8966f42f5fb6ebe2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2014 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.

define("mojo/services/public/js/application", [
  "mojo/public/js/bindings",
  "mojo/public/js/core",
  "mojo/public/js/connection",
  "mojo/public/js/threading",
  "mojo/public/interfaces/application/application.mojom",
  "mojo/services/public/js/service_provider",
  "mojo/services/public/js/shell",
], function(bindings, core, connection, threading, applicationMojom, serviceProvider, shell) {

  const ApplicationInterface = applicationMojom.Application;
  const ProxyBindings = bindings.ProxyBindings;
  const ServiceProvider = serviceProvider.ServiceProvider;
  const Shell = shell.Shell;

  class Application {
    constructor(appRequestHandle, url) {
      this.url = url;
      this.serviceProviders = [];
      this.exposedServiceProviders = [];
      this.appRequestHandle_ = appRequestHandle;
      this.appStub_ =
          connection.bindHandleToStub(appRequestHandle, ApplicationInterface);
      bindings.StubBindings(this.appStub_).delegate = {
          initialize: this.doInitialize.bind(this),
          acceptConnection: this.doAcceptConnection.bind(this),
      };
    }

    doInitialize(shellProxy, args) {
      this.shellProxy_ = shellProxy;
      this.shell = new Shell(shellProxy);
      this.initialize(args);
    }

    initialize(args) {}

    // The mojom signature of this function is:
    //   AcceptConnection(string requestor_url,
    //                    ServiceProvider&? services,
    //                    ServiceProvider? exposed_services);
    //
    // We want to bind |services| to our js implementation of ServiceProvider
    // and store |exposed_services| so we can request services of the connecting
    // application.
    doAcceptConnection(requestorUrl, servicesRequest, exposedServicesProxy) {
      // Construct a new js ServiceProvider that can make outgoing calls on
      // exposedServicesProxy.
      var serviceProvider =
          new ServiceProvider(servicesRequest, exposedServicesProxy);
      this.serviceProviders.push(serviceProvider);
      this.acceptConnection(requestorUrl, serviceProvider);
    }

    acceptConnection(requestorUrl, serviceProvider) {}

    quit() {
      this.serviceProviders.forEach(function(sp) {
        sp.close();
      });
      this.shell.close();
      core.close(this.appRequestHandle_);
      threading.quit();
    }
  }

  var exports = {};
  exports.Application = Application;
  return exports;
});