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. 4 5#include "content/browser/service_worker/service_worker_script_cache_map.h" 6 7#include "base/logging.h" 8#include "content/browser/service_worker/service_worker_context_core.h" 9#include "content/browser/service_worker/service_worker_storage.h" 10#include "content/browser/service_worker/service_worker_version.h" 11#include "content/common/service_worker/service_worker_types.h" 12 13namespace content { 14 15ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap( 16 ServiceWorkerVersion* owner, 17 base::WeakPtr<ServiceWorkerContextCore> context) 18 : owner_(owner), 19 context_(context) { 20} 21 22ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() { 23} 24 25int64 ServiceWorkerScriptCacheMap::Lookup(const GURL& url) { 26 ResourceIDMap::const_iterator found = resource_ids_.find(url); 27 if (found == resource_ids_.end()) 28 return kInvalidServiceWorkerResponseId; 29 return found->second; 30} 31 32void ServiceWorkerScriptCacheMap::NotifyStartedCaching( 33 const GURL& url, int64 resource_id) { 34 DCHECK_EQ(kInvalidServiceWorkerResponseId, Lookup(url)); 35 DCHECK(owner_->status() == ServiceWorkerVersion::NEW || 36 owner_->status() == ServiceWorkerVersion::INSTALLING); 37 resource_ids_[url] = resource_id; 38 context_->storage()->StoreUncommittedResponseId(resource_id); 39} 40 41void ServiceWorkerScriptCacheMap::NotifyFinishedCaching( 42 const GURL& url, const net::URLRequestStatus& status) { 43 DCHECK_NE(kInvalidServiceWorkerResponseId, Lookup(url)); 44 DCHECK(owner_->status() == ServiceWorkerVersion::NEW || 45 owner_->status() == ServiceWorkerVersion::INSTALLING); 46 if (!status.is_success()) { 47 context_->storage()->DoomUncommittedResponse(Lookup(url)); 48 resource_ids_.erase(url); 49 if (owner_->script_url() == url) 50 main_script_status_ = status; 51 } 52} 53 54void ServiceWorkerScriptCacheMap::GetResources( 55 std::vector<ServiceWorkerDatabase::ResourceRecord>* resources) { 56 DCHECK(resources->empty()); 57 for (ResourceIDMap::const_iterator it = resource_ids_.begin(); 58 it != resource_ids_.end(); ++it) { 59 resources->push_back( 60 ServiceWorkerDatabase::ResourceRecord(it->second, it->first)); 61 } 62} 63 64void ServiceWorkerScriptCacheMap::SetResources( 65 const std::vector<ServiceWorkerDatabase::ResourceRecord>& resources) { 66 DCHECK(resource_ids_.empty()); 67 typedef std::vector<ServiceWorkerDatabase::ResourceRecord> RecordVector; 68 for (RecordVector::const_iterator it = resources.begin(); 69 it != resources.end(); ++it) { 70 resource_ids_[it->url] = it->resource_id; 71 } 72} 73 74} // namespace content 75