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

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

Issue 467263006: JavaScript Content Handler Version 0.0 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use chromium application runner Created 6 years, 4 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
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/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.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 14
15 namespace mojo { 15 namespace mojo {
16 namespace common { 16 namespace common {
17 namespace {
17 18
18 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, 19 bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source,
Aaron Boodman 2014/08/25 17:25:37 I think the callback-based refactor makes sense, b
19 const base::FilePath& destination) { 20 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) {
20 base::ScopedFILE fp(base::OpenFile(destination, "wb"));
21 if (!fp)
22 return false;
23
24 for (;;) { 21 for (;;) {
25 const void* buffer; 22 const void* buffer;
26 uint32_t num_bytes; 23 uint32_t num_bytes;
27 MojoResult result = BeginReadDataRaw(source.get(), &buffer, &num_bytes, 24 MojoResult result = BeginReadDataRaw(
28 MOJO_READ_DATA_FLAG_NONE); 25 source.get(), &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE);
29 if (result == MOJO_RESULT_OK) { 26 if (result == MOJO_RESULT_OK) {
30 size_t bytes_written = fwrite(buffer, 1, num_bytes, fp.get()); 27 size_t bytes_written = write_bytes.Run(buffer, num_bytes);
31 result = EndReadDataRaw(source.get(), num_bytes); 28 result = EndReadDataRaw(source.get(), num_bytes);
32 if (bytes_written < num_bytes || result != MOJO_RESULT_OK) 29 if (bytes_written < num_bytes || result != MOJO_RESULT_OK)
33 return false; 30 return false;
34 } else if (result == MOJO_RESULT_SHOULD_WAIT) { 31 } else if (result == MOJO_RESULT_SHOULD_WAIT) {
35 result = Wait(source.get(), 32 result = Wait(source.get(),
36 MOJO_HANDLE_SIGNAL_READABLE, 33 MOJO_HANDLE_SIGNAL_READABLE,
37 MOJO_DEADLINE_INDEFINITE); 34 MOJO_DEADLINE_INDEFINITE);
38 if (result != MOJO_RESULT_OK) { 35 if (result != MOJO_RESULT_OK) {
39 // If the producer handle was closed, then treat as EOF. 36 // If the producer handle was closed, then treat as EOF.
40 return result == MOJO_RESULT_FAILED_PRECONDITION; 37 return result == MOJO_RESULT_FAILED_PRECONDITION;
41 } 38 }
42 } else if (result == MOJO_RESULT_FAILED_PRECONDITION) { 39 } else if (result == MOJO_RESULT_FAILED_PRECONDITION) {
43 // If the producer handle was closed, then treat as EOF. 40 // If the producer handle was closed, then treat as EOF.
44 return true; 41 return true;
45 } else { 42 } else {
46 // Some other error occurred. 43 // Some other error occurred.
47 break; 44 break;
48 } 45 }
49 } 46 }
50 47
51 return false; 48 return false;
52 } 49 }
53 50
51 size_t CopyToStringHelper(
darin (slow to review) 2014/08/26 05:23:22 nit: Append{Bytes}ToString ?
hansmuller 2014/08/26 23:39:12 I thought there was some value in repeating most o
52 std::string* result, const void* buffer, uint32_t num_bytes) {
53 if (result)
54 result->append(static_cast<const char*>(buffer), num_bytes);
55 return num_bytes;
56 }
57
58 size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) {
darin (slow to review) 2014/08/26 05:23:23 nit: Append{Bytes}ToFile ?
hansmuller 2014/08/26 23:39:12 [see previous comment]
59 return fwrite(buffer, 1, num_bytes, fp);
60 }
61
62 } // namespace
63
64
65 // Similar to base::ReadFileToString().
66 // TODO(hansmuller): Add a max_size parameter.
67 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source,
68 std::string* result) {
69 if (result)
Aaron Boodman 2014/08/25 17:25:38 Is there a reason to not pass |result|, or is this
hansmuller 2014/08/26 23:39:12 I was emulating base::ReadFileToString() which al
Aaron Boodman 2014/08/27 05:55:53 OK, that functions gives the example of priming a
70 result->clear();
71 return BlockingCopyHelper(
72 source.Pass(), base::Bind(&CopyToStringHelper, result));
73 }
74
75 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source,
76 const base::FilePath& destination) {
77 base::ScopedFILE fp(base::OpenFile(destination, "wb"));
78 if (!fp)
79 return false;
80 return BlockingCopyHelper(
81 source.Pass(), base::Bind(&CopyToFileHelper, fp.get()));
82 }
83
84 // TODO(hansmuller): Should this be deleted?
54 void CompleteBlockingCopyToFile(const base::Callback<void(bool)>& callback, 85 void CompleteBlockingCopyToFile(const base::Callback<void(bool)>& callback,
Aaron Boodman 2014/08/25 17:25:38 Yeah, looks like dead code.
hansmuller 2014/08/26 23:39:12 OK, I deleted it.
55 bool result) { 86 bool result) {
56 callback.Run(result); 87 callback.Run(result);
57 } 88 }
58 89
59 void CopyToFile(ScopedDataPipeConsumerHandle source, 90 void CopyToFile(ScopedDataPipeConsumerHandle source,
60 const base::FilePath& destination, 91 const base::FilePath& destination,
61 base::TaskRunner* task_runner, 92 base::TaskRunner* task_runner,
62 const base::Callback<void(bool)>& callback) { 93 const base::Callback<void(bool)>& callback) {
63 base::PostTaskAndReplyWithResult( 94 base::PostTaskAndReplyWithResult(
64 task_runner, 95 task_runner,
65 FROM_HERE, 96 FROM_HERE,
66 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), 97 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination),
67 callback); 98 callback);
68 } 99 }
69 100
70 } // namespace common 101 } // namespace common
71 } // namespace mojo 102 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698