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