OLD | NEW |
---|---|
(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 // This is a helper class for implementing a |mojo::files::File| that behaves | |
6 // like an "output stream" ("output" from the point of view of the client -- | |
7 // i.e., the client can write/stream output to it, but not read or seek). | |
8 | |
9 #ifndef MOJO_SERVICES_FILES_PUBLIC_CPP_OUTPUT_STREAM_FILE_H_ | |
10 #define MOJO_SERVICES_FILES_PUBLIC_CPP_OUTPUT_STREAM_FILE_H_ | |
11 | |
12 #include <stddef.h> | |
13 | |
14 #include <memory> | |
15 | |
16 #include "mojo/public/cpp/bindings/binding.h" | |
17 #include "mojo/public/cpp/bindings/interface_request.h" | |
18 #include "mojo/public/cpp/system/macros.h" | |
19 #include "mojo/services/files/public/interfaces/file.mojom.h" | |
20 #include "mojo/services/files/public/interfaces/types.mojom.h" | |
21 | |
22 class OutputStreamFile : public mojo::files::File { | |
vardhan
2015/09/25 17:19:15
should this be under a mojo namespace? (or like mo
| |
23 public: | |
24 // The |Client| receives data written to the stream "file" as well as other | |
25 // notifications (e.g., of the "file" being closed). From any of the methods | |
26 // below, the client may choose to destroy the |OutputStreamFile|. | |
27 class Client { | |
28 public: | |
29 // Called when we receive data from the stream "file". | |
30 // TODO(vtl): Maybe add a way to throttle (e.g., for the client to not | |
31 // accept all the data). | |
32 virtual void OnDataReceived(const void* bytes, size_t num_bytes) = 0; | |
33 | |
34 // Called when the stream "file" is closed, via |Close()| or due to the | |
35 // other end of the message pipe being closed. (This will not be called due | |
36 // the |OutputStreamFile| being destroyed.) | |
37 virtual void OnClosed() = 0; | |
38 | |
39 protected: | |
40 virtual ~Client() {} | |
41 }; | |
42 | |
43 // Static factory method. |client| may be null, but if not it should typically | |
44 // outlive us (see |set_client()|). | |
45 static std::unique_ptr<OutputStreamFile> Create( | |
46 Client* client, | |
47 mojo::InterfaceRequest<mojo::files::File> request); | |
48 | |
49 ~OutputStreamFile() override; | |
50 | |
51 // Sets the client (which may be null, in which case all |Write()|s to the | |
52 // stream "file" will just succeed). If non-null, |client| must be valid | |
53 // whenever the run (a.k.a. message) loop is run, i.e., whenever a client | |
54 // method may be called. | |
55 void set_client(Client* client) { client_ = client; } | |
56 | |
57 private: | |
58 OutputStreamFile(Client* client, | |
59 mojo::InterfaceRequest<mojo::files::File> request); | |
60 | |
61 // We should only be deleted by "ourself" (via the strong binding). | |
62 friend class mojo::Binding<mojo::files::File>; | |
63 | |
64 // |mojo::files::File| implementation: | |
65 void Close(const CloseCallback& callback) override; | |
66 void Read(uint32_t num_bytes_to_read, | |
67 int64_t offset, | |
68 mojo::files::Whence whence, | |
69 const ReadCallback& callback) override; | |
70 void Write(mojo::Array<uint8_t> bytes_to_write, | |
71 int64_t offset, | |
72 mojo::files::Whence whence, | |
73 const WriteCallback& callback) override; | |
74 void ReadToStream(mojo::ScopedDataPipeProducerHandle source, | |
75 int64_t offset, | |
76 mojo::files::Whence whence, | |
77 int64_t num_bytes_to_read, | |
78 const ReadToStreamCallback& callback) override; | |
79 void WriteFromStream(mojo::ScopedDataPipeConsumerHandle sink, | |
80 int64_t offset, | |
81 mojo::files::Whence whence, | |
82 const WriteFromStreamCallback& callback) override; | |
83 void Tell(const TellCallback& callback) override; | |
84 void Seek(int64_t offset, | |
85 mojo::files::Whence whence, | |
86 const SeekCallback& callback) override; | |
87 void Stat(const StatCallback& callback) override; | |
88 void Truncate(int64_t size, const TruncateCallback& callback) override; | |
89 void Touch(mojo::files::TimespecOrNowPtr atime, | |
90 mojo::files::TimespecOrNowPtr mtime, | |
91 const TouchCallback& callback) override; | |
92 void Dup(mojo::InterfaceRequest<mojo::files::File> file, | |
93 const DupCallback& callback) override; | |
94 void Reopen(mojo::InterfaceRequest<mojo::files::File> file, | |
95 uint32_t open_flags, | |
96 const ReopenCallback& callback) override; | |
97 void AsBuffer(const AsBufferCallback& callback) override; | |
98 void Ioctl(uint32_t request, | |
99 mojo::Array<uint32_t> in_values, | |
100 const IoctlCallback& callback) override; | |
101 | |
102 Client* client_; | |
103 bool is_closed_; | |
104 | |
105 mojo::Binding<mojo::files::File> binding_; | |
106 | |
107 MOJO_DISALLOW_COPY_AND_ASSIGN(OutputStreamFile); | |
108 }; | |
109 | |
110 #endif // MOJO_SERVICES_FILES_PUBLIC_CPP_OUTPUT_STREAM_FILE_H_ | |
OLD | NEW |