OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "base/lazy_instance.h" | 5 #include "base/lazy_instance.h" |
6 #include "base/memory/singleton.h" | 6 #include "base/memory/singleton.h" |
7 #include "base/trace_event/trace_event_impl.h" | 7 #include "base/trace_event/trace_event_impl.h" |
8 #include "content/browser/tracing/battor_power_trace_provider.h" | 8 #include "content/browser/tracing/battor_power_trace_provider.h" |
9 #include "content/browser/tracing/power_tracing_agent.h" | 9 #include "content/browser/tracing/power_tracing_agent.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
| 14 namespace { |
| 15 |
| 16 const char kPowerTracingAgentName[] = "battor"; |
| 17 const char kPowerTraceLabel[] = "powerTraceAsString"; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 // static |
| 22 PowerTracingAgent* PowerTracingAgent::GetInstance() { |
| 23 return base::Singleton<PowerTracingAgent>::get(); |
| 24 } |
| 25 |
14 PowerTracingAgent::PowerTracingAgent() : is_tracing_(false) { | 26 PowerTracingAgent::PowerTracingAgent() : is_tracing_(false) { |
15 battor_trace_provider_.reset(new BattorPowerTraceProvider()); | 27 battor_trace_provider_.reset(new BattorPowerTraceProvider()); |
16 } | 28 } |
17 | 29 |
18 PowerTracingAgent::~PowerTracingAgent() {} | 30 PowerTracingAgent::~PowerTracingAgent() {} |
19 | 31 |
20 bool PowerTracingAgent::StartTracing() { | 32 std::string PowerTracingAgent::GetTracingAgentName() { |
| 33 return kPowerTracingAgentName; |
| 34 } |
| 35 |
| 36 std::string PowerTracingAgent::GetTraceEventLabel() { |
| 37 return kPowerTraceLabel; |
| 38 } |
| 39 |
| 40 bool PowerTracingAgent::StartAgentTracing( |
| 41 const base::trace_event::TraceConfig& trace_config) { |
21 // Tracing session already in progress. | 42 // Tracing session already in progress. |
22 if (is_tracing_) | 43 if (is_tracing_) |
23 return false; | 44 return false; |
24 | 45 |
25 // TODO(prabhur) Start tracing probably needs to be done in a | 46 // TODO(prabhur) Start tracing probably needs to be done in a |
26 // separate thread since it involves talking to the h/w. | 47 // separate thread since it involves talking to the h/w. |
27 // Revisit once battor h/w communication is enabled. | 48 // Revisit once battor h/w communication is enabled. |
28 is_tracing_ = battor_trace_provider_->StartTracing(); | 49 is_tracing_ = battor_trace_provider_->StartTracing(); |
29 return is_tracing_; | 50 return is_tracing_; |
30 } | 51 } |
31 | 52 |
32 void PowerTracingAgent::StopTracing(const OutputCallback& callback) { | 53 void PowerTracingAgent::StopAgentTracing( |
| 54 const StopAgentTracingCallback& callback) { |
33 // No tracing session in progress. | 55 // No tracing session in progress. |
34 if (!is_tracing_) | 56 if (!is_tracing_) |
35 return; | 57 return; |
36 | 58 |
37 // Stop tracing & collect logs. | 59 // Stop tracing & collect logs. |
38 OutputCallback on_stop_power_tracing_done_callback = base::Bind( | |
39 &PowerTracingAgent::OnStopTracingDone, base::Unretained(this), callback); | |
40 BrowserThread::PostTask( | 60 BrowserThread::PostTask( |
41 BrowserThread::UI, FROM_HERE, | 61 BrowserThread::UI, FROM_HERE, |
42 base::Bind(&PowerTracingAgent::FlushOnThread, base::Unretained(this), | 62 base::Bind(&PowerTracingAgent::FlushOnThread, |
43 on_stop_power_tracing_done_callback)); | 63 base::Unretained(this), |
| 64 callback)); |
44 } | 65 } |
45 | 66 |
46 void PowerTracingAgent::OnStopTracingDone( | 67 void PowerTracingAgent::OnStopTracingDone( |
47 const OutputCallback& callback, | 68 const StopAgentTracingCallback& callback, |
48 const scoped_refptr<base::RefCountedString>& result) { | 69 const scoped_refptr<base::RefCountedString>& result) { |
49 is_tracing_ = false; | 70 is_tracing_ = false; |
50 // Pass the serialized events. | 71 // Pass the serialized events. |
51 callback.Run(result); | 72 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), result); |
52 } | 73 } |
53 | 74 |
54 // static | 75 void PowerTracingAgent::FlushOnThread( |
55 PowerTracingAgent* PowerTracingAgent::GetInstance() { | 76 const StopAgentTracingCallback& callback) { |
56 return base::Singleton<PowerTracingAgent>::get(); | |
57 } | |
58 | |
59 void PowerTracingAgent::FlushOnThread(const OutputCallback& callback) { | |
60 // Pass the result to the UI Thread. | 77 // Pass the result to the UI Thread. |
61 | 78 |
62 // TODO(prabhur) StopTracing & GetLog need to be called on a | 79 // TODO(prabhur) StopTracing & GetLog need to be called on a |
63 // separate thread depending on how BattorPowerTraceProvider is implemented. | 80 // separate thread depending on how BattorPowerTraceProvider is implemented. |
64 battor_trace_provider_->StopTracing(); | 81 battor_trace_provider_->StopTracing(); |
65 std::string battor_logs; | 82 std::string battor_logs; |
66 battor_trace_provider_->GetLog(&battor_logs); | 83 battor_trace_provider_->GetLog(&battor_logs); |
67 scoped_refptr<base::RefCountedString> result = | 84 scoped_refptr<base::RefCountedString> result = |
68 base::RefCountedString::TakeString(&battor_logs); | 85 base::RefCountedString::TakeString(&battor_logs); |
69 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 86 BrowserThread::PostTask( |
70 base::Bind(callback, result)); | 87 BrowserThread::UI, FROM_HERE, |
| 88 base::Bind(&PowerTracingAgent::OnStopTracingDone, |
| 89 base::Unretained(this), |
| 90 callback, |
| 91 result)); |
| 92 } |
| 93 |
| 94 bool PowerTracingAgent::SupportsExplicitClockSync() { |
| 95 // TODO(zhenw): return true after implementing explicit clock sync. |
| 96 return false; |
| 97 } |
| 98 |
| 99 void PowerTracingAgent::RecordClockSyncMarker( |
| 100 int sync_id, |
| 101 const RecordClockSyncMarkerCallback& callback) { |
| 102 DCHECK(SupportsExplicitClockSync()); |
| 103 // TODO(zhenw): implement explicit clock sync. |
71 } | 104 } |
72 | 105 |
73 } // namespace content | 106 } // namespace content |
OLD | NEW |