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