Skip to content

Commit

Permalink
TracingController: Simplify public API
Browse files Browse the repository at this point in the history
Cleaned up the TraceController public API a little bit by removing
the metadata DictionaryValue which is now inlined in the JSON rather
than treated as separate data, and hold the tracing data in
a std::unique_ptr<std::string> rather than a RefCountedString
as the data only has one owner at any time.

Split out from and already LGTM'd (the tracing bits) in
https://chromium-review.googlesource.com/c/chromium/src/+/1817207/28

TBR=ssid@chromium.org,zork@chromium.org,erikchen@chromium.org,boliu@chromium.org

Bug: 839086
Change-Id: I84a717e7c6915a81348d7e91fbd6fe2ba00a4d46
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1834576
Commit-Queue: oysteine <oysteine@chromium.org>
Reviewed-by: oysteine <oysteine@chromium.org>
Reviewed-by: ssid <ssid@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702185}
  • Loading branch information
Oystein Eftevaag authored and Commit Bot committed Oct 2, 2019
1 parent d31a632 commit 2386e05
Show file tree
Hide file tree
Showing 29 changed files with 205 additions and 357 deletions.
19 changes: 7 additions & 12 deletions android_webview/browser/tracing/aw_tracing_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,17 @@ class AwTraceDataEndpoint
public:
using ReceivedChunkCallback =
base::RepeatingCallback<void(std::unique_ptr<std::string>)>;
using CompletedCallback =
base::OnceCallback<void(std::unique_ptr<const base::DictionaryValue>)>;

static scoped_refptr<content::TracingController::TraceDataEndpoint> Create(
ReceivedChunkCallback received_chunk_callback,
CompletedCallback completed_callback) {
base::OnceClosure completed_callback) {
return new AwTraceDataEndpoint(std::move(received_chunk_callback),
std::move(completed_callback));
}

void ReceiveTraceFinalContents(
std::unique_ptr<const base::DictionaryValue> metadata) override {
base::PostTask(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(std::move(completed_callback_), std::move(metadata)));
void ReceivedTraceFinalContents() override {
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(std::move(completed_callback_)));
}

void ReceiveTraceChunk(std::unique_ptr<std::string> chunk) override {
Expand All @@ -56,15 +52,15 @@ class AwTraceDataEndpoint
}

explicit AwTraceDataEndpoint(ReceivedChunkCallback received_chunk_callback,
CompletedCallback completed_callback)
base::OnceClosure completed_callback)
: received_chunk_callback_(std::move(received_chunk_callback)),
completed_callback_(std::move(completed_callback)) {}

private:
~AwTraceDataEndpoint() override {}

ReceivedChunkCallback received_chunk_callback_;
CompletedCallback completed_callback_;
base::OnceClosure completed_callback_;

DISALLOW_COPY_AND_ASSIGN(AwTraceDataEndpoint);
};
Expand Down Expand Up @@ -108,8 +104,7 @@ bool AwTracingController::StopAndFlush(JNIEnv* env,
weak_factory_.GetWeakPtr())));
}

void AwTracingController::OnTraceDataComplete(
std::unique_ptr<const base::DictionaryValue> metadata) {
void AwTracingController::OnTraceDataComplete() {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env);
if (obj.obj()) {
Expand Down
4 changes: 1 addition & 3 deletions android_webview/browser/tracing/aw_tracing_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "base/android/jni_weak_ref.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"

namespace android_webview {

Expand All @@ -28,8 +27,7 @@ class AwTracingController {
~AwTracingController();

void OnTraceDataReceived(std::unique_ptr<std::string> chunk);
void OnTraceDataComplete(
std::unique_ptr<const base::DictionaryValue> metadata);
void OnTraceDataComplete();

JavaObjectWeakGlobalRef weak_java_object_;
base::WeakPtrFactory<AwTracingController> weak_factory_{this};
Expand Down
6 changes: 4 additions & 2 deletions chrome/browser/tracing/background_tracing_field_trial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "content/public/browser/browser_thread.h"
#include "net/base/network_change_notifier.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/tracing/public/cpp/perfetto/trace_event_data_source.h"
#include "url/gurl.h"

namespace tracing {
Expand All @@ -48,8 +49,7 @@ void OnBackgroundTracingUploadComplete(

void BackgroundTracingUploadCallback(
const std::string& upload_url,
const scoped_refptr<base::RefCountedString>& file_contents,
std::unique_ptr<const base::DictionaryValue> metadata,
std::unique_ptr<std::string> file_contents,
content::BackgroundTracingManager::FinishedProcessingCallback callback) {
TraceCrashServiceUploader* uploader = new TraceCrashServiceUploader(
g_browser_process->shared_url_loader_factory());
Expand All @@ -66,6 +66,8 @@ void BackgroundTracingUploadCallback(
uploader->SetMaxUploadBytes(100 * 1024);
}
#endif
std::unique_ptr<base::DictionaryValue> metadata =
TraceEventMetadataSource::GetInstance()->GenerateLegacyMetadataDict();

uploader->DoUpload(
file_contents->data(), content::TraceUploader::UNCOMPRESSED_UPLOAD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class BackgroundTracingMetricsProviderTest : public testing::Test {
ASSERT_TRUE(
content::BackgroundTracingManager::GetInstance()->SetActiveScenario(
std::move(config),
base::BindRepeating([](const scoped_refptr<base::RefCountedString>&,
std::unique_ptr<const base::DictionaryValue>,
base::BindRepeating([](std::unique_ptr<std::string>,
content::BackgroundTracingManager::
FinishedProcessingCallback) {}),
content::BackgroundTracingManager::ANONYMIZE_DATA));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ class ChromeTracingDelegateBrowserTest : public InProcessBrowserTest {
}

private:
void OnUpload(const scoped_refptr<base::RefCountedString>& file_contents,
std::unique_ptr<const base::DictionaryValue> metadata,
void OnUpload(std::unique_ptr<std::string> file_contents,
content::BackgroundTracingManager::FinishedProcessingCallback
done_callback) {
receive_count_ += 1;
Expand Down
9 changes: 6 additions & 3 deletions chrome/browser/tracing/navigation_tracing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/tracing/public/cpp/perfetto/trace_event_data_source.h"

using content::RenderFrameHost;

Expand All @@ -37,12 +38,14 @@ void OnNavigationTracingUploadComplete(
}

void NavigationUploadCallback(
const scoped_refptr<base::RefCountedString>& file_contents,
std::unique_ptr<const base::DictionaryValue> metadata,
std::unique_ptr<std::string> file_contents,
content::BackgroundTracingManager::FinishedProcessingCallback callback) {
TraceCrashServiceUploader* uploader = new TraceCrashServiceUploader(
g_browser_process->shared_url_loader_factory());

std::unique_ptr<base::DictionaryValue> metadata =
TraceEventMetadataSource::GetInstance()->GenerateLegacyMetadataDict();

uploader->DoUpload(
file_contents->data(), content::TraceUploader::UNCOMPRESSED_UPLOAD,
std::move(metadata), content::TraceUploader::UploadProgressCallback(),
Expand Down Expand Up @@ -93,7 +96,7 @@ void SetupNavigationTracing() {
DCHECK(config);

content::BackgroundTracingManager::GetInstance()->SetActiveScenario(
std::move(config), base::Bind(&NavigationUploadCallback),
std::move(config), base::BindRepeating(&NavigationUploadCallback),
content::BackgroundTracingManager::NO_DATA_FILTERING);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ void ArcGraphicsTracingHandler::StopTracing() {
return;

controller->StopTracing(content::TracingController::CreateStringEndpoint(
base::BindRepeating(&ArcGraphicsTracingHandler::OnTracingStopped,
weak_ptr_factory_.GetWeakPtr())));
base::BindOnce(&ArcGraphicsTracingHandler::OnTracingStopped,
weak_ptr_factory_.GetWeakPtr())));
}

void ArcGraphicsTracingHandler::SetStatus(const std::string& status) {
Expand All @@ -459,10 +459,9 @@ void ArcGraphicsTracingHandler::OnTracingStarted() {
}

void ArcGraphicsTracingHandler::OnTracingStopped(
std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString* trace_data) {
std::unique_ptr<std::string> trace_data) {
std::string string_data;
string_data.swap(trace_data->data());
string_data.swap(*trace_data);
base::PostTaskAndReplyWithResult(
FROM_HERE,
{base::ThreadPool(), base::MayBlock(), base::TaskPriority::BEST_EFFORT},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ArcSystemStatCollector;

namespace base {
class ListValue;
class RefCountedString;
} // namespace base

namespace exo {
Expand Down Expand Up @@ -71,8 +70,7 @@ class ArcGraphicsTracingHandler : public content::WebUIMessageHandler,
void SetStatus(const std::string& status);

void OnTracingStarted();
void OnTracingStopped(std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString* trace_data);
void OnTracingStopped(std::unique_ptr<std::string> trace_data);

// Called when graphics model is built or load. Extra string parameter
// contains a status. In case model cannot be built/load empty |base::Value|
Expand Down
3 changes: 1 addition & 2 deletions chrome/test/base/tracing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class StringTraceEndpoint
*result_ += *chunk;
}

void ReceiveTraceFinalContents(
std::unique_ptr<const base::DictionaryValue> metadata) override {
void ReceivedTraceFinalContents() override {
if (!result_->empty())
*result_ += "]";
completion_callback_.Run();
Expand Down
11 changes: 5 additions & 6 deletions components/feedback/tracing_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ int TracingManager::RequestTrace() {
++g_next_trace_id;
content::TracingController::GetInstance()->StopTracing(
content::TracingController::CreateStringEndpoint(
base::Bind(&TracingManager::OnTraceDataCollected,
weak_ptr_factory_.GetWeakPtr())));
base::BindOnce(&TracingManager::OnTraceDataCollected,
weak_ptr_factory_.GetWeakPtr())));
return current_trace_id_;
}

Expand Down Expand Up @@ -93,14 +93,13 @@ void TracingManager::StartTracing() {
}

void TracingManager::OnTraceDataCollected(
std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString* trace_data) {
std::unique_ptr<std::string> trace_data) {
if (!current_trace_id_)
return;

std::string output_val;
feedback_util::ZipString(
base::FilePath(kTracingFilename), trace_data->data(), &output_val);
feedback_util::ZipString(base::FilePath(kTracingFilename), *trace_data,
&output_val);

scoped_refptr<base::RefCountedString> output(
base::RefCountedString::TakeString(&output_val));
Expand Down
4 changes: 1 addition & 3 deletions components/feedback/tracing_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ class TracingManager {
TracingManager();

void StartTracing();
void OnTraceDataCollected(
std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString* data);
void OnTraceDataCollected(std::unique_ptr<std::string> data);

// ID of the trace that is being collected.
int current_trace_id_;
Expand Down
7 changes: 3 additions & 4 deletions components/heap_profiling/supervisor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,11 @@ void Supervisor::RequestTraceWithHeapDump(TraceFinishedCallback callback,
auto finished_dump_callback = base::BindOnce(
[](TraceFinishedCallback callback, bool success, uint64_t dump_guid) {
// Once the trace has stopped, run |callback| on the UI thread.
auto finish_sink_callback = base::Bind(
auto finish_sink_callback = base::BindOnce(
[](TraceFinishedCallback callback,
std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString* in) {
std::unique_ptr<std::string> in) {
std::string result;
result.swap(in->data());
result.swap(*in);
base::CreateSingleThreadTaskRunner({content::BrowserThread::UI})
->PostTask(FROM_HERE,
base::BindOnce(std::move(callback), true,
Expand Down
20 changes: 9 additions & 11 deletions content/browser/devtools/protocol/tracing_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ class DevToolsTraceEndpointProxy : public TracingController::TraceDataEndpoint {
if (TracingHandler* h = tracing_handler_.get())
h->OnTraceDataCollected(std::move(chunk));
}
void ReceiveTraceFinalContents(
std::unique_ptr<const base::DictionaryValue> metadata) override {
void ReceivedTraceFinalContents() override {
if (TracingHandler* h = tracing_handler_.get())
h->OnTraceComplete();
}
Expand Down Expand Up @@ -152,13 +151,12 @@ class DevToolsStreamEndpoint : public TracingController::TraceDataEndpoint {
stream_->Append(std::move(chunk));
}

void ReceiveTraceFinalContents(
std::unique_ptr<const base::DictionaryValue> metadata) override {
void ReceivedTraceFinalContents() override {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
base::PostTask(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&DevToolsStreamEndpoint::ReceiveTraceFinalContents,
this, std::move(metadata)));
base::BindOnce(&DevToolsStreamEndpoint::ReceivedTraceFinalContents,
this));
return;
}
if (TracingHandler* h = tracing_handler_.get())
Expand Down Expand Up @@ -324,7 +322,7 @@ class TracingHandler::PerfettoTracingSession
if (!tracing_session_host_) {
if (endpoint_) {
// Will delete |this|.
endpoint_->ReceiveTraceFinalContents(nullptr);
endpoint_->ReceivedTraceFinalContents();
}
return;
}
Expand Down Expand Up @@ -445,7 +443,7 @@ class TracingHandler::PerfettoTracingSession
if (endpoint_) {
// TODO(oysteine): Signal to the client that tracing failed.
// Will delete |this|.
endpoint_->ReceiveTraceFinalContents(nullptr);
endpoint_->ReceivedTraceFinalContents();
}
}

Expand Down Expand Up @@ -492,7 +490,7 @@ class TracingHandler::PerfettoTracingSession
if (!endpoint_)
return;
// Will delete |this|.
endpoint_->ReceiveTraceFinalContents(nullptr);
endpoint_->ReceivedTraceFinalContents();
}

mojo::Binding<tracing::mojom::TracingSessionClient> binding_{this};
Expand All @@ -517,11 +515,11 @@ class TracingHandler::PerfettoTracingSession
#endif
};

TracingHandler::TracingHandler(FrameTreeNode* frame_tree_node_,
TracingHandler::TracingHandler(FrameTreeNode* frame_tree_node,
DevToolsIOContext* io_context)
: DevToolsDomainHandler(Tracing::Metainfo::domainName),
io_context_(io_context),
frame_tree_node_(frame_tree_node_),
frame_tree_node_(frame_tree_node),
did_initiate_recording_(false),
return_as_stream_(false),
gzip_compression_(false),
Expand Down
10 changes: 4 additions & 6 deletions content/browser/renderer_host/input/mouse_latency_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,9 @@ class MouseLatencyBrowserTest : public ContentBrowserTest {
runner_->Quit();
}

void OnTraceDataCollected(
std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString* trace_data_string) {
void OnTraceDataCollected(std::unique_ptr<std::string> trace_data_string) {
std::unique_ptr<base::Value> trace_data =
base::JSONReader::ReadDeprecated(trace_data_string->data());
base::JSONReader::ReadDeprecated(*trace_data_string);
ASSERT_TRUE(trace_data);
trace_data_ = trace_data->Clone();
runner_->Quit();
Expand Down Expand Up @@ -264,8 +262,8 @@ class MouseLatencyBrowserTest : public ContentBrowserTest {
const base::Value& StopTracing() {
bool success = TracingController::GetInstance()->StopTracing(
TracingController::CreateStringEndpoint(
base::Bind(&MouseLatencyBrowserTest::OnTraceDataCollected,
base::Unretained(this))));
base::BindOnce(&MouseLatencyBrowserTest::OnTraceDataCollected,
base::Unretained(this))));
EXPECT_TRUE(success);

// Runs until we get the OnTraceDataCollected callback, which populates
Expand Down
Loading

0 comments on commit 2386e05

Please sign in to comment.