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

Unified Diff: mojo/common/data_pipe_utils.cc

Issue 323593002: Mojo: Use network service to load non-local Mojo Apps. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/common/data_pipe_utils.h ('k') | mojo/mojo.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/common/data_pipe_utils.cc
diff --git a/mojo/common/data_pipe_utils.cc b/mojo/common/data_pipe_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2722bc3c2176b511c2e9e4af131be2a034b23d57
--- /dev/null
+++ b/mojo/common/data_pipe_utils.cc
@@ -0,0 +1,72 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/common/data_pipe_utils.h"
+
+#include <stdio.h>
+
+#include "base/file_util.h"
+#include "base/files/file_path.h"
+#include "base/files/scoped_file.h"
+#include "base/message_loop/message_loop.h"
+#include "base/task_runner_util.h"
+#include "mojo/common/handle_watcher.h"
+
+namespace mojo {
+namespace common {
+
+bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source,
+ const base::FilePath& destination) {
+ base::ScopedFILE fp(base::OpenFile(destination, "wb"));
+ if (!fp)
+ return false;
+
+ for (;;) {
+ const void* buffer;
+ uint32_t num_bytes;
+ MojoResult result = BeginReadDataRaw(source.get(), &buffer, &num_bytes,
+ MOJO_READ_DATA_FLAG_NONE);
+ if (result == MOJO_RESULT_OK) {
+ fwrite(buffer, 1, num_bytes, fp.get());
+ result = EndReadDataRaw(source.get(), num_bytes);
+ if (result != MOJO_RESULT_OK)
+ return false;
+ } else if (result == MOJO_RESULT_SHOULD_WAIT) {
+ result = Wait(source.get(),
+ MOJO_WAIT_FLAG_READABLE,
+ MOJO_DEADLINE_INDEFINITE);
+ if (result != MOJO_RESULT_OK) {
+ // If the producer handle was closed, then treat as EOF.
+ return result == MOJO_RESULT_FAILED_PRECONDITION;
+ }
+ } else if (result == MOJO_RESULT_FAILED_PRECONDITION) {
+ // If the producer handle was closed, then treat as EOF.
+ return true;
+ } else {
+ // Some other error occurred.
+ break;
+ }
+ }
+
+ return false;
+}
+
+void CompleteBlockingCopyToFile(const base::Callback<void(bool)>& callback,
+ bool result) {
+ callback.Run(result);
+}
+
+void CopyToFile(ScopedDataPipeConsumerHandle source,
+ const base::FilePath& destination,
+ base::TaskRunner* task_runner,
+ const base::Callback<void(bool)>& callback) {
+ base::PostTaskAndReplyWithResult(
+ task_runner,
+ FROM_HERE,
+ base::Bind(&BlockingCopyToFile, base::Passed(&source), destination),
+ callback);
+}
+
+} // namespace common
+} // namespace mojo
« no previous file with comments | « mojo/common/data_pipe_utils.h ('k') | mojo/mojo.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698