network_notification_thread_mac.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2019 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.
  4. #include "net/base/network_notification_thread_mac.h"
  5. #include "base/message_loop/message_pump_type.h"
  6. #include "base/no_destructor.h"
  7. #include "base/threading/thread.h"
  8. namespace net {
  9. namespace {
  10. class NotificationThreadMac {
  11. public:
  12. NotificationThreadMac(const NotificationThreadMac&) = delete;
  13. NotificationThreadMac& operator=(const NotificationThreadMac&) = delete;
  14. scoped_refptr<base::SingleThreadTaskRunner> task_runner() const {
  15. return task_runner_;
  16. }
  17. private:
  18. friend base::NoDestructor<NotificationThreadMac>;
  19. NotificationThreadMac() : thread_("NetworkNotificationThreadMac") {
  20. base::Thread::Options options;
  21. options.message_pump_type = base::MessagePumpType::UI;
  22. options.joinable = false;
  23. thread_.StartWithOptions(std::move(options));
  24. task_runner_ = thread_.task_runner();
  25. thread_.DetachFromSequence();
  26. }
  27. ~NotificationThreadMac() = delete;
  28. // The |thread_| object is not thread-safe. This should not be accessed
  29. // outside the constructor.
  30. base::Thread thread_;
  31. // Saved TaskRunner handle that can be accessed from any thread.
  32. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  33. };
  34. } // namespace
  35. scoped_refptr<base::SingleThreadTaskRunner> GetNetworkNotificationThreadMac() {
  36. static base::NoDestructor<NotificationThreadMac> notification_thread;
  37. return notification_thread->task_runner();
  38. }
  39. } // namespace net