[Extensions Toolbar] Move context menu logic to view delegates
[chromium-blink-merge.git] / media / midi / midi_scheduler.cc
bloba65e703673b839c025e1d3384795000132cadb6d
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
3 // found in the LICENSE file.
5 #include "media/midi/midi_scheduler.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/time/time.h"
10 #include "media/midi/midi_manager.h"
12 namespace media {
14 MidiScheduler::MidiScheduler(MidiManager* manager)
15 : manager_(manager),
16 weak_factory_(this) {
19 MidiScheduler::~MidiScheduler() {
22 // TODO(crbug.com/467442): Use CancelableTaskTracker once it supports
23 // DelayedTask.
24 void MidiScheduler::PostSendDataTask(MidiManagerClient* client,
25 size_t length,
26 double timestamp,
27 const base::Closure& closure) {
28 DCHECK(client);
30 const base::Closure& weak_closure = base::Bind(
31 &MidiScheduler::InvokeClosure,
32 weak_factory_.GetWeakPtr(),
33 client,
34 length,
35 closure);
37 base::TimeDelta delay;
38 if (timestamp != 0.0) {
39 base::TimeTicks time_to_send =
40 base::TimeTicks() + base::TimeDelta::FromMicroseconds(
41 timestamp * base::Time::kMicrosecondsPerSecond);
42 delay = std::max(time_to_send - base::TimeTicks::Now(), base::TimeDelta());
44 base::MessageLoop::current()->task_runner()->PostDelayedTask(
45 FROM_HERE, weak_closure, delay);
48 void MidiScheduler::InvokeClosure(MidiManagerClient* client,
49 size_t length,
50 const base::Closure& closure) {
51 closure.Run();
52 manager_->AccumulateMidiBytesSent(client, length);
55 } // namespace media