IOThread.h (2897B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_ipc_IOThreadParent_h 8 #define mozilla_ipc_IOThreadParent_h 9 10 #include "base/thread.h" 11 #include "chrome/common/ipc_channel.h" 12 #include "mozilla/ipc/ScopedPort.h" 13 14 namespace mozilla::ipc { 15 16 // Abstract background thread used for IPC I/O. 17 class IOThread : private base::Thread { 18 public: 19 // Lifecycle Note: The IOThread is stored in a static, and is returned by raw 20 // pointer here from potentially any thread. This is OK because the IOThread 21 // is very long lived, and should outlive any other thread which would 22 // reference it (other than the main thread, which is responsible for the 23 // lifetime of the IO Thread). 24 static IOThread* Get() { return sSingleton; } 25 26 // Called during XPCOM component startup and shutdown. 27 static void Startup(); 28 static void Shutdown(); 29 30 // Get the nsISerialEventTarget which should be used to dispatch events to run 31 // on the IOThreadBase. 32 nsISerialEventTarget* GetEventTarget() { 33 return base::Thread::message_loop()->SerialEventTarget(); 34 } 35 36 protected: 37 IOThread(const char* aName); 38 ~IOThread(); 39 40 // Called by subclasses in the constructor/destructor to start/join the target 41 // thread. This cannot be done in the base class constructor/destructor, as 42 // the virtual Init()/CleanUp() methods need to be available. 43 void StartThread(); 44 void StopThread(); 45 46 // Init() and Cleanup() methods which will be invoked on the IOThread when the 47 // IOThread is started/stopped. 48 void Init() override = 0; 49 void CleanUp() override = 0; 50 51 private: 52 static IOThread* sSingleton; 53 }; 54 55 // Background I/O thread used by the parent process. 56 class IOThreadParent : public IOThread { 57 protected: 58 void Init() override; 59 void CleanUp() override; 60 61 private: 62 friend class IOThread; 63 64 IOThreadParent(); 65 ~IOThreadParent(); 66 67 const IPC::Channel::ChannelKind* mChannelKind; 68 }; 69 70 // Background I/O thread used by the child process. 71 class IOThreadChild : public IOThread { 72 public: 73 IOThreadChild(IPC::Channel::ChannelHandle aClientHandle, 74 base::ProcessId aParentPid); 75 ~IOThreadChild(); 76 77 mozilla::ipc::ScopedPort TakeInitialPort() { return std::move(mInitialPort); } 78 79 protected: 80 void Init() override; 81 void CleanUp() override; 82 83 private: 84 mozilla::ipc::ScopedPort mInitialPort; 85 IPC::Channel::ChannelHandle mClientHandle; 86 base::ProcessId mParentPid; 87 }; 88 89 inline void AssertIOThread() { 90 MOZ_ASSERT(MessageLoop::TYPE_IO == MessageLoop::current()->type(), 91 "should be on the IO thread!"); 92 } 93 94 } // namespace mozilla::ipc 95 96 #endif // mozilla_ipc_IOThreadParent_h