IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / tracing / tracing_ui.cc
blob6b541f8c1a4ed9060111466079e6700b39017095
1 // Copyright (c) 2012 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 "content/browser/tracing/tracing_ui.h"
7 #include <string>
9 #include "base/base64.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/file_util.h"
13 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/values.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/tracing_controller.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_ui.h"
23 #include "content/public/browser/web_ui_data_source.h"
24 #include "content/public/common/url_constants.h"
25 #include "grit/tracing_resources.h"
27 namespace content {
28 namespace {
30 void OnGotCategories(const WebUIDataSource::GotDataCallback& callback,
31 const std::set<std::string>& categorySet) {
33 scoped_ptr<base::ListValue> category_list(new base::ListValue());
34 for (std::set<std::string>::const_iterator it = categorySet.begin();
35 it != categorySet.end(); it++) {
36 category_list->AppendString(*it);
39 base::RefCountedString* res = new base::RefCountedString();
40 base::JSONWriter::Write(category_list.get(), &res->data());
41 callback.Run(res);
44 bool GetTracingOptions(const std::string& data64,
45 std::string* category_filter_string,
46 int* tracing_options)
48 std::string data;
49 if (!base::Base64Decode(data64, &data)) {
50 LOG(ERROR) << "Options were not base64 encoded.";
51 return false;
54 scoped_ptr<base::Value> optionsRaw(base::JSONReader::Read(data));
55 if (!optionsRaw) {
56 LOG(ERROR) << "Options were not valid JSON";
57 return false;
59 base::DictionaryValue* options;
60 if (!optionsRaw->GetAsDictionary(&options)) {
61 LOG(ERROR) << "Options must be dict";
62 return false;
65 bool use_system_tracing;
66 bool use_continuous_tracing;
67 bool use_sampling;
69 bool options_ok = true;
70 options_ok &= options->GetString("categoryFilter", category_filter_string);
71 options_ok &= options->GetBoolean("useSystemTracing", &use_system_tracing);
72 options_ok &= options->GetBoolean("useContinuousTracing",
73 &use_continuous_tracing);
74 options_ok &= options->GetBoolean("useSampling", &use_sampling);
75 if (!options_ok) {
76 LOG(ERROR) << "Malformed options";
77 return false;
80 *tracing_options = 0;
81 if (use_system_tracing)
82 *tracing_options |= TracingController::ENABLE_SYSTRACE;
83 if (use_sampling)
84 *tracing_options |= TracingController::ENABLE_SAMPLING;
85 if (use_continuous_tracing)
86 *tracing_options |= TracingController::RECORD_CONTINUOUSLY;
87 return true;
90 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback);
92 bool OnBeginRecording(const std::string& data64,
93 const WebUIDataSource::GotDataCallback& callback) {
94 std::string category_filter_string;
95 int tracing_options = 0;
96 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options))
97 return false;
99 return TracingController::GetInstance()->EnableRecording(
100 category_filter_string,
101 static_cast<TracingController::Options>(tracing_options),
102 base::Bind(&OnRecordingEnabledAck, callback));
105 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback) {
106 base::RefCountedString* res = new base::RefCountedString();
107 callback.Run(res);
110 void OnTraceBufferPercentFullResult(
111 const WebUIDataSource::GotDataCallback& callback, float result) {
112 std::string str = base::DoubleToString(result);
113 callback.Run(base::RefCountedString::TakeString(&str));
116 void ReadRecordingResult(const WebUIDataSource::GotDataCallback& callback,
117 const base::FilePath& path) {
118 std::string tmp;
119 if (!base::ReadFileToString(path, &tmp))
120 LOG(ERROR) << "Failed to read file " << path.value();
121 base::DeleteFile(path, false);
122 callback.Run(base::RefCountedString::TakeString(&tmp));
125 void BeginReadingRecordingResult(
126 const WebUIDataSource::GotDataCallback& callback,
127 const base::FilePath& path) {
128 BrowserThread::PostTask(
129 BrowserThread::FILE, FROM_HERE,
130 base::Bind(ReadRecordingResult, callback, path));
133 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback);
135 bool OnEnableMonitoring(const std::string& data64,
136 const WebUIDataSource::GotDataCallback& callback) {
137 std::string category_filter_string;
138 int tracing_options = 0;
139 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options))
140 return false;
142 return TracingController::GetInstance()->EnableMonitoring(
143 category_filter_string,
144 static_cast<TracingController::Options>(tracing_options),
145 base::Bind(OnMonitoringEnabledAck, callback));
148 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback) {
149 base::RefCountedString* res = new base::RefCountedString();
150 callback.Run(res);
153 void OnMonitoringDisabled(const WebUIDataSource::GotDataCallback& callback) {
154 base::RefCountedString* res = new base::RefCountedString();
155 callback.Run(res);
158 void ReadMonitoringSnapshot(const WebUIDataSource::GotDataCallback& callback,
159 const base::FilePath& path) {
160 std::string tmp;
161 if (!base::ReadFileToString(path, &tmp))
162 LOG(ERROR) << "Failed to read file " << path.value();
163 base::DeleteFile(path, false);
164 callback.Run(base::RefCountedString::TakeString(&tmp));
167 void OnMonitoringSnapshotCaptured(
168 const WebUIDataSource::GotDataCallback& callback,
169 const base::FilePath& path) {
170 BrowserThread::PostTask(
171 BrowserThread::FILE, FROM_HERE,
172 base::Bind(ReadMonitoringSnapshot, callback, path));
175 bool OnBeginJSONRequest(const std::string& path,
176 const WebUIDataSource::GotDataCallback& callback) {
177 if (path == "json/categories") {
178 return TracingController::GetInstance()->GetCategories(
179 base::Bind(OnGotCategories, callback));
182 const char* beginRecordingPath = "json/begin_recording?";
183 if (StartsWithASCII(path, beginRecordingPath, true)) {
184 std::string data = path.substr(strlen(beginRecordingPath));
185 return OnBeginRecording(data, callback);
187 if (path == "json/get_buffer_percent_full") {
188 return TracingController::GetInstance()->GetTraceBufferPercentFull(
189 base::Bind(OnTraceBufferPercentFullResult, callback));
191 if (path == "json/end_recording") {
192 return TracingController::GetInstance()->DisableRecording(
193 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback));
196 const char* enableMonitoringPath = "json/begin_monitoring?";
197 if (path.find(enableMonitoringPath) == 0) {
198 std::string data = path.substr(strlen(enableMonitoringPath));
199 return OnEnableMonitoring(data, callback);
201 if (path == "json/end_monitoring") {
202 return TracingController::GetInstance()->DisableMonitoring(
203 base::Bind(OnMonitoringDisabled, callback));
205 if (path == "json/capture_monitoring") {
206 TracingController::GetInstance()->CaptureMonitoringSnapshot(
207 base::FilePath(), base::Bind(OnMonitoringSnapshotCaptured, callback));
208 return true;
210 LOG(ERROR) << "Unhandled request to " << path;
211 return false;
214 bool OnTracingRequest(const std::string& path,
215 const WebUIDataSource::GotDataCallback& callback) {
216 if (StartsWithASCII(path, "json/", true)) {
217 if (!OnBeginJSONRequest(path, callback)) {
218 std::string error("##ERROR##");
219 callback.Run(base::RefCountedString::TakeString(&error));
221 return true;
223 return false;
226 } // namespace
229 ////////////////////////////////////////////////////////////////////////////////
231 // TracingUI
233 ////////////////////////////////////////////////////////////////////////////////
235 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) {
236 // Set up the chrome://tracing/ source.
237 BrowserContext* browser_context =
238 web_ui->GetWebContents()->GetBrowserContext();
240 WebUIDataSource* source = WebUIDataSource::Create(kChromeUITracingHost);
241 source->SetJsonPath("strings.js");
242 source->SetDefaultResource(IDR_TRACING_HTML);
243 source->AddResourcePath("tracing.js", IDR_TRACING_JS);
244 source->SetRequestFilter(base::Bind(OnTracingRequest));
245 WebUIDataSource::Add(browser_context, source);
248 } // namespace content