| Index: content/browser/tracing/background_tracing_manager_impl.cc
|
| diff --git a/content/browser/tracing/background_tracing_manager_impl.cc b/content/browser/tracing/background_tracing_manager_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2c8f267228cd91b8158c5483454cfa8dda3a64ad
|
| --- /dev/null
|
| +++ b/content/browser/tracing/background_tracing_manager_impl.cc
|
| @@ -0,0 +1,294 @@
|
| +// 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 "content/browser/tracing/background_tracing_manager_impl.h"
|
| +
|
| +#include "base/macros.h"
|
| +#include "content/public/browser/background_tracing_preemptive_config.h"
|
| +#include "content/public/browser/background_tracing_reactive_config.h"
|
| +#include "content/public/browser/background_tracing_upload_sink.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +
|
| +namespace content {
|
| +
|
| +namespace {
|
| +
|
| +base::LazyInstance<BackgroundTracingManagerImpl>::Leaky g_controller =
|
| + LAZY_INSTANCE_INITIALIZER;
|
| +
|
| +} // namespace
|
| +
|
| +BackgroundTracingManagerImpl::TraceDataEndpointWrapper::
|
| + TraceDataEndpointWrapper(base::Callback<void()> done_callback)
|
| + : done_callback_(done_callback) {
|
| +}
|
| +
|
| +BackgroundTracingManagerImpl::TraceDataEndpointWrapper::
|
| + ~TraceDataEndpointWrapper() {
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::TraceDataEndpointWrapper::
|
| + ReceiveTraceFinalContents(const std::string& file_contents) {
|
| + upload_sink_->Upload(file_contents, done_callback_);
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::TraceDataEndpointWrapper::SetUploadSink(
|
| + scoped_refptr<content::BackgroundTracingUploadSink> upload_sink) {
|
| + upload_sink_ = upload_sink;
|
| +}
|
| +
|
| +BackgroundTracingManager* BackgroundTracingManager::GetInstance() {
|
| + return BackgroundTracingManagerImpl::GetInstance();
|
| +}
|
| +
|
| +BackgroundTracingManagerImpl* BackgroundTracingManagerImpl::GetInstance() {
|
| + return g_controller.Pointer();
|
| +}
|
| +
|
| +BackgroundTracingManagerImpl::BackgroundTracingManagerImpl()
|
| + : is_gathering_(false), trigger_handle_ids_(0) {
|
| + data_endpoint_wrapper_ = new TraceDataEndpointWrapper(
|
| + base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() {
|
| + NOTREACHED();
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::WhenIdle(
|
| + base::Callback<void()> idle_callback) {
|
| + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| + idle_callback_ = idle_callback;
|
| +}
|
| +
|
| +bool BackgroundTracingManagerImpl::IsSupportedConfig(
|
| + scoped_refptr<BackgroundTracingConfig> config) {
|
| + // No config is just fine, we just don't do anything.
|
| + if (!config)
|
| + return true;
|
| +
|
| + // TODO(simonhatch): Implement reactive tracing path.
|
| + if (config->mode != BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE)
|
| + return false;
|
| +
|
| + // TODO(fmeawad): Implement uma triggers.
|
| + BackgroundTracingPreemptiveConfig* preemptive_config =
|
| + static_cast<BackgroundTracingPreemptiveConfig*>(config.get());
|
| + const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>&
|
| + configs = preemptive_config->configs;
|
| + for (size_t i = 0; i < configs.size(); ++i) {
|
| + if (configs[i].type !=
|
| + BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED)
|
| + return false;
|
| + ;
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool BackgroundTracingManagerImpl::SetActiveScenario(
|
| + scoped_refptr<BackgroundTracingConfig> config,
|
| + scoped_refptr<BackgroundTracingUploadSink> upload_sink) {
|
| + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| + if (!IsSupportedConfig(config))
|
| + return false;
|
| +
|
| + // No point in tracing if there's nowhere to send it.
|
| + if (config && !upload_sink)
|
| + return false;
|
| +
|
| + config_ = config;
|
| +
|
| + data_endpoint_wrapper_->SetUploadSink(upload_sink);
|
| +
|
| + EnableRecordingIfConfigNeedsIt();
|
| +
|
| + return true;
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::EnableRecordingIfConfigNeedsIt() {
|
| + if (!config_)
|
| + return;
|
| +
|
| + if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) {
|
| + EnableRecording(GetCategoryFilterForCategoryPreset(
|
| + static_cast<BackgroundTracingPreemptiveConfig*>(config_.get())
|
| + ->category_preset));
|
| + } else {
|
| + // TODO(simonhatch): Implement reactive tracing path.
|
| + NOTREACHED();
|
| + }
|
| +}
|
| +
|
| +bool BackgroundTracingManagerImpl::IsAbleToTriggerTracing(
|
| + TriggerHandle handle) const {
|
| + if (!config_)
|
| + return false;
|
| +
|
| + // If the last trace is still uploading, we don't allow a new one to trigger.
|
| + if (is_gathering_)
|
| + return false;
|
| +
|
| + if (!IsTriggerHandleValid(handle)) {
|
| + return false;
|
| + }
|
| +
|
| + std::string trigger_name = GetTriggerNameFromHandle(handle);
|
| +
|
| + if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) {
|
| + BackgroundTracingPreemptiveConfig* preemptive_config =
|
| + static_cast<BackgroundTracingPreemptiveConfig*>(config_.get());
|
| +
|
| + const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>&
|
| + configs = preemptive_config->configs;
|
| +
|
| + for (size_t i = 0; i < configs.size(); ++i) {
|
| + if (configs[i].type != BackgroundTracingPreemptiveConfig::
|
| + MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED)
|
| + continue;
|
| +
|
| + if (trigger_name == configs[i].named_trigger_info.trigger_name) {
|
| + return true;
|
| + }
|
| + }
|
| + } else {
|
| + // TODO(simonhatch): Implement reactive path.
|
| + NOTREACHED();
|
| + }
|
| +
|
| + return false;
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::DidTriggerHappen(
|
| + BackgroundTracingManagerImpl::TriggerHandle handle,
|
| + StartedFinalizingCallback callback) {
|
| + if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
| + content::BrowserThread::PostTask(
|
| + content::BrowserThread::UI, FROM_HERE,
|
| + base::Bind(&BackgroundTracingManagerImpl::DidTriggerHappen,
|
| + base::Unretained(this), handle, callback));
|
| + return;
|
| + }
|
| +
|
| + if (!IsAbleToTriggerTracing(handle)) {
|
| + if (!callback.is_null())
|
| + callback.Run(false);
|
| + return;
|
| + }
|
| +
|
| + if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) {
|
| + BeginFinalizing(callback);
|
| + } else {
|
| + // TODO(simonhatch): Implement reactive tracing path.
|
| + NOTREACHED();
|
| + }
|
| +}
|
| +
|
| +BackgroundTracingManagerImpl::TriggerHandle
|
| +BackgroundTracingManagerImpl::RegisterTriggerType(const char* trigger_name) {
|
| + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| +
|
| + trigger_handle_ids_ += 1;
|
| +
|
| + trigger_handles_.insert(
|
| + std::pair<TriggerHandle, std::string>(trigger_handle_ids_, trigger_name));
|
| +
|
| + return static_cast<TriggerHandle>(trigger_handle_ids_);
|
| +}
|
| +
|
| +bool BackgroundTracingManagerImpl::IsTriggerHandleValid(
|
| + BackgroundTracingManager::TriggerHandle handle) const {
|
| + return trigger_handles_.find(handle) != trigger_handles_.end();
|
| +}
|
| +
|
| +std::string BackgroundTracingManagerImpl::GetTriggerNameFromHandle(
|
| + BackgroundTracingManager::TriggerHandle handle) const {
|
| + CHECK(IsTriggerHandleValid(handle));
|
| + return trigger_handles_.find(handle)->second;
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::GetTriggerNameList(
|
| + std::vector<std::string>& trigger_names) {
|
| + for (std::map<TriggerHandle, std::string>::iterator it =
|
| + trigger_handles_.begin();
|
| + it != trigger_handles_.end(); ++it)
|
| + trigger_names.push_back(it->second);
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() {
|
| + trigger_handles_.clear();
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::EnableRecording(
|
| + base::trace_event::CategoryFilter category_filter) {
|
| + TracingController::GetInstance()->EnableRecording(
|
| + category_filter,
|
| + base::trace_event::TraceOptions(base::trace_event::RECORD_CONTINUOUSLY),
|
| + TracingController::EnableRecordingDoneCallback());
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::OnFinalizeComplete() {
|
| + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
|
| + BrowserThread::PostTask(
|
| + BrowserThread::UI, FROM_HERE,
|
| + base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete,
|
| + base::Unretained(this)));
|
| + return;
|
| + }
|
| +
|
| + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| +
|
| + is_gathering_ = false;
|
| +
|
| + if (!idle_callback_.is_null())
|
| + idle_callback_.Run();
|
| +
|
| + // Now that a trace has completed, we may need to enable recording again.
|
| + EnableRecordingIfConfigNeedsIt();
|
| +}
|
| +
|
| +void BackgroundTracingManagerImpl::BeginFinalizing(
|
| + StartedFinalizingCallback callback) {
|
| + is_gathering_ = true;
|
| +
|
| + content::TracingController::GetInstance()->DisableRecording(
|
| + content::TracingController::CreateCompressedStringSink(
|
| + data_endpoint_wrapper_));
|
| +
|
| + if (!callback.is_null())
|
| + callback.Run(true);
|
| +}
|
| +
|
| +base::trace_event::CategoryFilter
|
| +BackgroundTracingManagerImpl::GetCategoryFilterForCategoryPreset(
|
| + BackgroundTracingConfig::CategoryPreset preset) const {
|
| + switch (preset) {
|
| + case BackgroundTracingConfig::CategoryPreset::BENCHMARK:
|
| + return base::trace_event::CategoryFilter(
|
| + "benchmark,"
|
| + "disabled-by-default-toplevel.flow,"
|
| + "disabled-by-default-ipc.flow");
|
| + case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP:
|
| + return base::trace_event::CategoryFilter(
|
| + "*,disabled-by-default-blink.debug.layout");
|
| + }
|
| + NOTREACHED();
|
| + return base::trace_event::CategoryFilter();
|
| +}
|
| +
|
| +BackgroundTracingConfig* BackgroundTracingConfig::FromDict(
|
| + const base::DictionaryValue* dict) {
|
| + // TODO(simonhatch): Implement this.
|
| + CHECK(false);
|
| + return NULL;
|
| +}
|
| +
|
| +void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config,
|
| + base::DictionaryValue* dict) {
|
| + // TODO(simonhatch): Implement this.
|
| + CHECK(false);
|
| +}
|
| +
|
| +} // namspace content
|
|
|