xref: /aosp_15_r20/external/cronet/ipc/ipc_channel_factory.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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 "ipc/ipc_channel_factory.h"
6 
7 #include "base/memory/ptr_util.h"
8 #include "base/task/single_thread_task_runner.h"
9 #include "build/build_config.h"
10 #include "ipc/ipc_channel_mojo.h"
11 
12 namespace IPC {
13 
14 namespace {
15 
16 class PlatformChannelFactory : public ChannelFactory {
17  public:
PlatformChannelFactory(ChannelHandle handle,Channel::Mode mode,const scoped_refptr<base::SingleThreadTaskRunner> & ipc_task_runner)18   PlatformChannelFactory(
19       ChannelHandle handle,
20       Channel::Mode mode,
21       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner)
22       : handle_(handle), mode_(mode), ipc_task_runner_(ipc_task_runner) {}
23 
24   PlatformChannelFactory(const PlatformChannelFactory&) = delete;
25   PlatformChannelFactory& operator=(const PlatformChannelFactory&) = delete;
26 
BuildChannel(Listener * listener)27   std::unique_ptr<Channel> BuildChannel(Listener* listener) override {
28 #if BUILDFLAG(IS_NACL)
29     return Channel::Create(handle_, mode_, listener);
30 #else
31     DCHECK(handle_.is_mojo_channel_handle());
32     return ChannelMojo::Create(
33         mojo::ScopedMessagePipeHandle(handle_.mojo_handle), mode_, listener,
34         ipc_task_runner_, base::SingleThreadTaskRunner::GetCurrentDefault());
35 #endif
36   }
37 
GetIPCTaskRunner()38   scoped_refptr<base::SingleThreadTaskRunner> GetIPCTaskRunner() override {
39     return ipc_task_runner_;
40   }
41 
42  private:
43   ChannelHandle handle_;
44   Channel::Mode mode_;
45   scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
46 };
47 
48 } // namespace
49 
50 // static
Create(const ChannelHandle & handle,Channel::Mode mode,const scoped_refptr<base::SingleThreadTaskRunner> & ipc_task_runner)51 std::unique_ptr<ChannelFactory> ChannelFactory::Create(
52     const ChannelHandle& handle,
53     Channel::Mode mode,
54     const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) {
55   return std::make_unique<PlatformChannelFactory>(handle, mode,
56                                                   ipc_task_runner);
57 }
58 
59 }  // namespace IPC
60