OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/loader/stream_resource_handler.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "content/browser/streams/stream.h" |
| 9 #include "content/public/browser/resource_controller.h" |
| 10 #include "net/base/io_buffer.h" |
| 11 #include "net/url_request/url_request_status.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 StreamResourceHandler::StreamResourceHandler( |
| 16 net::URLRequest* request, |
| 17 scoped_refptr<Stream> stream) |
| 18 : request_(request), |
| 19 stream_(stream), |
| 20 read_buffer_(NULL) { |
| 21 stream_->AddWriteObserver(this); |
| 22 } |
| 23 |
| 24 StreamResourceHandler::~StreamResourceHandler() { |
| 25 stream_->RemoveWriteObserver(this); |
| 26 } |
| 27 |
| 28 bool StreamResourceHandler::OnUploadProgress(int request_id, |
| 29 uint64 position, |
| 30 uint64 size) { |
| 31 return true; |
| 32 } |
| 33 |
| 34 bool StreamResourceHandler::OnRequestRedirected(int request_id, |
| 35 const GURL& url, |
| 36 ResourceResponse* resp, |
| 37 bool* defer) { |
| 38 return true; |
| 39 } |
| 40 |
| 41 bool StreamResourceHandler::OnResponseStarted(int request_id, |
| 42 ResourceResponse* resp, |
| 43 bool* defer) { |
| 44 return true; |
| 45 } |
| 46 |
| 47 bool StreamResourceHandler::OnWillStart(int request_id, |
| 48 const GURL& url, |
| 49 bool* defer) { |
| 50 return true; |
| 51 } |
| 52 |
| 53 bool StreamResourceHandler::OnWillRead(int request_id, |
| 54 net::IOBuffer** buf, |
| 55 int* buf_size, |
| 56 int min_size) { |
| 57 static const int kReadBufSize = 32768; |
| 58 |
| 59 DCHECK(buf && buf_size); |
| 60 if (!read_buffer_) { |
| 61 read_buffer_ = new net::IOBuffer(kReadBufSize); |
| 62 } |
| 63 *buf = read_buffer_.get(); |
| 64 *buf_size = kReadBufSize; |
| 65 |
| 66 return true; |
| 67 } |
| 68 |
| 69 bool StreamResourceHandler::OnReadCompleted(int request_id, |
| 70 int bytes_read, |
| 71 bool* defer) { |
| 72 if (!bytes_read) |
| 73 return true; |
| 74 |
| 75 // We have more data to read. |
| 76 DCHECK(read_buffer_); |
| 77 |
| 78 // Release the ownership of the buffer, and store a reference |
| 79 // to it. A new one will be allocated in OnWillRead(). |
| 80 net::IOBuffer* buffer = NULL; |
| 81 read_buffer_.swap(&buffer); |
| 82 stream_->AddData(buffer, bytes_read); |
| 83 |
| 84 if (!stream_->can_add_data()) { |
| 85 *defer = true; |
| 86 } |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 bool StreamResourceHandler::OnResponseCompleted( |
| 92 int request_id, |
| 93 const net::URLRequestStatus& urs, |
| 94 const std::string& sec_info) { |
| 95 stream_->MarkComplete(); |
| 96 if (urs.status() != net::URLRequestStatus::SUCCESS) |
| 97 return false; |
| 98 |
| 99 return true; |
| 100 } |
| 101 |
| 102 void StreamResourceHandler::OnDataDownloaded( |
| 103 int request_id, |
| 104 int bytes_downloaded) { |
| 105 NOTREACHED(); |
| 106 } |
| 107 |
| 108 void StreamResourceHandler::OnSpaceAvailable(Stream* stream) { |
| 109 controller()->Resume(); |
| 110 } |
| 111 |
| 112 } // namespace content |
| 113 |
OLD | NEW |