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 "net/dns/serial_worker.h" | 5 #include "net/dns/serial_worker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/thread_task_runner_handle.h" |
10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 SerialWorker::SerialWorker() | 14 SerialWorker::SerialWorker() |
15 : message_loop_(base::MessageLoopProxy::current()), | 15 : task_runner_(base::ThreadTaskRunnerHandle::Get()), state_(IDLE) { |
16 state_(IDLE) {} | 16 } |
17 | 17 |
18 SerialWorker::~SerialWorker() {} | 18 SerialWorker::~SerialWorker() {} |
19 | 19 |
20 void SerialWorker::WorkNow() { | 20 void SerialWorker::WorkNow() { |
21 DCHECK(message_loop_->BelongsToCurrentThread()); | 21 DCHECK(task_runner_->BelongsToCurrentThread()); |
22 switch (state_) { | 22 switch (state_) { |
23 case IDLE: | 23 case IDLE: |
24 if (!base::WorkerPool::PostTask(FROM_HERE, base::Bind( | 24 if (!base::WorkerPool::PostTask(FROM_HERE, base::Bind( |
25 &SerialWorker::DoWorkJob, this), false)) { | 25 &SerialWorker::DoWorkJob, this), false)) { |
26 #if defined(OS_POSIX) | 26 #if defined(OS_POSIX) |
27 // See worker_pool_posix.cc. | 27 // See worker_pool_posix.cc. |
28 NOTREACHED() << "WorkerPool::PostTask is not expected to fail on posix"; | 28 NOTREACHED() << "WorkerPool::PostTask is not expected to fail on posix"; |
29 #else | 29 #else |
30 LOG(WARNING) << "Failed to WorkerPool::PostTask, will retry later"; | 30 LOG(WARNING) << "Failed to WorkerPool::PostTask, will retry later"; |
31 const int kWorkerPoolRetryDelayMs = 100; | 31 const int kWorkerPoolRetryDelayMs = 100; |
32 message_loop_->PostDelayedTask( | 32 task_runner_->PostDelayedTask( |
33 FROM_HERE, | 33 FROM_HERE, |
34 base::Bind(&SerialWorker::RetryWork, this), | 34 base::Bind(&SerialWorker::RetryWork, this), |
35 base::TimeDelta::FromMilliseconds(kWorkerPoolRetryDelayMs)); | 35 base::TimeDelta::FromMilliseconds(kWorkerPoolRetryDelayMs)); |
36 state_ = WAITING; | 36 state_ = WAITING; |
37 return; | 37 return; |
38 #endif | 38 #endif |
39 } | 39 } |
40 state_ = WORKING; | 40 state_ = WORKING; |
41 return; | 41 return; |
42 case WORKING: | 42 case WORKING: |
43 // Remember to re-read after |DoRead| finishes. | 43 // Remember to re-read after |DoRead| finishes. |
44 state_ = PENDING; | 44 state_ = PENDING; |
45 return; | 45 return; |
46 case CANCELLED: | 46 case CANCELLED: |
47 case PENDING: | 47 case PENDING: |
48 case WAITING: | 48 case WAITING: |
49 return; | 49 return; |
50 default: | 50 default: |
51 NOTREACHED() << "Unexpected state " << state_; | 51 NOTREACHED() << "Unexpected state " << state_; |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 void SerialWorker::Cancel() { | 55 void SerialWorker::Cancel() { |
56 DCHECK(message_loop_->BelongsToCurrentThread()); | 56 DCHECK(task_runner_->BelongsToCurrentThread()); |
57 state_ = CANCELLED; | 57 state_ = CANCELLED; |
58 } | 58 } |
59 | 59 |
60 void SerialWorker::DoWorkJob() { | 60 void SerialWorker::DoWorkJob() { |
61 this->DoWork(); | 61 this->DoWork(); |
62 // If this fails, the loop is gone, so there is no point retrying. | 62 // If this fails, the loop is gone, so there is no point retrying. |
63 message_loop_->PostTask(FROM_HERE, base::Bind( | 63 task_runner_->PostTask(FROM_HERE, |
64 &SerialWorker::OnWorkJobFinished, this)); | 64 base::Bind(&SerialWorker::OnWorkJobFinished, this)); |
65 } | 65 } |
66 | 66 |
67 void SerialWorker::OnWorkJobFinished() { | 67 void SerialWorker::OnWorkJobFinished() { |
68 DCHECK(message_loop_->BelongsToCurrentThread()); | 68 DCHECK(task_runner_->BelongsToCurrentThread()); |
69 switch (state_) { | 69 switch (state_) { |
70 case CANCELLED: | 70 case CANCELLED: |
71 return; | 71 return; |
72 case WORKING: | 72 case WORKING: |
73 state_ = IDLE; | 73 state_ = IDLE; |
74 this->OnWorkFinished(); | 74 this->OnWorkFinished(); |
75 return; | 75 return; |
76 case PENDING: | 76 case PENDING: |
77 state_ = IDLE; | 77 state_ = IDLE; |
78 WorkNow(); | 78 WorkNow(); |
79 return; | 79 return; |
80 default: | 80 default: |
81 NOTREACHED() << "Unexpected state " << state_; | 81 NOTREACHED() << "Unexpected state " << state_; |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 void SerialWorker::RetryWork() { | 85 void SerialWorker::RetryWork() { |
86 DCHECK(message_loop_->BelongsToCurrentThread()); | 86 DCHECK(task_runner_->BelongsToCurrentThread()); |
87 switch (state_) { | 87 switch (state_) { |
88 case CANCELLED: | 88 case CANCELLED: |
89 return; | 89 return; |
90 case WAITING: | 90 case WAITING: |
91 state_ = IDLE; | 91 state_ = IDLE; |
92 WorkNow(); | 92 WorkNow(); |
93 return; | 93 return; |
94 default: | 94 default: |
95 NOTREACHED() << "Unexpected state " << state_; | 95 NOTREACHED() << "Unexpected state " << state_; |
96 } | 96 } |
97 } | 97 } |
98 | 98 |
99 } // namespace net | 99 } // namespace net |
100 | 100 |
OLD | NEW |