Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "content/browser/tracing/background_tracing_manager.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted_memory.h" | |
| 8 #include "base/memory/singleton.h" | |
| 9 #include "base/strings/string_tokenizer.h" | |
| 10 #include "content/browser/tracing/background_tracing_endpoint.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "content/public/browser/tracing_controller.h" | |
| 13 | |
| 14 BackgroundTracingManager::BackgroundTracingManager() | |
| 15 : is_gathering_(false), is_recording_(false), weak_ptr_factory_(this) { | |
| 16 } | |
| 17 | |
| 18 BackgroundTracingManager::~BackgroundTracingManager() { | |
| 19 } | |
| 20 | |
| 21 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
| |
| 22 base::StringTokenizer tokenizer(contents, "|"); | |
| 23 if (!tokenizer.GetNext()) | |
| 24 return ""; | |
| 25 return tokenizer.token(); | |
| 26 } | |
| 27 | |
| 28 std::string BackgroundTracingManager::ParseOutScenarioContents(const std::string & contents) const { | |
| 29 base::StringTokenizer tokenizer(contents, "|"); | |
| 30 if (!tokenizer.GetNext()) | |
| 31 return ""; | |
| 32 if (!tokenizer.GetNext()) | |
| 33 return ""; | |
| 34 return tokenizer.token(); | |
| 35 } | |
| 36 | |
| 37 BackgroundTracingScenario* BackgroundTracingManager::CreateScenarioFromString( | |
| 38 const std::string& contents, | |
| 39 scoped_refptr<BackgroundTracingEndpoint> endpoint) const { | |
| 40 printf("BackgroundTracingManager::CreateScenarioFromString\n"); | |
| 41 base::StringTokenizer tokenizer(contents, ":"); | |
| 42 if (!tokenizer.GetNext()) | |
| 43 return NULL; | |
| 44 printf(" -> Tokenizing :\n"); | |
| 45 std::string class_name = tokenizer.token(); | |
| 46 std::string serialized_contents; | |
| 47 if (!tokenizer.GetNext()) | |
| 48 serialized_contents = tokenizer.token(); | |
| 49 | |
| 50 printf(" -> Finding: %s\n", class_name.c_str()); | |
| 51 if (scenario_factories_.find(class_name) == scenario_factories_.end()) | |
| 52 return NULL; | |
| 53 | |
| 54 printf(" -> Creating.\n"); | |
| 55 return scenario_factories_.find(class_name)->second->CreateFromString( | |
| 56 serialized_contents, endpoint); | |
| 57 } | |
| 58 | |
| 59 BackgroundTracingEndpoint* BackgroundTracingManager::CreateEndpointFromString( | |
| 60 const std::string& contents) const { | |
| 61 printf("BackgroundTracingManager::CreateEndpointFromString\n"); | |
| 62 base::StringTokenizer tokenizer(contents, ":"); | |
| 63 if (!tokenizer.GetNext()) | |
| 64 return NULL; | |
| 65 printf(" -> Tokenizing :\n"); | |
| 66 std::string class_name = tokenizer.token(); | |
| 67 std::string serialized_contents; | |
| 68 if (!tokenizer.GetNext()) | |
| 69 serialized_contents = tokenizer.token(); | |
| 70 | |
| 71 printf(" -> Finding: %s\n", class_name.c_str()); | |
| 72 if (endpoint_factories_.find(class_name) == endpoint_factories_.end()) | |
| 73 return NULL; | |
| 74 | |
| 75 printf(" -> Creating.\n"); | |
| 76 return endpoint_factories_.find(class_name)->second->CreateFromString( | |
| 77 serialized_contents); | |
| 78 } | |
| 79 | |
| 80 void BackgroundTracingManager::RegisterFactory( | |
| 81 BackgroundTracingManagerScenarioFactory* f) { | |
| 82 scenario_factories_[f->GetClassName()] = f; | |
| 83 } | |
| 84 | |
| 85 void BackgroundTracingManager::RegisterFactory( | |
| 86 BackgroundTracingManagerEndpointFactory* f) { | |
| 87 endpoint_factories_[f->GetClassName()] = f; | |
| 88 } | |
| 89 | |
| 90 scoped_refptr<BackgroundTracingScenario> | |
| 91 BackgroundTracingManager::CreateFromString(const std::string& contents) const { | |
| 92 std::string scenario_contents = ParseOutScenarioContents(contents); | |
| 93 std::string endpoint_contents = ParseOutEndpointContents(contents); | |
| 94 scoped_refptr<BackgroundTracingEndpoint> endpoint = CreateEndpointFromString( | |
| 95 endpoint_contents); | |
| 96 return CreateScenarioFromString(scenario_contents, endpoint); | |
| 97 } | |
| 98 | |
| 99 void BackgroundTracingManager::SetActiveScenario( | |
| 100 scoped_refptr<BackgroundTracingScenario> scenario) { | |
| 101 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 102 CHECK(!active_scenario_); | |
| 103 active_scenario_ = scenario; | |
| 104 | |
| 105 printf("activating: %s\n", scenario->ToStringWithEndpoint().c_str()); | |
| 106 | |
| 107 EnableRecording(); | |
| 108 } | |
| 109 | |
| 110 void BackgroundTracingManager::EnableRecording() { | |
| 111 is_recording_ = true; | |
| 112 content::TracingController::GetInstance()->EnableRecording( | |
| 113 active_scenario_->GetCategoryFilter(), | |
| 114 base::trace_event::TraceOptions(base::trace_event::RECORD_CONTINUOUSLY), | |
| 115 content::TracingController::EnableRecordingDoneCallback()); | |
| 116 } | |
| 117 | |
| 118 void BackgroundTracingManager::OnFinalizeComplete() { | |
| 119 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | |
| 120 content::BrowserThread::PostTask( | |
| 121 content::BrowserThread::UI, FROM_HERE, | |
| 122 base::Bind(&BackgroundTracingManager::OnFinalizeComplete, | |
| 123 weak_ptr_factory_.GetWeakPtr())); | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 128 | |
| 129 is_gathering_ = false; | |
| 130 | |
| 131 EnableRecording(); | |
| 132 } | |
| 133 | |
| 134 void BackgroundTracingManager::BeginFinalizingBackgroundTracing() { | |
| 135 active_scenario_->GetEndpoint()->SetDoneCallback( | |
| 136 base::Bind(&BackgroundTracingManager::OnFinalizeComplete, | |
| 137 weak_ptr_factory_.GetWeakPtr())); | |
| 138 content::TracingController::GetInstance()->DisableRecording( | |
| 139 content::TracingController::CreateCompressedStringSink( | |
| 140 active_scenario_->GetEndpoint())); | |
| 141 } | |
| 142 | |
| 143 void BackgroundTracingManager::TryFinalizingBackgroundTracing( | |
| 144 StartedFinalizingCallback callback) { | |
| 145 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | |
| 146 content::BrowserThread::PostTask( | |
| 147 content::BrowserThread::UI, FROM_HERE, | |
| 148 base::Bind(&BackgroundTracingManager::TryFinalizingBackgroundTracing, | |
| 149 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 if (!active_scenario_ || is_gathering_) { | |
| 154 if (!callback.is_null()) | |
| 155 callback.Run(false); | |
| 156 return; | |
| 157 } | |
| 158 | |
| 159 is_gathering_ = true; | |
| 160 | |
| 161 BeginFinalizingBackgroundTracing(); | |
| 162 | |
| 163 if (!callback.is_null()) | |
| 164 callback.Run(true); | |
| 165 } | |
| 166 | |
| 167 BackgroundTracingManager* BackgroundTracingManager::GetInstance() { | |
| 168 return Singleton<BackgroundTracingManager>::get(); | |
| 169 } | |
| OLD | NEW |