blob: f5c1fb5f4b12f9219f5dbf493b18d0c53e3dbf74 [file] [log] [blame] [view]
chasejc74a4c9c2017-02-10 20:38:091# Integrating a feature with the Origin Trials framework
2
3To expose your feature via the origin trials framework, there are a few code
4changes required.
5
6[TOC]
7
8## Code Changes
9
10### Runtime Enabled Features
11
Kent Tamurab10f7eda2017-09-15 06:45:2012First, youll need to configure [runtime\_enabled\_features.json5]. This is
chasejc74a4c9c2017-02-10 20:38:0913explained in the file, but you use `origin_trial_feature_name` to associate your
14runtime feature flag with a name for your origin trial. The name can be the
15same as your runtime feature flag, or different. Eventually, this configured
16name will be used in the Origin Trials developer console (still under
17development). You can have both `status: experimental` and
18`origin_trial_feature_name` if you want your feature to be enabled either by
19using the `--enable-experimental-web-platform-features` flag **or** the origin
20trial.
21
22You may have a feature that is not available on all platforms, or need to limit
23the trial to specific platforms. Use `origin_trial_os: [list]` to specify which
24platforms will allow the trial to be enabled. The list values are case-
25insensitive, but must match one of the defined `OS_<platform>` macros (see
26[build_config.h]).
27
28#### Examples
29
30Flag name and trial name are the same:
31```
32{
33 name: "MyFeature",
34 origin_trial_feature_name: "MyFeature",
35 status: "experimental",
36},
37```
38Flag name and trial name are different:
39```
40{
41 name: "MyFeature",
42 origin_trial_feature_name: "MyFeatureTrial",
43 status: "experimental",
44},
45```
46Trial limited to specific platform:
47``` json
48{
49 name: "MyFeature",
50 origin_trial_feature_name: "MyFeature",
51 origin_trial_os: ["android"],
52 status: "experimental",
53},
54```
55
56### Gating Access
57
58Once configured, there are two mechanisms to gate access to your feature behind
59an origin trial. You can use either mechanism, or both, as appropriate to your
60feature implementation.
61
621. A native C++ method that you can call in Blink code at runtime to expose your
63 feature: `bool OriginTrials::myFeatureEnabled()`
642. An IDL attribute \[[OriginTrialEnabled]\] that you can use to automatically
Ian Clellanda94fcfb2017-07-20 03:43:4865 generate code to expose and hide JavaScript methods/attributes/objects. This
66 attribute works very similarly to \[RuntimeEnabled\].
chasejc74a4c9c2017-02-10 20:38:0967```
68[OriginTrialEnabled=MyFeature]
69partial interface Navigator {
70 readonly attribute MyFeatureManager myFeature;
71}
72```
73
74**NOTE:** Your feature implementation must not persist the result of the enabled
75check. Your code should simply call `OriginTrials::myFeatureEnabled()` as often
76as necessary to gate access to your feature.
77
chasejc74a4c9c2017-02-10 20:38:0978## Limitations
79
80What you can't do, because of the nature of these Origin Trials, is know at
81either browser or renderer startup time whether your feature is going to be used
82in the current page/context. This means that if you require lots of expensive
83processing to begin (say you index the user's hard drive, or scan an entire city
84for interesting weather patterns,) that you will have to either do it on browser
85startup for *all* users, just in case it's used, or do it on first access. (If
86you go with first access, then only people trying the experiment will notice the
87delay, and hopefully only the first time they use it.). We are investigating
88providing a method like `OriginTrials::myFeatureShouldInitialize()` that will
89hint if you should do startup initialization. For example, this could include
90checks for trials that have been revoked (or throttled) due to usage, if the
91entire origin trials framework has been disabled, etc. The method would be
92conservative and assume initialization is required, but it could avoid expensive
93startup in some known scenarios.
94
95Similarly, if you need to know in the browser process whether a feature should
96be enabled, then you will have to either have the renderer inform it at runtime,
97or else just assume that it's always enabled, and gate access to the feature
98from the renderer.
99
100## Testing
101
102If you want to test your code's interactions with the framework, you'll need to
103generate some tokens of your own. To generate your own tokens, use
104[generate_token.py]. You can generate signed tokens for localhost, or for
105127.0.0.1, or for any origin that you need to help you test. For example:
106
107```
108tools/origin_trials/generate_token.py http://localhost:8000 MyFeature
109```
110
111The file `tools/origin_trials/eftest.key` is used by default as the private key
112for the test keypair used by Origin Trials unit tests and layout tests (i.e. in
113content shell). Tokens generated with this key will **not** work in the browser
114by default (see the [Developer Guide] for instructions on creating real tokens).
115To use a test token with the browser, run Chrome with the command-line flag:
116
117```
118--origin-trial-public-key=dRCs+TocuKkocNKa0AtZ4awrt9XKH2SQCI6o4FY6BNA=
119```
120
121This is the base64 encoding of the public key associated with `eftest.key`. If
122it doesn't work, see [trial_token_unittest.cc]. If you cannot set command-line
123switches (e.g., on Chrome OS), you can also directly modify
124[chrome_origin_trial_policy.cc].
125
chasej0ac7dd22017-02-23 18:16:17126### Layout Tests
127When using the \[OriginTrialEnabled\] IDL attribute, you should add layout tests
Ian Clellanda94fcfb2017-07-20 03:43:48128to verify that the V8 bindings code is working as expected. Depending on how
chasej0ac7dd22017-02-23 18:16:17129your feature is exposed, you'll want tests for the exposed interfaces, as well
130as tests for script-added tokens. For examples, refer to the existing tests in
131[origin_trials/webexposed].
132
chasejc74a4c9c2017-02-10 20:38:09133[build_config.h]: /build/build_config.h
134[chrome_origin_trial_policy.cc]: /chrome/common/origin_trials/chrome_origin_trial_policy.cc
chasejc74a4c9c2017-02-10 20:38:09135[generate_token.py]: /tools/origin_trials/generate_token.py
136[Developer Guide]: https://github.com/jpchase/OriginTrials/blob/gh-pages/developer-guide.md
137[OriginTrialEnabled]: /third_party/WebKit/Source/bindings/IDLExtendedAttributes.md#_OriginTrialEnabled_i_m_a_c_
chasej0ac7dd22017-02-23 18:16:17138[origin_trials/webexposed]: /third_party/WebKit/LayoutTests/http/tests/origin_trials/webexposed/
Kent Tamurab10f7eda2017-09-15 06:45:20139[runtime\_enabled\_features.json5]: /third_party/WebKit/Source/platform/runtime_enabled_features.json5
chasejc74a4c9c2017-02-10 20:38:09140[trial_token_unittest.cc]: /content/common/origin_trials/trial_token_unittest.cc