James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 5 | #ifndef SHELL_EXTERNAL_APPLICATION_REGISTRAR_CONNECTION_H_ |
| 6 | #define SHELL_EXTERNAL_APPLICATION_REGISTRAR_CONNECTION_H_ |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 7 | |
| 8 | #include "base/callback_forward.h" |
| 9 | #include "base/files/file_path.h" |
| 10 | #include "base/memory/scoped_ptr.h" |
| 11 | #include "mojo/common/common_type_converters.h" |
Viet-Trung Luu | f8a197c | 2015-02-12 13:08:07 -0800 | [diff] [blame] | 12 | #include "mojo/edk/embedder/channel_info_forward.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 13 | #include "mojo/public/cpp/bindings/error_handler.h" |
| 14 | #include "mojo/public/interfaces/application/application.mojom.h" |
| 15 | #include "mojo/public/interfaces/application/shell.mojom.h" |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 16 | #include "shell/domain_socket/socket_descriptor.h" |
| 17 | #include "shell/domain_socket/unix_domain_client_socket_posix.h" |
| 18 | #include "shell/external_application_registrar.mojom.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 19 | #include "url/gurl.h" |
| 20 | |
| 21 | namespace mojo { |
James Robinson | e5ae9e4 | 2015-01-26 17:53:08 -0800 | [diff] [blame] | 22 | |
| 23 | class Application; |
| 24 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 25 | namespace shell { |
| 26 | |
| 27 | // Externally-running applications can use this class to discover and register |
| 28 | // with a running mojo_shell instance. |
| 29 | // MUST be run on an IO thread |
| 30 | class ExternalApplicationRegistrarConnection : public ErrorHandler { |
| 31 | public: |
| 32 | // Configures client_socket_ to point at socket_path. |
| 33 | explicit ExternalApplicationRegistrarConnection( |
| 34 | const base::FilePath& socket_path); |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 35 | ~ExternalApplicationRegistrarConnection() override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 36 | |
| 37 | // Implementation of ErrorHandler |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 38 | void OnConnectionError() override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 39 | |
James Robinson | d56bace | 2015-01-22 15:18:31 -0800 | [diff] [blame] | 40 | // Connects the client socket and spins a message loop until the connection is |
| 41 | // initiated, returning false if it was unable to do so. |
| 42 | bool Connect(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 43 | |
James Robinson | e5ae9e4 | 2015-01-26 17:53:08 -0800 | [diff] [blame] | 44 | using RegisterCallback = base::Callback<void(InterfaceRequest<Application>)>; |
| 45 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 46 | // Registers this app with the shell at the provided URL. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 47 | void Register(const GURL& app_url, |
Dave Moore | f2efe55 | 2015-01-26 14:31:45 -0800 | [diff] [blame] | 48 | const std::vector<std::string>& args, |
James Robinson | e5ae9e4 | 2015-01-26 17:53:08 -0800 | [diff] [blame] | 49 | const RegisterCallback& callback); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | // Handles the result of Connect(). If it was successful, promotes the socket |
| 53 | // to a MessagePipe and binds it to registrar_. |
James Robinson | d56bace | 2015-01-22 15:18:31 -0800 | [diff] [blame] | 54 | void OnConnect(const base::Closure& cb, int rv); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 55 | |
Chris Masone | 8a7d06b | 2014-10-24 14:47:12 -0700 | [diff] [blame] | 56 | scoped_ptr<UnixDomainClientSocket> client_socket_; |
Viet-Trung Luu | f8a197c | 2015-02-12 13:08:07 -0800 | [diff] [blame] | 57 | embedder::ChannelInfo* channel_info_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 58 | ExternalApplicationRegistrarPtr registrar_; |
| 59 | }; |
| 60 | |
| 61 | } // namespace shell |
| 62 | } // namespace mojo |
| 63 | |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 64 | #endif // SHELL_EXTERNAL_APPLICATION_REGISTRAR_CONNECTION_H_ |