OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/feature_list.h" | 5 #include "base/feature_list.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
15 #include "base/pickle.h" | 15 #include "base/pickle.h" |
16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 // Pointer to the FeatureList instance singleton that was set via | 23 // Pointer to the FeatureList instance singleton that was set via |
24 // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to | 24 // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to |
25 // have more control over initialization timing. Leaky. | 25 // have more control over initialization timing. Leaky. |
26 FeatureList* g_instance = nullptr; | 26 FeatureList* g_instance = nullptr; |
27 | 27 |
| 28 bool g_block_overriding_instances_for_testing = false; |
| 29 |
28 // Tracks whether the FeatureList instance was initialized via an accessor. | 30 // Tracks whether the FeatureList instance was initialized via an accessor. |
29 bool g_initialized_from_accessor = false; | 31 bool g_initialized_from_accessor = false; |
30 | 32 |
31 // An allocator entry for a feature in shared memory. The FeatureEntry is | 33 // An allocator entry for a feature in shared memory. The FeatureEntry is |
32 // followed by a base::Pickle object that contains the feature and trial name. | 34 // followed by a base::Pickle object that contains the feature and trial name. |
33 struct FeatureEntry { | 35 struct FeatureEntry { |
34 // SHA1(FeatureEntry): Increment this if structure changes! | 36 // SHA1(FeatureEntry): Increment this if structure changes! |
35 static constexpr uint32_t kPersistentTypeId = 0x06567CA6 + 1; | 37 static constexpr uint32_t kPersistentTypeId = 0x06567CA6 + 1; |
36 | 38 |
37 // Expected size for 32/64-bit check. | 39 // Expected size for 32/64-bit check. |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 void FeatureList::SetInstance(std::unique_ptr<FeatureList> instance) { | 275 void FeatureList::SetInstance(std::unique_ptr<FeatureList> instance) { |
274 DCHECK(!g_instance); | 276 DCHECK(!g_instance); |
275 instance->FinalizeInitialization(); | 277 instance->FinalizeInitialization(); |
276 | 278 |
277 // Note: Intentional leak of global singleton. | 279 // Note: Intentional leak of global singleton. |
278 g_instance = instance.release(); | 280 g_instance = instance.release(); |
279 } | 281 } |
280 | 282 |
281 // static | 283 // static |
282 std::unique_ptr<FeatureList> FeatureList::ClearInstanceForTesting() { | 284 std::unique_ptr<FeatureList> FeatureList::ClearInstanceForTesting() { |
| 285 DCHECK(!g_block_overriding_instances_for_testing); |
283 FeatureList* old_instance = g_instance; | 286 FeatureList* old_instance = g_instance; |
284 g_instance = nullptr; | 287 g_instance = nullptr; |
285 g_initialized_from_accessor = false; | 288 g_initialized_from_accessor = false; |
286 return base::WrapUnique(old_instance); | 289 return base::WrapUnique(old_instance); |
287 } | 290 } |
288 | 291 |
289 // static | 292 // static |
290 void FeatureList::RestoreInstanceForTesting( | 293 void FeatureList::RestoreInstanceForTesting( |
291 std::unique_ptr<FeatureList> instance) { | 294 std::unique_ptr<FeatureList> instance) { |
| 295 DCHECK(!g_block_overriding_instances_for_testing); |
292 DCHECK(!g_instance); | 296 DCHECK(!g_instance); |
293 // Note: Intentional leak of global singleton. | 297 // Note: Intentional leak of global singleton. |
294 g_instance = instance.release(); | 298 g_instance = instance.release(); |
295 } | 299 } |
296 | 300 |
| 301 // static |
| 302 void FeatureList::BlockOverridingInstanceForTesting(bool block) { |
| 303 g_block_overriding_instances_for_testing = block; |
| 304 } |
| 305 |
297 void FeatureList::FinalizeInitialization() { | 306 void FeatureList::FinalizeInitialization() { |
298 DCHECK(!initialized_); | 307 DCHECK(!initialized_); |
299 initialized_ = true; | 308 initialized_ = true; |
300 } | 309 } |
301 | 310 |
302 bool FeatureList::IsFeatureEnabled(const Feature& feature) { | 311 bool FeatureList::IsFeatureEnabled(const Feature& feature) { |
303 DCHECK(initialized_); | 312 DCHECK(initialized_); |
304 DCHECK(IsValidFeatureOrFieldTrialName(feature.name)) << feature.name; | 313 DCHECK(IsValidFeatureOrFieldTrialName(feature.name)) << feature.name; |
305 DCHECK(CheckFeatureIdentity(feature)) << feature.name; | 314 DCHECK(CheckFeatureIdentity(feature)) << feature.name; |
306 | 315 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 return it->second == &feature; | 397 return it->second == &feature; |
389 } | 398 } |
390 | 399 |
391 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state, | 400 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state, |
392 FieldTrial* field_trial) | 401 FieldTrial* field_trial) |
393 : overridden_state(overridden_state), | 402 : overridden_state(overridden_state), |
394 field_trial(field_trial), | 403 field_trial(field_trial), |
395 overridden_by_field_trial(field_trial != nullptr) {} | 404 overridden_by_field_trial(field_trial != nullptr) {} |
396 | 405 |
397 } // namespace base | 406 } // namespace base |
OLD | NEW |