[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 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" |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 12 | #include "net/base/io_buffer.h" |
| 13 | #include "net/url_request/url_request_status.h" |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 14 | #include "url/url_constants.h" |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 15 | |
| 16 | namespace content { |
| 17 | |
[email protected] | 6a94bff | 2014-03-01 04:37:09 | [diff] [blame] | 18 | StreamResourceHandler::StreamResourceHandler(net::URLRequest* request, |
| 19 | StreamRegistry* registry, |
| 20 | const GURL& origin) |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 21 | : ResourceHandler(request), |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 22 | read_buffer_(NULL) { |
[email protected] | 749b68b | 2013-07-26 10:54:37 | [diff] [blame] | 23 | // TODO(tyoshino): Find a way to share this with the blob URL creation in |
| 24 | // WebKit. |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 25 | GURL url(std::string(url::kBlobScheme) + ":" + origin.spec() + |
[email protected] | 6a94bff | 2014-03-01 04:37:09 | [diff] [blame] | 26 | base::GenerateGUID()); |
[email protected] | 749b68b | 2013-07-26 10:54:37 | [diff] [blame] | 27 | stream_ = new Stream(registry, this, url); |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | StreamResourceHandler::~StreamResourceHandler() { |
| 31 | stream_->RemoveWriteObserver(this); |
| 32 | } |
| 33 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame^] | 34 | bool StreamResourceHandler::OnUploadProgress(uint64 position, |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 35 | uint64 size) { |
| 36 | return true; |
| 37 | } |
| 38 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame^] | 39 | bool StreamResourceHandler::OnRequestRedirected(const GURL& url, |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 40 | ResourceResponse* resp, |
| 41 | bool* defer) { |
| 42 | return true; |
| 43 | } |
| 44 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame^] | 45 | bool StreamResourceHandler::OnResponseStarted(ResourceResponse* resp, |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 46 | bool* defer) { |
| 47 | return true; |
| 48 | } |
| 49 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame^] | 50 | bool StreamResourceHandler::OnWillStart(const GURL& url, bool* defer) { |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame^] | 54 | bool StreamResourceHandler::OnBeforeNetworkStart(const GURL& url, bool* defer) { |
[email protected] | 5584eab | 2014-01-09 22:16:15 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame^] | 58 | bool StreamResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 59 | int* buf_size, |
| 60 | int min_size) { |
| 61 | static const int kReadBufSize = 32768; |
| 62 | |
| 63 | DCHECK(buf && buf_size); |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 64 | if (!read_buffer_.get()) |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 65 | read_buffer_ = new net::IOBuffer(kReadBufSize); |
| 66 | *buf = read_buffer_.get(); |
| 67 | *buf_size = kReadBufSize; |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame^] | 72 | bool StreamResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 73 | if (!bytes_read) |
| 74 | return true; |
| 75 | |
| 76 | // We have more data to read. |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 77 | DCHECK(read_buffer_.get()); |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 78 | |
| 79 | // Release the ownership of the buffer, and store a reference |
| 80 | // to it. A new one will be allocated in OnWillRead(). |
[email protected] | f5bf5ba | 2014-04-10 19:20:01 | [diff] [blame] | 81 | scoped_refptr<net::IOBuffer> buffer; |
| 82 | read_buffer_.swap(buffer); |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 83 | stream_->AddData(buffer, bytes_read); |
| 84 | |
| 85 | if (!stream_->can_add_data()) |
| 86 | *defer = true; |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 91 | void StreamResourceHandler::OnResponseCompleted( |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 92 | const net::URLRequestStatus& status, |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 93 | const std::string& sec_info, |
| 94 | bool* defer) { |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 95 | stream_->Finalize(); |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame^] | 98 | void StreamResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 99 | NOTREACHED(); |
| 100 | } |
| 101 | |
| 102 | void StreamResourceHandler::OnSpaceAvailable(Stream* stream) { |
| 103 | controller()->Resume(); |
| 104 | } |
| 105 | |
| 106 | void StreamResourceHandler::OnClose(Stream* stream) { |
| 107 | controller()->Cancel(); |
| 108 | } |
| 109 | |
| 110 | } // namespace content |