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

Side by Side Diff: mojo/public/shell/service.h

Issue 219583003: Mojo: Move mojo/public/shell to mojo/public/{cpp,interfaces}/shell. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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
« no previous file with comments | « mojo/public/shell/lib/service.cc ('k') | mojo/public/shell/shell.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_PUBLIC_SHELL_SERVICE_H_
6 #define MOJO_PUBLIC_SHELL_SERVICE_H_
7
8 #include <vector>
9
10 #include "mojo/public/bindings/error_handler.h"
11 #include "mojo/public/bindings/remote_ptr.h"
12 #include "mojo/public/cpp/system/core.h"
13 #include "mojo/public/shell/shell.mojom.h"
14
15 // Utility classes for creating ShellClients that vend service instances.
16 // To use define a class that implements your specific server api, e.g. FooImpl
17 // to implement a service named Foo. That class must define an empty constructor
18 // and the Initialize() method.
19 // class FooImpl : public Foo {
20 // public:
21 // FooImpl();
22 // void Initialize(ServiceFactory<FooImpl>* service_factory,
23 // ScopedMessagePipeHandle client_handle
24 // private:
25 // ServiceFactory<FooImpl>* service_factory_;
26 // RemotePtr<FooPeer> client_;
27 // };
28 //
29 //
30 // To simplify further FooImpl can use the Service<> template.
31 // class FooImpl : public Service<Foo, FooImpl> {
32 // public:
33 // FooImpl();
34 // ...
35 // <Foo implementation>
36 // };
37 //
38 // Instances of FooImpl will be created by a specialized ServiceFactory
39 //
40 // ServiceFactory<FooImpl>
41 //
42 // Optionally the classes can be specializeed with a shared context
43 // class ServiceFactory<FooImpl, MyContext>
44 // and
45 // class FooImpl : public Service<Foo, FooImpl, MyContext>
46 //
47 // foo_factory = new ServiceFactory<FooImpl, MyContext>(my_context);
48 // instances of FooImpl can call context() and retrieve the value of my_context.
49 //
50 // Lastly create an Application instance that collects all the ServiceFactories.
51 //
52 // Application app(shell_handle);
53 // app.AddServiceFactory(new ServiceFactory<FooImpl>);
54 //
55 //
56 // Specialization of ServiceFactory.
57 // ServiceImpl: Implementation of Service interface.
58 // Context: Optional type of shared context.v
59 //
60 //
61 namespace mojo {
62
63 namespace internal {
64 class ServiceFactoryBase {
65 public:
66 class Owner : public ShellClient {
67 public:
68 Owner(ScopedShellHandle shell_handle);
69 ~Owner();
70 Shell* shell() { return shell_.get(); }
71 virtual void AddServiceFactory(
72 internal::ServiceFactoryBase* service_factory) = 0;
73 virtual void RemoveServiceFactory(
74 internal::ServiceFactoryBase* service_factory) = 0;
75
76 protected:
77 void set_service_factory_owner(ServiceFactoryBase* service_factory,
78 Owner* owner) {
79 service_factory->owner_ = owner;
80 }
81 RemotePtr<Shell> shell_;
82 };
83 ServiceFactoryBase() : owner_(NULL) {}
84 virtual ~ServiceFactoryBase();
85 Shell* shell() { return owner_->shell(); }
86 virtual void AcceptConnection(const std::string& url,
87 ScopedMessagePipeHandle client_handle) = 0;
88
89 protected:
90 Owner* owner_;
91 };
92 } // namespace internal
93
94 template <class ServiceImpl, typename Context=void>
95 class ServiceFactory : public internal::ServiceFactoryBase {
96 public:
97 ServiceFactory(Context* context = NULL) : context_(context) {}
98
99 virtual ~ServiceFactory() {
100 for (typename ServiceList::iterator it = services_.begin();
101 it != services_.end(); ++it) {
102 delete *it;
103 }
104 }
105
106 virtual void AcceptConnection(const std::string& url,
107 ScopedMessagePipeHandle client_handle)
108 MOJO_OVERRIDE {
109 ServiceImpl* service = new ServiceImpl();
110 service->Initialize(this, client_handle.Pass());
111 services_.push_back(service);
112 }
113
114 void RemoveService(ServiceImpl* service) {
115 for (typename ServiceList::iterator it = services_.begin();
116 it != services_.end(); ++it) {
117 if (*it == service) {
118 services_.erase(it);
119 delete service;
120 if (services_.empty())
121 owner_->RemoveServiceFactory(this);
122 return;
123 }
124 }
125 }
126
127 Context* context() const { return context_; }
128
129 private:
130 typedef std::vector<ServiceImpl*> ServiceList;
131 ServiceList services_;
132 Context* context_;
133 };
134
135 // Specialization of ServiceFactory.
136 // ServiceInterface: Service interface.
137 // ServiceImpl: Implementation of Service interface.
138 // Context: Optional type of shared context.
139 template <class ServiceInterface, class ServiceImpl, typename Context=void>
140 class Service : public ServiceInterface {
141 public:
142 virtual ~Service() {}
143
144 protected:
145 Service() : reaper_(this), service_factory_(NULL) {}
146
147 void Initialize(ServiceFactory<ServiceImpl, Context>* service_factory,
148 ScopedMessagePipeHandle client_handle) {
149 service_factory_ = service_factory;
150 client_.reset(
151 MakeScopedHandle(
152 InterfaceHandle<typename ServiceInterface::_Peer>(
153 client_handle.release().value())).Pass(),
154 this,
155 &reaper_);
156 }
157
158 Shell* shell() { return service_factory_->shell(); }
159 Context* context() const { return service_factory_->context(); }
160 typename ServiceInterface::_Peer* client() { return client_.get(); }
161
162 private:
163 // The Reaper class allows us to handle errors on the client proxy without
164 // polluting the name space of the Service<> class.
165 class Reaper : public ErrorHandler {
166 public:
167 Reaper(Service<ServiceInterface, ServiceImpl, Context>* service)
168 : service_(service) {}
169 virtual void OnError() {
170 service_->service_factory_->RemoveService(
171 static_cast<ServiceImpl*>(service_));
172 }
173 private:
174 Service<ServiceInterface, ServiceImpl, Context>* service_;
175 };
176 friend class ServiceFactory<ServiceImpl, Context>;
177 Reaper reaper_;
178 ServiceFactory<ServiceImpl, Context>* service_factory_;
179 RemotePtr<typename ServiceInterface::_Peer> client_;
180 };
181
182 } // namespace mojo
183
184 #endif // MOJO_PUBLIC_SHELL_SERVICE_H_
OLDNEW
« no previous file with comments | « mojo/public/shell/lib/service.cc ('k') | mojo/public/shell/shell.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698