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

Side by Side Diff: content/browser/download/byte_stream.h

Issue 10244001: Creation of ByteStream class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Incorporated comments and fixed a few callback problems. Created 8 years, 7 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
(Empty)
1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_
6 #define CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_
7 #pragma once
8
9 #include <set>
10 #include <utility>
11 #include <deque>
12
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/synchronization/lock.h"
16 #include "content/public/browser/download_interrupt_reasons.h"
17 #include "net/base/io_buffer.h"
18
19 namespace base {
20 class SequencedTaskRunner;
21 }
22
23 namespace content {
24
25 // A byte stream is a pipe to transfer bytes between a source and a
26 // sink, which may be on different threads. It is intended to be the
27 // only connection between source and sink; they need have no
28 // awareness of each other aside from the byte stream. The source and
29 // the sink have different interfaces to a byte stream, |ByteStreamInput|
30 // and |ByteStreamOutput|. A pair of connected interfaces is generated by
31 // calling |CreateByteStream|.
32 //
33 // The source adds bytes to the bytestream via |ByteStreamInput::Write|
34 // and the sync retrieves bytes already written via |ByteStreamOutput::Read|.
benjhayden 2012/05/16 14:26:25 s/sync/sink/g?
Randy Smith (Not in Mondays) 2012/05/16 20:30:16 Done.
35 //
36 // When the source has no more data to add, it will call
37 // |ByteStreamInput::Close| to indicate that. Errors at the source
38 // are indicated to the sync via a non-DOWNLOAD_INTERRUPT_REASON_NONE code.
39 //
40 // Normally the source is not managed after the relationship is setup;
41 // it is expected to provide data and then close itself. If an error
42 // occurs on the sink, it is not signalled to the source via this
43 // mechanism; instead, the source will write data until it exausts the
44 // available space. Instead, it is the responsibility of the sink,
45 // usually through notifying whatever controller setup the
46 // relationship, to signal the source in some other fashion.
benjhayden 2012/05/16 14:26:25 Does this contradict the second sentence of the fi
Randy Smith (Not in Mondays) 2012/05/16 20:30:16 Modified the text a bit. My intention is that the
47 //
48 // Callback lifetime management: No lifetime management is done in this
49 // class to prevent registered callbacks from being called after any
50 // objects to which they may refer have been destroyed. It is the
51 // responsibility of the callers to avoid use-after-free references.
52 // This may be done by any of several mechanisms, including weak
53 // pointers, scoped_refptr references, or calling the registration
54 // function with a null callback from a destructor. To enable the null
55 // callback strategy, callbacks will always be evaluated immediately
benjhayden 2012/05/16 14:26:25 Sorry, what does it mean to evaluate a callback?
Randy Smith (Not in Mondays) 2012/05/16 20:30:16 Modified the text; let me know what you think.
56 // before execution.
57 //
58 // Class methods are virtual to allow mocking for tests; these classes
59 // aren't intended to be base classes for other classes.
60 class CONTENT_EXPORT ByteStreamInput {
61 public:
62 virtual ~ByteStreamInput() = 0;
63
64 // Always adds the data passed into the ByteStream. Returns true
65 // if more data may be added without exceeding the class limit
66 // on data. Takes ownership of |buffer|.
67 virtual bool Write(scoped_refptr<net::IOBuffer> buffer,
68 size_t byte_count) = 0;
69
70 // Signal that all data that is going to be sent, has been sent,
71 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be
72 // passed for successful completion.
73 virtual void Close(DownloadInterruptReason status) = 0;
74
75 // Register a callback to be called
benjhayden 2012/05/16 14:26:25 M-q to reflow?
Randy Smith (Not in Mondays) 2012/05/16 20:30:16 Done.
76 // when the stream transitions from full to having space available.
77 // The callback will always be called on the task runner associated
78 // with the ByteStreamInput.
79 // This callback will only be called if a call to Write has previously
80 // returned false (i.e. the ByteStream has been filled).
81 // Multiple calls to this function are supported, though note that it
82 // is the callers responsibility to handle races with space becoming
83 // available (i.e. in the case of that race either of the before
84 // or after callbacks may be called).
85 virtual void RegisterCallback(const base::Closure& source_callback) = 0;
86 };
87
88 class CONTENT_EXPORT ByteStreamOutput {
89 public:
90 typedef enum { STREAM_EMPTY, STREAM_HAS_DATA, STREAM_COMPLETE } StreamState;
benjhayden 2012/05/16 14:26:25 Why do you need typedef?
Randy Smith (Not in Mondays) 2012/05/16 20:30:16 Because I'm an old programmer who isn't used to th
91
92 virtual ~ByteStreamOutput() = 0;
93
94 // Returns STREAM_EMPTY if there is no data on the ByteStream and
95 // Close() has not been called, and STREAM_COMPLETE if there
96 // is no data on the ByteStream and Close() has been called.
97 // If there is data on the ByteStream, returns STREAM_HAS_DATA
98 // and fills in |*data| with a pointer to the data, and |*length|
99 // with its length.
100 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data,
101 size_t* length) = 0;
102
103 // Only valid to call if Read() has returned STREAM_COMPLETE.
104 virtual DownloadInterruptReason GetStatus() const = 0;
105
106 // Register a callback to be called
benjhayden 2012/05/16 14:26:25 M-q?
Randy Smith (Not in Mondays) 2012/05/16 20:30:16 Done.
107 // when data is added or the source completes.
108 // The callback will be always be called on the owning task runner.
109 // Multiple calls to this function are supported, though note that it
110 // is the callers responsibility to handle races with data becoming
111 // available (i.e. in the case of that race either of the before
112 // or after callbacks may be called).
113 virtual void RegisterCallback(const base::Closure& sink_callback) = 0;
114 };
115
116 CONTENT_EXPORT void CreateByteStream(
117 scoped_ptr<ByteStreamInput>* input,
118 scoped_ptr<ByteStreamOutput>* output,
119 scoped_refptr<base::SequencedTaskRunner> input_task_runner,
benjhayden 2012/05/16 14:26:25 I thought that function inputs (task_runners, buff
Randy Smith (Not in Mondays) 2012/05/16 20:30:16 If so, the folks who wrote the style guide also kn
120 scoped_refptr<base::SequencedTaskRunner> output_task_runner,
121 size_t buffer_size);
122
123 } // namespace content
124
125 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_
OLDNEW
« no previous file with comments | « no previous file | content/browser/download/byte_stream.cc » ('j') | content/browser/download/byte_stream.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698