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.
5 #include "content/browser/service_worker/service_worker_script_cache_map.h"
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"
15 ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap(
16 ServiceWorkerVersion
* owner
,
17 base::WeakPtr
<ServiceWorkerContextCore
> context
)
23 ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() {
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
;
33 void ServiceWorkerScriptCacheMap::NotifyStartedCaching(
34 const GURL
& url
, int64 resource_id
) {
35 DCHECK_EQ(kInvalidServiceWorkerResponseId
, Lookup(url
));
36 DCHECK(owner_
->status() == ServiceWorkerVersion::NEW
||
37 owner_
->status() == ServiceWorkerVersion::INSTALLING
);
38 resource_ids_
[url
] = resource_id
;
39 context_
->storage()->StoreUncommittedResponseId(resource_id
);
42 void ServiceWorkerScriptCacheMap::NotifyFinishedCaching(
43 const GURL
& url
, bool success
) {
44 DCHECK_NE(kInvalidServiceWorkerResponseId
, Lookup(url
));
45 DCHECK(owner_
->status() == ServiceWorkerVersion::NEW
||
46 owner_
->status() == ServiceWorkerVersion::INSTALLING
);
48 context_
->storage()->DoomUncommittedResponse(Lookup(url
));
50 resource_ids_
.erase(url
);
54 void 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
) {
60 ServiceWorkerDatabase::ResourceRecord(it
->second
, it
->first
));
64 void 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
;
74 } // namespace content