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_PUBLIC_CPP_CONNECTION_H_ | 5 #ifndef MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ |
6 #define MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ | 6 #define MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "mojo/shell/public/cpp/lib/interface_factory_connector.h" | 14 #include "mojo/shell/public/cpp/lib/interface_factory_binder.h" |
15 #include "mojo/shell/public/interfaces/service_provider.mojom.h" | 15 #include "mojo/shell/public/interfaces/service_provider.mojom.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 | 18 |
19 class ServiceConnector; | 19 class InterfaceBinder; |
20 | 20 |
21 // Represents a connection to another application. An instance of this class is | 21 // Represents a connection to another application. An instance of this class is |
22 // passed to ShellClient's AcceptConnection() method each | 22 // passed to ShellClient's AcceptConnection() method each |
23 // time a connection is made to this app. | 23 // time a connection is made to this app. |
24 // | 24 // |
25 // To use, define a class that implements your specific service API (e.g., | 25 // To use, define a class that implements your specific service API (e.g., |
26 // FooImpl to implement a service named Foo). Then implement an | 26 // FooImpl to implement a service named Foo). Then implement an |
27 // InterfaceFactory<Foo> that binds instances of FooImpl to | 27 // InterfaceFactory<Foo> that binds instances of FooImpl to |
28 // InterfaceRequest<Foo>s and register that on the connection like this: | 28 // InterfaceRequest<Foo>s and register that on the connection like this: |
29 // | 29 // |
30 // connection->AddService(&factory); | 30 // connection->AddService(&factory); |
31 // | 31 // |
32 // Or, if you have multiple factories implemented by the same type, explicitly | 32 // Or, if you have multiple factories implemented by the same type, explicitly |
33 // specify the interface to register the factory for: | 33 // specify the interface to register the factory for: |
34 // | 34 // |
35 // connection->AddService<Foo>(&my_foo_and_bar_factory_); | 35 // connection->AddService<Foo>(&my_foo_and_bar_factory_); |
36 // connection->AddService<Bar>(&my_foo_and_bar_factory_); | 36 // connection->AddService<Bar>(&my_foo_and_bar_factory_); |
37 // | 37 // |
38 // The InterfaceFactory must outlive the Connection. | 38 // The InterfaceFactory must outlive the Connection. |
39 // | 39 // |
40 // Additionally you specify a ServiceConnector. If a ServiceConnector has | 40 // Additionally you specify a InterfaceBinder. If a InterfaceBinder has |
41 // been set and an InterfaceFactory has not been registered for the interface | 41 // been set and an InterfaceFactory has not been registered for the interface |
42 // request, than the interface request is sent to the ServiceConnector. | 42 // request, than the interface request is sent to the InterfaceBinder. |
43 // | 43 // |
44 // Just as with InterfaceFactory, ServiceConnector must outlive Connection. | 44 // Just as with InterfaceFactory, InterfaceBinder must outlive Connection. |
45 // | 45 // |
46 // An Connection's lifetime is managed by an ShellConnection. To close a | 46 // An Connection's lifetime is managed by an ShellConnection. To close a |
47 // connection, call CloseConnection which will destroy this object. | 47 // connection, call CloseConnection which will destroy this object. |
48 class Connection { | 48 class Connection { |
49 public: | 49 public: |
50 virtual ~Connection() {} | 50 virtual ~Connection() {} |
51 | 51 |
52 class TestApi { | 52 class TestApi { |
53 public: | 53 public: |
54 explicit TestApi(Connection* connection) : connection_(connection) {} | 54 explicit TestApi(Connection* connection) : connection_(connection) {} |
55 base::WeakPtr<Connection> GetWeakPtr() { | 55 base::WeakPtr<Connection> GetWeakPtr() { |
56 return connection_->GetWeakPtr(); | 56 return connection_->GetWeakPtr(); |
57 } | 57 } |
58 | 58 |
59 private: | 59 private: |
60 Connection* connection_; | 60 Connection* connection_; |
61 }; | 61 }; |
62 | 62 |
63 // See class description for details. | 63 // See class description for details. |
64 virtual void SetServiceConnector(ServiceConnector* connector) = 0; | 64 virtual void SetDefaultInterfaceBinder(InterfaceBinder* binder) = 0; |
65 | 65 |
66 // Makes Interface available as a service to the remote application. | 66 // Makes Interface available as a service to the remote application. |
67 // |factory| will create implementations of Interface on demand. | 67 // |factory| will create implementations of Interface on demand. |
68 // Returns true if the service was exposed, false if capability filtering | 68 // Returns true if the service was exposed, false if capability filtering |
69 // from the shell prevented the service from being exposed. | 69 // from the shell prevented the service from being exposed. |
70 template <typename Interface> | 70 template <typename Interface> |
71 bool AddService(InterfaceFactory<Interface>* factory) { | 71 bool AddService(InterfaceFactory<Interface>* factory) { |
72 return SetServiceConnectorForName( | 72 return SetInterfaceBinderForName( |
73 new internal::InterfaceFactoryConnector<Interface>(factory), | 73 new internal::InterfaceFactoryBinder<Interface>(factory), |
74 Interface::Name_); | 74 Interface::Name_); |
75 } | 75 } |
76 | 76 |
77 // Binds |ptr| to an implemention of Interface in the remote application. | 77 // Binds |ptr| to an implemention of Interface in the remote application. |
78 // |ptr| can immediately be used to start sending requests to the remote | 78 // |ptr| can immediately be used to start sending requests to the remote |
79 // service. | 79 // service. |
80 template <typename Interface> | 80 template <typename Interface> |
81 void ConnectToService(InterfacePtr<Interface>* ptr) { | 81 void ConnectToService(InterfacePtr<Interface>* ptr) { |
82 if (ServiceProvider* sp = GetServiceProvider()) { | 82 if (ServiceProvider* sp = GetServiceProvider()) { |
83 MessagePipe pipe; | 83 MessagePipe pipe; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // value. A |content_handler_id| value of Shell::kInvalidApplicationID | 128 // value. A |content_handler_id| value of Shell::kInvalidApplicationID |
129 // indicates no content handler was used in connecting to the application. | 129 // indicates no content handler was used in connecting to the application. |
130 virtual bool GetRemoteContentHandlerID( | 130 virtual bool GetRemoteContentHandlerID( |
131 uint32_t* content_handler_id) const = 0; | 131 uint32_t* content_handler_id) const = 0; |
132 | 132 |
133 // See description in GetRemoteApplicationID()/GetRemoteContentHandlerID(). If | 133 // See description in GetRemoteApplicationID()/GetRemoteContentHandlerID(). If |
134 // the ids are available, |callback| is run immediately. | 134 // the ids are available, |callback| is run immediately. |
135 virtual void AddRemoteIDCallback(const Closure& callback) = 0; | 135 virtual void AddRemoteIDCallback(const Closure& callback) = 0; |
136 | 136 |
137 protected: | 137 protected: |
138 // Returns true if the connector was set, false if it was not set (e.g. by | 138 // Returns true if the binder was set, false if it was not set (e.g. by |
139 // some filtering policy preventing this interface from being exposed). | 139 // some filtering policy preventing this interface from being exposed). |
140 virtual bool SetServiceConnectorForName(ServiceConnector* service_connector, | 140 virtual bool SetInterfaceBinderForName(InterfaceBinder* binder, |
141 const std::string& name) = 0; | 141 const std::string& name) = 0; |
142 | 142 |
143 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; | 143 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; |
144 }; | 144 }; |
145 | 145 |
146 } // namespace mojo | 146 } // namespace mojo |
147 | 147 |
148 #endif // MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ | 148 #endif // MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ |
OLD | NEW |