summaryrefslogtreecommitdiff
path: root/chromium/content/browser/cache_storage/cache_storage_blob_to_disk_cache.cc
blob: bc58008ae297ed1883c3e8a3abe8f20bf1943041 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2015 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/cache_storage/cache_storage_blob_to_disk_cache.h"

#include <utility>

#include "base/logging.h"
#include "net/base/io_buffer.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "storage/browser/blob/blob_data_handle.h"
#include "storage/browser/blob/blob_url_request_job_factory.h"

namespace content {

const int CacheStorageBlobToDiskCache::kBufferSize = 1024 * 512;

CacheStorageBlobToDiskCache::CacheStorageBlobToDiskCache()
    : cache_entry_offset_(0),
      disk_cache_body_index_(0),
      buffer_(new net::IOBufferWithSize(kBufferSize)),
      weak_ptr_factory_(this) {
}

CacheStorageBlobToDiskCache::~CacheStorageBlobToDiskCache() {
  if (blob_request_)
    request_context_getter_->RemoveObserver(this);
}

void CacheStorageBlobToDiskCache::StreamBlobToCache(
    disk_cache::ScopedEntryPtr entry,
    int disk_cache_body_index,
    net::URLRequestContextGetter* request_context_getter,
    scoped_ptr<storage::BlobDataHandle> blob_data_handle,
    const EntryAndBoolCallback& callback) {
  DCHECK(entry);
  DCHECK_LE(0, disk_cache_body_index);
  DCHECK(blob_data_handle);
  DCHECK(!blob_request_);
  DCHECK(request_context_getter);

  if (!request_context_getter->GetURLRequestContext()) {
    callback.Run(std::move(entry), false /* success */);
    return;
  }

  disk_cache_body_index_ = disk_cache_body_index;

  entry_ = std::move(entry);
  callback_ = callback;
  request_context_getter_ = request_context_getter;

  blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest(
      std::move(blob_data_handle),
      request_context_getter->GetURLRequestContext(), this);
  request_context_getter_->AddObserver(this);
  blob_request_->Start();
}

void CacheStorageBlobToDiskCache::OnResponseStarted(net::URLRequest* request) {
  if (!request->status().is_success()) {
    RunCallbackAndRemoveObserver(false);
    return;
  }

  ReadFromBlob();
}

void CacheStorageBlobToDiskCache::OnReadCompleted(net::URLRequest* request,
                                                  int bytes_read) {
  if (!request->status().is_success()) {
    RunCallbackAndRemoveObserver(false);
    return;
  }

  if (bytes_read == 0) {
    RunCallbackAndRemoveObserver(true);
    return;
  }

  net::CompletionCallback cache_write_callback =
      base::Bind(&CacheStorageBlobToDiskCache::DidWriteDataToEntry,
                 weak_ptr_factory_.GetWeakPtr(), bytes_read);

  int rv = entry_->WriteData(disk_cache_body_index_, cache_entry_offset_,
                             buffer_.get(), bytes_read, cache_write_callback,
                             true /* truncate */);
  if (rv != net::ERR_IO_PENDING)
    cache_write_callback.Run(rv);
}

void CacheStorageBlobToDiskCache::OnReceivedRedirect(
    net::URLRequest* request,
    const net::RedirectInfo& redirect_info,
    bool* defer_redirect) {
  NOTREACHED();
}

void CacheStorageBlobToDiskCache::OnAuthRequired(
    net::URLRequest* request,
    net::AuthChallengeInfo* auth_info) {
  NOTREACHED();
}
void CacheStorageBlobToDiskCache::OnCertificateRequested(
    net::URLRequest* request,
    net::SSLCertRequestInfo* cert_request_info) {
  NOTREACHED();
}
void CacheStorageBlobToDiskCache::OnSSLCertificateError(
    net::URLRequest* request,
    const net::SSLInfo& ssl_info,
    bool fatal) {
  NOTREACHED();
}
void CacheStorageBlobToDiskCache::OnBeforeNetworkStart(net::URLRequest* request,
                                                       bool* defer) {
  NOTREACHED();
}

void CacheStorageBlobToDiskCache::OnContextShuttingDown() {
  DCHECK(blob_request_);
  RunCallbackAndRemoveObserver(false);
}

void CacheStorageBlobToDiskCache::ReadFromBlob() {
  int bytes_read = 0;
  bool done = blob_request_->Read(buffer_.get(), buffer_->size(), &bytes_read);
  if (done)
    OnReadCompleted(blob_request_.get(), bytes_read);
}

void CacheStorageBlobToDiskCache::DidWriteDataToEntry(int expected_bytes,
                                                      int rv) {
  if (rv != expected_bytes) {
    RunCallbackAndRemoveObserver(false);
    return;
  }

  cache_entry_offset_ += rv;
  ReadFromBlob();
}

void CacheStorageBlobToDiskCache::RunCallbackAndRemoveObserver(bool success) {
  DCHECK(request_context_getter_);

  request_context_getter_->RemoveObserver(this);
  blob_request_.reset();
  callback_.Run(std::move(entry_), success);
}

}  // namespace content