Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 1 | # Adding a new feature flag in chrome://flags |
| 2 | |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 3 | This document describes how to add a new Chrome feature flag visible to users |
Solomon Kinard | 584278e | 2023-02-22 04:35:29 | [diff] [blame] | 4 | via `chrome://flags` UI. |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 5 | |
| 6 | *** note |
| 7 | **NOTE:** It's NOT required if you don't intend to make your feature appear in |
Solomon Kinard | 584278e | 2023-02-22 04:35:29 | [diff] [blame] | 8 | `chrome://flags` UI. |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 9 | *** |
| 10 | |
| 11 | See also the following for definitions: |
| 12 | * [Configuration: Features](configuration.md#features) |
| 13 | * [Configuration: Flags](configuration.md#flags) |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 14 | |
| 15 | ## Step 1: Adding a new `base::Feature` |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 16 | |
| 17 | This step would be different depending on where you want to use the flag: |
| 18 | |
| 19 | ### To Use the Flag in `content/` Only |
| 20 | |
| 21 | Add a `base::Feature` to the following files: |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 22 | |
| 23 | * [content/public/common/content_features.cc](https://cs.chromium.org/chromium/src/content/public/common/content_features.cc) |
| 24 | * [content/public/common/content_features.h](https://cs.chromium.org/chromium/src/content/public/common/content_features.h) |
| 25 | |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 26 | ### To Use the Flag in `third_party/blink/` (and Possibly in `content/`) |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 27 | |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 28 | Add a `base::Feature` to the following files: |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 29 | |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 30 | * [third_party/blink/common/features.cc](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/common/features.cc) |
| 31 | * [third_party/blink/public/common/features.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/public/common/features.h) |
| 32 | |
| 33 | Historically, Blink also has its own runtime feature mechanism. So if you |
| 34 | feature needs to be runtime-enabled, read also Blink's |
| 35 | [Runtime Enable Features][blink-rte] doc and |
| 36 | [Initialization of Blink runtime features in content layer][blink-rte-init]. |
| 37 | |
| 38 | [blink-rte]: ../third_party/blink/renderer/platform/RuntimeEnabledFeatures.md |
| 39 | |
| 40 | ### Examples |
| 41 | |
| 42 | You can refer to [this CL](https://chromium-review.googlesource.com/c/554510/) |
| 43 | and [this document](initialize_blink_features.md) to see |
| 44 | |
| 45 | 1. Where to add the `base::Feature`: |
| 46 | [[1](https://chromium-review.googlesource.com/c/554510/8/content/public/common/content_features.cc#253)] |
| 47 | [[2](https://chromium-review.googlesource.com/c/554510/8/content/public/common/content_features.h)] |
| 48 | 2. How to use it: |
| 49 | [[1](https://chromium-review.googlesource.com/c/554510/8/content/common/service_worker/service_worker_utils.cc#153)] |
| 50 | 3. How to wire your new `base::Feature` to a Blink runtime feature: |
| 51 | [[1][blink-rte-init]] |
| 52 | 4. How to use it in Blink: |
| 53 | [[1](https://chromium-review.googlesource.com/c/554510/8/third_party/blnk/renderere/core/workers/worker_thread.cc)] |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 54 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 55 | Also, this patch added a virtual test for running web tests with the flag. |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 56 | When you add a flag, you can consider to use that. |
| 57 | |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 58 | [blink-rte-init]: initialize_blink_features.md |
| 59 | |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 60 | ## Step 2: Adding the feature flag to the chrome://flags UI. |
Nate Fischer | 132c171 | 2021-09-21 20:03:32 | [diff] [blame] | 61 | |
| 62 | *** promo |
Johann Koenig | bac66bc | 2022-11-24 05:39:18 | [diff] [blame] | 63 | Googlers: Read also [Chrome Feature Flag in chrome://flags](http://go/finch-feature-api#chrome-feature-flag-in-chromeflags). |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 64 | *** |
| 65 | |
| 66 | *** promo |
Nate Fischer | 132c171 | 2021-09-21 20:03:32 | [diff] [blame] | 67 | **Tip:** Android WebView has its own flag UI. The WebView team recommends adding |
| 68 | your features there too if they are supported on WebView. Follow |
| 69 | [these steps](/android_webview/docs/developer-ui.md#Adding-your-flags-and-features-to-the-UI) |
| 70 | for WebView flags. |
| 71 | *** |
| 72 | |
Andrew Moylan | 9b2c3b0 | 2019-02-19 14:32:51 | [diff] [blame] | 73 | You have to modify these five files in total. |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 74 | |
| 75 | * [chrome/browser/about_flags.cc](https://cs.chromium.org/chromium/src/chrome/browser/about_flags.cc) |
| 76 | * [chrome/browser/flag_descriptions.cc](https://cs.chromium.org/chromium/src/chrome/browser/flag_descriptions.cc) |
| 77 | * [chrome/browser/flag_descriptions.h](https://cs.chromium.org/chromium/src/chrome/browser/flag_descriptions.h) |
| 78 | * [tools/metrics/histograms/enums.xml](https://cs.chromium.org/chromium/src/tools/metrics/histograms/enums.xml) |
Andrew Moylan | 9b2c3b0 | 2019-02-19 14:32:51 | [diff] [blame] | 79 | * [chrome/browser/flag-metadata.json](https://cs.chromium.org/chromium/src/chrome/browser/flag-metadata.json) |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 80 | |
| 81 | At first you need to add an entry to __about_flags.cc__, |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 82 | __flag_descriptions.cc__ and __flag_descriptions.h__. After that, try running |
| 83 | the following test. |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 84 | |
| 85 | ```bash |
| 86 | # Build unit_tests |
Max Moroz | f5b31fcd | 2018-08-10 21:55:48 | [diff] [blame] | 87 | autoninja -C out/Default unit_tests |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 88 | # Run AboutFlagsHistogramTest.CheckHistograms |
| 89 | ./out/Default/unit_tests --gtest_filter=AboutFlagsHistogramTest.CheckHistograms |
Becky Zhou | 273cd2d5 | 2019-01-08 22:46:58 | [diff] [blame] | 90 | # Run AboutFlagsHistogramTest.CheckHistograms on Android |
| 91 | ./out/Default/bin/run_unit_tests --gtest_filter=AboutFlagsHistogramTest.CheckHistograms |
Makoto Shimazu | 9f0bd3a | 2017-08-08 01:54:24 | [diff] [blame] | 92 | ``` |
| 93 | |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 94 | That test will ask you to add several entries to enums.xml. After doing so, run |
| 95 | `git cl format` which will insert the entries in enums.xml in the correct order |
| 96 | and run the tests again. |
| 97 | You can refer to [this CL](https://chromium-review.googlesource.com/c/593707) as |
| 98 | an example. |
Andrew Moylan | 9b2c3b0 | 2019-02-19 14:32:51 | [diff] [blame] | 99 | |
| 100 | Finally, run the following test. |
| 101 | |
| 102 | ```bash |
| 103 | ./out/Default/unit_tests --gtest_filter=AboutFlagsTest.EveryFlagHasMetadata |
| 104 | ``` |
| 105 | |
| 106 | That test will ask you to update the flag expiry metadata in |
| 107 | [flag-metadata.json](https://cs.chromium.org/chromium/src/chrome/browser/flag-metadata.json). |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 108 | |
Johann Koenig | ccd30e5 | 2022-11-21 06:05:39 | [diff] [blame] | 109 | ## Removing the feature flag. |
| 110 | |
| 111 | When a feature flag is no longer used it should be removed. Once it has reached it's final state it |
| 112 | can be removed in stages. |
| 113 | |
| 114 | First remove the flag from the UI: |
| 115 | * [chrome/browser/about_flags.cc](https://cs.chromium.org/chromium/src/chrome/browser/about_flags.cc) |
| 116 | * [chrome/browser/flag_descriptions.cc](https://cs.chromium.org/chromium/src/chrome/browser/flag_descriptions.cc) |
| 117 | * [chrome/browser/flag_descriptions.h](https://cs.chromium.org/chromium/src/chrome/browser/flag_descriptions.h) |
| 118 | * [chrome/browser/flag-metadata.json](https://cs.chromium.org/chromium/src/chrome/browser/flag-metadata.json) |
| 119 | * Do not edit enums.xml. Keep the flag for archeological purposes. |
| 120 | |
Solomon Kinard | 9cecfcf | 2023-06-26 22:19:59 | [diff] [blame] | 121 | Once there is no way to change the flag value, it's usage can be removed from the code. |
Johann Koenig | ccd30e5 | 2022-11-21 06:05:39 | [diff] [blame] | 122 | |
| 123 | Finally, once the flag is no longer referenced, it can be removed from content/ and |
| 124 | third_party/blink/ |
| 125 | |
Ming-Ying Chung | c23505d6 | 2022-09-22 10:07:33 | [diff] [blame] | 126 | ## Related Documents |
| 127 | |
| 128 | * [Chromium Feature API & Finch (Googler-only)](http://go/finch-feature-api) |
| 129 | * [Configuration: Prefs, Settings, Features, Switches & Flags](configuration.md) |
| 130 | * [Runtime Enabled Features](../third_party/blink/renderer/platform/RuntimeEnabledFeatures.md) |
| 131 | * [Initialization of Blink runtime features in content layer](initialize_blink_features.md) |