1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/unexpire_flags.h"
7 #include "base/no_destructor.h"
8 #include "chrome/browser/expired_flags_list.h"
9 #include "chrome/browser/unexpire_flags_gen.h"
10 #include "chrome/common/chrome_version.h"
16 class FlagPredicateSingleton {
18 FlagPredicateSingleton() = default;
19 ~FlagPredicateSingleton() = default;
21 static const testing::FlagPredicate& GetPredicate() {
22 return GetInstance()->predicate_;
24 static void SetPredicate(testing::FlagPredicate predicate) {
25 GetInstance()->predicate_ = predicate;
29 static FlagPredicateSingleton* GetInstance() {
30 static base::NoDestructor<FlagPredicateSingleton> instance;
31 return instance.get();
34 testing::FlagPredicate predicate_;
39 bool IsFlagExpired(const char* internal_name) {
40 constexpr int kChromeVersion[] = {CHROME_VERSION};
41 constexpr int kChromeVersionMajor = kChromeVersion[0];
43 if (FlagPredicateSingleton::GetPredicate())
44 return FlagPredicateSingleton::GetPredicate().Run(internal_name);
46 for (int i = 0; kExpiredFlags[i].name; ++i) {
47 const ExpiredFlag* f = &kExpiredFlags[i];
48 if (strcmp(f->name, internal_name))
51 // To keep the size of the expired flags list down,
52 // //tools/flags/generate_expired_flags.py doesn't emit flags with expiry
53 // mstone -1; it makes no sense for these flags to be in the expiry list
54 // anyway. However, if a bug did cause that to happen, and this function
55 // didn't handle that case, all flags with expiration -1 would immediately
56 // expire, which would be very bad. As such there's an extra error-check
57 // here: a DCHECK to catch bugs in the script, and a regular if to ensure we
58 // never expire flags that should never expire.
59 DCHECK_NE(f->mstone, -1);
63 const base::Feature* expiry_feature =
64 GetUnexpireFeatureForMilestone(f->mstone);
66 // If there's an unexpiry feature, and the unexpiry feature is *disabled*,
67 // then the flag is expired. The double-negative is very unfortunate.
69 return !base::FeatureList::IsEnabled(*expiry_feature);
70 return f->mstone < kChromeVersionMajor;
77 void SetFlagExpiredPredicate(FlagPredicate predicate) {
78 FlagPredicateSingleton::SetPredicate(predicate);
81 } // namespace testing