Don't preload rarely seen large images
[chromium-blink-merge.git] / components / feedback / tracing_manager.cc
blob04bf1b1a308d3ca1cef9a4246ea389d4be0525f8
1 // Copyright 2014 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.
5 #include "components/feedback/tracing_manager.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "components/feedback/feedback_util.h"
13 #include "content/public/browser/tracing_controller.h"
15 namespace {
17 // Only once trace manager can exist at a time.
18 TracingManager* g_tracing_manager = NULL;
19 // Trace IDs start at 1 and increase.
20 int g_next_trace_id = 1;
21 // Name of the file to store the tracing data as.
22 const base::FilePath::CharType kTracingFilename[] =
23 FILE_PATH_LITERAL("tracing.json");
26 TracingManager::TracingManager()
27 : current_trace_id_(0),
28 weak_ptr_factory_(this) {
29 DCHECK(!g_tracing_manager);
30 g_tracing_manager = this;
31 StartTracing();
34 TracingManager::~TracingManager() {
35 DCHECK(g_tracing_manager == this);
36 g_tracing_manager = NULL;
39 int TracingManager::RequestTrace() {
40 // Return the current trace if one is being collected.
41 if (current_trace_id_)
42 return current_trace_id_;
44 current_trace_id_ = g_next_trace_id;
45 ++g_next_trace_id;
46 content::TracingController::GetInstance()->DisableRecording(
47 content::TracingController::CreateStringSink(
48 base::Bind(&TracingManager::OnTraceDataCollected,
49 weak_ptr_factory_.GetWeakPtr())));
50 return current_trace_id_;
53 bool TracingManager::GetTraceData(int id, const TraceDataCallback& callback) {
54 // If a trace is being collected currently, send it via callback when
55 // complete.
56 if (current_trace_id_) {
57 // Only allow one trace data request at a time.
58 if (trace_callback_.is_null()) {
59 trace_callback_ = callback;
60 return true;
61 } else {
62 return false;
64 } else {
65 std::map<int, scoped_refptr<base::RefCountedString> >::iterator data =
66 trace_data_.find(id);
67 if (data == trace_data_.end())
68 return false;
70 // Always return the data asychronously, so the behavior is consistant.
71 base::ThreadTaskRunnerHandle::Get()->PostTask(
72 FROM_HERE, base::Bind(callback, data->second));
73 return true;
77 void TracingManager::DiscardTraceData(int id) {
78 trace_data_.erase(id);
80 // If the trace is discarded before it is complete, clean up the accumulators.
81 if (id == current_trace_id_) {
82 current_trace_id_ = 0;
84 // If the trace has already been requested, provide an empty string.
85 if (!trace_callback_.is_null()) {
86 trace_callback_.Run(scoped_refptr<base::RefCountedString>());
87 trace_callback_.Reset();
92 void TracingManager::StartTracing() {
93 content::TracingController::GetInstance()->EnableRecording(
94 base::trace_event::TraceConfig(),
95 content::TracingController::EnableRecordingDoneCallback());
98 void TracingManager::OnTraceDataCollected(base::RefCountedString* trace_data) {
99 if (!current_trace_id_)
100 return;
102 std::string output_val;
103 feedback_util::ZipString(
104 base::FilePath(kTracingFilename), trace_data->data(), &output_val);
106 scoped_refptr<base::RefCountedString> output(
107 base::RefCountedString::TakeString(&output_val));
109 trace_data_[current_trace_id_] = output;
111 if (!trace_callback_.is_null()) {
112 trace_callback_.Run(output);
113 trace_callback_.Reset();
116 current_trace_id_ = 0;
118 // Tracing has to be restarted asynchronous, so the TracingController can
119 // clean up.
120 base::ThreadTaskRunnerHandle::Get()->PostTask(
121 FROM_HERE, base::Bind(&TracingManager::StartTracing,
122 weak_ptr_factory_.GetWeakPtr()));
125 // static
126 scoped_ptr<TracingManager> TracingManager::Create() {
127 if (g_tracing_manager)
128 return scoped_ptr<TracingManager>();
129 return scoped_ptr<TracingManager>(new TracingManager());
132 TracingManager* TracingManager::Get() {
133 return g_tracing_manager;