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
)
22 ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() {
25 int64
ServiceWorkerScriptCacheMap::LookupResourceId(const GURL
& url
) {
26 ResourceMap::const_iterator found
= resource_map_
.find(url
);
27 if (found
== resource_map_
.end())
28 return kInvalidServiceWorkerResponseId
;
29 return found
->second
.resource_id
;
32 int64
ServiceWorkerScriptCacheMap::LookupResourceSize(const GURL
& url
) {
33 ResourceMap::const_iterator found
= resource_map_
.find(url
);
34 if (found
== resource_map_
.end())
35 return kInvalidServiceWorkerResponseId
;
36 return found
->second
.size_bytes
;
39 void ServiceWorkerScriptCacheMap::NotifyStartedCaching(
40 const GURL
& url
, int64 resource_id
) {
41 DCHECK_EQ(kInvalidServiceWorkerResponseId
, LookupResourceId(url
));
42 DCHECK(owner_
->status() == ServiceWorkerVersion::NEW
||
43 owner_
->status() == ServiceWorkerVersion::INSTALLING
);
45 ServiceWorkerDatabase::ResourceRecord(resource_id
, url
, -1);
46 context_
->storage()->StoreUncommittedResponseId(resource_id
);
49 void ServiceWorkerScriptCacheMap::NotifyFinishedCaching(
52 const net::URLRequestStatus
& status
) {
53 DCHECK_NE(kInvalidServiceWorkerResponseId
, LookupResourceId(url
));
54 DCHECK(owner_
->status() == ServiceWorkerVersion::NEW
||
55 owner_
->status() == ServiceWorkerVersion::INSTALLING
);
56 if (!status
.is_success()) {
57 context_
->storage()->DoomUncommittedResponse(LookupResourceId(url
));
58 resource_map_
.erase(url
);
59 if (owner_
->script_url() == url
)
60 main_script_status_
= status
;
62 resource_map_
[url
].size_bytes
= size_bytes
;
66 void ServiceWorkerScriptCacheMap::GetResources(
67 std::vector
<ServiceWorkerDatabase::ResourceRecord
>* resources
) {
68 DCHECK(resources
->empty());
69 for (ResourceMap::const_iterator it
= resource_map_
.begin();
70 it
!= resource_map_
.end();
72 resources
->push_back(it
->second
);
76 void ServiceWorkerScriptCacheMap::SetResources(
77 const std::vector
<ServiceWorkerDatabase::ResourceRecord
>& resources
) {
78 DCHECK(resource_map_
.empty());
79 typedef std::vector
<ServiceWorkerDatabase::ResourceRecord
> RecordVector
;
80 for (RecordVector::const_iterator it
= resources
.begin();
81 it
!= resources
.end(); ++it
) {
82 resource_map_
[it
->url
] = *it
;
86 } // namespace content