| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/loader/stream_writer.h" | 5 #include "content/browser/loader/stream_writer.h" |
| 6 | 6 |
| 7 #include "base/guid.h" | 7 #include "base/guid.h" |
| 8 #include "content/browser/streams/stream.h" | 8 #include "content/browser/streams/stream.h" |
| 9 #include "content/browser/streams/stream_registry.h" | 9 #include "content/browser/streams/stream_registry.h" |
| 10 #include "content/public/browser/resource_controller.h" | |
| 11 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| 12 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 13 #include "url/url_constants.h" | 12 #include "url/url_constants.h" |
| 14 | 13 |
| 15 namespace content { | 14 namespace content { |
| 16 | 15 |
| 17 StreamWriter::StreamWriter() : controller_(nullptr), immediate_mode_(false) { | 16 StreamWriter::StreamWriter() : immediate_mode_(false) {} |
| 18 } | |
| 19 | 17 |
| 20 StreamWriter::~StreamWriter() { | 18 StreamWriter::~StreamWriter() { |
| 21 if (stream_.get()) | 19 if (stream_.get()) |
| 22 Finalize(0); | 20 Finalize(0); |
| 23 } | 21 } |
| 24 | 22 |
| 25 void StreamWriter::InitializeStream(StreamRegistry* registry, | 23 void StreamWriter::InitializeStream(StreamRegistry* registry, |
| 26 const GURL& origin) { | 24 const GURL& origin, |
| 25 const base::Closure& cancel_callback) { |
| 26 cancel_callback_ = cancel_callback; |
| 27 DCHECK(!stream_.get()); | 27 DCHECK(!stream_.get()); |
| 28 | 28 |
| 29 // TODO(tyoshino): Find a way to share this with the blob URL creation in | 29 // TODO(tyoshino): Find a way to share this with the blob URL creation in |
| 30 // WebKit. | 30 // WebKit. |
| 31 GURL url(std::string(url::kBlobScheme) + ":" + origin.spec() + | 31 GURL url(std::string(url::kBlobScheme) + ":" + origin.spec() + |
| 32 base::GenerateGUID()); | 32 base::GenerateGUID()); |
| 33 stream_ = new Stream(registry, this, url); | 33 stream_ = new Stream(registry, this, url); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void StreamWriter::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 36 void StreamWriter::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 37 int* buf_size, | 37 int* buf_size, |
| 38 int min_size) { | 38 int min_size) { |
| 39 static const int kReadBufSize = 32768; | 39 static const int kReadBufSize = 32768; |
| 40 | 40 |
| 41 DCHECK(buf); | 41 DCHECK(buf); |
| 42 DCHECK(buf_size); | 42 DCHECK(buf_size); |
| 43 DCHECK_LE(min_size, kReadBufSize); | 43 DCHECK_LE(min_size, kReadBufSize); |
| 44 if (!read_buffer_.get()) | 44 if (!read_buffer_.get()) |
| 45 read_buffer_ = new net::IOBuffer(kReadBufSize); | 45 read_buffer_ = new net::IOBuffer(kReadBufSize); |
| 46 *buf = read_buffer_.get(); | 46 *buf = read_buffer_.get(); |
| 47 *buf_size = kReadBufSize; | 47 *buf_size = kReadBufSize; |
| 48 } | 48 } |
| 49 | 49 |
| 50 void StreamWriter::OnReadCompleted(int bytes_read, bool* defer) { | 50 void StreamWriter::OnReadCompleted( |
| 51 if (!bytes_read) | 51 int bytes_read, |
| 52 const base::Closure& need_more_data_callback) { |
| 53 DCHECK(!need_more_data_callback_); |
| 54 if (!bytes_read) { |
| 55 need_more_data_callback.Run(); |
| 52 return; | 56 return; |
| 57 } |
| 53 | 58 |
| 54 // We have more data to read. | 59 // We have more data to read. |
| 55 DCHECK(read_buffer_.get()); | 60 DCHECK(read_buffer_.get()); |
| 56 | 61 |
| 57 // Release the ownership of the buffer, and store a reference | 62 // Release the ownership of the buffer, and store a reference |
| 58 // to it. A new one will be allocated in OnWillRead(). | 63 // to it. A new one will be allocated in OnWillRead(). |
| 59 scoped_refptr<net::IOBuffer> buffer; | 64 scoped_refptr<net::IOBuffer> buffer; |
| 60 read_buffer_.swap(buffer); | 65 read_buffer_.swap(buffer); |
| 61 stream_->AddData(buffer, bytes_read); | 66 stream_->AddData(buffer, bytes_read); |
| 62 | 67 |
| 63 if (immediate_mode_) | 68 if (immediate_mode_) |
| 64 stream_->Flush(); | 69 stream_->Flush(); |
| 65 | 70 |
| 66 if (!stream_->can_add_data()) | 71 if (!stream_->can_add_data()) { |
| 67 *defer = true; | 72 need_more_data_callback_ = need_more_data_callback; |
| 73 return; |
| 74 } |
| 75 need_more_data_callback.Run(); |
| 68 } | 76 } |
| 69 | 77 |
| 70 void StreamWriter::Finalize(int status) { | 78 void StreamWriter::Finalize(int status) { |
| 71 DCHECK(stream_.get()); | 79 DCHECK(stream_.get()); |
| 72 stream_->Finalize(status); | 80 stream_->Finalize(status); |
| 73 stream_->RemoveWriteObserver(this); | 81 stream_->RemoveWriteObserver(this); |
| 74 stream_ = nullptr; | 82 stream_ = nullptr; |
| 75 } | 83 } |
| 76 | 84 |
| 77 void StreamWriter::OnSpaceAvailable(Stream* stream) { | 85 void StreamWriter::OnSpaceAvailable(Stream* stream) { |
| 78 controller_->Resume(); | 86 need_more_data_callback_.Run(); |
| 79 } | 87 } |
| 80 | 88 |
| 81 void StreamWriter::OnClose(Stream* stream) { | 89 void StreamWriter::OnClose(Stream* stream) { |
| 82 controller_->Cancel(); | 90 cancel_callback_.Run(); |
| 83 } | 91 } |
| 84 | 92 |
| 85 } // namespace content | 93 } // namespace content |
| OLD | NEW |