[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / tracing / background_tracing_config_impl.cc
blobcc944ce90ef6a624284c8c43f8fbf29d0f9d2a2f
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
3 // found in the LICENSE file.
5 #include "base/macros.h"
6 #include "base/values.h"
7 #include "content/browser/tracing/background_tracing_config_impl.h"
8 #include "content/browser/tracing/background_tracing_rule.h"
10 namespace content {
12 namespace {
14 const char kConfigsKey[] = "configs";
16 const char kConfigModeKey[] = "mode";
17 const char kConfigModePreemptive[] = "PREEMPTIVE_TRACING_MODE";
18 const char kConfigModeReactive[] = "REACTIVE_TRACING_MODE";
20 const char kConfigCategoryKey[] = "category";
21 const char kConfigCategoryBenchmark[] = "BENCHMARK";
22 const char kConfigCategoryBenchmarkDeep[] = "BENCHMARK_DEEP";
23 const char kConfigCategoryBenchmarkGPU[] = "BENCHMARK_GPU";
24 const char kConfigCategoryBenchmarkIPC[] = "BENCHMARK_IPC";
25 const char kConfigCategoryBenchmarkStartup[] = "BENCHMARK_STARTUP";
27 } // namespace
29 BackgroundTracingConfigImpl::BackgroundTracingConfigImpl(
30 TracingMode tracing_mode)
31 : BackgroundTracingConfig(tracing_mode),
32 category_preset_(BackgroundTracingConfigImpl::BENCHMARK) {}
34 BackgroundTracingConfigImpl::~BackgroundTracingConfigImpl() {}
36 std::string BackgroundTracingConfigImpl::CategoryPresetToString(
37 BackgroundTracingConfigImpl::CategoryPreset category_preset) {
38 switch (category_preset) {
39 case BackgroundTracingConfigImpl::BENCHMARK:
40 return kConfigCategoryBenchmark;
41 case BackgroundTracingConfigImpl::BENCHMARK_DEEP:
42 return kConfigCategoryBenchmarkDeep;
43 case BackgroundTracingConfigImpl::BENCHMARK_GPU:
44 return kConfigCategoryBenchmarkGPU;
45 case BackgroundTracingConfigImpl::BENCHMARK_IPC:
46 return kConfigCategoryBenchmarkIPC;
47 case BackgroundTracingConfigImpl::BENCHMARK_STARTUP:
48 return kConfigCategoryBenchmarkStartup;
50 NOTREACHED();
51 return "";
54 bool BackgroundTracingConfigImpl::StringToCategoryPreset(
55 const std::string& category_preset_string,
56 BackgroundTracingConfigImpl::CategoryPreset* category_preset) {
57 if (category_preset_string == kConfigCategoryBenchmark) {
58 *category_preset = BackgroundTracingConfigImpl::BENCHMARK;
59 return true;
62 if (category_preset_string == kConfigCategoryBenchmarkDeep) {
63 *category_preset = BackgroundTracingConfigImpl::BENCHMARK_DEEP;
64 return true;
67 if (category_preset_string == kConfigCategoryBenchmarkGPU) {
68 *category_preset = BackgroundTracingConfigImpl::BENCHMARK_GPU;
69 return true;
72 if (category_preset_string == kConfigCategoryBenchmarkIPC) {
73 *category_preset = BackgroundTracingConfigImpl::BENCHMARK_IPC;
74 return true;
77 if (category_preset_string == kConfigCategoryBenchmarkStartup) {
78 *category_preset = BackgroundTracingConfigImpl::BENCHMARK_STARTUP;
79 return true;
82 return false;
85 void BackgroundTracingConfigImpl::IntoDict(base::DictionaryValue* dict) const {
86 switch (tracing_mode()) {
87 case BackgroundTracingConfigImpl::PREEMPTIVE:
88 dict->SetString(kConfigModeKey, kConfigModePreemptive);
89 dict->SetString(kConfigCategoryKey,
90 CategoryPresetToString(category_preset_));
91 break;
92 case BackgroundTracingConfigImpl::REACTIVE:
93 dict->SetString(kConfigModeKey, kConfigModeReactive);
94 break;
97 scoped_ptr<base::ListValue> configs_list(new base::ListValue());
98 for (const auto& it : rules_) {
99 scoped_ptr<base::DictionaryValue> config_dict(new base::DictionaryValue());
100 DCHECK(it);
101 it->IntoDict(config_dict.get());
102 configs_list->Append(config_dict.Pass());
105 dict->Set(kConfigsKey, configs_list.Pass());
108 void BackgroundTracingConfigImpl::AddPreemptiveRule(
109 const base::DictionaryValue* dict) {
110 scoped_ptr<BackgroundTracingRule> rule =
111 BackgroundTracingRule::PreemptiveRuleFromDict(dict);
112 if (rule)
113 rules_.push_back(rule.Pass());
116 void BackgroundTracingConfigImpl::AddReactiveRule(
117 const base::DictionaryValue* dict,
118 BackgroundTracingConfigImpl::CategoryPreset category_preset) {
119 scoped_ptr<BackgroundTracingRule> rule =
120 BackgroundTracingRule::ReactiveRuleFromDict(dict, category_preset);
121 if (rule)
122 rules_.push_back(rule.Pass());
125 scoped_ptr<BackgroundTracingConfigImpl> BackgroundTracingConfigImpl::FromDict(
126 const base::DictionaryValue* dict) {
127 DCHECK(dict);
129 std::string mode;
130 if (!dict->GetString(kConfigModeKey, &mode))
131 return nullptr;
133 scoped_ptr<BackgroundTracingConfigImpl> config;
135 if (mode == kConfigModePreemptive) {
136 config = PreemptiveFromDict(dict);
137 } else if (mode == kConfigModeReactive) {
138 config = ReactiveFromDict(dict);
139 } else {
140 return nullptr;
143 return config.Pass();
146 scoped_ptr<BackgroundTracingConfigImpl>
147 BackgroundTracingConfigImpl::PreemptiveFromDict(
148 const base::DictionaryValue* dict) {
149 DCHECK(dict);
151 scoped_ptr<BackgroundTracingConfigImpl> config(
152 new BackgroundTracingConfigImpl(BackgroundTracingConfigImpl::PREEMPTIVE));
154 std::string category_preset_string;
155 if (!dict->GetString(kConfigCategoryKey, &category_preset_string))
156 return nullptr;
158 if (!StringToCategoryPreset(category_preset_string,
159 &config->category_preset_))
160 return nullptr;
162 const base::ListValue* configs_list = nullptr;
163 if (!dict->GetList(kConfigsKey, &configs_list))
164 return nullptr;
166 for (const auto& it : *configs_list) {
167 const base::DictionaryValue* config_dict = nullptr;
168 if (!it->GetAsDictionary(&config_dict))
169 return nullptr;
171 config->AddPreemptiveRule(config_dict);
174 if (config->rules().empty())
175 return nullptr;
177 return config.Pass();
180 scoped_ptr<BackgroundTracingConfigImpl>
181 BackgroundTracingConfigImpl::ReactiveFromDict(
182 const base::DictionaryValue* dict) {
183 DCHECK(dict);
185 scoped_ptr<BackgroundTracingConfigImpl> config(
186 new BackgroundTracingConfigImpl(BackgroundTracingConfigImpl::REACTIVE));
188 const base::ListValue* configs_list = nullptr;
189 if (!dict->GetList(kConfigsKey, &configs_list))
190 return nullptr;
192 for (const auto& it : *configs_list) {
193 const base::DictionaryValue* config_dict = nullptr;
194 if (!it->GetAsDictionary(&config_dict))
195 return nullptr;
197 std::string category_preset_string;
198 if (!config_dict->GetString(kConfigCategoryKey, &category_preset_string))
199 return nullptr;
201 BackgroundTracingConfigImpl::CategoryPreset new_category_preset;
202 if (!StringToCategoryPreset(category_preset_string, &new_category_preset))
203 return nullptr;
205 config->AddReactiveRule(config_dict, new_category_preset);
208 if (config->rules().empty())
209 return nullptr;
211 return config.Pass();
214 } // namspace content