summaryrefslogtreecommitdiffstats
path: root/third_party/mojo_services/src/public/js/shell.js
blob: 358f2726d68ccc17b8aeafc39959eb4d2101ba7d (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
// 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/shell", [
  "mojo/public/js/bindings",
  "mojo/public/js/core",
  "mojo/public/js/connection",
  "mojo/public/interfaces/application/shell.mojom",
  "mojo/public/interfaces/application/service_provider.mojom",
  "mojo/services/public/js/service_exchange",
], function(bindings, core, connection, shellMojom, spMojom, serviceExchange) {

  const ProxyBindings = bindings.ProxyBindings;
  const StubBindings = bindings.StubBindings;
  const ServiceExchange = serviceExchange.ServiceExchange;
  const ServiceProviderInterface = spMojom.ServiceProvider;
  const ShellInterface = shellMojom.Shell;

  class Shell {
    constructor(shellProxy) {
      this.shellProxy = shellProxy;
      this.applications_ = new Map();
    }

    connectToApplication(url) {
      var application = this.applications_.get(url);
      if (application)
        return application;

      var application = new ServiceExchange();
      this.shellProxy.connectToApplication(url,
          function(servicesProxy) {
            application.proxy = servicesProxy;
          },
          function(exposedServicesStub) {
            application.stub = exposedServicesStub;
            StubBindings(exposedServicesStub).delegate = application;
          });
      this.applications_.set(url, application);
      return application;
    }

    connectToService(url, service, client) {
      return this.connectToApplication(url).requestService(service, client);
    };

    close() {
      this.applications_.forEach(function(application, url) {
        application.close();
      });
      ProxyBindings(this.shellProxy).close();
      this.applications_.clear();
    }
  }

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