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