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"
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"
18 StreamResourceHandler::StreamResourceHandler(net::URLRequest
* request
,
19 StreamRegistry
* registry
,
21 : ResourceHandler(request
),
23 // TODO(tyoshino): Find a way to share this with the blob URL creation in
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
,
39 bool StreamResourceHandler::OnRequestRedirected(
40 const net::RedirectInfo
& redirect_info
,
41 ResourceResponse
* resp
,
46 bool StreamResourceHandler::OnResponseStarted(ResourceResponse
* resp
,
51 bool StreamResourceHandler::OnWillStart(const GURL
& url
, bool* defer
) {
55 bool StreamResourceHandler::OnBeforeNetworkStart(const GURL
& url
, bool* defer
) {
59 bool StreamResourceHandler::OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
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
;
73 bool StreamResourceHandler::OnReadCompleted(int bytes_read
, bool* defer
) {
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())
92 void StreamResourceHandler::OnResponseCompleted(
93 const net::URLRequestStatus
& status
,
94 const std::string
& sec_info
,
99 void StreamResourceHandler::OnDataDownloaded(int bytes_downloaded
) {
103 void StreamResourceHandler::OnSpaceAvailable(Stream
* stream
) {
104 controller()->Resume();
107 void StreamResourceHandler::OnClose(Stream
* stream
) {
108 controller()->Cancel();
111 } // namespace content