Rodney Ding | afca336 | 2019-11-01 20:07:34 | [diff] [blame] | 1 | # Initialization of Blink runtime features in content layer |
| 2 | This document outlines how to initialize your Blink runtime features in the |
| 3 | Chromium content layer, more specifically in |
| 4 | [content/child/runtime_features.cc][runtime_features]. To learn more on how to |
| 5 | set up features in blink, see |
| 6 | [Runtime Enabled Features][RuntimeEnabledFeatures]. |
| 7 | |
| 8 | ## Step 1: Do you need a custom Blink feature enabler function? |
| 9 | If you simply need to enable/disable the Blink feature you can simply use |
| 10 | [WebRuntimeFeatures::EnableFeatureFromString()][EnableFeatureFromString]. |
| 11 | |
| 12 | However, if there are side effects (e.g. you need to disable other features if |
| 13 | this feature is also disabled), you should declare a custom enabler function in |
Kevin McNee | 92b18f73 | 2020-11-10 00:48:28 | [diff] [blame] | 14 | - [third_party/blink/public/platform/web_runtime_features.h][web_runtime_features.h] |
| 15 | - [third_party/blink/renderer/platform/exported/web_runtime_features.cc][web_runtime_features.cc] |
Rodney Ding | afca336 | 2019-11-01 20:07:34 | [diff] [blame] | 16 | |
| 17 | ## Step 2: Determine how your feature is initialized. |
| 18 | ### 1) Depends on OS-specific Macros: |
| 19 | Add your code for controlling the Blink feature in |
| 20 | [SetRuntimeFeatureDefaultsForPlatform()][SetRuntimeFeatureDefaultsForPlatform] |
| 21 | using the appropriate OS macros. |
| 22 | ### 2) Depends on the status of a base::Feature: |
| 23 | Add your code to the function |
| 24 | [SetRuntimeFeaturesFromChromiumFeatures()][SetRuntimeFeaturesFromChromiumFeatures]. |
| 25 | |
| 26 | If your Blink feature has a custom enabler function, add a new entry to |
| 27 | `blinkFeatureToBaseFeatureMapping`. For example, a new entry like this: |
| 28 | ``` |
Xianzhu Wang | 05355f4a | 2020-09-02 01:22:16 | [diff] [blame] | 29 | {wf::EnableNewFeatureX, features::kNewFeatureX, kDefault}, |
Rodney Ding | afca336 | 2019-11-01 20:07:34 | [diff] [blame] | 30 | ``` |
| 31 | will call `wf::EnableNewFeatureX` to enable it only if `features::kNewFeatureX` |
Xianzhu Wang | 05355f4a | 2020-09-02 01:22:16 | [diff] [blame] | 32 | is enabled, or to set it to the same status as `features::kNewFeatureX` if its |
| 33 | default status is overridden by any field trial or command line switch. |
Rodney Ding | afca336 | 2019-11-01 20:07:34 | [diff] [blame] | 34 | |
| 35 | If your Blink feature does not have a custom enabler function, you need to add |
| 36 | the entry to `runtimeFeatureNameToChromiumFeatureMapping`. For example, a new |
| 37 | entry like this: |
| 38 | ``` |
Xianzhu Wang | 05355f4a | 2020-09-02 01:22:16 | [diff] [blame] | 39 | {"NewFeatureY", features::kNewFeatureY, kDefault}, |
Rodney Ding | afca336 | 2019-11-01 20:07:34 | [diff] [blame] | 40 | ``` |
Xianzhu Wang | 05355f4a | 2020-09-02 01:22:16 | [diff] [blame] | 41 | will call `wf::EnableFeatureFromString` with your feature name instead of |
| 42 | `wf::EnableNewFeatureX` in the same cases as above. |
| 43 | |
| 44 | The following table summarizes the relationship between the default status of |
| 45 | the Chromium feature and the status of the blink feature, when `kDefault` is |
| 46 | specified, if **not overridden** by field trial or command line switches |
| 47 | (horizontal headers: blink feature status; vertical headers: chromium feature |
| 48 | default status): |
| 49 | |
| 50 | | |No status|`status:"test"`|`status:"experimental"`|`status:"stable"`| |
Charlie Harrison | 1daccc9 | 2021-11-05 15:21:17 | [diff] [blame] | 51 | |---|---------|-----------------|--------------------------|-------------------| |
Xianzhu Wang | 05355f4a | 2020-09-02 01:22:16 | [diff] [blame] | 52 | |`FEATURE_DISABLED_BY_DEFAULT`|Disabled everywhere|Blink feature is enabled for tests, or everywhere with `--enable-blink-test-features` [1]|Blink feature is enabled for tests, or everywhere with `--enable-experimental-web-platform-features` [1]|Blink feature is enabled everywhere [2]| |
| 53 | |`FEATURE_ENABLED_BY_DEFAULT`|Enabled everywhere|Enabled everywhere|Enabled everywhere|Enabled everywhere| |
| 54 | |
| 55 | \[1]: `base::FeatureList::IsEnabled(features::kNewFeatureX)` is still |
| 56 | false. These combinations are suitable for features there are fully implemented |
| 57 | at blink side. Otherwise normally the blink feature should not have a status so |
Quinten Yearsley | 317532d | 2021-10-20 17:10:31 | [diff] [blame] | 58 | that the Chromium feature can fully control the feature. |
Xianzhu Wang | 05355f4a | 2020-09-02 01:22:16 | [diff] [blame] | 59 | |
| 60 | \[2]: This combination is counter-intuitive and should be avoided. |
| 61 | |
| 62 | Field trial and command line switches can always override the Chromium feature |
| 63 | status and the blink feature status. |
| 64 | |
| 65 | Besides `kDefault`, there are also other options for the relationship |
| 66 | between the Chromium feature and the blink feature. These other options should |
| 67 | only be used in rare cases when the default relationship doesn't work. |
Rodney Ding | afca336 | 2019-11-01 20:07:34 | [diff] [blame] | 68 | |
| 69 | For more detailed explanation on the options you have, read the comment in enum |
| 70 | [RuntimeFeatureEnableOptions][EnableOptions]. |
| 71 | ### 3) Set by a command line switch to enable or disable: |
| 72 | Add your code to the function |
| 73 | [SetRuntimeFeaturesFromCommandLine()][SetRuntimeFeaturesFromCommandLine]. |
| 74 | |
| 75 | If your Blink feature has a custom enabler function, add a new entry to |
| 76 | `switchToFeatureMapping`. For example, a new entry like this: |
| 77 | ``` |
| 78 | {wrf::EnableNewFeatureX, switches::kNewFeatureX, false}, |
| 79 | ``` |
| 80 | will call `wf::EnableNewFeatureX` to disable it only if that |
| 81 | `switches::kNewFeatureX` exists on the command line. |
| 82 | |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame^] | 83 | ### 4) Combination of the previous options or not covered: |
Rodney Ding | afca336 | 2019-11-01 20:07:34 | [diff] [blame] | 84 | For example, you Blink feature could be controlled by both a base::Feature and a |
| 85 | command line switch. In this case, your custom logic should live here in |
| 86 | [`SetCustomizedRuntimeFeaturesFromCombinedArgs()`][SetCustomizedRuntimeFeaturesFromCombinedArgs]. |
| 87 | |
| 88 | |
| 89 | [EnableOptions]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#135> |
| 90 | [runtime_features]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc> |
| 91 | [RuntimeEnabledFeatures]: |
| 92 | <https://chromium.googlesource.com/chromium/src/+/HEAD/third_party/blink/renderer/platform/RuntimeEnabledFeatures.md> |
Kevin McNee | 92b18f73 | 2020-11-10 00:48:28 | [diff] [blame] | 93 | [web_runtime_features.h]: |
| 94 | <https://chromium.googlesource.com/chromium/src/+/HEAD/third_party/blink/public/platform/web_runtime_features.h> |
| 95 | [web_runtime_features.cc]: |
Rodney Ding | afca336 | 2019-11-01 20:07:34 | [diff] [blame] | 96 | <https://chromium.googlesource.com/chromium/src/+/HEAD/third_party/blink/renderer/platform/exported/web_runtime_features.cc> |
| 97 | [EnableFeatureFromString]:<https://chromium.googlesource.com/chromium/src/+/HEAD/third_party/blink/public/platform/web_runtime_features.h#56> |
| 98 | [SetRuntimeFeatureDefaultsForPlatform]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#46> |
| 99 | [SetCustomizedRuntimeFeaturesFromCombinedArgs]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#487> |
| 100 | [SetRuntimeFeaturesFromChromiumFeatures]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#160> |
| 101 | [SetRuntimeFeaturesFromCommandLine]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#390> |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame^] | 102 | [SetRuntimeFeaturesFromFieldTrialParams]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#448> |