xref: /aosp_15_r20/external/cronet/net/base/network_notification_thread_mac.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
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/base/network_notification_thread_mac.h"
6 
7 #include "base/message_loop/message_pump_type.h"
8 #include "base/no_destructor.h"
9 #include "base/task/single_thread_task_runner.h"
10 #include "base/threading/thread.h"
11 
12 namespace net {
13 
14 namespace {
15 
16 class NotificationThreadMac {
17  public:
18   NotificationThreadMac(const NotificationThreadMac&) = delete;
19   NotificationThreadMac& operator=(const NotificationThreadMac&) = delete;
20 
task_runner() const21   scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
22     return task_runner_;
23   }
24 
25  private:
26   friend base::NoDestructor<NotificationThreadMac>;
27 
NotificationThreadMac()28   NotificationThreadMac() : thread_("NetworkNotificationThreadMac") {
29     base::Thread::Options options;
30     options.message_pump_type = base::MessagePumpType::UI;
31     options.joinable = false;
32     thread_.StartWithOptions(std::move(options));
33     task_runner_ = thread_.task_runner();
34     thread_.DetachFromSequence();
35   }
36 
37   ~NotificationThreadMac() = delete;
38 
39   // The |thread_| object is not thread-safe. This should not be accessed
40   // outside the constructor.
41   base::Thread thread_;
42 
43   // Saved TaskRunner handle that can be accessed from any thread.
44   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
45 };
46 
47 }  // namespace
48 
GetNetworkNotificationThreadMac()49 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkNotificationThreadMac() {
50   static base::NoDestructor<NotificationThreadMac> notification_thread;
51   return notification_thread->task_runner();
52 }
53 
54 }  // namespace net
55