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/renderer/media/render_media_log.h" | 5 #include "content/renderer/media/render_media_log.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 } // namespace | 41 } // namespace |
42 | 42 |
43 namespace content { | 43 namespace content { |
44 | 44 |
45 RenderMediaLog::RenderMediaLog(const GURL& security_origin) | 45 RenderMediaLog::RenderMediaLog(const GURL& security_origin) |
46 : security_origin_(security_origin), | 46 : security_origin_(security_origin), |
47 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 47 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
48 tick_clock_(new base::DefaultTickClock()), | 48 tick_clock_(new base::DefaultTickClock()), |
49 last_ipc_send_time_(tick_clock_->NowTicks()), | 49 last_ipc_send_time_(tick_clock_->NowTicks()), |
50 ipc_send_pending_(false) { | 50 ipc_send_pending_(false), |
| 51 weak_factory_(this) { |
51 DCHECK(RenderThread::Get()) | 52 DCHECK(RenderThread::Get()) |
52 << "RenderMediaLog must be constructed on the render thread"; | 53 << "RenderMediaLog must be constructed on the render thread"; |
| 54 // Pre-bind the WeakPtr on the right thread since we'll receive calls from |
| 55 // other threads and don't want races. |
| 56 weak_this_ = weak_factory_.GetWeakPtr(); |
| 57 } |
| 58 |
| 59 RenderMediaLog::~RenderMediaLog() { |
| 60 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 61 |
| 62 // There's no further chance to handle this, so send them now. This should not |
| 63 // be racy since nothing should have a pointer to the media log on another |
| 64 // thread by this point. |
| 65 if (ipc_send_pending_) |
| 66 SendQueuedMediaEvents(); |
53 } | 67 } |
54 | 68 |
55 void RenderMediaLog::AddEvent(std::unique_ptr<media::MediaLogEvent> event) { | 69 void RenderMediaLog::AddEvent(std::unique_ptr<media::MediaLogEvent> event) { |
56 Log(event.get()); | 70 Log(event.get()); |
57 | 71 |
58 // For enforcing delay until it's been a second since the last ipc message was | 72 // For enforcing delay until it's been a second since the last ipc message was |
59 // sent. | 73 // sent. |
60 base::TimeDelta delay_for_next_ipc_send; | 74 base::TimeDelta delay_for_next_ipc_send; |
61 | 75 |
62 { | 76 { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 if (ipc_send_pending_) | 111 if (ipc_send_pending_) |
98 return; | 112 return; |
99 | 113 |
100 ipc_send_pending_ = true; | 114 ipc_send_pending_ = true; |
101 delay_for_next_ipc_send = base::TimeDelta::FromSeconds(1) - | 115 delay_for_next_ipc_send = base::TimeDelta::FromSeconds(1) - |
102 (tick_clock_->NowTicks() - last_ipc_send_time_); | 116 (tick_clock_->NowTicks() - last_ipc_send_time_); |
103 } | 117 } |
104 | 118 |
105 if (delay_for_next_ipc_send > base::TimeDelta()) { | 119 if (delay_for_next_ipc_send > base::TimeDelta()) { |
106 task_runner_->PostDelayedTask( | 120 task_runner_->PostDelayedTask( |
107 FROM_HERE, base::Bind(&RenderMediaLog::SendQueuedMediaEvents, this), | 121 FROM_HERE, |
| 122 base::Bind(&RenderMediaLog::SendQueuedMediaEvents, weak_this_), |
108 delay_for_next_ipc_send); | 123 delay_for_next_ipc_send); |
109 return; | 124 return; |
110 } | 125 } |
111 | 126 |
112 // It's been more than a second so send ASAP. | 127 // It's been more than a second so send ASAP. |
113 if (task_runner_->BelongsToCurrentThread()) { | 128 if (task_runner_->BelongsToCurrentThread()) { |
114 SendQueuedMediaEvents(); | 129 SendQueuedMediaEvents(); |
115 return; | 130 return; |
116 } | 131 } |
117 task_runner_->PostTask( | 132 task_runner_->PostTask( |
118 FROM_HERE, base::Bind(&RenderMediaLog::SendQueuedMediaEvents, this)); | 133 FROM_HERE, |
| 134 base::Bind(&RenderMediaLog::SendQueuedMediaEvents, weak_this_)); |
119 } | 135 } |
120 | 136 |
121 std::string RenderMediaLog::GetLastErrorMessage() { | 137 std::string RenderMediaLog::GetLastErrorMessage() { |
122 base::AutoLock auto_lock(lock_); | 138 base::AutoLock auto_lock(lock_); |
123 | 139 |
124 // Return the conditional concatenation of the last pipeline error and the | 140 // Return the conditional concatenation of the last pipeline error and the |
125 // last media error log. | 141 // last media error log. |
126 std::stringstream result; | 142 std::stringstream result; |
127 if (last_pipeline_error_) { | 143 if (last_pipeline_error_) { |
128 result << MediaEventToLogString(*last_pipeline_error_) | 144 result << MediaEventToLogString(*last_pipeline_error_) |
129 << (last_media_error_log_entry_ ? ", " : ""); | 145 << (last_media_error_log_entry_ ? ", " : ""); |
130 } | 146 } |
131 if (last_media_error_log_entry_) | 147 if (last_media_error_log_entry_) |
132 result << MediaEventToLogString(*last_media_error_log_entry_); | 148 result << MediaEventToLogString(*last_media_error_log_entry_); |
133 return result.str(); | 149 return result.str(); |
134 } | 150 } |
135 | 151 |
136 void RenderMediaLog::RecordRapporWithSecurityOrigin(const std::string& metric) { | 152 void RenderMediaLog::RecordRapporWithSecurityOrigin(const std::string& metric) { |
137 if (!task_runner_->BelongsToCurrentThread()) { | 153 if (!task_runner_->BelongsToCurrentThread()) { |
138 task_runner_->PostTask( | 154 task_runner_->PostTask( |
139 FROM_HERE, base::Bind(&RenderMediaLog::RecordRapporWithSecurityOrigin, | 155 FROM_HERE, base::Bind(&RenderMediaLog::RecordRapporWithSecurityOrigin, |
140 this, metric)); | 156 weak_this_, metric)); |
141 return; | 157 return; |
142 } | 158 } |
143 | 159 |
144 GetContentClient()->renderer()->RecordRapporURL(metric, security_origin_); | 160 GetContentClient()->renderer()->RecordRapporURL(metric, security_origin_); |
145 } | 161 } |
146 | 162 |
147 void RenderMediaLog::SendQueuedMediaEvents() { | 163 void RenderMediaLog::SendQueuedMediaEvents() { |
148 DCHECK(task_runner_->BelongsToCurrentThread()); | 164 DCHECK(task_runner_->BelongsToCurrentThread()); |
149 | 165 |
150 std::vector<media::MediaLogEvent> events_to_send; | 166 std::vector<media::MediaLogEvent> events_to_send; |
(...skipping 10 matching lines...) Expand all Loading... |
161 | 177 |
162 if (last_duration_changed_event_) { | 178 if (last_duration_changed_event_) { |
163 queued_media_events_.push_back(*last_duration_changed_event_); | 179 queued_media_events_.push_back(*last_duration_changed_event_); |
164 last_duration_changed_event_.reset(); | 180 last_duration_changed_event_.reset(); |
165 } | 181 } |
166 | 182 |
167 queued_media_events_.swap(events_to_send); | 183 queued_media_events_.swap(events_to_send); |
168 last_ipc_send_time_ = tick_clock_->NowTicks(); | 184 last_ipc_send_time_ = tick_clock_->NowTicks(); |
169 } | 185 } |
170 | 186 |
| 187 if (events_to_send.empty()) |
| 188 return; |
| 189 |
171 RenderThread::Get()->Send(new ViewHostMsg_MediaLogEvents(events_to_send)); | 190 RenderThread::Get()->Send(new ViewHostMsg_MediaLogEvents(events_to_send)); |
172 } | 191 } |
173 | 192 |
174 RenderMediaLog::~RenderMediaLog() { | |
175 } | |
176 | |
177 void RenderMediaLog::SetTickClockForTesting( | 193 void RenderMediaLog::SetTickClockForTesting( |
178 std::unique_ptr<base::TickClock> tick_clock) { | 194 std::unique_ptr<base::TickClock> tick_clock) { |
179 base::AutoLock auto_lock(lock_); | 195 base::AutoLock auto_lock(lock_); |
180 tick_clock_.swap(tick_clock); | 196 tick_clock_.swap(tick_clock); |
181 last_ipc_send_time_ = tick_clock_->NowTicks(); | 197 last_ipc_send_time_ = tick_clock_->NowTicks(); |
182 } | 198 } |
183 | 199 |
184 void RenderMediaLog::SetTaskRunnerForTesting( | 200 void RenderMediaLog::SetTaskRunnerForTesting( |
185 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | 201 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
186 task_runner_ = task_runner; | 202 task_runner_ = task_runner; |
187 } | 203 } |
188 | 204 |
189 } // namespace content | 205 } // namespace content |
OLD | NEW |