Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(777)

Side by Side Diff: base/feature_list.cc

Issue 2546653002: Store and retrieve features from shared memory (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/strings/string_split.h" 16 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
17 18
18 namespace base { 19 namespace base {
19 20
20 namespace { 21 namespace {
21 22
22 // Pointer to the FeatureList instance singleton that was set via 23 // Pointer to the FeatureList instance singleton that was set via
23 // 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
24 // have more control over initialization timing. Leaky. 25 // have more control over initialization timing. Leaky.
25 FeatureList* g_instance = nullptr; 26 FeatureList* g_instance = nullptr;
26 27
27 // Tracks whether the FeatureList instance was initialized via an accessor. 28 // Tracks whether the FeatureList instance was initialized via an accessor.
28 bool g_initialized_from_accessor = false; 29 bool g_initialized_from_accessor = false;
29 30
31 const uint32_t kFeatureType = 0x06567CA6 + 1; // SHA1(FeatureEntry) v1
32
33 struct FeatureEntry {
34 // Expected size for 32/64-bit check.
35 static constexpr size_t kExpectedInstanceSize = 8;
36
37 // Specifies whether a feature override enables or disables the future. Same
38 // values as the OverrideState enum in feature_list.h
39 uint32_t override_state;
40
41 // Size of the pickled structure, NOT the total size of this entry.
42 uint32_t size;
43 };
44
30 // Some characters are not allowed to appear in feature names or the associated 45 // Some characters are not allowed to appear in feature names or the associated
31 // field trial names, as they are used as special characters for command-line 46 // field trial names, as they are used as special characters for command-line
32 // serialization. This function checks that the strings are ASCII (since they 47 // serialization. This function checks that the strings are ASCII (since they
33 // are used in command-line API functions that require ASCII) and whether there 48 // are used in command-line API functions that require ASCII) and whether there
34 // are any reserved characters present, returning true if the string is valid. 49 // are any reserved characters present, returning true if the string is valid.
35 // Only called in DCHECKs. 50 // Only called in DCHECKs.
36 bool IsValidFeatureOrFieldTrialName(const std::string& name) { 51 bool IsValidFeatureOrFieldTrialName(const std::string& name) {
37 return IsStringASCII(name) && name.find_first_of(",<*") == std::string::npos; 52 return IsStringASCII(name) && name.find_first_of(",<*") == std::string::npos;
38 } 53 }
39 54
40 } // namespace 55 } // namespace
41 56
42 FeatureList::FeatureList() {} 57 FeatureList::FeatureList() {}
43 58
44 FeatureList::~FeatureList() {} 59 FeatureList::~FeatureList() {}
45 60
46 void FeatureList::InitializeFromCommandLine( 61 void FeatureList::InitializeFromCommandLine(
47 const std::string& enable_features, 62 const std::string& enable_features,
48 const std::string& disable_features) { 63 const std::string& disable_features) {
49 DCHECK(!initialized_); 64 DCHECK(!initialized_);
50 65
lawrencewu 2016/12/01 02:39:48 What we probably wanna do here is try calling Init
lawrencewu 2016/12/01 18:47:10 Moved the fallback code into a new function Create
51 // Process disabled features first, so that disabled ones take precedence over 66 // Process disabled features first, so that disabled ones take precedence over
52 // enabled ones (since RegisterOverride() uses insert()). 67 // enabled ones (since RegisterOverride() uses insert()).
53 RegisterOverridesFromCommandLine(disable_features, OVERRIDE_DISABLE_FEATURE); 68 RegisterOverridesFromCommandLine(disable_features, OVERRIDE_DISABLE_FEATURE);
54 RegisterOverridesFromCommandLine(enable_features, OVERRIDE_ENABLE_FEATURE); 69 RegisterOverridesFromCommandLine(enable_features, OVERRIDE_ENABLE_FEATURE);
55 70
56 initialized_from_command_line_ = true; 71 initialized_from_command_line_ = true;
57 } 72 }
58 73
74 void FeatureList::InitializeFromSharedMemory(
75 SharedPersistentMemoryAllocator* allocator) {
76 DCHECK(!initialized_);
77
78 SharedPersistentMemoryAllocator::Iterator iter(allocator);
79
80 SharedPersistentMemoryAllocator::Reference ref;
81 while ((ref = iter.GetNextOfType(kFeatureType)) !=
82 SharedPersistentMemoryAllocator::kReferenceNull) {
83 const FeatureEntry* entry =
84 allocator->GetAsObject<const FeatureEntry>(ref, kFeatureType);
85 LOG(ERROR) << entry;
86 // get the field trial, feature name, and overiden state and call
87 // registeroverride here.
88 }
89 }
90
59 bool FeatureList::IsFeatureOverriddenFromCommandLine( 91 bool FeatureList::IsFeatureOverriddenFromCommandLine(
60 const std::string& feature_name, 92 const std::string& feature_name,
61 OverrideState state) const { 93 OverrideState state) const {
62 auto it = overrides_.find(feature_name); 94 auto it = overrides_.find(feature_name);
63 return it != overrides_.end() && it->second.overridden_state == state && 95 return it != overrides_.end() && it->second.overridden_state == state &&
64 !it->second.overridden_by_field_trial; 96 !it->second.overridden_by_field_trial;
65 } 97 }
66 98
67 void FeatureList::AssociateReportingFieldTrial( 99 void FeatureList::AssociateReportingFieldTrial(
68 const std::string& feature_name, 100 const std::string& feature_name,
(...skipping 22 matching lines...) Expand all
91 DCHECK(!ContainsKey(overrides_, feature_name) || 123 DCHECK(!ContainsKey(overrides_, feature_name) ||
92 !overrides_.find(feature_name)->second.field_trial) 124 !overrides_.find(feature_name)->second.field_trial)
93 << "Feature " << feature_name 125 << "Feature " << feature_name
94 << " has conflicting field trial overrides: " 126 << " has conflicting field trial overrides: "
95 << overrides_.find(feature_name)->second.field_trial->trial_name() 127 << overrides_.find(feature_name)->second.field_trial->trial_name()
96 << " / " << field_trial->trial_name(); 128 << " / " << field_trial->trial_name();
97 129
98 RegisterOverride(feature_name, override_state, field_trial); 130 RegisterOverride(feature_name, override_state, field_trial);
99 } 131 }
100 132
133 void FeatureList::AddFeaturesToAllocator(
134 FieldTrialList::FieldTrialAllocator* allocator) {
135 DCHECK(initialized_);
136
137 for (const auto& override : overrides_) {
138 Pickle pickle;
139 pickle.WriteString(override.first);
140 if (override.second.field_trial)
141 pickle.WriteString(override.second.field_trial->trial_name());
142
143 size_t total_size = sizeof(FeatureEntry) + pickle.size();
144 SharedPersistentMemoryAllocator::Reference ref =
145 allocator->Allocate(total_size, kFeatureType);
146 if (!ref)
147 return;
148
149 FeatureEntry* entry =
150 allocator->GetAsObject<FeatureEntry>(ref, kFeatureType);
151 entry->override_state = override.second.overridden_state;
152 entry->size = pickle.size();
153
154 char* dst = reinterpret_cast<char*>(entry) + sizeof(FeatureEntry);
155 memcpy(dst, pickle.data(), pickle.size());
156
157 allocator->MakeIterable(ref);
158 // should we add a ref to each feature?
159 }
160 }
161
101 void FeatureList::GetFeatureOverrides(std::string* enable_overrides, 162 void FeatureList::GetFeatureOverrides(std::string* enable_overrides,
102 std::string* disable_overrides) { 163 std::string* disable_overrides) {
103 DCHECK(initialized_); 164 DCHECK(initialized_);
104 165
105 enable_overrides->clear(); 166 enable_overrides->clear();
106 disable_overrides->clear(); 167 disable_overrides->clear();
107 168
108 // Note: Since |overrides_| is a std::map, iteration will be in alphabetical 169 // Note: Since |overrides_| is a std::map, iteration will be in alphabetical
109 // order. This not guaranteed to users of this function, but is useful for 170 // order. This not guaranteed to users of this function, but is useful for
110 // tests to assume the order. 171 // tests to assume the order.
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 return it->second == &feature; 368 return it->second == &feature;
308 } 369 }
309 370
310 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state, 371 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state,
311 FieldTrial* field_trial) 372 FieldTrial* field_trial)
312 : overridden_state(overridden_state), 373 : overridden_state(overridden_state),
313 field_trial(field_trial), 374 field_trial(field_trial),
314 overridden_by_field_trial(field_trial != nullptr) {} 375 overridden_by_field_trial(field_trial != nullptr) {}
315 376
316 } // namespace base 377 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698