Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / loader / stream_resource_handler.cc
blob0a2d6e83afbe20bc67237e0b8061206ff2a33548
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.
5 #include "content/browser/loader/stream_resource_handler.h"
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"
16 namespace content {
18 StreamResourceHandler::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);
30 StreamResourceHandler::~StreamResourceHandler() {
31 stream_->RemoveWriteObserver(this);
34 bool StreamResourceHandler::OnUploadProgress(uint64 position,
35 uint64 size) {
36 return true;
39 bool StreamResourceHandler::OnRequestRedirected(
40 const net::RedirectInfo& redirect_info,
41 ResourceResponse* resp,
42 bool* defer) {
43 return true;
46 bool StreamResourceHandler::OnResponseStarted(ResourceResponse* resp,
47 bool* defer) {
48 return true;
51 bool StreamResourceHandler::OnWillStart(const GURL& url, bool* defer) {
52 return true;
55 bool StreamResourceHandler::OnBeforeNetworkStart(const GURL& url, bool* defer) {
56 return true;
59 bool StreamResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
60 int* buf_size,
61 int min_size) {
62 static const int kReadBufSize = 32768;
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;
70 return true;
73 bool StreamResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
74 if (!bytes_read)
75 return true;
77 // We have more data to read.
78 DCHECK(read_buffer_.get());
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);
86 if (!stream_->can_add_data())
87 *defer = true;
89 return true;
92 void StreamResourceHandler::OnResponseCompleted(
93 const net::URLRequestStatus& status,
94 const std::string& sec_info,
95 bool* defer) {
96 stream_->Finalize();
99 void StreamResourceHandler::OnDataDownloaded(int bytes_downloaded) {
100 NOTREACHED();
103 void StreamResourceHandler::OnSpaceAvailable(Stream* stream) {
104 controller()->Resume();
107 void StreamResourceHandler::OnClose(Stream* stream) {
108 controller()->Cancel();
111 } // namespace content