Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: mojo/shell/external_application_listener.h

Issue 522443003: Accept inbound connections on unix domain socket (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bug407782
Patch Set: Fix clang style complaints Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_EXTERNAL_APPLICATION_LISTENER_H_
6 #define MOJO_SHELL_EXTERNAL_APPLICATION_LISTENER_H_
7
8 #include "base/callback.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread_checker.h"
16 #include "mojo/common/common_type_converters.h"
17 #include "mojo/embedder/channel_init.h"
18 #include "mojo/public/interfaces/application/shell.mojom.h"
19 #include "mojo/shell/external_application_registrar.mojom.h"
20 #include "mojo/shell/incoming_connection_listener.h"
21 #include "net/socket/socket_descriptor.h"
22 #include "net/socket/unix_domain_server_socket_posix.h"
23 #include "url/gurl.h"
24
25 namespace mojo {
26 namespace shell {
27
28 // In order to support Mojo apps whose lifetime is managed by
29 // something other than mojo_shell, mojo_shell needs to support a
30 // mechanism by which such an application can discover a running shell
31 // instance, connect to it, and ask to be "registered" at a given
32 // URL. Registration implies that the app can be connected to at that
33 // URL from then on out, and that the app has received a usable ShellPtr.
34 //
35 // This class implements most of the mojo_shell side of external application
36 // registration. It handles:
37 // 1) discoverability - sets up a unix domain socket at a well-known location,
38 // 2) incoming connections - listens for and accepts incoming connections on
39 // that socket, and
40 // 3) registration requests - forwarded to a RegisterCallback that implements
41 // the actual registration logic.
42 //
43 // External applications can connect to the shell using the
44 // ExternalApplicationRegistrarConnection class.
45 class ExternalApplicationListener
46 : public IncomingConnectionListener::Delegate {
47 public:
48 // When run, a RegisterCallback should note that an app has asked to be
49 // registered at app_url and Bind the provided pipe handle to a ShellImpl.
50 typedef base::Callback<void(const GURL& app_url,
51 ScopedMessagePipeHandle shell)> RegisterCallback;
52 typedef base::Callback<void(int rv)> ErrorCallback;
53
54 static const char kDefaultListenSocketPath[];
55
56 // This class uses two threads, an IO thread for listening and accepting
57 // incoming sockets, and a "main" thread where all Mojo traffic is processed
58 // and provided callbacks are run.
59 ExternalApplicationListener(
60 const scoped_refptr<base::SequencedTaskRunner>& shell_runner,
61 const scoped_refptr<base::SequencedTaskRunner>& io_runner);
62
63 // Some of this class' internal state needs to be destroyed on io_runner_,
64 // so the destructor will post a task to that thread to call StopListening()
65 // and then wait for it to complete.
66 virtual ~ExternalApplicationListener();
67
68 // Begin listening (on io_runner) to a socket at listen_socket_path.
69 // Incoming registration requests will be forwarded to register_callback.
70 // Errors are ignored.
71 void ListenInBackground(const base::FilePath& listen_socket_path,
72 const RegisterCallback& register_callback);
73
74 // Begin listening (on io_runner) to a socket at listen_socket_path.
75 // Incoming registration requests will be forwarded to register_callback.
76 // Errors are reported via error_callback.
77 void ListenInBackgroundWithErrorCallback(
78 const base::FilePath& listen_socket_path,
79 const RegisterCallback& register_callback,
80 const ErrorCallback& error_callback);
81
82 // Block the current thread until listening has started on io_runner.
83 // If listening has already started, returns immediately.
84 void WaitForListening();
85
86 private:
87 class RegistrarImpl;
88
89 // MUST be called on io_runner.
90 // Creates listener_ and tells it to StartListening() on a socket it creates
91 // at listen_socket_path.
92 void StartListening(const base::FilePath& listen_socket_path);
93
94 // MUST be called on io_runner.
95 // Destroys listener_ and signals event when done.
96 void StopListening(base::WaitableEvent* event);
97
98 // Implementation of IncomingConnectionListener::Delegate
99 virtual void OnListening(int rv) MOJO_OVERRIDE;
100 virtual void OnConnection(net::SocketDescriptor incoming) MOJO_OVERRIDE;
101
102 // If listener_ fails to start listening, this method is run on shell_runner_
103 // to report the error.
104 void RunErrorCallbackIfListeningFailed(int rv);
105
106 // When a connection succeeds, it is passed to this method running
107 // on shell_runner_, where it is "promoted" to a Mojo MessagePipe and
108 // bound to a RegistrarImpl.
109 void CreatePipeAndBindToRegistrarImpl(net::SocketDescriptor incoming_socket);
110
111 scoped_refptr<base::SequencedTaskRunner> shell_runner_;
112 scoped_refptr<base::SequencedTaskRunner> io_runner_;
113
114 // MUST be created, used, and destroyed on io_runner_.
115 scoped_ptr<IncomingConnectionListener> listener_;
116
117 // Callers can wait on this event, which will be signalled once listening
118 // has either successfully begun or definitively failed.
119 base::WaitableEvent signal_on_listening_;
120
121 // Locked to thread on which StartListening() is run (should be io_runner_).
122 // All methods that touch listener_ check that they're on that same thread.
123 base::ThreadChecker listener_thread_checker_;
124
125 ErrorCallback error_callback_;
126 RegisterCallback register_callback_;
127 base::ThreadChecker register_thread_checker_;
128
129 // Used on shell_runner_.
130 base::WeakPtrFactory<ExternalApplicationListener> weak_ptr_factory_;
131 };
132
133 } // namespace shell
134 } // namespace mojo
135
136 #endif // MOJO_SHELL_EXTERNAL_APPLICATION_LISTENER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698