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 "net/dns/serial_worker.h"
8 #include "base/location.h"
9 #include "base/message_loop_proxy.h"
10 #include "base/threading/worker_pool.h"
15 // Delay between calls to WorkerPool::PostTask
16 const int kWorkerPoolRetryDelayMs
= 100;
19 SerialWorker::SerialWorker()
20 : message_loop_(base::MessageLoopProxy::current()),
23 SerialWorker::~SerialWorker() {}
25 void SerialWorker::WorkNow() {
26 DCHECK(message_loop_
->BelongsToCurrentThread());
29 if (!base::WorkerPool::PostTask(FROM_HERE
, base::Bind(
30 &SerialWorker::DoWorkJob
, this), false)) {
32 // See worker_pool_posix.cc.
33 NOTREACHED() << "WorkerPool::PostTask is not expected to fail on posix";
35 LOG(WARNING
) << "Failed to WorkerPool::PostTask, will retry later";
36 message_loop_
->PostDelayedTask(
38 base::Bind(&SerialWorker::RetryWork
, this),
39 base::TimeDelta::FromMilliseconds(kWorkerPoolRetryDelayMs
));
47 // Remember to re-read after |DoRead| finishes.
55 NOTREACHED() << "Unexpected state " << state_
;
59 void SerialWorker::Cancel() {
60 DCHECK(message_loop_
->BelongsToCurrentThread());
64 void SerialWorker::DoWorkJob() {
66 // If this fails, the loop is gone, so there is no point retrying.
67 message_loop_
->PostTask(FROM_HERE
, base::Bind(
68 &SerialWorker::OnWorkJobFinished
, this));
71 void SerialWorker::OnWorkJobFinished() {
72 DCHECK(message_loop_
->BelongsToCurrentThread());
78 this->OnWorkFinished();
85 NOTREACHED() << "Unexpected state " << state_
;
89 void SerialWorker::RetryWork() {
90 DCHECK(message_loop_
->BelongsToCurrentThread());
99 NOTREACHED() << "Unexpected state " << state_
;