web_sub_thread.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2018 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. #ifndef IOS_WEB_WEB_SUB_THREAD_H_
  5. #define IOS_WEB_WEB_SUB_THREAD_H_
  6. #include "base/threading/thread.h"
  7. #include "base/threading/thread_checker.h"
  8. #include "ios/web/public/thread/web_thread.h"
  9. namespace web {
  10. class WebThreadImpl;
  11. }
  12. namespace web {
  13. // A WebSubThread is a physical thread backing a WebThread.
  14. class WebSubThread : public base::Thread {
  15. public:
  16. // Constructs a WebSubThread for |identifier|.
  17. explicit WebSubThread(WebThread::ID identifier);
  18. WebSubThread(const WebSubThread&) = delete;
  19. WebSubThread& operator=(const WebSubThread&) = delete;
  20. ~WebSubThread() override;
  21. // Registers this thread to represent |identifier_| in the web_thread.h
  22. // API. This thread must already be running when this is called. This can only
  23. // be called once per WebSubThread instance.
  24. void RegisterAsWebThread();
  25. // Ideally there wouldn't be a special blanket allowance to block the
  26. // WebThreads in tests but TestWebThreadImpl previously bypassed
  27. // WebSubThread and hence wasn't subject to ThreadRestrictions...
  28. // Flipping that around in favor of explicit scoped allowances would be
  29. // preferable but a non-trivial amount of work. Can only be called before
  30. // starting this WebSubThread.
  31. void AllowBlockingForTesting();
  32. protected:
  33. void Init() override;
  34. void Run(base::RunLoop* run_loop) override;
  35. void CleanUp() override;
  36. private:
  37. // Second Init() phase that must happen on this thread but can only happen
  38. // after it's promoted to a WebThread in |RegisterAsWebThread()|.
  39. void CompleteInitializationOnWebThread();
  40. // These methods merely forwards to Thread::Run() but are useful to identify
  41. // which WebThread this represents in stack traces.
  42. void UIThreadRun(base::RunLoop* run_loop);
  43. void IOThreadRun(base::RunLoop* run_loop);
  44. const WebThread::ID identifier_;
  45. // WebThreads are not allowed to do file I/O nor wait on synchronization
  46. // primitives except when explicitly allowed in tests.
  47. bool is_blocking_allowed_for_testing_ = false;
  48. // The WebThread registration for this |identifier_|, initialized in
  49. // RegisterAsWebThread().
  50. std::unique_ptr<WebThreadImpl> web_thread_;
  51. THREAD_CHECKER(web_thread_checker_);
  52. };
  53. } // namespace web
  54. #endif // IOS_WEB_WEB_SUB_THREAD_H_