OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_SHELL_SHELL_H_ | |
6 #define MOJO_SHELL_SHELL_H_ | |
7 | |
8 #include <map> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "mojo/public/cpp/bindings/binding_set.h" | |
15 #include "mojo/public/cpp/bindings/interface_ptr_set.h" | |
16 #include "mojo/shell/connect_params.h" | |
17 #include "mojo/shell/loader.h" | |
18 #include "mojo/shell/native_runner.h" | |
19 #include "mojo/shell/public/cpp/capabilities.h" | |
20 #include "mojo/shell/public/cpp/identity.h" | |
21 #include "mojo/shell/public/cpp/interface_factory.h" | |
22 #include "mojo/shell/public/cpp/shell_client.h" | |
23 #include "mojo/shell/public/interfaces/connector.mojom.h" | |
24 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" | |
25 #include "mojo/shell/public/interfaces/shell.mojom.h" | |
26 #include "mojo/shell/public/interfaces/shell_client.mojom.h" | |
27 #include "mojo/shell/public/interfaces/shell_client_factory.mojom.h" | |
28 #include "mojo/shell/public/interfaces/shell_resolver.mojom.h" | |
29 | |
30 namespace mojo { | |
31 class ShellConnection; | |
32 namespace shell { | |
33 | |
34 // Creates an identity for the Shell, used when the Shell connects to | |
35 // applications. | |
36 Identity CreateShellIdentity(); | |
37 | |
38 class Shell : public ShellClient { | |
39 public: | |
40 // API for testing. | |
41 class TestAPI { | |
42 public: | |
43 explicit TestAPI(Shell* shell); | |
44 ~TestAPI(); | |
45 | |
46 // Returns true if there is a Instance for this name. | |
47 bool HasRunningInstanceForName(const std::string& name) const; | |
48 private: | |
49 Shell* shell_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(TestAPI); | |
52 }; | |
53 | |
54 // |native_runner_factory| is an instance of an object capable of vending | |
55 // implementations of NativeRunner, e.g. for in or out-of-process execution. | |
56 // See native_runner.h and RunNativeApplication(). | |
57 // |file_task_runner| provides access to a thread to perform file copy | |
58 // operations on. This may be null only in testing environments where | |
59 // applications are loaded via Loader implementations. | |
60 Shell(scoped_ptr<NativeRunnerFactory> native_runner_factory, | |
61 mojom::ShellClientPtr catalog); | |
62 ~Shell() override; | |
63 | |
64 // Provide a callback to be notified whenever an instance is destroyed. | |
65 // Typically the creator of the Shell will use this to determine when some set | |
66 // of instances it created are destroyed, so it can shut down. | |
67 void SetInstanceQuitCallback(base::Callback<void(const Identity&)> callback); | |
68 | |
69 // Completes a connection between a source and target application as defined | |
70 // by |params|, exchanging InterfaceProviders between them. If no existing | |
71 // instance of the target application is running, one will be loaded. | |
72 void Connect(scoped_ptr<ConnectParams> params); | |
73 | |
74 // Creates a new Instance identified as |name|. This is intended for use by | |
75 // the Shell's embedder to register itself with the shell. This must only be | |
76 // called once. | |
77 mojom::ShellClientRequest InitInstanceForEmbedder(const std::string& name); | |
78 | |
79 // Sets the default Loader to be used if not overridden by SetLoaderForName(). | |
80 void set_default_loader(scoped_ptr<Loader> loader) { | |
81 default_loader_ = std::move(loader); | |
82 } | |
83 | |
84 // Sets a Loader to be used for a specific name. | |
85 void SetLoaderForName(scoped_ptr<Loader> loader, const std::string& name); | |
86 | |
87 private: | |
88 class Instance; | |
89 | |
90 // ShellClient: | |
91 bool AcceptConnection(Connection* connection) override; | |
92 | |
93 void InitCatalog(mojom::ShellClientPtr catalog); | |
94 | |
95 // Destroys all Shell-ends of connections established with Applications. | |
96 // Applications connected by this Shell will observe pipe errors and have a | |
97 // chance to shutdown. | |
98 void TerminateShellConnections(); | |
99 | |
100 // Removes a Instance when it encounters an error. | |
101 void OnInstanceError(Instance* instance); | |
102 | |
103 // Completes a connection between a source and target application as defined | |
104 // by |params|, exchanging InterfaceProviders between them. If no existing | |
105 // instance of the target application is running, one will be loaded. | |
106 // | |
107 // If |client| is not null, there must not be an instance of the target | |
108 // application already running. The shell will create a new instance and use | |
109 // |client| to control it. | |
110 void Connect(scoped_ptr<ConnectParams> params, mojom::ShellClientPtr client); | |
111 | |
112 // Returns a running instance matching |identity|. This might be an instance | |
113 // running as a different user if one is available that services all users. | |
114 Instance* GetExistingInstance(const Identity& identity) const; | |
115 | |
116 void NotifyPIDAvailable(uint32_t id, base::ProcessId pid); | |
117 | |
118 // Attempt to complete the connection requested by |params| by connecting to | |
119 // an existing instance. If there is an existing instance, |params| is taken, | |
120 // and this function returns true. | |
121 bool ConnectToExistingInstance(scoped_ptr<ConnectParams>* params); | |
122 | |
123 Instance* CreateInstance(const Identity& source, | |
124 const Identity& target, | |
125 const CapabilitySpec& spec); | |
126 | |
127 // Called from the instance implementing mojom::Shell. | |
128 void AddInstanceListener(mojom::InstanceListenerPtr listener); | |
129 | |
130 void CreateShellClientWithFactory(const Identity& source, | |
131 const Identity& shell_client_factory, | |
132 const std::string& name, | |
133 mojom::ShellClientRequest request); | |
134 // Returns a running ShellClientFactory for |shell_client_factory_identity|. | |
135 // If there is not one running one is started for |source_identity|. | |
136 mojom::ShellClientFactory* GetShellClientFactory( | |
137 const Identity& shell_client_factory_identity, | |
138 const Identity& source_identity); | |
139 void OnShellClientFactoryLost(const Identity& which); | |
140 | |
141 // Callback when remote Catalog resolves mojo:foo to mojo:bar. | |
142 // |params| are the params passed to Connect(). | |
143 // |client| if provided is a ShellClientPtr which should be used to manage the | |
144 // new application instance. This may be null. | |
145 // |result| contains the result of the resolve operation. | |
146 void OnGotResolvedName(mojom::ShellResolverPtr resolver, | |
147 scoped_ptr<ConnectParams> params, | |
148 mojom::ShellClientPtr client, | |
149 mojom::ResolveResultPtr result); | |
150 | |
151 // Tries to load |target| with an Loader. Returns true if one was registered | |
152 // and it was loaded, in which case |request| is taken. | |
153 bool LoadWithLoader(const Identity& target, | |
154 mojom::ShellClientRequest* request); | |
155 | |
156 // Returns the appropriate loader for |name|, or the default loader if there | |
157 // is no loader configured for the name. | |
158 Loader* GetLoaderForName(const std::string& name); | |
159 | |
160 base::WeakPtr<Shell> GetWeakPtr(); | |
161 | |
162 void CleanupRunner(NativeRunner* runner); | |
163 | |
164 // Loader management. | |
165 // Loaders are chosen in the order they are listed here. | |
166 std::map<std::string, Loader*> name_to_loader_; | |
167 scoped_ptr<Loader> default_loader_; | |
168 | |
169 std::map<Identity, Instance*> identity_to_instance_; | |
170 | |
171 // Tracks the names of instances that are allowed to field connection requests | |
172 // from all users. | |
173 std::set<std::string> singletons_; | |
174 | |
175 std::map<Identity, mojom::ShellClientFactoryPtr> shell_client_factories_; | |
176 // Counter used to assign ids to client factories. | |
177 uint32_t shell_client_factory_id_counter_; | |
178 | |
179 InterfacePtrSet<mojom::InstanceListener> instance_listeners_; | |
180 | |
181 base::Callback<void(const Identity&)> instance_quit_callback_; | |
182 scoped_ptr<NativeRunnerFactory> native_runner_factory_; | |
183 std::vector<scoped_ptr<NativeRunner>> native_runners_; | |
184 scoped_ptr<ShellConnection> shell_connection_; | |
185 base::WeakPtrFactory<Shell> weak_ptr_factory_; | |
186 | |
187 DISALLOW_COPY_AND_ASSIGN(Shell); | |
188 }; | |
189 | |
190 mojom::Connector::ConnectCallback EmptyConnectCallback(); | |
191 | |
192 } // namespace shell | |
193 } // namespace mojo | |
194 | |
195 #endif // MOJO_SHELL_SHELL_H_ | |
OLD | NEW |