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"
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";
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
;
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
;
62 if (category_preset_string
== kConfigCategoryBenchmarkDeep
) {
63 *category_preset
= BackgroundTracingConfigImpl::BENCHMARK_DEEP
;
67 if (category_preset_string
== kConfigCategoryBenchmarkGPU
) {
68 *category_preset
= BackgroundTracingConfigImpl::BENCHMARK_GPU
;
72 if (category_preset_string
== kConfigCategoryBenchmarkIPC
) {
73 *category_preset
= BackgroundTracingConfigImpl::BENCHMARK_IPC
;
77 if (category_preset_string
== kConfigCategoryBenchmarkStartup
) {
78 *category_preset
= BackgroundTracingConfigImpl::BENCHMARK_STARTUP
;
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_
));
92 case BackgroundTracingConfigImpl::REACTIVE
:
93 dict
->SetString(kConfigModeKey
, kConfigModeReactive
);
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());
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
);
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
);
122 rules_
.push_back(rule
.Pass());
125 scoped_ptr
<BackgroundTracingConfigImpl
> BackgroundTracingConfigImpl::FromDict(
126 const base::DictionaryValue
* dict
) {
130 if (!dict
->GetString(kConfigModeKey
, &mode
))
133 scoped_ptr
<BackgroundTracingConfigImpl
> config
;
135 if (mode
== kConfigModePreemptive
) {
136 config
= PreemptiveFromDict(dict
);
137 } else if (mode
== kConfigModeReactive
) {
138 config
= ReactiveFromDict(dict
);
143 return config
.Pass();
146 scoped_ptr
<BackgroundTracingConfigImpl
>
147 BackgroundTracingConfigImpl::PreemptiveFromDict(
148 const base::DictionaryValue
* 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
))
158 if (!StringToCategoryPreset(category_preset_string
,
159 &config
->category_preset_
))
162 const base::ListValue
* configs_list
= nullptr;
163 if (!dict
->GetList(kConfigsKey
, &configs_list
))
166 for (const auto& it
: *configs_list
) {
167 const base::DictionaryValue
* config_dict
= nullptr;
168 if (!it
->GetAsDictionary(&config_dict
))
171 config
->AddPreemptiveRule(config_dict
);
174 if (config
->rules().empty())
177 return config
.Pass();
180 scoped_ptr
<BackgroundTracingConfigImpl
>
181 BackgroundTracingConfigImpl::ReactiveFromDict(
182 const base::DictionaryValue
* 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
))
192 for (const auto& it
: *configs_list
) {
193 const base::DictionaryValue
* config_dict
= nullptr;
194 if (!it
->GetAsDictionary(&config_dict
))
197 std::string category_preset_string
;
198 if (!config_dict
->GetString(kConfigCategoryKey
, &category_preset_string
))
201 BackgroundTracingConfigImpl::CategoryPreset new_category_preset
;
202 if (!StringToCategoryPreset(category_preset_string
, &new_category_preset
))
205 config
->AddReactiveRule(config_dict
, new_category_preset
);
208 if (config
->rules().empty())
211 return config
.Pass();
214 } // namspace content