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 "content/public/common/url_constants.h"
13 #include "net/base/io_buffer.h"
14 #include "net/url_request/url_request_status.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(kBlobScheme
) + ":" + origin
.spec() +
26 base::GenerateGUID());
27 stream_
= new Stream(registry
, this, url
);
30 StreamResourceHandler::~StreamResourceHandler() {
31 stream_
->RemoveWriteObserver(this);
34 bool StreamResourceHandler::OnUploadProgress(int request_id
,
40 bool StreamResourceHandler::OnRequestRedirected(int request_id
,
42 ResourceResponse
* resp
,
47 bool StreamResourceHandler::OnResponseStarted(int request_id
,
48 ResourceResponse
* resp
,
53 bool StreamResourceHandler::OnWillStart(int request_id
,
59 bool StreamResourceHandler::OnBeforeNetworkStart(int request_id
,
65 bool StreamResourceHandler::OnWillRead(int request_id
,
66 scoped_refptr
<net::IOBuffer
>* buf
,
69 static const int kReadBufSize
= 32768;
71 DCHECK(buf
&& buf_size
);
72 if (!read_buffer_
.get())
73 read_buffer_
= new net::IOBuffer(kReadBufSize
);
74 *buf
= read_buffer_
.get();
75 *buf_size
= kReadBufSize
;
80 bool StreamResourceHandler::OnReadCompleted(int request_id
,
86 // We have more data to read.
87 DCHECK(read_buffer_
.get());
89 // Release the ownership of the buffer, and store a reference
90 // to it. A new one will be allocated in OnWillRead().
91 net::IOBuffer
* buffer
= NULL
;
92 read_buffer_
.swap(&buffer
);
93 stream_
->AddData(buffer
, bytes_read
);
95 if (!stream_
->can_add_data())
101 void StreamResourceHandler::OnResponseCompleted(
103 const net::URLRequestStatus
& status
,
104 const std::string
& sec_info
,
109 void StreamResourceHandler::OnDataDownloaded(
111 int bytes_downloaded
) {
115 void StreamResourceHandler::OnSpaceAvailable(Stream
* stream
) {
116 controller()->Resume();
119 void StreamResourceHandler::OnClose(Stream
* stream
) {
120 controller()->Cancel();
123 } // namespace content