Chromium Code Reviews| Index: content/browser/tracing/background_tracing_manager.cc |
| diff --git a/content/browser/tracing/background_tracing_manager.cc b/content/browser/tracing/background_tracing_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b20997b01249c9ff211a67c2752e8be1b72f9725 |
| --- /dev/null |
| +++ b/content/browser/tracing/background_tracing_manager.cc |
| @@ -0,0 +1,88 @@ |
| +// Copyright (c) 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.h" |
| + |
| +#include "base/memory/ref_counted_memory.h" |
| +#include "base/memory/singleton.h" |
| +#include "content/browser/tracing/background_tracing_endpoint.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/tracing_controller.h" |
| + |
| +BackgroundTracingManager::BackgroundTracingManager() : weak_ptr_factory_(this) { |
|
oystein (OOO til 10th of July)
2015/04/21 17:31:44
is_recording_ isn't being initialized looks like?
|
| + active_scenario_ = NULL; |
| + is_gathering_ = false; |
| +} |
| + |
| +BackgroundTracingManager::~BackgroundTracingManager() { |
| +} |
| + |
| +void BackgroundTracingManager::SetActiveScenario( |
| + scoped_refptr<BackgroundTracingScenario> scenario) { |
| + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + CHECK(!active_scenario_); |
| + active_scenario_ = scenario; |
| + |
| + EnableRecording(); |
| +} |
| + |
| +void BackgroundTracingManager::EnableRecording() { |
| + is_recording_ = true; |
| + content::TracingController::GetInstance()->EnableRecording( |
| + active_scenario_->GetCategoryFilter(), |
| + base::trace_event::TraceOptions(base::trace_event::RECORD_CONTINUOUSLY), |
| + content::TracingController::EnableRecordingDoneCallback()); |
| +} |
| + |
| +void BackgroundTracingManager::OnFinalizeComplete() { |
| + if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&BackgroundTracingManager::OnFinalizeComplete, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + return; |
| + } |
| + |
| + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + |
| + is_gathering_ = false; |
| + |
| + EnableRecording(); |
| +} |
| + |
| +void BackgroundTracingManager::BeginFinalizingBackgroundTracing() { |
| + active_scenario_->GetEndpoint()->SetDoneCallback( |
| + base::Bind(&BackgroundTracingManager::OnFinalizeComplete, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + content::TracingController::GetInstance()->DisableRecording( |
| + content::TracingController::CreateCompressedStringSink( |
| + active_scenario_->GetEndpoint())); |
| +} |
| + |
| +void BackgroundTracingManager::TryFinalizingBackgroundTracing( |
| + StartedFinalizingCallback callback) { |
| + if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&BackgroundTracingManager::TryFinalizingBackgroundTracing, |
| + weak_ptr_factory_.GetWeakPtr(), callback)); |
| + return; |
| + } |
| + |
| + if (!active_scenario_ || is_gathering_) { |
| + if (!callback.is_null()) |
| + callback.Run(false); |
| + return; |
| + } |
| + |
| + is_gathering_ = true; |
| + |
| + BeginFinalizingBackgroundTracing(); |
| + |
| + if (!callback.is_null()) |
| + callback.Run(true); |
| +} |
| + |
| +BackgroundTracingManager* BackgroundTracingManager::GetInstance() { |
| + return Singleton<BackgroundTracingManager>::get(); |
| +} |