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

Side by Side Diff: mojo/common/data_pipe_utils.cc

Issue 694303002: Allow local file to run though content handler. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Small refactor Created 6 years, 1 month 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
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 "mojo/common/data_pipe_utils.h" 5 #include "mojo/common/data_pipe_utils.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/files/scoped_file.h" 11 #include "base/files/scoped_file.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/task_runner_util.h" 13 #include "base/task_runner_util.h"
14 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
15 15
16 namespace mojo { 16 namespace mojo {
17 namespace common { 17 namespace common {
18 namespace { 18 namespace {
19 19
20 bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source, 20 bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source,
21 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) { 21 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) {
22 for (;;) { 22 for (;;) {
23 const void* buffer; 23 const void* buffer = nullptr;
24 uint32_t num_bytes; 24 uint32_t num_bytes = 0;
25 MojoResult result = BeginReadDataRaw( 25 MojoResult result = BeginReadDataRaw(
26 source.get(), &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE); 26 source.get(), &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE);
27 if (result == MOJO_RESULT_OK) { 27 if (result == MOJO_RESULT_OK) {
28 size_t bytes_written = write_bytes.Run(buffer, num_bytes); 28 size_t bytes_written = write_bytes.Run(buffer, num_bytes);
29 result = EndReadDataRaw(source.get(), num_bytes); 29 result = EndReadDataRaw(source.get(), num_bytes);
30 if (bytes_written < num_bytes || result != MOJO_RESULT_OK) 30 if (bytes_written < num_bytes || result != MOJO_RESULT_OK)
31 return false; 31 return false;
32 } else if (result == MOJO_RESULT_SHOULD_WAIT) { 32 } else if (result == MOJO_RESULT_SHOULD_WAIT) {
33 result = Wait(source.get(), 33 result = Wait(source.get(),
34 MOJO_HANDLE_SIGNAL_READABLE, 34 MOJO_HANDLE_SIGNAL_READABLE,
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 uint32 buffer_num_bytes, 166 uint32 buffer_num_bytes,
167 std::string* bytes) { 167 std::string* bytes) {
168 if (buffer_num_bytes >= bytes_length) { 168 if (buffer_num_bytes >= bytes_length) {
169 const char* p = static_cast<const char*>(buffer); 169 const char* p = static_cast<const char*>(buffer);
170 *bytes = std::string(p, bytes_length); 170 *bytes = std::string(p, bytes_length);
171 return PeekStatus::kSuccess; 171 return PeekStatus::kSuccess;
172 } 172 }
173 return PeekStatus::kKeepReading; 173 return PeekStatus::kKeepReading;
174 } 174 }
175 175
176 bool BlockingCopyFromFile(const base::FilePath& source,
177 ScopedDataPipeProducerHandle destination,
178 uint32_t skip) {
179 base::File file(source, base::File::FLAG_OPEN | base::File::FLAG_READ);
180 if (!file.IsValid())
181 return false;
182 if (file.Seek(base::File::FROM_BEGIN, skip) != skip) {
183 return false;
184 }
185 for (;;) {
186 void* buffer;
187 uint32_t buffer_num_bytes;
188 MojoResult result = BeginWriteDataRaw(destination.get(),
189 &buffer,
190 &buffer_num_bytes,
191 MOJO_WRITE_DATA_FLAG_NONE);
192 if (result == MOJO_RESULT_OK) {
193 int bytes_read =
194 file.ReadAtCurrentPos(static_cast<char*>(buffer), buffer_num_bytes);
195 if (bytes_read >= 0) {
196 EndWriteDataRaw(destination.get(), bytes_read);
197 if (bytes_read == 0) {
198 // eof
199 return true;
200 }
201 } else {
202 // error
203 EndWriteDataRaw(destination.get(), 0);
204 return false;
205 }
206 } else if (result == MOJO_RESULT_SHOULD_WAIT) {
207 result = Wait(destination.get(),
208 MOJO_HANDLE_SIGNAL_WRITABLE,
209 MOJO_DEADLINE_INDEFINITE);
210 if (result != MOJO_RESULT_OK) {
211 // If the consumer handle was closed, then treat as EOF.
212 return result == MOJO_RESULT_FAILED_PRECONDITION;
213 }
214 } else {
215 // If the consumer handle was closed, then treat as EOF.
216 return result == MOJO_RESULT_FAILED_PRECONDITION;
217 }
218 }
219 NOTREACHED();
220 return false;
221 }
222
176 } // namespace 223 } // namespace
177 224
178 bool BlockingPeekNBytes(DataPipeConsumerHandle source, 225 bool BlockingPeekNBytes(DataPipeConsumerHandle source,
179 std::string* bytes, 226 std::string* bytes,
180 size_t bytes_length, 227 size_t bytes_length,
181 MojoDeadline timeout) { 228 MojoDeadline timeout) {
182 PeekFunc peek_nbytes = base::Bind(PeekNBytes, bytes_length); 229 PeekFunc peek_nbytes = base::Bind(PeekNBytes, bytes_length);
183 return BlockingPeekHelper(source, bytes, timeout, peek_nbytes); 230 return BlockingPeekHelper(source, bytes, timeout, peek_nbytes);
184 } 231 }
185 232
(...skipping 27 matching lines...) Expand all
213 const base::FilePath& destination, 260 const base::FilePath& destination,
214 base::TaskRunner* task_runner, 261 base::TaskRunner* task_runner,
215 const base::Callback<void(bool)>& callback) { 262 const base::Callback<void(bool)>& callback) {
216 base::PostTaskAndReplyWithResult( 263 base::PostTaskAndReplyWithResult(
217 task_runner, 264 task_runner,
218 FROM_HERE, 265 FROM_HERE,
219 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), 266 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination),
220 callback); 267 callback);
221 } 268 }
222 269
270 void CopyFromFile(const base::FilePath& source,
271 ScopedDataPipeProducerHandle destination,
272 uint32_t skip,
273 base::TaskRunner* task_runner,
274 const base::Callback<void(bool)>& callback) {
275 base::PostTaskAndReplyWithResult(
276 task_runner,
277 FROM_HERE,
278 base::Bind(
279 &BlockingCopyFromFile, source, base::Passed(&destination), skip),
280 callback);
281 }
282
223 } // namespace common 283 } // namespace common
224 } // namespace mojo 284 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698