blob: ba0f6330e5385f47d6e61e4f553c8ddb7f244122 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2019 The Chromium Authors
Carlos Caballeroe840fc32019-05-27 14:16:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gabriel Charette748577aa2019-08-12 12:53:555#ifndef CONTENT_BROWSER_SCHEDULER_BROWSER_IO_THREAD_DELEGATE_H_
6#define CONTENT_BROWSER_SCHEDULER_BROWSER_IO_THREAD_DELEGATE_H_
Carlos Caballeroe840fc32019-05-27 14:16:377
8#include "base/memory/ptr_util.h"
Keishi Hattori0e45c022021-11-27 09:25:529#include "base/memory/raw_ptr.h"
Carlos Caballeroe840fc32019-05-27 14:16:3710#include "base/memory/scoped_refptr.h"
11#include "base/task/sequence_manager/task_queue.h"
12#include "base/threading/thread.h"
Gabriel Charetted87f10f2022-03-31 00:44:2213#include "base/time/time.h"
Carlos Caballeroe840fc32019-05-27 14:16:3714#include "content/browser/scheduler/browser_task_queues.h"
15#include "content/common/content_export.h"
16
17namespace base {
18class SingleThreadTaskRunner;
19
20namespace sequence_manager {
21class SequenceManager;
22} // namespace sequence_manager
23
24} // namespace base
25
26namespace content {
27
Gabriel Charette748577aa2019-08-12 12:53:5528// Delegate for the IO thread.
29class CONTENT_EXPORT BrowserIOThreadDelegate : public base::Thread::Delegate {
Carlos Caballeroe840fc32019-05-27 14:16:3730 public:
31 using Handle = BrowserTaskQueues::Handle;
32
Alex Clarkebbf891dc2019-10-09 14:18:0233 // Creates a BrowserIOThreadDelegate for use with a real IO thread.
34 BrowserIOThreadDelegate();
35 ~BrowserIOThreadDelegate() override;
Karolina Soltysb083f932019-09-25 16:18:0636
Gabriel Charette748577aa2019-08-12 12:53:5537 static std::unique_ptr<BrowserIOThreadDelegate> CreateForTesting(
Carlos Caballeroe840fc32019-05-27 14:16:3738 base::sequence_manager::SequenceManager* sequence_manager) {
Alex Clarkebbf891dc2019-10-09 14:18:0239 DCHECK(sequence_manager);
Gabriel Charette748577aa2019-08-12 12:53:5540 return base::WrapUnique(new BrowserIOThreadDelegate(sequence_manager));
Carlos Caballeroe840fc32019-05-27 14:16:3741 }
42
Carlos Caballeroe840fc32019-05-27 14:16:3743 scoped_refptr<base::SingleThreadTaskRunner> GetDefaultTaskRunner() override;
Etienne Pierre-dorayef5dcca2023-07-11 16:46:2744 void BindToCurrentThread() override;
Carlos Caballeroe840fc32019-05-27 14:16:3745
Jiahe Zhang93e2eda2025-07-16 03:32:3446 void AddTaskObserver(base::TaskObserver* observer) override;
47
Carlos Caballeroe840fc32019-05-27 14:16:3748 bool allow_blocking_for_testing() const {
49 return allow_blocking_for_testing_;
50 }
51
52 // Call this before handing this over to a base::Thread to allow blocking in
53 // tests.
54 void SetAllowBlockingForTesting() { allow_blocking_for_testing_ = true; }
55
Alex Clarkebbf891dc2019-10-09 14:18:0256 scoped_refptr<Handle> GetHandle() { return task_queues_->GetHandle(); }
Carlos Caballeroe840fc32019-05-27 14:16:3757
58 private:
Alex Clarkebbf891dc2019-10-09 14:18:0259 // Creates a sequence funneled BrowserIOThreadDelegate for use in testing.
Gabriel Charette748577aa2019-08-12 12:53:5560 explicit BrowserIOThreadDelegate(
Carlos Caballeroe840fc32019-05-27 14:16:3761 base::sequence_manager::SequenceManager* sequence_manager);
62
63 // Performs the actual initialization of all the members that require a
Alex Clarkebbf891dc2019-10-09 14:18:0264 // SequenceManager.
65 void Init();
Carlos Caballeroe840fc32019-05-27 14:16:3766
67 bool allow_blocking_for_testing_ = false;
68 // Owned SequenceManager, null if instance created via CreateForTesting.
Alex Clarkebbf891dc2019-10-09 14:18:0269 const std::unique_ptr<base::sequence_manager::SequenceManager>
70 owned_sequence_manager_;
71
Keishi Hattori0e45c022021-11-27 09:25:5272 const raw_ptr<base::sequence_manager::SequenceManager> sequence_manager_;
Carlos Caballeroe840fc32019-05-27 14:16:3773
74 std::unique_ptr<BrowserTaskQueues> task_queues_;
Carlos Caballeroe840fc32019-05-27 14:16:3775 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
Karolina Soltys1e76b452019-08-22 20:35:0776};
Findit30f71a72019-08-22 22:02:0177
Carlos Caballeroe840fc32019-05-27 14:16:3778} // namespace content
79
Gabriel Charette748577aa2019-08-12 12:53:5580#endif // CONTENT_BROWSER_SCHEDULER_BROWSER_IO_THREAD_DELEGATE_H_