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