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..89f929af2ec7f93aa6c23905d46b02054a9b28db |
--- /dev/null |
+++ b/content/browser/tracing/background_tracing_manager.cc |
@@ -0,0 +1,169 @@ |
+// 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.h" |
+ |
+#include "base/memory/ref_counted_memory.h" |
+#include "base/memory/singleton.h" |
+#include "base/strings/string_tokenizer.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() |
+ : is_gathering_(false), is_recording_(false), weak_ptr_factory_(this) { |
+} |
+ |
+BackgroundTracingManager::~BackgroundTracingManager() { |
+} |
+ |
+std::string BackgroundTracingManager::ParseOutEndpointContents(const std::string& contents) const { |
shatch
2015/04/28 17:00:23
Thinking about refactoring all of this out into so
|
+ base::StringTokenizer tokenizer(contents, "|"); |
+ if (!tokenizer.GetNext()) |
+ return ""; |
+ return tokenizer.token(); |
+} |
+ |
+std::string BackgroundTracingManager::ParseOutScenarioContents(const std::string& contents) const { |
+ base::StringTokenizer tokenizer(contents, "|"); |
+ if (!tokenizer.GetNext()) |
+ return ""; |
+ if (!tokenizer.GetNext()) |
+ return ""; |
+ return tokenizer.token(); |
+} |
+ |
+BackgroundTracingScenario* BackgroundTracingManager::CreateScenarioFromString( |
+ const std::string& contents, |
+ scoped_refptr<BackgroundTracingEndpoint> endpoint) const { |
+ printf("BackgroundTracingManager::CreateScenarioFromString\n"); |
+ base::StringTokenizer tokenizer(contents, ":"); |
+ if (!tokenizer.GetNext()) |
+ return NULL; |
+ printf(" -> Tokenizing :\n"); |
+ std::string class_name = tokenizer.token(); |
+ std::string serialized_contents; |
+ if (!tokenizer.GetNext()) |
+ serialized_contents = tokenizer.token(); |
+ |
+ printf(" -> Finding: %s\n", class_name.c_str()); |
+ if (scenario_factories_.find(class_name) == scenario_factories_.end()) |
+ return NULL; |
+ |
+ printf(" -> Creating.\n"); |
+ return scenario_factories_.find(class_name)->second->CreateFromString( |
+ serialized_contents, endpoint); |
+} |
+ |
+BackgroundTracingEndpoint* BackgroundTracingManager::CreateEndpointFromString( |
+ const std::string& contents) const { |
+ printf("BackgroundTracingManager::CreateEndpointFromString\n"); |
+ base::StringTokenizer tokenizer(contents, ":"); |
+ if (!tokenizer.GetNext()) |
+ return NULL; |
+ printf(" -> Tokenizing :\n"); |
+ std::string class_name = tokenizer.token(); |
+ std::string serialized_contents; |
+ if (!tokenizer.GetNext()) |
+ serialized_contents = tokenizer.token(); |
+ |
+ printf(" -> Finding: %s\n", class_name.c_str()); |
+ if (endpoint_factories_.find(class_name) == endpoint_factories_.end()) |
+ return NULL; |
+ |
+ printf(" -> Creating.\n"); |
+ return endpoint_factories_.find(class_name)->second->CreateFromString( |
+ serialized_contents); |
+} |
+ |
+void BackgroundTracingManager::RegisterFactory( |
+ BackgroundTracingManagerScenarioFactory* f) { |
+ scenario_factories_[f->GetClassName()] = f; |
+} |
+ |
+void BackgroundTracingManager::RegisterFactory( |
+ BackgroundTracingManagerEndpointFactory* f) { |
+ endpoint_factories_[f->GetClassName()] = f; |
+} |
+ |
+scoped_refptr<BackgroundTracingScenario> |
+BackgroundTracingManager::CreateFromString(const std::string& contents) const { |
+ std::string scenario_contents = ParseOutScenarioContents(contents); |
+ std::string endpoint_contents = ParseOutEndpointContents(contents); |
+ scoped_refptr<BackgroundTracingEndpoint> endpoint = CreateEndpointFromString( |
+ endpoint_contents); |
+ return CreateScenarioFromString(scenario_contents, endpoint); |
+} |
+ |
+void BackgroundTracingManager::SetActiveScenario( |
+ scoped_refptr<BackgroundTracingScenario> scenario) { |
+ CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ CHECK(!active_scenario_); |
+ active_scenario_ = scenario; |
+ |
+ printf("activating: %s\n", scenario->ToStringWithEndpoint().c_str()); |
+ |
+ 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(); |
+} |