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

Unified Diff: content/browser/tracing/background_tracing_config.cc

Issue 1162623002: Implement FromDict/IntoDict for BackgroundTracingConfig. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | content/browser/tracing/background_tracing_config_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/tracing/background_tracing_config.cc
diff --git a/content/browser/tracing/background_tracing_config.cc b/content/browser/tracing/background_tracing_config.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ec4c41888ccb8e48fb7132a203e6939a88dbbb68
--- /dev/null
+++ b/content/browser/tracing/background_tracing_config.cc
@@ -0,0 +1,274 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/macros.h"
+#include "base/values.h"
+#include "content/public/browser/background_tracing_preemptive_config.h"
+#include "content/public/browser/background_tracing_reactive_config.h"
+
+namespace content {
+
+namespace {
+
+const char kConfigsKey[] = "configs";
+
+const char kConfigModeKey[] = "mode";
+const char kConfigModePreemptive[] = "preemptive";
+const char kConfigModeReactive[] = "reactive";
+
+const char kConfigCategoryKey[] = "category";
+const char kConfigCategoryBenchmark[] = "benchmark";
+const char kConfigCategoryBenchmarkDeep[] = "benchmark-deep";
+
+const char kConfigRuleKey[] = "rule";
+const char kConfigRuleMonitorNamed[] = "monitor_named";
+const char kConfigRuleTriggerNameKey[] = "trigger_name";
+const char kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull[] =
+ "trace_on_trigger_or_10s_or_trigger_or_full";
oystein (OOO til 10th of July) 2015/05/28 17:00:46 Shouldn't this be trace_on_trigger_until_10s_or_tr
shatch 2015/05/28 19:17:15 Ya was trying to shorten it since it was so long,
+
+const char kReactiveConfigTriggerNameKey[] = "trigger_name";
+
+} // namespace
+
+std::string CategoryPresetToString(
+ BackgroundTracingConfig::CategoryPreset category_preset) {
+ switch (category_preset) {
+ case BackgroundTracingConfig::BENCHMARK:
+ return kConfigCategoryBenchmark;
+ case BackgroundTracingConfig::BENCHMARK_DEEP:
+ return kConfigCategoryBenchmarkDeep;
+ break;
+ }
+ CHECK(false);
oystein (OOO til 10th of July) 2015/05/28 17:00:46 NOTREACHED()?
shatch 2015/05/28 19:17:15 Done.
+ return "";
+}
+
+bool IsStringValidCategoryPreset(const std::string& category_preset_string) {
oystein (OOO til 10th of July) 2015/05/28 17:00:46 Feels like IsStringValidCategoryPreset() and Strin
shatch 2015/05/28 19:17:14 Done.
+ if (category_preset_string == kConfigCategoryBenchmark ||
+ category_preset_string == kConfigCategoryBenchmarkDeep) {
+ return true;
+ }
+ return false;
+}
+
+BackgroundTracingConfig::CategoryPreset StringToCategoryPreset(
+ const std::string& category_preset_string) {
+ if (category_preset_string == kConfigCategoryBenchmark) {
+ return BackgroundTracingConfig::BENCHMARK;
+ } else if (category_preset_string == kConfigCategoryBenchmarkDeep) {
+ return BackgroundTracingConfig::BENCHMARK_DEEP;
+ }
+
+ CHECK(false);
oystein (OOO til 10th of July) 2015/05/28 17:00:46 NOTREACHED()?
shatch 2015/05/28 19:17:15 Done.
+ return BackgroundTracingConfig::BENCHMARK;
+}
+
+BackgroundTracingReactiveConfig::RuleType StringToReactiveConfigRuleType(
+ const std::string& rule_type) {
+ if (rule_type == kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull) {
+ return BackgroundTracingReactiveConfig::
+ TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL;
+ }
+ CHECK(false);
oystein (OOO til 10th of July) 2015/05/28 17:00:46 NOTREACHED()?
shatch 2015/05/28 19:17:15 Done.
+ return BackgroundTracingReactiveConfig::
+ TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL;
+}
+
+scoped_ptr<BackgroundTracingPreemptiveConfig>
+BackgroundTracingPreemptiveConfig_FromDict(const base::DictionaryValue* dict) {
+ scoped_ptr<BackgroundTracingPreemptiveConfig> config(
+ new BackgroundTracingPreemptiveConfig());
+
+ std::string category_preset_string;
+ if (!dict->GetString(kConfigCategoryKey, &category_preset_string))
+ return NULL;
+
+ if (!IsStringValidCategoryPreset(category_preset_string))
+ return NULL;
+
+ config->category_preset = StringToCategoryPreset(category_preset_string);
+
+ const base::ListValue* configs_list = NULL;
+ if (!dict->GetList(kConfigsKey, &configs_list))
+ return NULL;
+
+ for (auto it = configs_list->begin(); it != configs_list->end(); ++it) {
+ const base::DictionaryValue* config_dict = NULL;
+ if (!(*it)->GetAsDictionary(&config_dict))
+ return NULL;
+
+ std::string type;
+ if (!config_dict->GetString(kConfigRuleKey, &type))
+ return NULL;
+
+ if (type == kConfigRuleMonitorNamed) {
+ std::string trigger_name;
+ if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name))
+ return NULL;
+
+ BackgroundTracingPreemptiveConfig::MonitoringRule rule;
+ rule.type = BackgroundTracingPreemptiveConfig::
+ MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED;
+ rule.named_trigger_info.trigger_name = trigger_name;
+
+ config->configs.push_back(rule);
+ } else {
+ // TODO(simonhatch): Implement UMA triggers.
oystein (OOO til 10th of July) 2015/05/28 17:00:46 Brainstorming for later CLs: Let's add UMAs that c
+ return NULL;
+ }
+ }
+
+ return config.Pass();
+}
+
+scoped_ptr<BackgroundTracingReactiveConfig>
+BackgroundTracingReactiveConfig_FromDict(const base::DictionaryValue* dict) {
+ scoped_ptr<BackgroundTracingReactiveConfig> config(
+ new BackgroundTracingReactiveConfig());
+
+ const base::ListValue* configs_list = NULL;
+ if (!dict->GetList(kConfigsKey, &configs_list))
+ return NULL;
+
+ for (auto it = configs_list->begin(); it != configs_list->end(); ++it) {
+ const base::DictionaryValue* config_dict = NULL;
+ if (!(*it)->GetAsDictionary(&config_dict))
+ return NULL;
+
+ BackgroundTracingReactiveConfig::TracingRule rule;
+
+ std::string category_preset_string;
+ if (!config_dict->GetString(kConfigCategoryKey, &category_preset_string))
+ return NULL;
+
+ if (!IsStringValidCategoryPreset(category_preset_string))
+ return NULL;
+
+ rule.category_preset = StringToCategoryPreset(category_preset_string);
+
+ std::string type;
+ if (!config_dict->GetString(kConfigRuleKey, &type))
+ return NULL;
+
+ rule.type = StringToReactiveConfigRuleType(type);
+
+ std::string trigger_name;
+ if (!config_dict->GetString(kReactiveConfigTriggerNameKey, &trigger_name))
+ return NULL;
+
+ rule.trigger_name = trigger_name;
+
+ config->configs.push_back(rule);
+ }
+
+ return config.Pass();
+}
+
+bool BackgroundTracingPreemptiveConfig_IntoDict(
+ const BackgroundTracingPreemptiveConfig* config,
+ base::DictionaryValue* dict) {
+ dict->SetString(kConfigCategoryKey,
+ CategoryPresetToString(config->category_preset));
+
+ base::ListValue* configs_list = new base::ListValue();
oystein (OOO til 10th of July) 2015/05/28 17:00:47 Is configs_list and config_dict leaking if we retu
shatch 2015/05/28 19:17:15 Done.
+
+ for (size_t i = 0; i < config->configs.size(); ++i) {
+ base::DictionaryValue* config_dict = new base::DictionaryValue();
+
+ if (config->configs[i].type == BackgroundTracingPreemptiveConfig::
+ MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) {
+ config_dict->SetString(kConfigRuleKey, kConfigRuleMonitorNamed);
+ config_dict->SetString(
+ kConfigRuleTriggerNameKey,
+ config->configs[i].named_trigger_info.trigger_name.c_str());
+ } else {
+ // TODO(simonhatch): Implement UMA triggers.
+ return false;
+ }
+
+ configs_list->Append(config_dict);
+ }
+
+ dict->Set(kConfigsKey, configs_list);
+
+ return true;
+}
+
+bool BackgroundTracingReactiveConfig_IntoDict(
+ const BackgroundTracingReactiveConfig* config,
+ base::DictionaryValue* dict) {
+ base::ListValue* configs_list = new base::ListValue();
+
+ for (size_t i = 0; i < config->configs.size(); ++i) {
+ base::DictionaryValue* config_dict = new base::DictionaryValue();
oystein (OOO til 10th of July) 2015/05/28 17:00:46 Same leak comment as above
shatch 2015/05/28 19:17:15 Done.
+
+ config_dict->SetString(
+ kConfigCategoryKey,
+ CategoryPresetToString(config->configs[i].category_preset));
+
+ switch (config->configs[i].type) {
+ case BackgroundTracingReactiveConfig::
+ TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL:
+ config_dict->SetString(
+ kConfigRuleKey,
+ kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull);
+ break;
+ default:
oystein (OOO til 10th of July) 2015/05/28 17:00:46 Do we need the default?
shatch 2015/05/28 19:17:15 Yeah to error out for any enums we don't support y
+ return false;
+ }
+
+ config_dict->SetString(kConfigRuleTriggerNameKey,
+ config->configs[i].trigger_name.c_str());
+
+ configs_list->Append(config_dict);
+ }
+
+ dict->Set(kConfigsKey, configs_list);
+
+ return true;
+}
+
+scoped_ptr<BackgroundTracingConfig> BackgroundTracingConfig::FromDict(
+ const base::DictionaryValue* dict) {
+ if (!dict)
oystein (OOO til 10th of July) 2015/05/28 17:00:46 Can this ever really be NULL? Feels like it should
shatch 2015/05/28 19:17:15 Done.
+ return NULL;
+
+ std::string mode;
+ if (!dict->GetString(kConfigModeKey, &mode))
+ return NULL;
+
+ scoped_ptr<BackgroundTracingConfig> config;
+
+ if (mode == kConfigModePreemptive) {
+ config = BackgroundTracingPreemptiveConfig_FromDict(dict);
+ } else if (mode == kConfigModeReactive) {
+ config = BackgroundTracingReactiveConfig_FromDict(dict);
+ } else {
+ return NULL;
+ }
+
+ return config.Pass();
+}
+
+void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config,
+ base::DictionaryValue* dict) {
+ switch (config->mode) {
oystein (OOO til 10th of July) 2015/05/28 17:00:46 Switching on this and then static_casting() makes
shatch 2015/05/28 19:17:15 Chatted offline, maybe we can clean this up with a
+ case PREEMPTIVE_TRACING_MODE:
+ dict->SetString(kConfigModeKey, kConfigModePreemptive);
+ if (!BackgroundTracingPreemptiveConfig_IntoDict(
+ static_cast<const BackgroundTracingPreemptiveConfig*>(config),
+ dict))
+ dict->Clear();
+ break;
+ case REACTIVE_TRACING_MODE:
+ dict->SetString(kConfigModeKey, kConfigModeReactive);
+ if (!BackgroundTracingReactiveConfig_IntoDict(
+ static_cast<const BackgroundTracingReactiveConfig*>(config),
+ dict))
+ dict->Clear();
+ break;
+ }
+}
+
+} // namspace content
« no previous file with comments | « no previous file | content/browser/tracing/background_tracing_config_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698