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
|
// 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_storage.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/common/service_worker/service_worker_types.h"
namespace content {
ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap(
ServiceWorkerVersion* owner,
base::WeakPtr<ServiceWorkerContextCore> context)
: owner_(owner),
context_(context) {
}
ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() {
}
int64 ServiceWorkerScriptCacheMap::LookupResourceId(const GURL& url) {
ResourceMap::const_iterator found = resource_map_.find(url);
if (found == resource_map_.end())
return kInvalidServiceWorkerResponseId;
return found->second.resource_id;
}
int64 ServiceWorkerScriptCacheMap::LookupResourceSize(const GURL& url) {
ResourceMap::const_iterator found = resource_map_.find(url);
if (found == resource_map_.end())
return kInvalidServiceWorkerResponseId;
return found->second.size_bytes;
}
void ServiceWorkerScriptCacheMap::NotifyStartedCaching(
const GURL& url, int64 resource_id) {
DCHECK_EQ(kInvalidServiceWorkerResponseId, LookupResourceId(url));
DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
owner_->status() == ServiceWorkerVersion::INSTALLING);
if (!context_)
return; // Our storage has been wiped via DeleteAndStartOver.
resource_map_[url] =
ServiceWorkerDatabase::ResourceRecord(resource_id, url, -1);
context_->storage()->StoreUncommittedResponseId(resource_id);
}
void ServiceWorkerScriptCacheMap::NotifyFinishedCaching(
const GURL& url,
int64 size_bytes,
const net::URLRequestStatus& status) {
DCHECK_NE(kInvalidServiceWorkerResponseId, LookupResourceId(url));
DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
owner_->status() == ServiceWorkerVersion::INSTALLING);
if (!context_)
return; // Our storage has been wiped via DeleteAndStartOver.
if (!status.is_success()) {
context_->storage()->DoomUncommittedResponse(LookupResourceId(url));
resource_map_.erase(url);
if (owner_->script_url() == url)
main_script_status_ = status;
} else {
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;
}
}
} // namespace content
|