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

Side by Side Diff: mojo/edk/system/parent_token_serializer_win.cc

Issue 1387963004: Create a broker interface for the new Mojo EDK so that the browser can create and duplicate messa... (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: presubmit whitespace error Created 5 years 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
OLDNEW
(Empty)
1 // Copyright 2015 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 "mojo/edk/system/parent_token_serializer_win.h"
6
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "mojo/edk/embedder/platform_channel_pair.h"
10 #include "mojo/edk/system/configuration.h"
11 #include "mojo/edk/system/parent_token_serializer_state_win.h"
12 #include "mojo/edk/system/token_serializer_messages_win.h"
13
14 namespace mojo {
15 namespace edk {
16
17 namespace {
18 static const int kDefaultReadBufferSize = 256;
19 }
20
21 ParentTokenSerializer::ParentTokenSerializer(HANDLE child_process,
22 ScopedPlatformHandle pipe)
23 : child_process_(child_process),
24 pipe_(pipe.Pass()),
25 num_bytes_read_(0) {
26 memset(&read_context_.overlapped, 0, sizeof(read_context_.overlapped));
27 read_context_.handler = this;
28 memset(&write_context_.overlapped, 0, sizeof(write_context_.overlapped));
29 write_context_.handler = this;
30
31 read_data_.resize(kDefaultReadBufferSize);
32 ParentTokenSerializerState::GetInstance()->token_serialize_thread()->PostTask(
33 FROM_HERE,
34 base::Bind(&ParentTokenSerializer::RegisterIOHandler,
35 base::Unretained(this)));
36 }
37
38 ParentTokenSerializer::~ParentTokenSerializer() {
39 }
40
41 void ParentTokenSerializer::RegisterIOHandler() {
42 base::MessageLoopForIO::current()->RegisterIOHandler(
43 pipe_.get().handle, this);
44 BeginRead();
45 }
46
47 void ParentTokenSerializer::BeginRead() {
48 BOOL rv = ReadFile(pipe_.get().handle, &read_data_[num_bytes_read_],
49 static_cast<int>(read_data_.size() - num_bytes_read_),
50 nullptr, &read_context_.overlapped);
51 if (rv || GetLastError() == ERROR_IO_PENDING)
52 return;
53
54 if (rv == ERROR_BROKEN_PIPE) {
55 delete this;
56 return;
57 }
58
59 NOTREACHED() << "Unknown error in ParentTokenSerializer " << rv;
60 }
61
62 void ParentTokenSerializer::OnIOCompleted(
63 base::MessageLoopForIO::IOContext* context,
64 DWORD bytes_transferred,
65 DWORD error) {
66 if (context != &read_context_)
67 return;
68
69 if (error == ERROR_BROKEN_PIPE) {
70 delete this;
71 return; // Child process exited or crashed.
72 }
73
74 if (error != ERROR_SUCCESS) {
75 NOTREACHED() << "Error " << error << " in ParentTokenSerializer.";
76 delete this;
77 return;
78 }
79
80 num_bytes_read_ += bytes_transferred;
81 CHECK_GE(num_bytes_read_, sizeof(uint32_t));
82 TokenSerializerMessage* message =
83 reinterpret_cast<TokenSerializerMessage*>(&read_data_[0]);
84 if (num_bytes_read_ < message->size) {
85 read_data_.resize(message->size);
86 BeginRead();
87 return;
88 }
89
90 if (message->id == CREATE_PLATFORM_CHANNEL_PAIR) {
91 PlatformChannelPair channel_pair;
92 uint32_t response_size = 2 * sizeof(HANDLE);
93 write_data_.resize(response_size);
94 HANDLE* handles = reinterpret_cast<HANDLE*>(&write_data_[0]);
95 handles[0] = DuplicateToChild(
96 channel_pair.PassServerHandle().release().handle);
97 handles[1] = DuplicateToChild(
98 channel_pair.PassClientHandle().release().handle);
99 } else if (message->id == HANDLE_TO_TOKEN) {
100 uint32_t count =
101 (message->size - kTokenSerializerMessageHeaderSize) / sizeof(HANDLE);
102 if (count > GetConfiguration().max_message_num_handles) {
103 NOTREACHED() << "Too many handles from child process. Closing channel.";
104 delete this;
105 return;
106 }
107 uint32_t response_size = count * sizeof(uint64_t);
108 write_data_.resize(response_size);
109 uint64_t* tokens = reinterpret_cast<uint64_t*>(&write_data_[0]);
110 std::vector<PlatformHandle> duplicated_handles(count);
111 for (uint32_t i = 0; i < count; ++i) {
112 duplicated_handles[i] =
113 PlatformHandle(DuplicateFromChild(message->handles[i]));
114 }
115 ParentTokenSerializerState::GetInstance()->HandleToToken(
116 &duplicated_handles[0], count, tokens);
117 } else if (message->id == TOKEN_TO_HANDLE) {
118 uint32_t count =
119 (message->size - kTokenSerializerMessageHeaderSize) /
120 sizeof(uint64_t);
121 if (count > GetConfiguration().max_message_num_handles) {
122 NOTREACHED() << "Too many tokens from child process. Closing channel.";
123 delete this;
124 return;
125 }
126 uint32_t response_size = count * sizeof(HANDLE);
127 write_data_.resize(response_size);
128 HANDLE* handles = reinterpret_cast<HANDLE*>(&write_data_[0]);
129 std::vector<PlatformHandle> temp_handles(count);
130 ParentTokenSerializerState::GetInstance()->TokenToHandle(
131 &message->tokens[0], count, &temp_handles[0]);
132 for (uint32_t i = 0; i < count; ++i) {
133 if (temp_handles[i].is_valid()) {
134 handles[i] = DuplicateToChild(temp_handles[i].handle);
135 } else {
136 NOTREACHED() << "Unknown token";
137 handles[i] = INVALID_HANDLE_VALUE;
138 }
139 }
140 } else {
141 NOTREACHED() << "Unknown command. Stopping reading.";
142 delete this;
143 return;
144 }
145
146 BOOL rv = WriteFile(pipe_.get().handle, &write_data_[0],
147 static_cast<int>(write_data_.size()), NULL,
148 &write_context_.overlapped);
149 DCHECK(rv || GetLastError() == ERROR_IO_PENDING);
150
151 // Start reading again.
152 num_bytes_read_ = 0;
153 BeginRead();
154 }
155
156
157 HANDLE ParentTokenSerializer::DuplicateToChild(HANDLE handle) {
158 HANDLE rv = INVALID_HANDLE_VALUE;
159 BOOL result = DuplicateHandle(base::GetCurrentProcessHandle(), handle,
160 child_process_, &rv, 0, FALSE,
161 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
162 DCHECK(result);
163 return rv;
164 }
165
166 HANDLE ParentTokenSerializer::DuplicateFromChild(HANDLE handle) {
167 HANDLE rv = INVALID_HANDLE_VALUE;
168 BOOL result = DuplicateHandle(child_process_, handle,
169 base::GetCurrentProcessHandle(), &rv, 0, FALSE,
170 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
171 DCHECK(result);
172 return rv;
173 }
174
175 } // namespace edk
176 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698