Change to let media_stream_devices_controller.cc decide if a gUM request fail if...
[chromium-blink-merge.git] / components / domain_reliability / dispatcher.cc
blobceb4da09c8b5e6ca76129831eacb696cb93af012
1 // Copyright 2014 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 "components/domain_reliability/dispatcher.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/stl_util.h"
10 #include "base/timer/timer.h"
11 #include "components/domain_reliability/util.h"
13 namespace domain_reliability {
15 DomainReliabilityDispatcher::DomainReliabilityDispatcher(MockableTime* time)
16 : time_(time) {}
18 DomainReliabilityDispatcher::~DomainReliabilityDispatcher() {
19 // TODO(ttuttle): STLElementDeleter?
20 STLDeleteElements(&tasks_);
23 void DomainReliabilityDispatcher::ScheduleTask(
24 const base::Closure& closure,
25 base::TimeDelta min_delay,
26 base::TimeDelta max_delay) {
27 DCHECK(!closure.is_null());
28 // Would be DCHECK_LE, but you can't << a TimeDelta.
29 DCHECK(min_delay <= max_delay);
31 Task* task = new Task(closure, time_->CreateTimer(), min_delay, max_delay);
32 tasks_.insert(task);
33 if (max_delay.InMicroseconds() < 0)
34 RunAndDeleteTask(task);
35 else if (min_delay.InMicroseconds() < 0)
36 MakeTaskEligible(task);
37 else
38 MakeTaskWaiting(task);
41 void DomainReliabilityDispatcher::RunEligibleTasks() {
42 // Move all eligible tasks to a separate set so that eligible_tasks_.erase in
43 // RunAndDeleteTask won't erase elements out from under the iterator. (Also
44 // keeps RunEligibleTasks from running forever if a task adds a new, already-
45 // eligible task that does the same, and so on.)
46 std::set<Task*> tasks;
47 tasks.swap(eligible_tasks_);
49 for (std::set<Task*>::const_iterator it = tasks.begin();
50 it != tasks.end();
51 ++it) {
52 Task* task = *it;
53 DCHECK(task);
54 DCHECK(task->eligible);
55 RunAndDeleteTask(task);
59 DomainReliabilityDispatcher::Task::Task(const base::Closure& closure,
60 scoped_ptr<MockableTime::Timer> timer,
61 base::TimeDelta min_delay,
62 base::TimeDelta max_delay)
63 : closure(closure),
64 timer(timer.Pass()),
65 min_delay(min_delay),
66 max_delay(max_delay),
67 eligible(false) {}
69 DomainReliabilityDispatcher::Task::~Task() {}
71 void DomainReliabilityDispatcher::MakeTaskWaiting(Task* task) {
72 DCHECK(task);
73 DCHECK(!task->eligible);
74 DCHECK(!task->timer->IsRunning());
75 task->timer->Start(
76 FROM_HERE,
77 task->min_delay,
78 base::Bind(
79 &DomainReliabilityDispatcher::MakeTaskEligible,
80 base::Unretained(this),
81 task));
84 void
85 DomainReliabilityDispatcher::MakeTaskEligible(Task* task) {
86 DCHECK(task);
87 DCHECK(!task->eligible);
88 task->eligible = true;
89 eligible_tasks_.insert(task);
90 task->timer->Start(
91 FROM_HERE,
92 task->max_delay - task->min_delay,
93 base::Bind(
94 &DomainReliabilityDispatcher::RunAndDeleteTask,
95 base::Unretained(this),
96 task));
99 void DomainReliabilityDispatcher::RunAndDeleteTask(Task* task) {
100 DCHECK(task);
101 DCHECK(!task->closure.is_null());
102 task->closure.Run();
103 if (task->eligible)
104 eligible_tasks_.erase(task);
105 tasks_.erase(task);
106 delete task;
109 } // namespace domain_reliability