| 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 MOJO_SHELL_APPLICATION_MANAGER_H_ | 5 #ifndef MOJO_SHELL_SHELL_H_ |
| 6 #define MOJO_SHELL_APPLICATION_MANAGER_H_ | 6 #define MOJO_SHELL_SHELL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.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 "mojo/services/package_manager/package_manager.h" | 16 #include "mojo/services/package_manager/package_manager.h" |
| 17 #include "mojo/services/package_manager/public/interfaces/shell_resolver.mojom.h
" | 17 #include "mojo/services/package_manager/public/interfaces/shell_resolver.mojom.h
" |
| 18 #include "mojo/shell/connect_params.h" | 18 #include "mojo/shell/connect_params.h" |
| 19 #include "mojo/shell/identity.h" | 19 #include "mojo/shell/identity.h" |
| 20 #include "mojo/shell/loader.h" | 20 #include "mojo/shell/loader.h" |
| 21 #include "mojo/shell/native_runner.h" | 21 #include "mojo/shell/native_runner.h" |
| 22 #include "mojo/shell/public/cpp/interface_factory.h" | 22 #include "mojo/shell/public/cpp/interface_factory.h" |
| 23 #include "mojo/shell/public/cpp/shell_client.h" | 23 #include "mojo/shell/public/cpp/shell_client.h" |
| 24 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | 24 #include "mojo/shell/public/interfaces/connector.mojom.h" |
| 25 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" | 25 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" |
| 26 #include "mojo/shell/public/interfaces/shell.mojom.h" | 26 #include "mojo/shell/public/interfaces/shell.mojom.h" |
| 27 #include "mojo/shell/public/interfaces/shell_client.mojom.h" | 27 #include "mojo/shell/public/interfaces/shell_client.mojom.h" |
| 28 #include "mojo/shell/public/interfaces/shell_client_factory.mojom.h" | 28 #include "mojo/shell/public/interfaces/shell_client_factory.mojom.h" |
| 29 | 29 |
| 30 namespace base { | 30 namespace base { |
| 31 class FilePath; | 31 class FilePath; |
| 32 class SequencedWorkerPool; | 32 class SequencedWorkerPool; |
| 33 } | 33 } |
| 34 | 34 |
| 35 namespace mojo { | 35 namespace mojo { |
| 36 class ShellConnection; | 36 class ShellConnection; |
| 37 namespace shell { | 37 namespace shell { |
| 38 | 38 |
| 39 class ApplicationManager : public ShellClient { | 39 class Shell : public ShellClient { |
| 40 public: | 40 public: |
| 41 // API for testing. | 41 // API for testing. |
| 42 class TestAPI { | 42 class TestAPI { |
| 43 public: | 43 public: |
| 44 explicit TestAPI(ApplicationManager* manager); | 44 explicit TestAPI(Shell* shell); |
| 45 ~TestAPI(); | 45 ~TestAPI(); |
| 46 | 46 |
| 47 // Returns true if there is a Instance for this name. | 47 // Returns true if there is a Instance for this name. |
| 48 bool HasRunningInstanceForName(const std::string& name) const; | 48 bool HasRunningInstanceForName(const std::string& name) const; |
| 49 private: | 49 private: |
| 50 ApplicationManager* manager_; | 50 Shell* shell_; |
| 51 | 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(TestAPI); | 52 DISALLOW_COPY_AND_ASSIGN(TestAPI); |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // |native_runner_factory| is an instance of an object capable of vending | 55 // |native_runner_factory| is an instance of an object capable of vending |
| 56 // implementations of NativeRunner, e.g. for in or out-of-process execution. | 56 // implementations of NativeRunner, e.g. for in or out-of-process execution. |
| 57 // See native_runner.h and RunNativeApplication(). | 57 // See native_runner.h and RunNativeApplication(). |
| 58 // |file_task_runner| provides access to a thread to perform file copy | 58 // |file_task_runner| provides access to a thread to perform file copy |
| 59 // operations on. This may be null only in testing environments where | 59 // operations on. This may be null only in testing environments where |
| 60 // applications are loaded via Loader implementations. | 60 // applications are loaded via Loader implementations. |
| 61 ApplicationManager( | 61 Shell(scoped_ptr<NativeRunnerFactory> native_runner_factory, |
| 62 scoped_ptr<NativeRunnerFactory> native_runner_factory, | 62 base::TaskRunner* file_task_runner, |
| 63 base::TaskRunner* file_task_runner, | 63 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog); |
| 64 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog); | 64 ~Shell() override; |
| 65 ~ApplicationManager() override; | |
| 66 | 65 |
| 67 // Provide a callback to be notified whenever an instance is destroyed. | 66 // Provide a callback to be notified whenever an instance is destroyed. |
| 68 // Typically the creator of the ApplicationManager will use this to determine | 67 // Typically the creator of the Shell will use this to determine when some set |
| 69 // when some set of instances it created are destroyed, so it can shut down. | 68 // of instances it created are destroyed, so it can shut down. |
| 70 void SetInstanceQuitCallback(base::Callback<void(const Identity&)> callback); | 69 void SetInstanceQuitCallback(base::Callback<void(const Identity&)> callback); |
| 71 | 70 |
| 72 // Completes a connection between a source and target application as defined | 71 // Completes a connection between a source and target application as defined |
| 73 // by |params|, exchanging InterfaceProviders between them. If no existing | 72 // by |params|, exchanging InterfaceProviders between them. If no existing |
| 74 // instance of the target application is running, one will be loaded. | 73 // instance of the target application is running, one will be loaded. |
| 75 void Connect(scoped_ptr<ConnectParams> params); | 74 void Connect(scoped_ptr<ConnectParams> params); |
| 76 | 75 |
| 77 // Creates a new Instance identified as |name|. This is intended for use by | 76 // Creates a new Instance identified as |name|. This is intended for use by |
| 78 // the ApplicationManager's embedder to register itself with the shell. The | 77 // the Shell's embedder to register itself with the shell. The name is never |
| 79 // name is never resolved and there must not be an existing instance | 78 // resolved and there must not be an existing instance associated with it. |
| 80 // associated with it. This must only be called once. | 79 // This must only be called once. |
| 81 mojom::ShellClientRequest InitInstanceForEmbedder(const std::string& name); | 80 mojom::ShellClientRequest InitInstanceForEmbedder(const std::string& name); |
| 82 | 81 |
| 83 // Sets the default Loader to be used if not overridden by SetLoaderForName(). | 82 // Sets the default Loader to be used if not overridden by SetLoaderForName(). |
| 84 void set_default_loader(scoped_ptr<Loader> loader) { | 83 void set_default_loader(scoped_ptr<Loader> loader) { |
| 85 default_loader_ = std::move(loader); | 84 default_loader_ = std::move(loader); |
| 86 } | 85 } |
| 87 | 86 |
| 88 // Sets a Loader to be used for a specific name. | 87 // Sets a Loader to be used for a specific name. |
| 89 void SetLoaderForName(scoped_ptr<Loader> loader, const std::string& name); | 88 void SetLoaderForName(scoped_ptr<Loader> loader, const std::string& name); |
| 90 | 89 |
| 91 private: | 90 private: |
| 92 class Instance; | 91 class Instance; |
| 93 | 92 |
| 94 using IdentityToInstanceMap = std::map<Identity, Instance*>; | 93 using IdentityToInstanceMap = std::map<Identity, Instance*>; |
| 95 using NameToLoaderMap = std::map<std::string, Loader*>; | 94 using NameToLoaderMap = std::map<std::string, Loader*>; |
| 96 using IdentityToShellClientFactoryMap = | 95 using IdentityToShellClientFactoryMap = |
| 97 std::map<Identity, mojom::ShellClientFactoryPtr>; | 96 std::map<Identity, mojom::ShellClientFactoryPtr>; |
| 98 | 97 |
| 99 // ShellClient: | 98 // ShellClient: |
| 100 bool AcceptConnection(Connection* connection) override; | 99 bool AcceptConnection(Connection* connection) override; |
| 101 | 100 |
| 102 void InitPackageManager( | 101 void InitPackageManager( |
| 103 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog); | 102 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog); |
| 104 | 103 |
| 105 // Destroys all Shell-ends of connections established with Applications. | 104 // Destroys all Shell-ends of connections established with Applications. |
| 106 // Applications connected by this ApplicationManager will observe pipe errors | 105 // Applications connected by this Shell will observe pipe errors and have a |
| 107 // and have a chance to shutdown. | 106 // chance to shutdown. |
| 108 void TerminateShellConnections(); | 107 void TerminateShellConnections(); |
| 109 | 108 |
| 110 // Removes a Instance when it encounters an error. | 109 // Removes a Instance when it encounters an error. |
| 111 void OnInstanceError(Instance* instance); | 110 void OnInstanceError(Instance* instance); |
| 112 | 111 |
| 113 Instance* GetExistingInstance(const Identity& identity) const; | 112 Instance* GetExistingInstance(const Identity& identity) const; |
| 114 | 113 |
| 115 void NotifyPIDAvailable(uint32_t id, base::ProcessId pid); | 114 void NotifyPIDAvailable(uint32_t id, base::ProcessId pid); |
| 116 | 115 |
| 117 // Attempt to complete the connection requested by |params| by connecting to | 116 // Attempt to complete the connection requested by |params| by connecting to |
| 118 // an existing instance. If there is an existing instance, |params| is taken, | 117 // an existing instance. If there is an existing instance, |params| is taken, |
| 119 // and this function returns true. | 118 // and this function returns true. |
| 120 bool ConnectToExistingInstance(scoped_ptr<ConnectParams>* params); | 119 bool ConnectToExistingInstance(scoped_ptr<ConnectParams>* params); |
| 121 | 120 |
| 122 Instance* CreateInstance(const Identity& target_id, | 121 Instance* CreateInstance(const Identity& target_id, |
| 123 mojom::ShellClientRequest* request); | 122 mojom::ShellClientRequest* request); |
| 124 | 123 |
| 125 // Called from the instance implementing mojom::ApplicationManager. |user_id| | 124 // Called from the instance implementing mojom::Shell. |user_id| must be |
| 126 // must be resolved by the instance (i.e. must not be | 125 // resolved by the instance (i.e. must not be mojom::Connector::kUserInherit). |
| 127 // mojom::Connector::kUserInherit). | |
| 128 void CreateInstanceForFactory( | 126 void CreateInstanceForFactory( |
| 129 mojom::ShellClientFactoryPtr factory, | 127 mojom::ShellClientFactoryPtr factory, |
| 130 const String& name, | 128 const String& name, |
| 131 uint32_t user_id, | 129 uint32_t user_id, |
| 132 mojom::CapabilityFilterPtr filter, | 130 mojom::CapabilityFilterPtr filter, |
| 133 mojom::PIDReceiverRequest pid_receiver); | 131 mojom::PIDReceiverRequest pid_receiver); |
| 134 // Called from the instance implementing mojom::ApplicationManager. | 132 // Called from the instance implementing mojom::Shell. |
| 135 void AddInstanceListener(mojom::InstanceListenerPtr listener); | 133 void AddInstanceListener(mojom::InstanceListenerPtr listener); |
| 136 | 134 |
| 137 void CreateShellClient(const Identity& source, | 135 void CreateShellClient(const Identity& source, |
| 138 const Identity& shell_client_factory, | 136 const Identity& shell_client_factory, |
| 139 const std::string& name, | 137 const std::string& name, |
| 140 mojom::ShellClientRequest request); | 138 mojom::ShellClientRequest request); |
| 141 // Returns a running ShellClientFactory for |shell_client_factory_identity|, | 139 // Returns a running ShellClientFactory for |shell_client_factory_identity|, |
| 142 // if there is not one running one is started for |source_identity|. | 140 // if there is not one running one is started for |source_identity|. |
| 143 mojom::ShellClientFactory* GetShellClientFactory( | 141 mojom::ShellClientFactory* GetShellClientFactory( |
| 144 const Identity& shell_client_factory_identity, | 142 const Identity& shell_client_factory_identity, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 // The Instance created by the shell embedder, if any. | 183 // The Instance created by the shell embedder, if any. |
| 186 Instance* embedder_instance_ = nullptr; | 184 Instance* embedder_instance_ = nullptr; |
| 187 | 185 |
| 188 InterfacePtrSet<mojom::InstanceListener> instance_listeners_; | 186 InterfacePtrSet<mojom::InstanceListener> instance_listeners_; |
| 189 | 187 |
| 190 base::Callback<void(const Identity&)> instance_quit_callback_; | 188 base::Callback<void(const Identity&)> instance_quit_callback_; |
| 191 base::TaskRunner* file_task_runner_; | 189 base::TaskRunner* file_task_runner_; |
| 192 scoped_ptr<NativeRunnerFactory> native_runner_factory_; | 190 scoped_ptr<NativeRunnerFactory> native_runner_factory_; |
| 193 std::vector<scoped_ptr<NativeRunner>> native_runners_; | 191 std::vector<scoped_ptr<NativeRunner>> native_runners_; |
| 194 scoped_ptr<ShellConnection> shell_connection_; | 192 scoped_ptr<ShellConnection> shell_connection_; |
| 195 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_; | 193 base::WeakPtrFactory<Shell> weak_ptr_factory_; |
| 196 | 194 |
| 197 DISALLOW_COPY_AND_ASSIGN(ApplicationManager); | 195 DISALLOW_COPY_AND_ASSIGN(Shell); |
| 198 }; | 196 }; |
| 199 | 197 |
| 200 mojom::Connector::ConnectCallback EmptyConnectCallback(); | 198 mojom::Connector::ConnectCallback EmptyConnectCallback(); |
| 201 | 199 |
| 202 } // namespace shell | 200 } // namespace shell |
| 203 } // namespace mojo | 201 } // namespace mojo |
| 204 | 202 |
| 205 #endif // MOJO_SHELL_APPLICATION_MANAGER_H_ | 203 #endif // MOJO_SHELL_SHELL_H_ |
| OLD | NEW |