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

Unified Diff: services/native_support/redirectors.h

Issue 1321253010: Add a "native_support" service. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: fix android? Created 5 years, 3 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 | « services/native_support/process_test_base.cc ('k') | services/native_support/redirectors.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: services/native_support/redirectors.h
diff --git a/services/native_support/redirectors.h b/services/native_support/redirectors.h
new file mode 100644
index 0000000000000000000000000000000000000000..a88eaea0e893747fb09c763fc42e9afb3b37cba8
--- /dev/null
+++ b/services/native_support/redirectors.h
@@ -0,0 +1,84 @@
+// Copyright 2015 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.
+
+#ifndef SERVICES_NATIVE_SUPPORT_REDIRECTORS_H_
+#define SERVICES_NATIVE_SUPPORT_REDIRECTORS_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <memory>
+
+#include "base/macros.h"
+#include "base/memory/weak_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "mojo/public/cpp/bindings/array.h"
+#include "mojo/services/files/public/interfaces/file.mojom.h"
+#include "mojo/services/files/public/interfaces/types.mojom.h"
+
+namespace native_support {
+
+// Reads from an FD (as possible without blocking) and writes the result to a
+// |mojo::files::File*|. This object must be used on an I/O thread.
+class FDToMojoFileRedirector : public base::MessageLoopForIO::Watcher {
+ public:
+ // Both |fd| and |file| must outlive this object.
+ FDToMojoFileRedirector(int fd, mojo::files::File* file, size_t buffer_size);
+ ~FDToMojoFileRedirector() override;
+
+ void Start();
+ void Stop();
+
+ private:
+ // |base::MessageLoopForIO::Watcher| implementation:
+ void OnFileCanReadWithoutBlocking(int fd) override;
+ void OnFileCanWriteWithoutBlocking(int fd) override;
+
+ void DoWrite();
+ void DidWrite(mojo::files::Error error, uint32_t num_bytes_written);
+
+ const int fd_;
+ mojo::files::File* const file_;
+ const size_t buffer_size_;
+ std::unique_ptr<char[]> buffer_;
+ base::MessageLoopForIO::FileDescriptorWatcher watcher_;
+
+ size_t num_bytes_;
+ size_t offset_;
+
+ base::WeakPtrFactory<FDToMojoFileRedirector> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(FDToMojoFileRedirector);
+};
+
+// Reads from a |mojo::files::File*| and writes the result to an FD.
+// TODO(vtl): This is very cheesy for now. It just sets |fd| to non-blocking,
+// and assumes that we'll never block on writing to it. (This is mostly "OK" for
+// interactive input redirection.)
+class MojoFileToFDRedirector {
+ public:
+ // Both |file| and |fd| must outlive this object.
+ MojoFileToFDRedirector(mojo::files::File* file, int fd, size_t buffer_size);
+ ~MojoFileToFDRedirector();
+
+ void Start();
+ void Stop();
+
+ private:
+ void DidRead(mojo::files::Error error, mojo::Array<uint8_t> bytes_read);
+
+ mojo::files::File* const file_;
+ const int fd_;
+ const size_t buffer_size_;
+ bool running_;
+ bool read_pending_;
+
+ base::WeakPtrFactory<MojoFileToFDRedirector> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(MojoFileToFDRedirector);
+};
+
+} // namespace native_support
+
+#endif // SERVICES_NATIVE_SUPPORT_REDIRECTORS_H_
« no previous file with comments | « services/native_support/process_test_base.cc ('k') | services/native_support/redirectors.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698