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

Side by Side Diff: content/browser/streams/stream.cc

Issue 12645004: Add Resource Handler for creating Streams to forward to extensions (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Disable Stream Resource Throttle Created 7 years, 9 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
« no previous file with comments | « content/browser/streams/stream.h ('k') | content/browser/streams/stream_handle_impl.h » ('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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 #include "content/browser/streams/stream.h" 5 #include "content/browser/streams/stream.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop_proxy.h" 8 #include "base/message_loop_proxy.h"
9 #include "content/browser/streams/stream_handle_impl.h"
9 #include "content/browser/streams/stream_read_observer.h" 10 #include "content/browser/streams/stream_read_observer.h"
10 #include "content/browser/streams/stream_registry.h" 11 #include "content/browser/streams/stream_registry.h"
11 #include "content/browser/streams/stream_write_observer.h" 12 #include "content/browser/streams/stream_write_observer.h"
12 #include "net/base/io_buffer.h" 13 #include "net/base/io_buffer.h"
13 14
14 namespace { 15 namespace {
15 // Start throttling the connection at about 1MB. 16 // Start throttling the connection at about 1MB.
16 const size_t kDeferSizeThreshold = 40 * 32768; 17 const size_t kDeferSizeThreshold = 40 * 32768;
17 } 18 }
18 19
19 namespace content { 20 namespace content {
20 21
21 Stream::Stream(StreamRegistry* registry, 22 Stream::Stream(StreamRegistry* registry,
22 StreamWriteObserver* write_observer, 23 StreamWriteObserver* write_observer,
23 const GURL& security_origin, 24 const GURL& security_origin,
24 const GURL& url) 25 const GURL& url)
25 : bytes_read_(0), 26 : bytes_read_(0),
26 can_add_data_(true), 27 can_add_data_(true),
27 security_origin_(security_origin), 28 security_origin_(security_origin),
28 url_(url), 29 url_(url),
29 data_length_(0), 30 data_length_(0),
30 registry_(registry), 31 registry_(registry),
31 read_observer_(NULL), 32 read_observer_(NULL),
32 write_observer_(write_observer), 33 write_observer_(write_observer),
34 stream_handle_(NULL),
33 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 35 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
34 CreateByteStream(base::MessageLoopProxy::current(), 36 CreateByteStream(base::MessageLoopProxy::current(),
35 base::MessageLoopProxy::current(), 37 base::MessageLoopProxy::current(),
36 kDeferSizeThreshold, 38 kDeferSizeThreshold,
37 &writer_, 39 &writer_,
38 &reader_); 40 &reader_);
39 41
40 // Setup callback for writing. 42 // Setup callback for writing.
41 writer_->RegisterCallback(base::Bind(&Stream::OnSpaceAvailable, 43 writer_->RegisterCallback(base::Bind(&Stream::OnSpaceAvailable,
42 weak_ptr_factory_.GetWeakPtr())); 44 weak_ptr_factory_.GetWeakPtr()));
(...skipping 11 matching lines...) Expand all
54 return false; 56 return false;
55 read_observer_ = observer; 57 read_observer_ = observer;
56 return true; 58 return true;
57 } 59 }
58 60
59 void Stream::RemoveReadObserver(StreamReadObserver* observer) { 61 void Stream::RemoveReadObserver(StreamReadObserver* observer) {
60 DCHECK(observer == read_observer_); 62 DCHECK(observer == read_observer_);
61 read_observer_ = NULL; 63 read_observer_ = NULL;
62 } 64 }
63 65
66 void Stream::RemoveWriteObserver(StreamWriteObserver* observer) {
67 DCHECK(observer == write_observer_);
68 write_observer_ = NULL;
69 }
70
64 void Stream::AddData(scoped_refptr<net::IOBuffer> buffer, size_t size) { 71 void Stream::AddData(scoped_refptr<net::IOBuffer> buffer, size_t size) {
65 can_add_data_ = writer_->Write(buffer, size); 72 can_add_data_ = writer_->Write(buffer, size);
66 } 73 }
67 74
68 void Stream::Finalize() { 75 void Stream::Finalize() {
69 writer_->Close(DOWNLOAD_INTERRUPT_REASON_NONE); 76 writer_->Close(DOWNLOAD_INTERRUPT_REASON_NONE);
70 writer_.reset(NULL); 77 writer_.reset(NULL);
71 78
72 OnDataAvailable(); 79 OnDataAvailable();
73 } 80 }
(...skipping 22 matching lines...) Expand all
96 buf_size : remaining_bytes; 103 buf_size : remaining_bytes;
97 memcpy(buf->data(), data_->data() + bytes_read_, to_read); 104 memcpy(buf->data(), data_->data() + bytes_read_, to_read);
98 bytes_read_ += to_read; 105 bytes_read_ += to_read;
99 if (bytes_read_ >= data_length_) 106 if (bytes_read_ >= data_length_)
100 data_ = NULL; 107 data_ = NULL;
101 108
102 *bytes_read = to_read; 109 *bytes_read = to_read;
103 return STREAM_HAS_DATA; 110 return STREAM_HAS_DATA;
104 } 111 }
105 112
113 scoped_ptr<StreamHandle> Stream::CreateHandle(const GURL& original_url,
114 const std::string& mime_type) {
115 CHECK(!stream_handle_);
116 stream_handle_ = new StreamHandleImpl(weak_ptr_factory_.GetWeakPtr(),
117 original_url,
118 mime_type);
119 return scoped_ptr<StreamHandle>(stream_handle_).Pass();
120 }
121
122 void Stream::CloseHandle() {
123 CHECK(stream_handle_);
124 stream_handle_ = NULL;
125 registry_->UnregisterStream(url());
126 if (write_observer_)
127 write_observer_->OnClose(this);
128 }
129
106 void Stream::OnSpaceAvailable() { 130 void Stream::OnSpaceAvailable() {
107 can_add_data_ = true; 131 can_add_data_ = true;
108 write_observer_->OnSpaceAvailable(this); 132 if (write_observer_)
133 write_observer_->OnSpaceAvailable(this);
109 } 134 }
110 135
111 void Stream::OnDataAvailable() { 136 void Stream::OnDataAvailable() {
112 if (read_observer_) 137 if (read_observer_)
113 read_observer_->OnDataAvailable(this); 138 read_observer_->OnDataAvailable(this);
114 } 139 }
115 140
116 } // namespace content 141 } // namespace content
117 142
OLDNEW
« no previous file with comments | « content/browser/streams/stream.h ('k') | content/browser/streams/stream_handle_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698