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(const GURL& url,
40                                                ResourceResponse* resp,
41                                                bool* defer) {
42  return true;
43}
44
45bool StreamResourceHandler::OnResponseStarted(ResourceResponse* resp,
46                                              bool* defer) {
47  return true;
48}
49
50bool StreamResourceHandler::OnWillStart(const GURL& url, bool* defer) {
51  return true;
52}
53
54bool StreamResourceHandler::OnBeforeNetworkStart(const GURL& url, bool* defer) {
55  return true;
56}
57
58bool StreamResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
59                                       int* buf_size,
60                                       int min_size) {
61  static const int kReadBufSize = 32768;
62
63  DCHECK(buf && buf_size);
64  if (!read_buffer_.get())
65    read_buffer_ = new net::IOBuffer(kReadBufSize);
66  *buf = read_buffer_.get();
67  *buf_size = kReadBufSize;
68
69  return true;
70}
71
72bool StreamResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
73  if (!bytes_read)
74    return true;
75
76  // We have more data to read.
77  DCHECK(read_buffer_.get());
78
79  // Release the ownership of the buffer, and store a reference
80  // to it. A new one will be allocated in OnWillRead().
81  scoped_refptr<net::IOBuffer> buffer;
82  read_buffer_.swap(buffer);
83  stream_->AddData(buffer, bytes_read);
84
85  if (!stream_->can_add_data())
86    *defer = true;
87
88  return true;
89}
90
91void StreamResourceHandler::OnResponseCompleted(
92    const net::URLRequestStatus& status,
93    const std::string& sec_info,
94    bool* defer) {
95  stream_->Finalize();
96}
97
98void StreamResourceHandler::OnDataDownloaded(int bytes_downloaded) {
99  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
111