• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

audio/H03-May-2022-17,43312,396

content/H12-Nov-2020-1,5361,044

data_decoder/H12-Nov-2020-7,7925,727

device/H12-Nov-2020-78,93258,152

image_annotation/H12-Nov-2020-4,2613,169

media_session/H12-Nov-2020-7,6495,452

metrics/H12-Nov-2020-1,8281,155

network/H03-May-2022-135,185100,490

preferences/H12-Nov-2020-8,3185,944

proxy_resolver/H12-Nov-2020-6,7844,799

resource_coordinator/H12-Nov-2020-9,7857,104

service_manager/H12-Nov-2020-19,41613,319

shape_detection/H12-Nov-2020-5,2374,004

strings/H12-Nov-2020-415412

test/H12-Nov-2020-180146

tracing/H12-Nov-2020-20,39014,790

video_capture/H03-May-2022-5,3334,089

viz/H12-Nov-2020-10,3138,179

BUILD.gnH A D07-Nov-20206.1 KiB157144

DEPSH A D07-Nov-2020231 86

OWNERSH A D07-Nov-2020106 65

README.mdH A D07-Nov-20209.2 KiB224169

services_strings.grdH A D07-Nov-202012.3 KiB190181

README.md

1# Service Development Guidelines
2
3[TOC]
4
5## Overview
6
7The 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.
12
13Each subdirectory here corresponds to a service that:
14
15- 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
20
21*** aside
22Note that there are other parts of the tree which aggregate
23slightly-less-than-foundational service definitions, such as services specific
24to 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.
28***
29
30One 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.
34
35Another 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
38system 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.
42
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[services-dev@chromium.org](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};
95```
96
97The 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:
101
102``` cpp
103interface GoatTeleporterFactory {
104  GetGoatTeleporter(GoatTeleporter& request, GoatTeleporterClient client);
105};
106
107interface GoatTeleporter {
108  TeleportGoat(string name);
109};
110```
111
112Instead 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.
116
117### Interface Naming
118
119Just some basic tips for service and interface naming:
120
121- 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`.
124
125- 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".
128
129- 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`.
134
135### Service Directory & Dependency Structure
136
137Services typically follow a canonical directory structure:
138
139```
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```
147
148As 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.
152
153Generally the language-specific client libraries are built against only the
154public mojom API of the service (and usually few other common dependencies like
155`//base` and `//mojo`).
156
157Even in the private service implementation, services should not depend on very
158large components like Content, Chrome, or Blink.
159
160*** 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
202## Adding a New Service
203
204Please start a thread on
205[services-dev@chromium.org](https://groups.google.com/a/chromium.org/forum/#!forum/services-dev)
206if 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)
217- [Servicification Homepage](https://sites.google.com/a/chromium.org/dev/servicification)
218
219## Additional Support
220
221You can always post to
222[services-dev@chromium.org](https://groups.google.com/a/chromium.org/forum#!forum/services-dev)
223with questions or concerns about anything related to service development.
224