summaryrefslogtreecommitdiffstats
path: root/mojo/shell/application_manager.h
blob: ecfc5e354a4e3d3d54dbb20f75487f1be6e1a1cc (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// 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.

#ifndef MOJO_SHELL_APPLICATION_MANAGER_H_
#define MOJO_SHELL_APPLICATION_MANAGER_H_

#include <map>
#include <utility>
#include <vector>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/interface_ptr_info.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/weak_interface_ptr_set.h"
#include "mojo/services/package_manager/public/interfaces/shell_resolver.mojom.h"
#include "mojo/shell/application_loader.h"
#include "mojo/shell/capability_filter.h"
#include "mojo/shell/connect_to_application_params.h"
#include "mojo/shell/fetcher.h"
#include "mojo/shell/identity.h"
#include "mojo/shell/native_runner.h"
#include "mojo/shell/public/interfaces/application_manager.mojom.h"
#include "mojo/shell/public/interfaces/interface_provider.mojom.h"
#include "mojo/shell/public/interfaces/shell.mojom.h"
#include "mojo/shell/public/interfaces/shell_client.mojom.h"
#include "url/gurl.h"

namespace base {
class FilePath;
class SequencedWorkerPool;
}

namespace mojo {
namespace shell {

class PackageManager;
class ApplicationInstance;
class ContentHandlerConnection;

class ApplicationManager {
 public:
  // API for testing.
  class TestAPI {
   public:
    explicit TestAPI(ApplicationManager* manager);
    ~TestAPI();

    // Returns true if the shared instance has been created.
    static bool HasCreatedInstance();
    // Returns true if there is a ApplicationInstance for this URL.
    bool HasRunningInstanceForURL(const GURL& url) const;
   private:
    ApplicationManager* manager_;

    DISALLOW_COPY_AND_ASSIGN(TestAPI);
  };

  // Creates an ApplicationManager.
  // |package_manager| is an instance of an object that handles URL resolution,
  // fetching and updating of applications. See package_manager.h.
  ApplicationManager(scoped_ptr<PackageManager> package_manager,
                     bool register_mojo_url_schemes);
  // |native_runner_factory| is an instance of an object capable of vending
  // implementations of NativeRunner, e.g. for in or out-of-process execution.
  // See native_runner.h and RunNativeApplication().
  // |task_runner| provides access to a thread to perform file copy operations
  // on. This may be null only in testing environments where applications are
  // loaded via ApplicationLoader implementations.
  // When |register_mojo_url_schemes| is true, mojo: and exe: URL schems are
  // registered as "standard" which faciliates resolving.
  ApplicationManager(scoped_ptr<PackageManager> package_manager,
                     scoped_ptr<NativeRunnerFactory> native_runner_factory,
                     base::TaskRunner* task_runner,
                     bool register_mojo_url_schemes);
  ~ApplicationManager();

  // Loads a service if necessary and establishes a new client connection.
  // Please see the comments in connect_to_application_params.h for more details
  // about the parameters.
  void ConnectToApplication(scoped_ptr<ConnectToApplicationParams> params);

  // Sets the default Loader to be used if not overridden by SetLoaderForURL().
  void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
    default_loader_ = std::move(loader);
  }

  // Sets a Loader to be used for a specific url.
  void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);

  // Destroys all Shell-ends of connections established with Applications.
  // Applications connected by this ApplicationManager will observe pipe errors
  // and have a chance to shutdown.
  void TerminateShellConnections();

  // Removes a ApplicationInstance when it encounters an error.
  void OnApplicationInstanceError(ApplicationInstance* instance);

  ApplicationInstance* GetApplicationInstance(const Identity& identity) const;

  void CreateInstanceForHandle(
      ScopedHandle channel,
      const GURL& url,
      mojom::CapabilityFilterPtr filter,
      InterfaceRequest<mojom::PIDReceiver> pid_receiver);
  void AddListener(mojom::ApplicationManagerListenerPtr listener);

  void ApplicationPIDAvailable(uint32_t id, base::ProcessId pid);

 private:
  using IdentityToInstanceMap = std::map<Identity, ApplicationInstance*>;
  using URLToLoaderMap = std::map<GURL, ApplicationLoader*>;

  void UseRemotePackageManager(bool register_mojo_url_schemes);

  // Takes the contents of |params| only when it returns true.
  bool ConnectToRunningApplication(
      scoped_ptr<ConnectToApplicationParams>* params);

  ApplicationInstance* CreateAndConnectToInstance(
      scoped_ptr<ConnectToApplicationParams> params,
      Identity* source,
      Identity* target,
      const std::string& application_name,
      mojom::ShellClientRequest* request);
  ApplicationInstance* CreateInstance(
      const Identity& target_id,
      const mojom::Shell::ConnectToApplicationCallback& connect_callback,
      const base::Closure& on_application_end,
      const String& application_name,
      mojom::ShellClientRequest* request);

  // Callback when remote PackageManager resolves mojo:foo to mojo:bar.
  // |params| are the params passed to Connect().
  // |resolved_url| is the mojo: url identifying the physical package
  // application.
  // |file_url| is the resolved file:// URL of the physical package.
  // |application_name| is the requested application's pretty name, from its
  // manifest.
  // |base_filter| is the CapabilityFilter the requested application should be
  // run with, from its manifest.
  void OnGotResolvedURL(scoped_ptr<ConnectToApplicationParams> params,
                        const String& resolved_url,
                        const String& file_url,
                        const String& application_name,
                        mojom::CapabilityFilterPtr base_filter);

  // In response to a request via Connect() with |params|, creates an
  // ApplicationInstance and runs the application at |file_url|.
  void CreateAndRunLocalApplication(
      scoped_ptr<ConnectToApplicationParams> params,
      const String& application_name,
      const GURL& file_url);

  void AddListenerManifestsReady(mojom::ApplicationManagerListenerPtr listener);

  // Called once |fetcher| has found app. |params->app_url()| is the url of
  // the requested application before any mappings/resolution have been applied.
  // The corresponding URLRequest struct in |params| has been taken.
  void HandleFetchCallback(scoped_ptr<ConnectToApplicationParams> params,
                           scoped_ptr<Fetcher> fetcher);

  void RunNativeApplication(InterfaceRequest<mojom::ShellClient> request,
                            bool start_sandboxed,
                            scoped_ptr<Fetcher> fetcher,
                            ApplicationInstance* instance,
                            const base::FilePath& file_path,
                            bool path_exists);

  // Returns the appropriate loader for |url|, or the default loader if there is
  // no loader configured for the URL.
  ApplicationLoader* GetLoaderForURL(const GURL& url);

  void CleanupRunner(NativeRunner* runner);

  mojom::ApplicationInfoPtr CreateApplicationInfoForInstance(
      ApplicationInstance* instance) const;

  bool use_remote_package_manager_;
  package_manager::mojom::ShellResolverPtr shell_resolver_;

  scoped_ptr<PackageManager> const package_manager_;
  // Loader management.
  // Loaders are chosen in the order they are listed here.
  URLToLoaderMap url_to_loader_;
  scoped_ptr<ApplicationLoader> default_loader_;

  IdentityToInstanceMap identity_to_instance_;

  WeakInterfacePtrSet<mojom::ApplicationManagerListener> listeners_;

  base::TaskRunner* task_runner_;
  scoped_ptr<NativeRunnerFactory> native_runner_factory_;
  std::vector<scoped_ptr<NativeRunner>> native_runners_;
  base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
};

mojom::Shell::ConnectToApplicationCallback EmptyConnectCallback();

}  // namespace shell
}  // namespace mojo

#endif  // MOJO_SHELL_APPLICATION_MANAGER_H_