blob: 858ac3d14366634cd35019c81902fab23bd819b6 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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 Robinsonb4b7af22014-12-05 11:21:01 -08005#ifndef SHELL_EXTERNAL_APPLICATION_REGISTRAR_CONNECTION_H_
6#define SHELL_EXTERNAL_APPLICATION_REGISTRAR_CONNECTION_H_
James Robinson646469d2014-10-03 15:33:28 -07007
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 Luuf8a197c2015-02-12 13:08:07 -080012#include "mojo/edk/embedder/channel_info_forward.h"
James Robinson646469d2014-10-03 15:33:28 -070013#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 Robinsonb4b7af22014-12-05 11:21:01 -080016#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 Robinson646469d2014-10-03 15:33:28 -070019#include "url/gurl.h"
20
21namespace mojo {
James Robinsone5ae9e42015-01-26 17:53:08 -080022
23class Application;
24
James Robinson646469d2014-10-03 15:33:28 -070025namespace 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
30class ExternalApplicationRegistrarConnection : public ErrorHandler {
31 public:
32 // Configures client_socket_ to point at socket_path.
33 explicit ExternalApplicationRegistrarConnection(
34 const base::FilePath& socket_path);
James Robinsone1b30cf2014-10-21 12:25:40 -070035 ~ExternalApplicationRegistrarConnection() override;
James Robinson646469d2014-10-03 15:33:28 -070036
37 // Implementation of ErrorHandler
James Robinsone1b30cf2014-10-21 12:25:40 -070038 void OnConnectionError() override;
James Robinson646469d2014-10-03 15:33:28 -070039
James Robinsond56bace2015-01-22 15:18:31 -080040 // 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 Robinson646469d2014-10-03 15:33:28 -070043
James Robinsone5ae9e42015-01-26 17:53:08 -080044 using RegisterCallback = base::Callback<void(InterfaceRequest<Application>)>;
45
James Robinson646469d2014-10-03 15:33:28 -070046 // Registers this app with the shell at the provided URL.
James Robinson646469d2014-10-03 15:33:28 -070047 void Register(const GURL& app_url,
Dave Mooref2efe552015-01-26 14:31:45 -080048 const std::vector<std::string>& args,
James Robinsone5ae9e42015-01-26 17:53:08 -080049 const RegisterCallback& callback);
James Robinson646469d2014-10-03 15:33:28 -070050
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 Robinsond56bace2015-01-22 15:18:31 -080054 void OnConnect(const base::Closure& cb, int rv);
James Robinson646469d2014-10-03 15:33:28 -070055
Chris Masone8a7d06b2014-10-24 14:47:12 -070056 scoped_ptr<UnixDomainClientSocket> client_socket_;
Viet-Trung Luuf8a197c2015-02-12 13:08:07 -080057 embedder::ChannelInfo* channel_info_;
James Robinson646469d2014-10-03 15:33:28 -070058 ExternalApplicationRegistrarPtr registrar_;
59};
60
61} // namespace shell
62} // namespace mojo
63
James Robinsonb4b7af22014-12-05 11:21:01 -080064#endif // SHELL_EXTERNAL_APPLICATION_REGISTRAR_CONNECTION_H_