[email protected] | cb50762 | 2012-03-23 16:17:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #include "net/dns/serial_worker.h" | ||||
6 | |||||
7 | #include "base/bind.h" | ||||
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 8 | #include "base/check_op.h" |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 9 | #include "base/location.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 10 | #include "base/notreached.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 11 | #include "base/task/post_task.h" |
Gabriel Charette | d5c656c | 2020-02-26 16:35:22 | [diff] [blame] | 12 | #include "base/task/thread_pool.h" |
Robbie McElrath | 31391ba | 2018-11-15 02:04:50 | [diff] [blame] | 13 | #include "base/threading/sequenced_task_runner_handle.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 14 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 15 | |
16 | namespace net { | ||||
17 | |||||
Erik Anderson | c243d71 | 2021-07-30 00:13:41 | [diff] [blame^] | 18 | SerialWorker::SerialWorker() |
Robbie McElrath | 31391ba | 2018-11-15 02:04:50 | [diff] [blame] | 19 | : base::RefCountedDeleteOnSequence<SerialWorker>( |
20 | base::SequencedTaskRunnerHandle::Get()), | ||||
Erik Anderson | c243d71 | 2021-07-30 00:13:41 | [diff] [blame^] | 21 | state_(IDLE) {} |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 22 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 23 | SerialWorker::~SerialWorker() = default; |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 24 | |
25 | void SerialWorker::WorkNow() { | ||||
Francois Doray | becd1ef7 | 2017-09-25 20:58:45 | [diff] [blame] | 26 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 27 | switch (state_) { |
28 | case IDLE: | ||||
Erik Anderson | c243d71 | 2021-07-30 00:13:41 | [diff] [blame^] | 29 | // We are posting weak pointer to OnWorkJobFinished to avoid leak when |
Sami Kyostila | 0b4314e | 2019-07-31 20:47:17 | [diff] [blame] | 30 | // PostTaskAndReply fails to post task back to the original |
Leonid Kaplan | 24c51bc | 2018-09-04 13:23:16 | [diff] [blame] | 31 | // task runner. In this case the callback is not destroyed, and the |
32 | // weak reference allows SerialWorker instance to be deleted. | ||||
Gabriel Charette | d5c656c | 2020-02-26 16:35:22 | [diff] [blame] | 33 | base::ThreadPool::PostTaskAndReply( |
34 | FROM_HERE, | ||||
35 | {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, | ||||
36 | base::BindOnce(&SerialWorker::DoWork, this), | ||||
37 | base::BindOnce(&SerialWorker::OnWorkJobFinished, | ||||
38 | weak_factory_.GetWeakPtr())); | ||||
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 39 | state_ = WORKING; |
40 | return; | ||||
41 | case WORKING: | ||||
42 | // Remember to re-read after |DoRead| finishes. | ||||
43 | state_ = PENDING; | ||||
44 | return; | ||||
45 | case CANCELLED: | ||||
46 | case PENDING: | ||||
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 47 | return; |
48 | default: | ||||
49 | NOTREACHED() << "Unexpected state " << state_; | ||||
50 | } | ||||
51 | } | ||||
52 | |||||
53 | void SerialWorker::Cancel() { | ||||
Francois Doray | becd1ef7 | 2017-09-25 20:58:45 | [diff] [blame] | 54 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 55 | state_ = CANCELLED; |
56 | } | ||||
Erik Anderson | c243d71 | 2021-07-30 00:13:41 | [diff] [blame^] | 57 | |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 58 | void SerialWorker::OnWorkJobFinished() { |
Francois Doray | becd1ef7 | 2017-09-25 20:58:45 | [diff] [blame] | 59 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 60 | switch (state_) { |
61 | case CANCELLED: | ||||
62 | return; | ||||
63 | case WORKING: | ||||
64 | state_ = IDLE; | ||||
Erik Anderson | c243d71 | 2021-07-30 00:13:41 | [diff] [blame^] | 65 | this->OnWorkFinished(); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 66 | return; |
67 | case PENDING: | ||||
68 | state_ = IDLE; | ||||
Erik Anderson | c243d71 | 2021-07-30 00:13:41 | [diff] [blame^] | 69 | WorkNow(); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 70 | return; |
71 | default: | ||||
72 | NOTREACHED() << "Unexpected state " << state_; | ||||
73 | } | ||||
74 | } | ||||
75 | |||||
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 76 | } // namespace net |