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

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

Issue 382333002: Introduce ChannelMojo (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing Mac build failure Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 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 "ipc/mojo/ipc_message_pipe_reader.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "mojo/public/cpp/environment/environment.h"
13
14 namespace IPC {
15 namespace internal {
16
17 MessagePipeReader::MessagePipeReader()
18 : pipe_wait_id_(0) {
19 }
20
21 MessagePipeReader::~MessagePipeReader() {
22 Close();
23 }
24
25 void MessagePipeReader::SetPipe(mojo::ScopedMessagePipeHandle handle) {
26 DCHECK(!pipe_.is_valid());
27 pipe_ = handle.Pass();
28 StartWaiting();
29 }
30
31 void MessagePipeReader::Close() {
32 StopWaiting();
33 pipe_.reset();
34 OnPipeClosed();
35 }
36
37 void MessagePipeReader::CloseWithError(MojoResult error) {
38 OnPipeError(error);
39 Close();
40 }
41
42 // static
43 void MessagePipeReader::InvokePipeIsReady(void* closure, MojoResult result) {
44 reinterpret_cast<MessagePipeReader*>(closure)->PipeIsReady(result);
45 }
46
47 void MessagePipeReader::StartWaiting() {
48 DCHECK(pipe_.is_valid());
49 DCHECK(!pipe_wait_id_);
50 // Not using MOJO_HANDLE_SIGNAL_WRITABLE here, expecting buffer in
51 // MessagePipe.
52 //
53 // TODO(morrita): Should we re-set the signal when we get new
54 // message to send?
55 pipe_wait_id_ = mojo::Environment::GetDefaultAsyncWaiter()->AsyncWait(
56 pipe_.get().value(),
57 MOJO_HANDLE_SIGNAL_READABLE,
58 MOJO_DEADLINE_INDEFINITE,
59 &InvokePipeIsReady,
60 this);
61 }
62
63 void MessagePipeReader::StopWaiting() {
64 if (!pipe_wait_id_)
65 return;
66 mojo::Environment::GetDefaultAsyncWaiter()->CancelWait(pipe_wait_id_);
67 pipe_wait_id_ = 0;
68 }
69
70 void MessagePipeReader::PipeIsReady(MojoResult wait_result) {
71 pipe_wait_id_ = 0;
72
73 if (wait_result != MOJO_RESULT_OK) {
74 // FAILED_PRECONDITION happens when the pipe is
75 // closed before the waiter is scheduled in a backend thread.
76 if (wait_result != MOJO_RESULT_ABORTED &&
77 wait_result != MOJO_RESULT_FAILED_PRECONDITION) {
78 DLOG(WARNING) << "Pipe got error from the waiter. Closing: "
79 << wait_result;
80 OnPipeError(wait_result);
81 }
82
83 Close();
84 return;
85 }
86
87 while (pipe_.is_valid()) {
88 MojoResult read_result = ReadMessageBytes();
89 if (read_result == MOJO_RESULT_SHOULD_WAIT)
90 break;
91 if (read_result != MOJO_RESULT_OK) {
92 // FAILED_PRECONDITION means that all the received messages
93 // got consumed and the peer is already closed.
94 if (read_result != MOJO_RESULT_FAILED_PRECONDITION) {
95 DLOG(WARNING)
96 << "Pipe got error from ReadMessage(). Closing: " << read_result;
97 OnPipeError(read_result);
98 }
99
100 Close();
101 break;
102 }
103
104 OnMessageArrived();
105 }
106
107 if (pipe_.is_valid())
108 StartWaiting();
109 }
110
111 MojoResult MessagePipeReader::ReadMessageBytes() {
112 uint32_t num_bytes = data_buffer_.size();
113 uint32_t num_handles = 0; // XXX: handle_buffer_.size()
114 MojoResult result = MojoReadMessage(pipe_.get().value(),
115 data_buffer_.data(), &num_bytes,
116 handle_buffer_.data(), &num_handles,
117 MOJO_READ_MESSAGE_FLAG_NONE);
118 data_buffer_.resize(num_bytes);
119 handle_buffer_.resize(num_handles);
120 if (result == MOJO_RESULT_RESOURCE_EXHAUSTED) {
121 // MOJO_RESULT_RESOURCE_EXHAUSTED was asking the caller that
122 // it needs more bufer. So we re-read it with resized buffers.
123 result = MojoReadMessage(pipe_.get().value(),
124 data_buffer_.data(), &num_bytes,
125 handle_buffer_.data(), &num_handles,
126 MOJO_READ_MESSAGE_FLAG_NONE);
127 }
128
129 DCHECK(0 == num_bytes || data_buffer_.size() == num_bytes);
130 DCHECK(0 == num_handles || handle_buffer_.size() == num_handles);
131 return result;
132 }
133
134 void MessagePipeReader::DelayedDeleter::operator()(
135 MessagePipeReader* ptr) const {
136 ptr->Close();
137 base::MessageLoopProxy::current()->PostTask(
138 FROM_HERE, base::Bind(&DeleteNow, ptr));
139 }
140
141 } // namespace internal
142 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698