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
|
// 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/service_worker/service_worker_script_cache_map.h"
#include "base/logging.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_disk_cache.h"
#include "content/browser/service_worker/service_worker_storage.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/common/service_worker/service_worker_types.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
namespace content {
ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap(
ServiceWorkerVersion* owner,
base::WeakPtr<ServiceWorkerContextCore> context)
: owner_(owner), context_(context), weak_factory_(this) {
}
ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() {
}
int64_t ServiceWorkerScriptCacheMap::LookupResourceId(const GURL& url) {
ResourceMap::const_iterator found = resource_map_.find(url);
if (found == resource_map_.end())
return kInvalidServiceWorkerResourceId;
return found->second.resource_id;
}
void ServiceWorkerScriptCacheMap::NotifyStartedCaching(const GURL& url,
int64_t resource_id) {
DCHECK_EQ(kInvalidServiceWorkerResourceId, LookupResourceId(url));
DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
owner_->status() == ServiceWorkerVersion::INSTALLING)
<< owner_->status();
if (!context_) {
if (IsMainScript(url)) {
main_script_start_status_ = StartStatus::NO_CONTEXT;
}
return; // Our storage has been wiped via DeleteAndStartOver.
}
if (IsMainScript(url)) {
main_script_start_status_ = StartStatus::STARTED;
}
resource_map_[url] =
ServiceWorkerDatabase::ResourceRecord(resource_id, url, -1);
context_->storage()->StoreUncommittedResourceId(resource_id);
}
void ServiceWorkerScriptCacheMap::NotifyFinishedCaching(
const GURL& url,
int64_t size_bytes,
net::Error net_error,
const std::string& status_message) {
DCHECK_NE(kInvalidServiceWorkerResourceId, LookupResourceId(url));
DCHECK_NE(net::ERR_IO_PENDING, net_error);
DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
owner_->status() == ServiceWorkerVersion::INSTALLING ||
owner_->status() == ServiceWorkerVersion::REDUNDANT);
if (!context_) {
if (IsMainScript(url)) {
main_script_finish_status_ = FinishStatus::NO_CONTEXT;
}
return; // Our storage has been wiped via DeleteAndStartOver.
}
if (net_error != net::OK) {
if (IsMainScript(url)) {
main_script_finish_status_ = FinishStatus::NET_ERROR;
}
context_->storage()->DoomUncommittedResource(LookupResourceId(url));
resource_map_.erase(url);
if (IsMainScript(url)) {
main_script_status_ = net::URLRequestStatus::FromError(net_error);
main_script_status_message_ = status_message;
}
} else {
if (IsMainScript(url)) {
main_script_finish_status_ = FinishStatus::FINISHED;
}
resource_map_[url].size_bytes = size_bytes;
}
}
void ServiceWorkerScriptCacheMap::GetResources(
std::vector<ServiceWorkerDatabase::ResourceRecord>* resources) {
DCHECK(resources->empty());
for (ResourceMap::const_iterator it = resource_map_.begin();
it != resource_map_.end();
++it) {
resources->push_back(it->second);
}
}
void ServiceWorkerScriptCacheMap::SetResources(
const std::vector<ServiceWorkerDatabase::ResourceRecord>& resources) {
DCHECK(resource_map_.empty());
typedef std::vector<ServiceWorkerDatabase::ResourceRecord> RecordVector;
for (RecordVector::const_iterator it = resources.begin();
it != resources.end(); ++it) {
resource_map_[it->url] = *it;
}
}
void ServiceWorkerScriptCacheMap::WriteMetadata(
const GURL& url,
const std::vector<char>& data,
const net::CompletionCallback& callback) {
ResourceMap::iterator found = resource_map_.find(url);
if (found == resource_map_.end() ||
found->second.resource_id == kInvalidServiceWorkerResourceId) {
callback.Run(net::ERR_FILE_NOT_FOUND);
return;
}
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(data.size()));
if (data.size())
memmove(buffer->data(), &data[0], data.size());
std::unique_ptr<ServiceWorkerResponseMetadataWriter> writer;
writer = context_->storage()->CreateResponseMetadataWriter(
found->second.resource_id);
ServiceWorkerResponseMetadataWriter* raw_writer = writer.get();
raw_writer->WriteMetadata(
buffer.get(), data.size(),
base::Bind(&ServiceWorkerScriptCacheMap::OnMetadataWritten,
weak_factory_.GetWeakPtr(), base::Passed(&writer), callback));
}
void ServiceWorkerScriptCacheMap::ClearMetadata(
const GURL& url,
const net::CompletionCallback& callback) {
WriteMetadata(url, std::vector<char>(), callback);
}
void ServiceWorkerScriptCacheMap::OnMetadataWritten(
std::unique_ptr<ServiceWorkerResponseMetadataWriter> writer,
const net::CompletionCallback& callback,
int result) {
callback.Run(result);
}
bool ServiceWorkerScriptCacheMap::IsMainScript(const GURL& url) {
return owner_->script_url() == url;
}
} // namespace content
|