OLD | NEW |
1 # Mojo in Chromium | 1 # Mojo in Chromium |
2 | 2 |
3 This document is intended to serve as a Mojo primer for Chromium developers. No | 3 This document is intended to serve as a Mojo primer for Chromium developers. No |
4 prior knowledge of Mojo is assumed. | 4 prior knowledge of Mojo is assumed. |
5 | 5 |
6 [TOC] | 6 [TOC] |
7 | 7 |
8 ## Should I Bother Reading This? | 8 ## Should I Bother Reading This? |
9 | 9 |
10 If you're planning to build a Chromium feature that needs IPC and you aren't | 10 If you're planning to build a Chromium feature that needs IPC and you aren't |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // src/components/frob/frobinator_impl.cc | 117 // src/components/frob/frobinator_impl.cc |
118 | 118 |
119 #include "components/frob/public/interfaces/frobinator.mojom.h" | 119 #include "components/frob/public/interfaces/frobinator.mojom.h" |
120 #include "mojo/public/cpp/bindings/binding.h" | 120 #include "mojo/public/cpp/bindings/binding.h" |
121 #include "mojo/public/cpp/bindings/interface_request.h" | 121 #include "mojo/public/cpp/bindings/interface_request.h" |
122 | 122 |
123 namespace frob { | 123 namespace frob { |
124 | 124 |
125 class FrobinatorImpl : public mojom::Frobinator { | 125 class FrobinatorImpl : public mojom::Frobinator { |
126 public: | 126 public: |
127 FrobinatorImpl(mojo::InterfaceRequest<mojom::Frobinator> request) | 127 FrobinatorImpl(mojom::FrobinatorRequest request) |
128 : binding_(this, std::move(request)) {} | 128 : binding_(this, std::move(request)) {} |
129 ~FrobinatorImpl() override {} | 129 ~FrobinatorImpl() override {} |
130 | 130 |
131 // mojom::Frobinator: | 131 // mojom::Frobinator: |
132 void Frobinate() override { DLOG(INFO) << "I can't stop frobinating!"; } | 132 void Frobinate() override { DLOG(INFO) << "I can't stop frobinating!"; } |
133 | 133 |
134 private: | 134 private: |
135 mojo::Binding<mojom::Frobinator> binding_; | 135 mojo::Binding<mojom::Frobinator> binding_; |
136 }; | 136 }; |
137 | 137 |
138 } // namespace frob | 138 } // namespace frob |
139 ``` | 139 ``` |
140 | 140 |
141 The first thing to note is that `mojo::Binding<T>` *binds* one end of a message | 141 The first thing to note is that `mojo::Binding<T>` *binds* one end of a message |
142 pipe to an implementation of a service. This means it watches that end of the | 142 pipe to an implementation of a service. This means it watches that end of the |
143 pipe for incoming messages; it knows how to decode messages for interface `T`, | 143 pipe for incoming messages; it knows how to decode messages for interface `T`, |
144 and it dispatches them to methods on the bound `T` implementation. | 144 and it dispatches them to methods on the bound `T` implementation. |
145 | 145 |
146 `mojo::InterfaceRequest<T>` is essentially semantic sugar for a strongly-typed | 146 `mojom::FrobinatorRequest` is a generated type alias for |
147 message pipe endpoint. A common way to create new message pipes is via the | 147 `mojo::InterfaceRequest<mojom::Frobinator>` and is essentially semantic sugar |
148 `GetProxy` call defined in `interface_request.h`: | 148 for a strongly-typed message pipe endpoint. A common way to create new message |
| 149 pipes is via the `GetProxy` call defined in `interface_request.h`: |
149 | 150 |
150 ``` | 151 ``` |
151 mojom::FrobinatorPtr proxy; | 152 mojom::FrobinatorPtr proxy; |
152 mojo::InterfaceRequest<mojom::Frobinator> request = mojo::GetProxy(&proxy); | 153 mojom::FrobinatorRequest request = mojo::GetProxy(&proxy); |
153 ``` | 154 ``` |
154 | 155 |
155 This creates a new message pipe with one end owned by `proxy` and the other end | 156 This creates a new message pipe with one end owned by `proxy` and the other end |
156 owned by `request`. It has the nice property of attaching common type | 157 owned by `request`. It has the nice property of attaching common type |
157 information to each end of the pipe. | 158 information to each end of the pipe. |
158 | 159 |
159 Note that `InterfaceRequest<T>` doesn't actually **do** anything. It just scopes | 160 Note that `InterfaceRequest<T>` doesn't actually **do** anything. It just scopes |
160 a pipe endpoint and associates it with an interface type at compile time. As | 161 a pipe endpoint and associates it with an interface type at compile time. As |
161 such, other typed service binding primitives such as `mojo::Binding<T>` take | 162 such, other typed service binding primitives such as `mojo::Binding<T>` take |
162 these objects as input when they need an endpoint to bind to. | 163 these objects as input when they need an endpoint to bind to. |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 takes a pipe endpoint and does something with it. It's expected that you'll | 259 takes a pipe endpoint and does something with it. It's expected that you'll |
259 either bind it to a service implementation of some kind or you will close it, ef
fectively rejecting the connection request. | 260 either bind it to a service implementation of some kind or you will close it, ef
fectively rejecting the connection request. |
260 | 261 |
261 We can build a simple browser-side `FrobinatorImpl` service that has access to a | 262 We can build a simple browser-side `FrobinatorImpl` service that has access to a |
262 `BrowserContext` for any frame which connects to it: | 263 `BrowserContext` for any frame which connects to it: |
263 | 264 |
264 ``` | 265 ``` |
265 #include "base/macros.h" | 266 #include "base/macros.h" |
266 #include "components/frob/public/interfaces/frobinator.mojom.h" | 267 #include "components/frob/public/interfaces/frobinator.mojom.h" |
267 #include "content/public/browser/browser_context.h" | 268 #include "content/public/browser/browser_context.h" |
268 #inlcude "mojo/public/cpp/system/interface_request.h" | 269 #include "mojo/public/cpp/system/interface_request.h" |
269 #inlcude "mojo/public/cpp/system/message_pipe.h" | 270 #include "mojo/public/cpp/system/strong_binding.h" |
270 #inlcude "mojo/public/cpp/system/strong_binding.h" | |
271 | 271 |
272 namespace frob { | 272 namespace frob { |
273 | 273 |
274 class FrobinatorImpl : public mojom::Frobinator { | 274 class FrobinatorImpl : public mojom::Frobinator { |
275 public: | 275 public: |
276 FrobinatorImpl(content::BrowserContext* context, | 276 FrobinatorImpl(content::BrowserContext* context, |
277 mojo::InterfaceRequest<mojom::Frobinator> request) | 277 mojom::FrobinatorRequest request) |
278 : context_(context), binding_(this, std::move(request)) {} | 278 : context_(context), binding_(this, std::move(request)) {} |
279 ~FrobinatorImpl() override {} | 279 ~FrobinatorImpl() override {} |
280 | 280 |
281 // A factory function to use in conjunction with ServiceRegistry. | 281 // A factory function to use in conjunction with ServiceRegistry. |
282 static void Create(content::BrowserContext* context, | 282 static void Create(content::BrowserContext* context, |
283 mojo::InterfaceRequest<mojom::Frobinator> request) { | 283 mojom::FrobinatorRequest request) { |
284 // See comment below for why this doesn't leak. | 284 // See comment below for why this doesn't leak. |
285 new FrobinatorImpl(context, | 285 new FrobinatorImpl(context, std::move(request)); |
286 mojo::MakeRequest<mojom::Frobinator>(std::move(pipe))); | |
287 } | 286 } |
288 | 287 |
289 private: | 288 private: |
290 // mojom::Frobinator: | 289 // mojom::Frobinator: |
291 void Frobinate() override { /* ... */ } | 290 void Frobinate() override { /* ... */ } |
292 | 291 |
293 content::BrowserContext* context_; | 292 content::BrowserContext* context_; |
294 | 293 |
295 // A StrongBinding is just like a Binding, except that it takes ownership of | 294 // A StrongBinding is just like a Binding, except that it takes ownership of |
296 // its bound implementation and deletes itself (and the impl) if and when the | 295 // its bound implementation and deletes itself (and the impl) if and when the |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 333 |
335 This is a work in progress. TL;DR: We'll also soon begin using Mojo services | 334 This is a work in progress. TL;DR: We'll also soon begin using Mojo services |
336 from Blink so that the platform layer can consume browser services | 335 from Blink so that the platform layer can consume browser services |
337 directly via Mojo. The long-term goal there is to eliminate `content/renderer`. | 336 directly via Mojo. The long-term goal there is to eliminate `content/renderer`. |
338 | 337 |
339 ## Questions, Discussion, etc. | 338 ## Questions, Discussion, etc. |
340 | 339 |
341 A good place to find highly concentrated doses of people who know and care | 340 A good place to find highly concentrated doses of people who know and care |
342 about Mojo in Chromium would be the [chromium-mojo](https://goo.gl/A4ebWB) | 341 about Mojo in Chromium would be the [chromium-mojo](https://goo.gl/A4ebWB) |
343 mailing list[.](https://goo.gl/L70ihQ) | 342 mailing list[.](https://goo.gl/L70ihQ) |
OLD | NEW |