blob: bb46e5fd52e5c994187360faa9a3177285bb3ccb [file] [log] [blame] [view]
Ken Rockotab035122019-02-06 00:35:241# Service Development Guidelines
benffa42c62016-04-09 05:10:132
rockotf59d2d62017-04-01 02:49:083[TOC]
4
5## Overview
benffa42c62016-04-09 05:10:136
Ken Rockotab035122019-02-06 00:35:247The top-level `//services` directory contains the sources, public Mojo interface
8definitions, and public client libraries for a number of essential services,
9designated as **Chrome Foundation Services**. If you think of Chrome as a
10"portable OS," Chrome Foundation Services can be thought of as the core system
11services of that OS.
benffa42c62016-04-09 05:10:1312
Ken Rockotab035122019-02-06 00:35:2413Each subdirectory here corresponds to a service that:
benffa42c62016-04-09 05:10:1314
Ken Rockotab035122019-02-06 00:35:2415- generally focuses on a subset of functionality or features which are
16 thematically or functionally related in a way that makes sense given the name
17 of the service
18- could logically run in an isolated process for security or performance
19 isolation, depending on the constraints of the host OS
Colin Blundell32945392017-06-28 08:52:1720
Ken Rockotab035122019-02-06 00:35:2421*** aside
22Note that there are other parts of the tree which aggregate
23slightly-less-than-foundational service definitions, such as services specific
Ken Rockot216eb5d2020-02-19 17:09:5524to the Chrome browser defined in `//chrome/services` or reusable services for
25Content or its embedders, defined in `//components/services`. The motivations,
26advice, and standards discussed in this document apply to all service
27definitions in the Chromium tree.
Ken Rockotab035122019-02-06 00:35:2428***
Colin Blundell32945392017-06-28 08:52:1729
Ken Rockotab035122019-02-06 00:35:2430One of the main motivations for expressing Chromium as a collection of services
31is long-term maintainability and code health. Because service API boundaries are
32strictly limited to Mojo interfaces, state owned and managed by each service is
33strongly isolated from other components in the system.
benffa42c62016-04-09 05:10:1334
Ken Rockotab035122019-02-06 00:35:2435Another key motivation is general modularity and reusability: in the past there
36have been a number of missed opportunities for potential new features or
37Chromium-based products due to the browser's generally monolothic and inflexible
Ken Rockot216eb5d2020-02-19 17:09:5538system design. With the services providing scaffolding for system components, it
39becomes progressively easier to build out newer use cases with *e.g.* a smaller
40resource footprint, or a different process model, or even a more granular binary
41distribution.
Ken Rockotab035122019-02-06 00:35:2442
43## Service Standards
44
45As outlined above, individual services are intended for graceful reusability
46across a broad variety of use cases. To enable this goal, we have rigorous
47standards on services' structure and public API design. Before doing significant
48work in `//services` (or other places where services are defined), please
49internalize these standards. All Chromium developers are responsible for
50upholding them!
51
52### Public Service APIs
53
54In creating and maintaining a service's public API, please respect the following
55principles:
56
57- The purpose of a service should be readily apparent.
58- The supported client use cases of the service should be easy for a new
59 consumer to understand.
60- The service should use idioms and design patterns consistent with other
61 services.
62- From the service's public API documentation and tests, it should be feasible
63 to develop a new implementation of the service which satisfies existing
64 clients and doesn't require mimicking internal implementation details of the
65 existing service.
66- Perhaps most important of all, a service's public API should be designed with
67 multiple hypothetical clients in mind, *not* focused on supporting only a
68 single narrow use known at development time. **Always be thinking about the
69 future!**
70
71If you're working on a new service and have concerns or doubts about API design,
72please post to
73[[email protected]](https://groups.google.com/a/chromium.org/forum#!forum/services-dev)
74and ask for help. The list is generally quite responsive, and it's loaded with
75people who have done a lot of work on services.
76
77### Service API Design Tips
78
79#### Using Interface Factories to Establish Context
80
81One common pitfall when designing service APIs is to write something like:
82
83``` cpp
84interface GoatTeleporter {
85 // Sets the client interface pipe for this teleporter. Must be called before
86 // other interface methods.
87 SetClient(GoatTeleporterClient client);
88
89 TeleportGoat(string name);
90};
91
92interface GoatTeleporterClient {
93 TeleporterReady();
94};
rockotf59d2d62017-04-01 02:49:0895```
benffa42c62016-04-09 05:10:1396
Ken Rockotab035122019-02-06 00:35:2497The problem with this approach is that a client may easily fail to call
98`SetClient` before calling `TeleportGoat`. When such ordering requirements are
99necessary, the service can benefit clients by designing an API that is harder
100to fail at. For example:
benffa42c62016-04-09 05:10:13101
Ken Rockotab035122019-02-06 00:35:24102``` cpp
103interface GoatTeleporterFactory {
104 GetGoatTeleporter(GoatTeleporter& request, GoatTeleporterClient client);
105};
benffa42c62016-04-09 05:10:13106
Ken Rockotab035122019-02-06 00:35:24107interface GoatTeleporter {
108 TeleportGoat(string name);
109};
110```
benffa42c62016-04-09 05:10:13111
Ken Rockotab035122019-02-06 00:35:24112Instead of exposing `GoatTeleporter` directly to other services, the service can
113expose `GoatTeleporterFactory` instead. Now it's impossible for a client to
114acquire a functioning `GoatTeleporter` pipe without also providing a
115corresponding client pipe to complement it.
benffa42c62016-04-09 05:10:13116
Ken Rockot216eb5d2020-02-19 17:09:55117### Interface Naming
jam3009e4c2016-06-09 16:34:05118
Ken Rockotab035122019-02-06 00:35:24119Just some basic tips for service and interface naming:
jamb249fa92017-02-01 23:19:42120
Ken Rockot216eb5d2020-02-19 17:09:55121- Strive to give your service's main interface a name that directly conveys the
122 general purpose of the service (*e.g.*, `NetworkService`, `StorageService`)
123 rather than a meaningless codename like `Cromulator`.
bena6e9b7f2016-06-11 00:58:46124
Ken Rockotab035122019-02-06 00:35:24125- Strive to avoid conceptual layering violations in naming and documentation --
126 *e.g.*, avoid referencing Blink or Content concepts like "renderers" or
127 "frame hosts".
rockotf59d2d62017-04-01 02:49:08128
Ken Rockotab035122019-02-06 00:35:24129- Use the names `FooClient` and `FooObserver` consistently in interfaces. If
130 there is an expected 1:1 correspondence between a Foo and its client interface
131 counterpart, that counterpart should most likely be called `FooClient`. If
132 there is expected to be 1-to-many correspondence between a Foo and its
133 counterpart clients, the client interface may be better named `FooObserver`.
blundell77afe7a62017-05-02 09:08:49134
Ken Rockotab035122019-02-06 00:35:24135### Service Directory & Dependency Structure
bena6e9b7f2016-06-11 00:58:46136
Ken Rockotab035122019-02-06 00:35:24137Services typically follow a canonical directory structure:
bena6e9b7f2016-06-11 00:58:46138
Ken Rockotab035122019-02-06 00:35:24139```
140//services/service_name/ # Private implementation
141 public/
142 mojom/ # Mojom interfaces
143 cpp/ # C++ client libraries (optional)
144 java/ # Java client libararies (optional, rare)
145 js/ # JS client libraries (optional, rare)
146```
jamb249fa92017-02-01 23:19:42147
Ken Rockotab035122019-02-06 00:35:24148As a general rule, **nothing below `/public` can depend on the private service
149implementation** (*i.e.* things above `/public`). Enforcing this principle makes
150it much easier to keep the service's state well-isolated from the rest of the
151system.
jamb249fa92017-02-01 23:19:42152
Ken Rockotab035122019-02-06 00:35:24153Generally the language-specific client libraries are built against only the
154public mojom API of the service (and usually few other common dependencies like
Ken Rockot216eb5d2020-02-19 17:09:55155`//base` and `//mojo`).
jamb249fa92017-02-01 23:19:42156
Ken Rockotab035122019-02-06 00:35:24157Even in the private service implementation, services should not depend on very
158large components like Content, Chrome, or Blink.
blundell77afe7a62017-05-02 09:08:49159
Ken Rockotab035122019-02-06 00:35:24160*** aside
161NOTE: Exceptions to the above rule are made in rare cases where Blink or V8 is
162actually required as part of the service implementation. For example
163`"data_decoder"` uses Blink implementation to decode common image formats, and
164`"proxy_resolver"` uses V8 to execute proxy autoconfig scripts.
165***
166
167### Service Documentation
168
169- Every service should have a top-level `README.md` that explains the purpose and
170 supported usage models of the service.
171
172- Every public interface should be documented within its Mojom file at both the
173 interface level and indivudal message level.
174
175- Interface documentation should be complete enough to serve as test
176 specifications. If the method returns information of a user's accounts, what
177 should happen if the user is not signed in? If the method makes a request for
178 an access token, what happens if a client makes a second method call before
179 the first one has completed? If the method returns a nullable object, under
180 which conditions will it be null?
181
182- Avoid writing interface documentation which is unnecessarily prescriptive
183 about implementation details. Keep in mind that these are **interface**
184 definitions, not implementations thereof.
185
186- Avoid writing documentation which is tailored to a specific client.
187
188### Service Testing
189
190- Try to cover service implementation details with unit tests tied as closely
191 as possible to the private implementation object or method being tested,
192 rather than exercising implementation details through public API surface.
193
194- For integration tests, try to have tests cover as much of the public API
195 surface as possible while mocking out as little of the underlying service as
196 possible.
197
198- Treat the public API tests as "conformance tests" which clearly demonstrate
199 what expectations and guarantees are supposed to be upheld by *any*
200 implementation of the service's APIs.
201
Ken Rockotab035122019-02-06 00:35:24202## Adding a New Service
203
Ken Rockotab035122019-02-06 00:35:24204Please start a thread on
Mathias Bynense7ee6ef2024-04-01 18:33:12205[[email protected]](https://groups.google.com/a/chromium.org/g/services-dev)
Ken Rockotab035122019-02-06 00:35:24206if you want to propose the introduction of a new service.
207
208If you are servicifying an existing Chromium feature, please check out
209[Servicifying Chromium Features](/docs/servicification.md).
210
211## Other Docs
212
213Here are some other external documents that aren't quite fully captured by any
214documents in the Chromium tree. Beware of obsolete information:
215
216- [High-level Design Doc](https://docs.google.com/document/d/15I7sQyQo6zsqXVNAlVd520tdGaS8FCicZHrN0yRu-oU)
Mathias Bynense7ee6ef2024-04-01 18:33:12217- [Servicification Homepage](https://www.chromium.org/servicification/)
Ken Rockotab035122019-02-06 00:35:24218
219## Additional Support
220
221You can always post to
Mathias Bynense7ee6ef2024-04-01 18:33:12222[services-dev@chromium.org](https://groups.google.com/a/chromium.org/g/services-dev)
Ken Rockotab035122019-02-06 00:35:24223with questions or concerns about anything related to service development.