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

Side by Side Diff: mojo/public/cpp/application/application_connection.h

Issue 380413003: Mojo: Use InterfaceFactory<Interface> for service registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review feedback Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
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_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ 5 #ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ 6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "mojo/public/cpp/application/interface_factory.h"
10 #include "mojo/public/cpp/application/lib/service_connector.h" 11 #include "mojo/public/cpp/application/lib/service_connector.h"
11 12
12 namespace mojo { 13 namespace mojo {
13 14
14 // An instance of this class is passed to 15 // An instance of this class is passed to
15 // ApplicationDelegate's ConfigureIncomingConnection() method each time a 16 // ApplicationDelegate's ConfigureIncomingConnection() method each time a
16 // connection is made to this app, and to ApplicationDelegate's 17 // connection is made to this app, and to ApplicationDelegate's
17 // ConfigureOutgoingConnection() method when the app connects to 18 // ConfigureOutgoingConnection() method when the app connects to
18 // another. 19 // another.
19 // 20 //
20 // To use define a class that implements your specific service api, e.g. FooImpl 21 // To use define a class that implements your specific service api, e.g. FooImpl
21 // to implement a service named Foo. 22 // to implement a service named Foo.
22 // That class must subclass an InterfaceImpl specialization. 23 // That class must subclass an InterfaceImpl specialization.
23 // 24 //
24 // If there is context that is to be shared amongst all instances, define a 25 // Then implement an InterfaceFactory<Foo> that binds instances of FooImpl to
25 // constructor with that class as its only argument, otherwise define an empty 26 // InterfaceRequest<Foo>s and register that on the connection.
26 // constructor.
27 // 27 //
28 // class FooImpl : public InterfaceImpl<Foo> { 28 // connection->AddServiceFactory(&factory);
29 // public:
30 // explicit FooImpl(ApplicationConnnection* connection) {}
31 // };
32 // 29 //
33 // or 30 // Or if you have multiple factories implemented by the same type, explicitly
31 // specify the interface to register the factory for:
34 // 32 //
35 // class BarImpl : public InterfaceImpl<Bar> { 33 // connection->AddServiceFactory<Foo>(&my_foo_and_bar_factory_);
36 // public: 34 // connection->AddServiceFactory<Bar>(&my_foo_and_bar_factory_);
37 // // contexts will remain valid for the lifetime of BarImpl.
38 // BarImpl(ApplicationConnnection* connection, BarContext* service_context)
39 // : connection_(connection), servicecontext_(context) {}
40 // 35 //
41 // Create an ApplicationDelegate instance and pass it to the constructor 36 // The InterfaceFactory must outlive the ApplicationConnection.
tim (not reviewing) 2014/07/16 17:57:09 I'm a bit confused here. I don't see any changes t
jamesr 2014/07/17 22:22:50 Right, and the service connector maintains a raw p
42 // of an ApplicationImpl. The delegate will be called when new connections are
43 // made to other applications.
44 //
45 // connection->AddService<FooImpl>();
46 //
47 // BarContext context;
48 // connection->AddService<BarImpl>(&context);
49 class ApplicationConnection { 37 class ApplicationConnection {
50 public: 38 public:
51 virtual ~ApplicationConnection(); 39 virtual ~ApplicationConnection();
52 40
53 // Impl’s constructor will receive two arguments: 41 template <typename Interface>
54 // Impl::Impl(Application::Context* app_context, 42 void AddServiceFactory(InterfaceFactory<Interface>* factory) {
55 // ServiceContext* svc_context)
56 template <typename Impl, typename ServiceContext>
57 void AddService(ServiceContext* context) {
58 AddServiceConnector( 43 AddServiceConnector(
59 new internal::ServiceConnector<Impl, ServiceContext>(Impl::Name_, 44 new internal::InterfaceFactoryConnector<Interface>(factory));
60 context));
61 }
62
63 // Impl’s constructor will receive one argument:
64 // Impl::Impl(Application::Context* app_context)
65 template <typename Impl>
66 void AddService() {
67 AddServiceConnector(
68 new internal::ServiceConnector<Impl, void>(Impl::Name_, NULL));
69 } 45 }
70 46
71 // Connect to the service implementing |Interface|. 47 // Connect to the service implementing |Interface|.
72 template <typename Interface> 48 template <typename Interface>
73 void ConnectToService(InterfacePtr<Interface>* ptr) { 49 void ConnectToService(InterfacePtr<Interface>* ptr) {
74 MessagePipe pipe; 50 MessagePipe pipe;
75 ptr->Bind(pipe.handle0.Pass()); 51 ptr->Bind(pipe.handle0.Pass());
76 GetServiceProvider()->ConnectToService(Interface::Name_, 52 GetServiceProvider()->ConnectToService(Interface::Name_,
77 pipe.handle1.Pass()); 53 pipe.handle1.Pass());
78 } 54 }
(...skipping 18 matching lines...) Expand all
97 virtual ServiceProvider* GetServiceProvider() = 0; 73 virtual ServiceProvider* GetServiceProvider() = 0;
98 74
99 private: 75 private:
100 virtual void AddServiceConnector( 76 virtual void AddServiceConnector(
101 internal::ServiceConnectorBase* service_connector) = 0; 77 internal::ServiceConnectorBase* service_connector) = 0;
102 }; 78 };
103 79
104 } // namespace mojo 80 } // namespace mojo
105 81
106 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ 82 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698