blob: 46713fc67fad6a455fb776909dc0b3b44efa4f06 [file] [log] [blame]
[email protected]646889822013-03-20 00:26:451// 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]646889822013-03-20 00:26:4512#include "net/base/io_buffer.h"
13#include "net/url_request/url_request_status.h"
[email protected]cca6f392014-05-28 21:32:2614#include "url/url_constants.h"
[email protected]646889822013-03-20 00:26:4515
16namespace content {
17
[email protected]6a94bff2014-03-01 04:37:0918StreamResourceHandler::StreamResourceHandler(net::URLRequest* request,
19 StreamRegistry* registry,
20 const GURL& origin)
[email protected]ef5306e2013-10-15 19:38:1821 : ResourceHandler(request),
[email protected]646889822013-03-20 00:26:4522 read_buffer_(NULL) {
[email protected]749b68b2013-07-26 10:54:3723 // TODO(tyoshino): Find a way to share this with the blob URL creation in
24 // WebKit.
[email protected]cca6f392014-05-28 21:32:2625 GURL url(std::string(url::kBlobScheme) + ":" + origin.spec() +
[email protected]6a94bff2014-03-01 04:37:0926 base::GenerateGUID());
[email protected]749b68b2013-07-26 10:54:3727 stream_ = new Stream(registry, this, url);
[email protected]646889822013-03-20 00:26:4528}
29
30StreamResourceHandler::~StreamResourceHandler() {
31 stream_->RemoveWriteObserver(this);
32}
33
[email protected]d22e800e2014-05-28 21:55:5734bool StreamResourceHandler::OnUploadProgress(uint64 position,
[email protected]646889822013-03-20 00:26:4535 uint64 size) {
36 return true;
37}
38
[email protected]d22e800e2014-05-28 21:55:5739bool StreamResourceHandler::OnRequestRedirected(const GURL& url,
[email protected]646889822013-03-20 00:26:4540 ResourceResponse* resp,
41 bool* defer) {
42 return true;
43}
44
[email protected]d22e800e2014-05-28 21:55:5745bool StreamResourceHandler::OnResponseStarted(ResourceResponse* resp,
[email protected]646889822013-03-20 00:26:4546 bool* defer) {
47 return true;
48}
49
[email protected]d22e800e2014-05-28 21:55:5750bool StreamResourceHandler::OnWillStart(const GURL& url, bool* defer) {
[email protected]646889822013-03-20 00:26:4551 return true;
52}
53
[email protected]d22e800e2014-05-28 21:55:5754bool StreamResourceHandler::OnBeforeNetworkStart(const GURL& url, bool* defer) {
[email protected]5584eab2014-01-09 22:16:1555 return true;
56}
57
[email protected]d22e800e2014-05-28 21:55:5758bool StreamResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
[email protected]646889822013-03-20 00:26:4559 int* buf_size,
60 int min_size) {
61 static const int kReadBufSize = 32768;
62
63 DCHECK(buf && buf_size);
[email protected]fc72bb12013-06-02 21:13:4664 if (!read_buffer_.get())
[email protected]646889822013-03-20 00:26:4565 read_buffer_ = new net::IOBuffer(kReadBufSize);
66 *buf = read_buffer_.get();
67 *buf_size = kReadBufSize;
68
69 return true;
70}
71
[email protected]d22e800e2014-05-28 21:55:5772bool StreamResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
[email protected]646889822013-03-20 00:26:4573 if (!bytes_read)
74 return true;
75
76 // We have more data to read.
[email protected]fc72bb12013-06-02 21:13:4677 DCHECK(read_buffer_.get());
[email protected]646889822013-03-20 00:26:4578
79 // Release the ownership of the buffer, and store a reference
80 // to it. A new one will be allocated in OnWillRead().
[email protected]f5bf5ba2014-04-10 19:20:0181 scoped_refptr<net::IOBuffer> buffer;
82 read_buffer_.swap(buffer);
[email protected]646889822013-03-20 00:26:4583 stream_->AddData(buffer, bytes_read);
84
85 if (!stream_->can_add_data())
86 *defer = true;
87
88 return true;
89}
90
[email protected]3780874a2013-11-18 05:49:0391void StreamResourceHandler::OnResponseCompleted(
[email protected]646889822013-03-20 00:26:4592 const net::URLRequestStatus& status,
[email protected]3780874a2013-11-18 05:49:0393 const std::string& sec_info,
94 bool* defer) {
[email protected]646889822013-03-20 00:26:4595 stream_->Finalize();
[email protected]646889822013-03-20 00:26:4596}
97
[email protected]d22e800e2014-05-28 21:55:5798void StreamResourceHandler::OnDataDownloaded(int bytes_downloaded) {
[email protected]646889822013-03-20 00:26:4599 NOTREACHED();
100}
101
102void StreamResourceHandler::OnSpaceAvailable(Stream* stream) {
103 controller()->Resume();
104}
105
106void StreamResourceHandler::OnClose(Stream* stream) {
107 controller()->Cancel();
108}
109
110} // namespace content