| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/devtools/devtools_tracing_handler.h" | 5 #include "content/browser/devtools/devtools_tracing_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "content/browser/devtools/devtools_http_handler_impl.h" | 12 #include "content/browser/devtools/devtools_http_handler_impl.h" |
| 13 #include "content/public/browser/trace_controller.h" | 13 #include "content/public/browser/trace_controller.h" |
| 14 #include "content/public/browser/trace_subscriber.h" | 14 #include "content/public/browser/trace_subscriber.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 namespace { |
| 19 |
| 20 const char kTracingDomain[] = "Tracing"; |
| 21 |
| 22 const char kTracingStartCommand[] = "Tracing.start"; |
| 23 const char kTracingEndCommand[] = "Tracing.end"; |
| 24 |
| 25 const char kTracingCompleteNotification[] = "Tracing.tracingComplete"; |
| 26 const char kTracingDataCollected[] = "Tracing.dataCollected"; |
| 27 |
| 28 const char kCategoriesParam[] = "categories"; |
| 29 |
| 30 } // namespace |
| 31 |
| 18 DevToolsTracingHandler::DevToolsTracingHandler() | 32 DevToolsTracingHandler::DevToolsTracingHandler() |
| 19 : is_running_(false) { | 33 : DevToolsBrowserTarget::DomainHandler(kTracingDomain), |
| 34 is_running_(false) { |
| 35 RegisterCommandHandler(kTracingStartCommand, |
| 36 Bind(&DevToolsTracingHandler::OnStart, |
| 37 base::Unretained(this))); |
| 38 RegisterCommandHandler(kTracingEndCommand, |
| 39 Bind(&DevToolsTracingHandler::OnEnd, |
| 40 base::Unretained(this))); |
| 20 } | 41 } |
| 21 | 42 |
| 22 DevToolsTracingHandler::~DevToolsTracingHandler() { | 43 DevToolsTracingHandler::~DevToolsTracingHandler() { |
| 23 } | 44 } |
| 24 | 45 |
| 25 std::string DevToolsTracingHandler::Domain() { | |
| 26 return "Tracing"; | |
| 27 } | |
| 28 | |
| 29 void DevToolsTracingHandler::OnEndTracingComplete() { | 46 void DevToolsTracingHandler::OnEndTracingComplete() { |
| 30 is_running_ = false; | 47 is_running_ = false; |
| 31 SendNotification("Tracing.tracingComplete", ""); | 48 SendNotification(kTracingCompleteNotification, NULL, NULL); |
| 32 } | 49 } |
| 33 | 50 |
| 34 void DevToolsTracingHandler::OnTraceDataCollected( | 51 void DevToolsTracingHandler::OnTraceDataCollected( |
| 35 const scoped_refptr<base::RefCountedString>& trace_fragment) { | 52 const scoped_refptr<base::RefCountedString>& trace_fragment) { |
| 36 if (is_running_) | 53 if (is_running_) { |
| 37 SendNotification("Tracing.dataCollected", trace_fragment->data()); | 54 base::DictionaryValue* params = new base::DictionaryValue(); |
| 55 params->SetString("value", trace_fragment->data()); |
| 56 SendNotification(kTracingDataCollected, params, NULL); |
| 57 } |
| 38 } | 58 } |
| 39 | 59 |
| 40 base::Value* DevToolsTracingHandler::OnProtocolCommand( | 60 base::DictionaryValue* DevToolsTracingHandler::OnStart( |
| 41 const std::string& method, | |
| 42 const base::DictionaryValue* params, | 61 const base::DictionaryValue* params, |
| 43 base::Value** error_out) { | 62 base::Value** error_out) { |
| 44 if (method == "Tracing.start") | 63 std::string categories; |
| 45 return Start(params); | 64 if (params && params->HasKey(kCategoriesParam)) |
| 46 else if (method == "Tracing.end") | 65 params->GetString(kCategoriesParam, &categories); |
| 47 return End(params); | 66 TraceController::GetInstance()->BeginTracing(this, categories); |
| 48 | 67 is_running_ = true; |
| 49 base::DictionaryValue* error_object = new base::DictionaryValue(); | |
| 50 error_object->SetInteger("code", -1); | |
| 51 error_object->SetString("message", "Invalid method"); | |
| 52 | |
| 53 *error_out = error_object; | |
| 54 | |
| 55 return NULL; | 68 return NULL; |
| 56 } | 69 } |
| 57 | 70 |
| 58 base::Value* DevToolsTracingHandler::Start( | 71 base::DictionaryValue* DevToolsTracingHandler::OnEnd( |
| 59 const base::DictionaryValue* params) { | 72 const base::DictionaryValue* params, |
| 60 std::string categories; | 73 base::Value** error_out) { |
| 61 if (params && params->HasKey("categories")) | 74 TraceController::GetInstance()->EndTracingAsync(this); |
| 62 params->GetString("categories", &categories); | 75 return NULL; |
| 63 TraceController::GetInstance()->BeginTracing(this, categories); | |
| 64 is_running_ = true; | |
| 65 | |
| 66 return base::Value::CreateBooleanValue(true); | |
| 67 } | 76 } |
| 68 | 77 |
| 69 base::Value* DevToolsTracingHandler::End( | |
| 70 const base::DictionaryValue* /* params */) { | |
| 71 TraceController::GetInstance()->EndTracingAsync(this); | |
| 72 | |
| 73 return base::Value::CreateBooleanValue(true); | |
| 74 } | |
| 75 | |
| 76 void DevToolsTracingHandler::SendNotification( | |
| 77 const std::string& method, const std::string& value) { | |
| 78 scoped_ptr<base::DictionaryValue> ret(new base::DictionaryValue()); | |
| 79 ret->SetString("method", method); | |
| 80 | |
| 81 if (!value.empty()) { | |
| 82 base::DictionaryValue* params = new base::DictionaryValue(); | |
| 83 params->SetString("value", value); | |
| 84 | |
| 85 ret->Set("params", params); | |
| 86 } | |
| 87 | |
| 88 // Serialize response. | |
| 89 std::string json_response; | |
| 90 base::JSONWriter::Write(ret.get(), &json_response); | |
| 91 | |
| 92 notifier()->Notify(json_response); | |
| 93 } | |
| 94 | |
| 95 | |
| 96 } // namespace content | 78 } // namespace content |
| OLD | NEW |