Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / media / render_media_log.cc
blobeae60c006a92522b9569c479210cf03a9a919f5b
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/renderer/media/render_media_log.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "content/common/view_messages.h"
13 #include "content/public/renderer/render_thread.h"
15 namespace {
17 // Print an event to the chromium log.
18 void Log(media::MediaLogEvent* event) {
19 if (event->type == media::MediaLogEvent::PIPELINE_ERROR) {
20 LOG(ERROR) << "MediaEvent: "
21 << media::MediaLog::MediaEventToLogString(*event);
22 } else if (event->type != media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED &&
23 event->type != media::MediaLogEvent::PROPERTY_CHANGE &&
24 event->type != media::MediaLogEvent::NETWORK_ACTIVITY_SET) {
25 DVLOG(1) << "MediaEvent: "
26 << media::MediaLog::MediaEventToLogString(*event);
30 } // namespace
32 namespace content {
34 RenderMediaLog::RenderMediaLog()
35 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
36 tick_clock_(new base::DefaultTickClock()),
37 last_ipc_send_time_(tick_clock_->NowTicks()) {
38 DCHECK(RenderThread::Get())
39 << "RenderMediaLog must be constructed on the render thread";
42 void RenderMediaLog::AddEvent(scoped_ptr<media::MediaLogEvent> event) {
43 // Always post to preserve the correct order of events.
44 // TODO(xhwang): Consider using sorted containers to keep the order and
45 // avoid extra posting.
46 task_runner_->PostTask(FROM_HERE,
47 base::Bind(&RenderMediaLog::AddEventInternal, this,
48 base::Passed(&event)));
51 void RenderMediaLog::AddEventInternal(scoped_ptr<media::MediaLogEvent> event) {
52 DCHECK(task_runner_->BelongsToCurrentThread());
54 Log(event.get());
56 // If there is an event waiting to be sent, there must be a send task pending.
57 const bool delayed_ipc_send_pending =
58 !queued_media_events_.empty() || last_buffered_extents_changed_event_;
60 // Keep track of the latest buffered extents properties to avoid sending
61 // thousands of events over IPC. See http://crbug.com/352585 for details.
62 if (event->type == media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED)
63 last_buffered_extents_changed_event_.swap(event);
64 else
65 queued_media_events_.push_back(*event);
67 if (delayed_ipc_send_pending)
68 return;
70 // Delay until it's been a second since the last ipc message was sent.
71 base::TimeDelta delay_for_next_ipc_send =
72 base::TimeDelta::FromSeconds(1) -
73 (tick_clock_->NowTicks() - last_ipc_send_time_);
74 if (delay_for_next_ipc_send > base::TimeDelta()) {
75 task_runner_->PostDelayedTask(
76 FROM_HERE, base::Bind(&RenderMediaLog::SendQueuedMediaEvents, this),
77 delay_for_next_ipc_send);
78 return;
81 // It's been more than a second so send now.
82 SendQueuedMediaEvents();
85 void RenderMediaLog::SendQueuedMediaEvents() {
86 DCHECK(task_runner_->BelongsToCurrentThread());
88 if (last_buffered_extents_changed_event_) {
89 queued_media_events_.push_back(*last_buffered_extents_changed_event_);
90 last_buffered_extents_changed_event_.reset();
93 RenderThread::Get()->Send(
94 new ViewHostMsg_MediaLogEvents(queued_media_events_));
95 queued_media_events_.clear();
96 last_ipc_send_time_ = tick_clock_->NowTicks();
99 RenderMediaLog::~RenderMediaLog() {
102 void RenderMediaLog::SetTickClockForTesting(
103 scoped_ptr<base::TickClock> tick_clock) {
104 tick_clock_.swap(tick_clock);
105 last_ipc_send_time_ = tick_clock_->NowTicks();
108 void RenderMediaLog::SetTaskRunnerForTesting(
109 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) {
110 task_runner_ = task_runner;
113 } // namespace content