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