Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: ipc/mojo/ipc_channel_mojo.cc

Issue 599333002: ChannelMojo: Handle when ChannelMojo outlives ChannelMojoHost (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Landing Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ipc/mojo/ipc_channel_mojo.h ('k') | ipc/mojo/ipc_channel_mojo_host.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ipc/mojo/ipc_channel_mojo.h" 5 #include "ipc/mojo/ipc_channel_mojo.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "ipc/ipc_listener.h" 10 #include "ipc/ipc_listener.h"
11 #include "ipc/mojo/ipc_channel_mojo_host.h"
12 #include "ipc/mojo/ipc_channel_mojo_readers.h" 11 #include "ipc/mojo/ipc_channel_mojo_readers.h"
13 #include "ipc/mojo/ipc_mojo_bootstrap.h" 12 #include "ipc/mojo/ipc_mojo_bootstrap.h"
14 #include "mojo/embedder/embedder.h" 13 #include "mojo/embedder/embedder.h"
15 14
16 #if defined(OS_POSIX) && !defined(OS_NACL) 15 #if defined(OS_POSIX) && !defined(OS_NACL)
17 #include "ipc/file_descriptor_set_posix.h" 16 #include "ipc/file_descriptor_set_posix.h"
18 #endif 17 #endif
19 18
20 namespace IPC { 19 namespace IPC {
21 20
22 namespace { 21 namespace {
23 22
24 class MojoChannelFactory : public ChannelFactory { 23 class MojoChannelFactory : public ChannelFactory {
25 public: 24 public:
26 MojoChannelFactory(ChannelMojoHost* host, 25 MojoChannelFactory(ChannelMojo::Delegate* delegate,
27 ChannelHandle channel_handle, 26 ChannelHandle channel_handle,
28 Channel::Mode mode) 27 Channel::Mode mode)
29 : host_(host), channel_handle_(channel_handle), mode_(mode) {} 28 : delegate_(delegate), channel_handle_(channel_handle), mode_(mode) {}
30 29
31 virtual std::string GetName() const OVERRIDE { 30 virtual std::string GetName() const OVERRIDE {
32 return channel_handle_.name; 31 return channel_handle_.name;
33 } 32 }
34 33
35 virtual scoped_ptr<Channel> BuildChannel(Listener* listener) OVERRIDE { 34 virtual scoped_ptr<Channel> BuildChannel(Listener* listener) OVERRIDE {
36 return ChannelMojo::Create(host_, channel_handle_, mode_, listener) 35 return ChannelMojo::Create(delegate_, channel_handle_, mode_, listener)
37 .PassAs<Channel>(); 36 .PassAs<Channel>();
38 } 37 }
39 38
40 private: 39 private:
41 ChannelMojoHost* host_; 40 ChannelMojo::Delegate* delegate_;
42 ChannelHandle channel_handle_; 41 ChannelHandle channel_handle_;
43 Channel::Mode mode_; 42 Channel::Mode mode_;
44 }; 43 };
45 44
46 } // namespace 45 } // namespace
47 46
48 //------------------------------------------------------------------------------ 47 //------------------------------------------------------------------------------
49 48
50 void ChannelMojo::ChannelInfoDeleter::operator()( 49 void ChannelMojo::ChannelInfoDeleter::operator()(
51 mojo::embedder::ChannelInfo* ptr) const { 50 mojo::embedder::ChannelInfo* ptr) const {
52 mojo::embedder::DestroyChannelOnIOThread(ptr); 51 mojo::embedder::DestroyChannelOnIOThread(ptr);
53 } 52 }
54 53
55 //------------------------------------------------------------------------------ 54 //------------------------------------------------------------------------------
56 55
57 // static 56 // static
58 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojoHost* host, 57 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate,
59 const ChannelHandle& channel_handle, 58 const ChannelHandle& channel_handle,
60 Mode mode, 59 Mode mode,
61 Listener* listener) { 60 Listener* listener) {
62 return make_scoped_ptr(new ChannelMojo(host, channel_handle, mode, listener)); 61 return make_scoped_ptr(
62 new ChannelMojo(delegate, channel_handle, mode, listener));
63 } 63 }
64 64
65 // static 65 // static
66 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( 66 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory(
67 ChannelMojoHost* host, 67 ChannelMojo::Delegate* delegate,
68 const ChannelHandle& channel_handle) { 68 const ChannelHandle& channel_handle) {
69 return make_scoped_ptr( 69 return make_scoped_ptr(new MojoChannelFactory(
70 new MojoChannelFactory(host, channel_handle, Channel::MODE_SERVER)) 70 delegate, channel_handle, Channel::MODE_SERVER))
71 .PassAs<ChannelFactory>(); 71 .PassAs<ChannelFactory>();
72 } 72 }
73 73
74 // static 74 // static
75 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( 75 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory(
76 const ChannelHandle& channel_handle) { 76 const ChannelHandle& channel_handle) {
77 return make_scoped_ptr( 77 return make_scoped_ptr(
78 new MojoChannelFactory(NULL, channel_handle, Channel::MODE_CLIENT)) 78 new MojoChannelFactory(NULL, channel_handle, Channel::MODE_CLIENT))
79 .PassAs<ChannelFactory>(); 79 .PassAs<ChannelFactory>();
80 } 80 }
81 81
82 ChannelMojo::ChannelMojo(ChannelMojoHost* host, 82 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate,
83 const ChannelHandle& handle, 83 const ChannelHandle& handle,
84 Mode mode, 84 Mode mode,
85 Listener* listener) 85 Listener* listener)
86 : host_(host), 86 : mode_(mode),
87 mode_(mode),
88 listener_(listener), 87 listener_(listener),
89 peer_pid_(base::kNullProcessId), 88 peer_pid_(base::kNullProcessId),
90 weak_factory_(this) { 89 weak_factory_(this) {
91 // Create MojoBootstrap after all members are set as it touches 90 // Create MojoBootstrap after all members are set as it touches
92 // ChannelMojo from a different thread. 91 // ChannelMojo from a different thread.
93 bootstrap_ = MojoBootstrap::Create(handle, mode, this); 92 bootstrap_ = MojoBootstrap::Create(handle, mode, this);
94 if (host_) 93 if (delegate) {
95 host_->OnChannelCreated(this); 94 if (delegate->GetIOTaskRunner() ==
95 base::MessageLoop::current()->message_loop_proxy()) {
96 InitDelegate(delegate);
97 } else {
98 delegate->GetIOTaskRunner()->PostTask(
99 FROM_HERE,
100 base::Bind(
101 &ChannelMojo::InitDelegate, base::Unretained(this), delegate));
102 }
103 }
96 } 104 }
97 105
98 ChannelMojo::~ChannelMojo() { 106 ChannelMojo::~ChannelMojo() {
99 Close(); 107 Close();
108 }
100 109
101 if (host_) 110 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) {
102 host_->OnChannelDestroyed(); 111 delegate_ = delegate->ToWeakPtr();
112 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr());
103 } 113 }
104 114
105 void ChannelMojo::InitControlReader( 115 void ChannelMojo::InitControlReader(
106 mojo::embedder::ScopedPlatformHandle handle) { 116 mojo::embedder::ScopedPlatformHandle handle) {
107 DCHECK(base::MessageLoopForIO::IsCurrent()); 117 DCHECK(base::MessageLoopForIO::IsCurrent());
108 mojo::embedder::ChannelInfo* channel_info; 118 mojo::embedder::ChannelInfo* channel_info;
109 mojo::ScopedMessagePipeHandle control_pipe = 119 mojo::ScopedMessagePipeHandle control_pipe =
110 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); 120 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info);
111 channel_info_.reset(channel_info); 121 channel_info_.reset(channel_info);
112 122
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 283
274 fdset->CommitAll(); 284 fdset->CommitAll();
275 } 285 }
276 286
277 return MOJO_RESULT_OK; 287 return MOJO_RESULT_OK;
278 } 288 }
279 289
280 #endif // defined(OS_POSIX) && !defined(OS_NACL) 290 #endif // defined(OS_POSIX) && !defined(OS_NACL)
281 291
282 } // namespace IPC 292 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/mojo/ipc_channel_mojo.h ('k') | ipc/mojo/ipc_channel_mojo_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698