[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / loader / stream_writer.cc
blob69db45a70a32dfa71b46701132cf1f9d4bea0fd1
1 // Copyright 2014 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_writer.h"
7 #include "base/guid.h"
8 #include "content/browser/streams/stream.h"
9 #include "content/browser/streams/stream_registry.h"
10 #include "content/public/browser/resource_controller.h"
11 #include "net/base/io_buffer.h"
12 #include "url/gurl.h"
13 #include "url/url_constants.h"
15 namespace content {
17 StreamWriter::StreamWriter() : controller_(nullptr), immediate_mode_(false) {
20 StreamWriter::~StreamWriter() {
21 if (stream_.get())
22 Finalize();
25 void StreamWriter::InitializeStream(StreamRegistry* registry,
26 const GURL& origin) {
27 DCHECK(!stream_.get());
29 // TODO(tyoshino): Find a way to share this with the blob URL creation in
30 // WebKit.
31 GURL url(std::string(url::kBlobScheme) + ":" + origin.spec() +
32 base::GenerateGUID());
33 stream_ = new Stream(registry, this, url);
36 void StreamWriter::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
37 int* buf_size,
38 int min_size) {
39 static const int kReadBufSize = 32768;
41 DCHECK(buf);
42 DCHECK(buf_size);
43 DCHECK_LE(min_size, kReadBufSize);
44 if (!read_buffer_.get())
45 read_buffer_ = new net::IOBuffer(kReadBufSize);
46 *buf = read_buffer_.get();
47 *buf_size = kReadBufSize;
50 void StreamWriter::OnReadCompleted(int bytes_read, bool* defer) {
51 if (!bytes_read)
52 return;
54 // We have more data to read.
55 DCHECK(read_buffer_.get());
57 // Release the ownership of the buffer, and store a reference
58 // to it. A new one will be allocated in OnWillRead().
59 scoped_refptr<net::IOBuffer> buffer;
60 read_buffer_.swap(buffer);
61 stream_->AddData(buffer, bytes_read);
63 if (immediate_mode_)
64 stream_->Flush();
66 if (!stream_->can_add_data())
67 *defer = true;
70 void StreamWriter::Finalize() {
71 DCHECK(stream_.get());
72 stream_->Finalize();
73 stream_->RemoveWriteObserver(this);
74 stream_ = nullptr;
77 void StreamWriter::OnSpaceAvailable(Stream* stream) {
78 controller_->Resume();
81 void StreamWriter::OnClose(Stream* stream) {
82 controller_->Cancel();
85 } // namespace content