summaryrefslogtreecommitdiff
path: root/chromium/content/browser/loader/stream_writer.cc
blob: 3792a17c48959374c344bc8ad7318cd9870717de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/loader/stream_writer.h"

#include "base/callback_helpers.h"
#include "base/guid.h"
#include "content/browser/loader/resource_controller.h"
#include "content/browser/streams/stream.h"
#include "content/browser/streams/stream_registry.h"
#include "net/base/io_buffer.h"
#include "url/gurl.h"
#include "url/url_constants.h"

namespace content {

StreamWriter::StreamWriter() : immediate_mode_(false) {}

StreamWriter::~StreamWriter() {
  if (stream_.get())
    Finalize(0);
}

void StreamWriter::InitializeStream(StreamRegistry* registry,
                                    const GURL& origin,
                                    const base::Closure& cancel_callback) {
  cancel_callback_ = cancel_callback;
  DCHECK(!stream_.get());

  // TODO(tyoshino): Find a way to share this with the blob URL creation in
  // WebKit.
  GURL url(std::string(url::kBlobScheme) + ":" + origin.spec() +
           base::GenerateGUID());
  stream_ = new Stream(registry, this, url);
}

void StreamWriter::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
                              int* buf_size,
                              int min_size) {
  static const int kReadBufSize = 32768;

  DCHECK(buf);
  DCHECK(buf_size);
  DCHECK_LE(min_size, kReadBufSize);
  if (!read_buffer_.get())
    read_buffer_ = new net::IOBuffer(kReadBufSize);
  *buf = read_buffer_.get();
  *buf_size = kReadBufSize;
}

void StreamWriter::OnReadCompleted(
    int bytes_read,
    const base::Closure& need_more_data_callback) {
  DCHECK(!need_more_data_callback_);
  if (!bytes_read) {
    need_more_data_callback.Run();
    return;
  }

  // We have more data to read.
  DCHECK(read_buffer_.get());

  // Release the ownership of the buffer, and store a reference
  // to it. A new one will be allocated in OnWillRead().
  scoped_refptr<net::IOBuffer> buffer;
  read_buffer_.swap(buffer);
  stream_->AddData(buffer, bytes_read);

  if (immediate_mode_)
    stream_->Flush();

  if (!stream_->can_add_data()) {
    need_more_data_callback_ = need_more_data_callback;
    return;
  }
  need_more_data_callback.Run();
}

void StreamWriter::Finalize(int status) {
  DCHECK(stream_.get());
  stream_->Finalize(status);
  stream_->RemoveWriteObserver(this);
  stream_ = nullptr;
}

void StreamWriter::OnSpaceAvailable(Stream* stream) {
  base::ResetAndReturn(&need_more_data_callback_).Run();
}

void StreamWriter::OnClose(Stream* stream) {
  cancel_callback_.Run();
}

}  // namespace content