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

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

Issue 10392111: Use ByteStream in downloads system to decouple source and sink. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync'd to LKGR. Created 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/byte_stream.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #ifndef CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ 5 #ifndef CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_
6 #define CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ 6 #define CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_
7 #pragma once 7 #pragma once
8 8
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
11 #include <deque> 11 #include <deque>
12 12
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/synchronization/lock.h" 15 #include "base/synchronization/lock.h"
16 #include "content/public/browser/download_interrupt_reasons.h" 16 #include "content/public/browser/download_interrupt_reasons.h"
17 #include "net/base/io_buffer.h" 17 #include "net/base/io_buffer.h"
18 18
19 namespace base { 19 namespace base {
20 class SequencedTaskRunner; 20 class SequencedTaskRunner;
21 } 21 }
22 22
23 namespace content { 23 namespace content {
24 24
25 // A byte stream is a pipe to transfer bytes between a source and a 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 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 27 // only connection between source and sink; they need have no
28 // direct awareness of each other aside from the byte stream. The source and 28 // direct awareness of each other aside from the byte stream. The source and
29 // the sink have different interfaces to a byte stream, |ByteStreamInput| 29 // the sink have different interfaces to a byte stream, |ByteStreamWriter|
30 // and |ByteStreamOutput|. A pair of connected interfaces is generated by 30 // and |ByteStreamReader|. A pair of connected interfaces is generated by
31 // calling |CreateByteStream|. 31 // calling |CreateByteStream|.
32 // 32 //
33 // The source adds bytes to the bytestream via |ByteStreamInput::Write| 33 // The source adds bytes to the bytestream via |ByteStreamWriter::Write|
34 // and the sink retrieves bytes already written via |ByteStreamOutput::Read|. 34 // and the sink retrieves bytes already written via |ByteStreamReader::Read|.
35 // 35 //
36 // When the source has no more data to add, it will call 36 // When the source has no more data to add, it will call
37 // |ByteStreamInput::Close| to indicate that. Errors at the source 37 // |ByteStreamWriter::Close| to indicate that. Errors at the source
38 // are indicated to the sink via a non-DOWNLOAD_INTERRUPT_REASON_NONE code. 38 // are indicated to the sink via a non-DOWNLOAD_INTERRUPT_REASON_NONE code.
39 // 39 //
40 // Normally the source is not managed after the relationship is setup; 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 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 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 43 // mechanism; instead, the source will write data until it exausts the
44 // available space. If the source needs to be aware of errors occuring 44 // available space. If the source needs to be aware of errors occuring
45 // on the sink, this must be signalled in some other fashion (usually 45 // on the sink, this must be signalled in some other fashion (usually
46 // through whatever controller setup the relationship). 46 // through whatever controller setup the relationship).
47 // 47 //
48 // Callback lifetime management: No lifetime management is done in this 48 // Callback lifetime management: No lifetime management is done in this
49 // class to prevent registered callbacks from being called after any 49 // class to prevent registered callbacks from being called after any
50 // objects to which they may refer have been destroyed. It is the 50 // objects to which they may refer have been destroyed. It is the
51 // responsibility of the callers to avoid use-after-free references. 51 // responsibility of the callers to avoid use-after-free references.
52 // This may be done by any of several mechanisms, including weak 52 // This may be done by any of several mechanisms, including weak
53 // pointers, scoped_refptr references, or calling the registration 53 // pointers, scoped_refptr references, or calling the registration
54 // function with a null callback from a destructor. To enable the null 54 // function with a null callback from a destructor. To enable the null
55 // callback strategy, callbacks will not be stored between retrieval and 55 // callback strategy, callbacks will not be stored between retrieval and
56 // evaluation, so setting a null callback will guarantee that the 56 // evaluation, so setting a null callback will guarantee that the
57 // previous callback will not be executed after setting. 57 // previous callback will not be executed after setting.
58 // 58 //
59 // Class methods are virtual to allow mocking for tests; these classes 59 // Class methods are virtual to allow mocking for tests; these classes
60 // aren't intended to be base classes for other classes. 60 // aren't intended to be base classes for other classes.
61 class CONTENT_EXPORT ByteStreamInput { 61 class CONTENT_EXPORT ByteStreamWriter {
62 public: 62 public:
63 virtual ~ByteStreamInput() = 0; 63 virtual ~ByteStreamWriter() = 0;
64 64
65 // Always adds the data passed into the ByteStream. Returns true 65 // Always adds the data passed into the ByteStream. Returns true
66 // if more data may be added without exceeding the class limit 66 // if more data may be added without exceeding the class limit
67 // on data. Takes ownership of |buffer|. 67 // on data. Takes ownership of |buffer|.
68 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, 68 virtual bool Write(scoped_refptr<net::IOBuffer> buffer,
69 size_t byte_count) = 0; 69 size_t byte_count) = 0;
70 70
71 // Signal that all data that is going to be sent, has been sent, 71 // Signal that all data that is going to be sent, has been sent,
72 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be 72 // and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be
73 // passed for successful completion. 73 // passed for successful completion.
74 virtual void Close(DownloadInterruptReason status) = 0; 74 virtual void Close(DownloadInterruptReason status) = 0;
75 75
76 // Register a callback to be called when the stream transitions from 76 // Register a callback to be called when the stream transitions from
77 // full to having space available. The callback will always be 77 // full to having space available. The callback will always be
78 // called on the task runner associated with the ByteStreamInput. 78 // called on the task runner associated with the ByteStreamWriter.
79 // This callback will only be called if a call to Write has previously 79 // This callback will only be called if a call to Write has previously
80 // returned false (i.e. the ByteStream has been filled). 80 // returned false (i.e. the ByteStream has been filled).
81 // Multiple calls to this function are supported, though note that it 81 // Multiple calls to this function are supported, though note that it
82 // is the callers responsibility to handle races with space becoming 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 83 // available (i.e. in the case of that race either of the before
84 // or after callbacks may be called). 84 // or after callbacks may be called).
85 // The callback will not be called after ByteStreamWriter destruction.
85 virtual void RegisterCallback(const base::Closure& source_callback) = 0; 86 virtual void RegisterCallback(const base::Closure& source_callback) = 0;
86 }; 87 };
87 88
88 class CONTENT_EXPORT ByteStreamOutput { 89 class CONTENT_EXPORT ByteStreamReader {
89 public: 90 public:
90 enum StreamState { STREAM_EMPTY, STREAM_HAS_DATA, STREAM_COMPLETE }; 91 enum StreamState { STREAM_EMPTY, STREAM_HAS_DATA, STREAM_COMPLETE };
91 92
92 virtual ~ByteStreamOutput() = 0; 93 virtual ~ByteStreamReader() = 0;
93 94
94 // Returns STREAM_EMPTY if there is no data on the ByteStream and 95 // Returns STREAM_EMPTY if there is no data on the ByteStream and
95 // Close() has not been called, and STREAM_COMPLETE if there 96 // Close() has not been called, and STREAM_COMPLETE if there
96 // is no data on the ByteStream and Close() has been called. 97 // is no data on the ByteStream and Close() has been called.
97 // If there is data on the ByteStream, returns STREAM_HAS_DATA 98 // 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 // and fills in |*data| with a pointer to the data, and |*length|
99 // with its length. 100 // with its length.
100 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data, 101 virtual StreamState Read(scoped_refptr<net::IOBuffer>* data,
101 size_t* length) = 0; 102 size_t* length) = 0;
102 103
103 // Only valid to call if Read() has returned STREAM_COMPLETE. 104 // Only valid to call if Read() has returned STREAM_COMPLETE.
104 virtual DownloadInterruptReason GetStatus() const = 0; 105 virtual DownloadInterruptReason GetStatus() const = 0;
105 106
106 // Register a callback to be called when data is added or the source 107 // Register a callback to be called when data is added or the source
107 // completes. The callback will be always be called on the owning 108 // completes. The callback will be always be called on the owning
108 // task runner. Multiple calls to this function are supported, 109 // task runner. Multiple calls to this function are supported,
109 // though note that it is the callers responsibility to handle races 110 // though note that it is the callers responsibility to handle races
110 // with data becoming available (i.e. in the case of that race 111 // with data becoming available (i.e. in the case of that race
111 // either of the before or after callbacks may be called). 112 // either of the before or after callbacks may be called).
113 // The callback will not be called after ByteStreamReader destruction.
112 virtual void RegisterCallback(const base::Closure& sink_callback) = 0; 114 virtual void RegisterCallback(const base::Closure& sink_callback) = 0;
113 }; 115 };
114 116
115 CONTENT_EXPORT void CreateByteStream( 117 CONTENT_EXPORT void CreateByteStream(
116 scoped_refptr<base::SequencedTaskRunner> input_task_runner, 118 scoped_refptr<base::SequencedTaskRunner> input_task_runner,
117 scoped_refptr<base::SequencedTaskRunner> output_task_runner, 119 scoped_refptr<base::SequencedTaskRunner> output_task_runner,
118 size_t buffer_size, 120 size_t buffer_size,
119 scoped_ptr<ByteStreamInput>* input, 121 scoped_ptr<ByteStreamWriter>* input,
120 scoped_ptr<ByteStreamOutput>* output); 122 scoped_ptr<ByteStreamReader>* output);
121 123
122 } // namespace content 124 } // namespace content
123 125
124 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_ 126 #endif // CONTENT_BROWSER_DOWNLOAD_BYTE_STREAM_H_
OLDNEW
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/byte_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698