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 |
| 14 | - [third_party/blink/public/platform/web_runtime_features.h][WebRuntimeFeatures.h] |
| 15 | - [third_party/blink/public/platform/web_runtime_features.cc][WebRuntimeFeatures.cc] |
| 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 | ``` |
| 29 | {wf::EnableNewFeatureX, features::kNewFeatureX, kEnableOnly}, |
| 30 | ``` |
| 31 | will call `wf::EnableNewFeatureX` to enable it only if `features::kNewFeatureX` |
| 32 | is enabled. |
| 33 | |
| 34 | If your Blink feature does not have a custom enabler function, you need to add |
| 35 | the entry to `runtimeFeatureNameToChromiumFeatureMapping`. For example, a new |
| 36 | entry like this: |
| 37 | ``` |
| 38 | {"NewFeatureY", features::kNewFeatureY, kUseFeatureState}, |
| 39 | ``` |
| 40 | will call `wf::EnableFeatureFromString` with your feature name to set it to |
| 41 | whichever state your `features::kNewFeatureY` is in. |
| 42 | |
| 43 | For more detailed explanation on the options you have, read the comment in enum |
| 44 | [RuntimeFeatureEnableOptions][EnableOptions]. |
| 45 | ### 3) Set by a command line switch to enable or disable: |
| 46 | Add your code to the function |
| 47 | [SetRuntimeFeaturesFromCommandLine()][SetRuntimeFeaturesFromCommandLine]. |
| 48 | |
| 49 | If your Blink feature has a custom enabler function, add a new entry to |
| 50 | `switchToFeatureMapping`. For example, a new entry like this: |
| 51 | ``` |
| 52 | {wrf::EnableNewFeatureX, switches::kNewFeatureX, false}, |
| 53 | ``` |
| 54 | will call `wf::EnableNewFeatureX` to disable it only if that |
| 55 | `switches::kNewFeatureX` exists on the command line. |
| 56 | |
| 57 | ### 4) Controlled by parameters from a field trial: |
| 58 | Add your code to the function |
| 59 | [SetRuntimeFeaturesFromFieldTrialParams()][SetRuntimeFeaturesFromFieldTrialParams]. |
| 60 | |
| 61 | ### 5) Combination of the previous options or not covered: |
| 62 | For example, you Blink feature could be controlled by both a base::Feature and a |
| 63 | command line switch. In this case, your custom logic should live here in |
| 64 | [`SetCustomizedRuntimeFeaturesFromCombinedArgs()`][SetCustomizedRuntimeFeaturesFromCombinedArgs]. |
| 65 | |
| 66 | |
| 67 | [EnableOptions]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#135> |
| 68 | [runtime_features]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc> |
| 69 | [RuntimeEnabledFeatures]: |
| 70 | <https://chromium.googlesource.com/chromium/src/+/HEAD/third_party/blink/renderer/platform/RuntimeEnabledFeatures.md> |
| 71 | [WebRuntimeFeatures.h]: |
| 72 | <https://chromium.googlesource.com/chromium/src/+/HEAD/third_party/blink/renderer/platform/exported/web_runtime_features.h> |
| 73 | [WebRuntimeFeatures.cc]: |
| 74 | <https://chromium.googlesource.com/chromium/src/+/HEAD/third_party/blink/renderer/platform/exported/web_runtime_features.cc> |
| 75 | [EnableFeatureFromString]:<https://chromium.googlesource.com/chromium/src/+/HEAD/third_party/blink/public/platform/web_runtime_features.h#56> |
| 76 | [SetRuntimeFeatureDefaultsForPlatform]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#46> |
| 77 | [SetCustomizedRuntimeFeaturesFromCombinedArgs]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#487> |
| 78 | [SetRuntimeFeaturesFromChromiumFeatures]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#160> |
| 79 | [SetRuntimeFeaturesFromCommandLine]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#390> |
| 80 | [SetRuntimeFeaturesFromFieldTrialParams]:<https://chromium.googlesource.com/chromium/src/+/HEAD/content/child/runtime_features.cc#448> |