OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 CONTENT_PUBLIC_COMMON_MOJO_SHELL_CONNECTION_H_ | |
6 #define CONTENT_PUBLIC_COMMON_MOJO_SHELL_CONNECTION_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/callback_forward.h" | |
11 #include "base/sequenced_task_runner.h" | |
12 #include "content/common/content_export.h" | |
13 #include "content/public/common/mojo_application_info.h" | |
14 #include "services/shell/public/cpp/identity.h" | |
15 #include "services/shell/public/interfaces/service.mojom.h" | |
16 | |
17 namespace shell { | |
18 class Connection; | |
19 class Connector; | |
20 class InterfaceProvider; | |
21 class InterfaceRegistry; | |
22 } | |
23 | |
24 namespace content { | |
25 | |
26 class ConnectionFilter; | |
27 | |
28 // Encapsulates a connection to a //services/shell. | |
29 // Access a global instance on the thread the ServiceContext was bound by | |
30 // calling Holder::Get(). | |
31 // Clients can add shell::Service implementations whose exposed interfaces | |
32 // will be exposed to inbound connections to this object's Service. | |
33 // Alternatively clients can define named services that will be constructed when | |
34 // requests for those service names are received. | |
35 // Clients must call any of the registration methods when receiving | |
36 // ContentBrowserClient::RegisterInProcessMojoApplications(). | |
37 class CONTENT_EXPORT MojoShellConnection { | |
38 public: | |
39 using ServiceRequestHandler = | |
40 base::Callback<void(shell::mojom::ServiceRequest)>; | |
41 using Factory = base::Callback<std::unique_ptr<MojoShellConnection>(void)>; | |
42 | |
43 // Stores an instance of |connection| in TLS for the current process. Must be | |
44 // called on the thread the connection was created on. | |
45 static void SetForProcess(std::unique_ptr<MojoShellConnection> connection); | |
46 | |
47 // Returns the per-process instance, or nullptr if the Shell connection has | |
48 // not yet been bound. Must be called on the thread the connection was created | |
49 // on. | |
50 static MojoShellConnection* GetForProcess(); | |
51 | |
52 // Destroys the per-process instance. Must be called on the thread the | |
53 // connection was created on. | |
54 static void DestroyForProcess(); | |
55 | |
56 virtual ~MojoShellConnection(); | |
57 | |
58 // Sets the factory used to create the MojoShellConnection. This must be | |
59 // called before the MojoShellConnection has been created. | |
60 static void SetFactoryForTest(Factory* factory); | |
61 | |
62 // Creates a MojoShellConnection from |request|. The connection binds | |
63 // its interfaces and accept new connections on |io_task_runner| only. Note | |
64 // that no incoming connections are accepted until Start() is called. | |
65 static std::unique_ptr<MojoShellConnection> Create( | |
66 shell::mojom::ServiceRequest request, | |
67 scoped_refptr<base::SequencedTaskRunner> io_task_runner); | |
68 | |
69 // Begins accepting incoming connections. Connection filters MUST be added | |
70 // before calling this in order to avoid races. See AddConnectionFilter() | |
71 // below. | |
72 virtual void Start() = 0; | |
73 | |
74 // Sets a closure to be invoked once the connection receives an Initialize() | |
75 // request from the shell. | |
76 virtual void SetInitializeHandler(const base::Closure& handler) = 0; | |
77 | |
78 // Returns the shell::Connector received via this connection's Service | |
79 // implementation. Use this to initiate connections as this object's Identity. | |
80 virtual shell::Connector* GetConnector() = 0; | |
81 | |
82 // Returns this connection's identity with the shell. Connections initiated | |
83 // via the shell::Connector returned by GetConnector() will use this. | |
84 virtual const shell::Identity& GetIdentity() const = 0; | |
85 | |
86 // Sets a closure that is called when the connection is lost. Note that | |
87 // connection may already have been closed, in which case |closure| will be | |
88 // run immediately before returning from this function. | |
89 virtual void SetConnectionLostClosure(const base::Closure& closure) = 0; | |
90 | |
91 // Provides an InterfaceRegistry to forward incoming interface requests to | |
92 // on the MojoShellConnection's own thread if they aren't bound by the | |
93 // connection's internal InterfaceRegistry on the IO thread. | |
94 // | |
95 // Also configures |interface_provider| to forward all of its outgoing | |
96 // interface requests to the connection's internal remote interface provider. | |
97 // | |
98 // Note that neither |interface_registry| or |interface_provider| is owned | |
99 // and both MUST outlive the MojoShellConnection. | |
100 // | |
101 // TODO(rockot): Remove this. It's a temporary solution to avoid porting all | |
102 // relevant code to ConnectionFilters at once. | |
103 virtual void SetupInterfaceRequestProxies( | |
104 shell::InterfaceRegistry* registry, | |
105 shell::InterfaceProvider* provider) = 0; | |
106 | |
107 static const int kInvalidConnectionFilterId = 0; | |
108 | |
109 // Allows the caller to filter inbound connections and/or expose interfaces | |
110 // on them. |filter| may be created on any thread, but will be used and | |
111 // destroyed exclusively on the IO thread (the thread corresponding to | |
112 // |io_task_runner| passed to Create() above.) | |
113 // | |
114 // Connection filters MUST be added before calling Start() in order to avoid | |
115 // races. | |
116 // | |
117 // Returns a unique identifier that can be passed to RemoveConnectionFilter() | |
118 // below. | |
119 virtual int AddConnectionFilter( | |
120 std::unique_ptr<ConnectionFilter> filter) = 0; | |
121 | |
122 // Removes a filter using the id value returned by AddConnectionFilter(). | |
123 // Removal (and destruction) happens asynchronously on the IO thread. | |
124 virtual void RemoveConnectionFilter(int filter_id) = 0; | |
125 | |
126 // Adds an embedded service to this connection's ServiceFactory. | |
127 // |info| provides details on how to construct new instances of the | |
128 // service when an incoming connection is made to |name|. | |
129 virtual void AddEmbeddedService(const std::string& name, | |
130 const MojoApplicationInfo& info) = 0; | |
131 | |
132 // Adds a generic ServiceRequestHandler for a given service name. This | |
133 // will be used to satisfy any incoming calls to CreateService() which | |
134 // reference the given name. | |
135 // | |
136 // For in-process services, it is preferable to use |AddEmbeddedService()| as | |
137 // defined above. | |
138 virtual void AddServiceRequestHandler( | |
139 const std::string& name, | |
140 const ServiceRequestHandler& handler) = 0; | |
141 }; | |
142 | |
143 } // namespace content | |
144 | |
145 #endif // CONTENT_PUBLIC_COMMON_MOJO_SHELL_CONNECTION_H_ | |
OLD | NEW |