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
13 namespace content {
14
ServiceWorkerScriptCacheMap(ServiceWorkerVersion * owner,base::WeakPtr<ServiceWorkerContextCore> context)15 ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap(
16 ServiceWorkerVersion* owner,
17 base::WeakPtr<ServiceWorkerContextCore> context)
18 : owner_(owner),
19 context_(context),
20 has_error_(false) {
21 }
22
~ServiceWorkerScriptCacheMap()23 ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() {
24 }
25
Lookup(const GURL & url)26 int64 ServiceWorkerScriptCacheMap::Lookup(const GURL& url) {
27 ResourceIDMap::const_iterator found = resource_ids_.find(url);
28 if (found == resource_ids_.end())
29 return kInvalidServiceWorkerResponseId;
30 return found->second;
31 }
32
NotifyStartedCaching(const GURL & url,int64 resource_id)33 void ServiceWorkerScriptCacheMap::NotifyStartedCaching(
34 const GURL& url, int64 resource_id) {
35 DCHECK_EQ(kInvalidServiceWorkerResponseId, Lookup(url));
36 DCHECK(owner_->status() == ServiceWorkerVersion::NEW);
37 resource_ids_[url] = resource_id;
38 context_->storage()->StoreUncommittedReponseId(resource_id);
39 }
40
NotifyFinishedCaching(const GURL & url,bool success)41 void ServiceWorkerScriptCacheMap::NotifyFinishedCaching(
42 const GURL& url, bool success) {
43 DCHECK_NE(kInvalidServiceWorkerResponseId, Lookup(url));
44 DCHECK(owner_->status() == ServiceWorkerVersion::NEW);
45 if (!success) {
46 context_->storage()->DoomUncommittedResponse(Lookup(url));
47 has_error_ = true;
48 resource_ids_.erase(url);
49 }
50 }
51
GetResources(std::vector<ServiceWorkerDatabase::ResourceRecord> * resources)52 void ServiceWorkerScriptCacheMap::GetResources(
53 std::vector<ServiceWorkerDatabase::ResourceRecord>* resources) {
54 DCHECK(resources->empty());
55 for (ResourceIDMap::const_iterator it = resource_ids_.begin();
56 it != resource_ids_.end(); ++it) {
57 resources->push_back(
58 ServiceWorkerDatabase::ResourceRecord(it->second, it->first));
59 }
60 }
61
SetResources(const std::vector<ServiceWorkerDatabase::ResourceRecord> & resources)62 void ServiceWorkerScriptCacheMap::SetResources(
63 const std::vector<ServiceWorkerDatabase::ResourceRecord>& resources) {
64 DCHECK(resource_ids_.empty());
65 typedef std::vector<ServiceWorkerDatabase::ResourceRecord> RecordVector;
66 for (RecordVector::const_iterator it = resources.begin();
67 it != resources.end(); ++it) {
68 resource_ids_[it->url] = it->resource_id;
69 }
70 }
71
72 } // namespace content
73