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_disk_cache.h"
10 #include "content/browser/service_worker/service_worker_storage.h"
11 #include "content/browser/service_worker/service_worker_version.h"
12 #include "content/common/service_worker/service_worker_types.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
18 ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap(
19 ServiceWorkerVersion
* owner
,
20 base::WeakPtr
<ServiceWorkerContextCore
> context
)
21 : owner_(owner
), context_(context
), weak_factory_(this) {
24 ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() {
27 int64
ServiceWorkerScriptCacheMap::LookupResourceId(const GURL
& url
) {
28 ResourceMap::const_iterator found
= resource_map_
.find(url
);
29 if (found
== resource_map_
.end())
30 return kInvalidServiceWorkerResponseId
;
31 return found
->second
.resource_id
;
34 int64
ServiceWorkerScriptCacheMap::LookupResourceSize(const GURL
& url
) {
35 ResourceMap::const_iterator found
= resource_map_
.find(url
);
36 if (found
== resource_map_
.end())
37 return kInvalidServiceWorkerResponseId
;
38 return found
->second
.size_bytes
;
41 void ServiceWorkerScriptCacheMap::NotifyStartedCaching(
42 const GURL
& url
, int64 resource_id
) {
43 DCHECK_EQ(kInvalidServiceWorkerResponseId
, LookupResourceId(url
));
44 DCHECK(owner_
->status() == ServiceWorkerVersion::NEW
||
45 owner_
->status() == ServiceWorkerVersion::INSTALLING
);
47 return; // Our storage has been wiped via DeleteAndStartOver.
49 ServiceWorkerDatabase::ResourceRecord(resource_id
, url
, -1);
50 context_
->storage()->StoreUncommittedResponseId(resource_id
);
53 void ServiceWorkerScriptCacheMap::NotifyFinishedCaching(
56 const net::URLRequestStatus
& status
,
57 const std::string
& status_message
) {
58 DCHECK_NE(kInvalidServiceWorkerResponseId
, LookupResourceId(url
));
59 DCHECK(owner_
->status() == ServiceWorkerVersion::NEW
||
60 owner_
->status() == ServiceWorkerVersion::INSTALLING
||
61 owner_
->status() == ServiceWorkerVersion::REDUNDANT
);
63 return; // Our storage has been wiped via DeleteAndStartOver.
64 if (!status
.is_success()) {
65 context_
->storage()->DoomUncommittedResponse(LookupResourceId(url
));
66 resource_map_
.erase(url
);
67 if (owner_
->script_url() == url
) {
68 main_script_status_
= status
;
69 main_script_status_message_
= status_message
;
72 resource_map_
[url
].size_bytes
= size_bytes
;
76 void ServiceWorkerScriptCacheMap::GetResources(
77 std::vector
<ServiceWorkerDatabase::ResourceRecord
>* resources
) {
78 DCHECK(resources
->empty());
79 for (ResourceMap::const_iterator it
= resource_map_
.begin();
80 it
!= resource_map_
.end();
82 resources
->push_back(it
->second
);
86 void ServiceWorkerScriptCacheMap::SetResources(
87 const std::vector
<ServiceWorkerDatabase::ResourceRecord
>& resources
) {
88 DCHECK(resource_map_
.empty());
89 typedef std::vector
<ServiceWorkerDatabase::ResourceRecord
> RecordVector
;
90 for (RecordVector::const_iterator it
= resources
.begin();
91 it
!= resources
.end(); ++it
) {
92 resource_map_
[it
->url
] = *it
;
96 void ServiceWorkerScriptCacheMap::WriteMetadata(
98 const std::vector
<char>& data
,
99 const net::CompletionCallback
& callback
) {
100 ResourceMap::iterator found
= resource_map_
.find(url
);
101 if (found
== resource_map_
.end() ||
102 found
->second
.resource_id
== kInvalidServiceWorkerResponseId
) {
103 callback
.Run(net::ERR_FILE_NOT_FOUND
);
106 scoped_refptr
<net::IOBuffer
> buffer(new net::IOBuffer(data
.size()));
108 memmove(buffer
->data(), &data
[0], data
.size());
109 scoped_ptr
<ServiceWorkerResponseMetadataWriter
> writer
;
110 writer
= context_
->storage()->CreateResponseMetadataWriter(
111 found
->second
.resource_id
);
112 ServiceWorkerResponseMetadataWriter
* raw_writer
= writer
.get();
113 raw_writer
->WriteMetadata(
114 buffer
.get(), data
.size(),
115 base::Bind(&ServiceWorkerScriptCacheMap::OnMetadataWritten
,
116 weak_factory_
.GetWeakPtr(), Passed(&writer
), callback
));
119 void ServiceWorkerScriptCacheMap::ClearMetadata(
121 const net::CompletionCallback
& callback
) {
122 WriteMetadata(url
, std::vector
<char>(), callback
);
125 void ServiceWorkerScriptCacheMap::OnMetadataWritten(
126 scoped_ptr
<ServiceWorkerResponseMetadataWriter
> writer
,
127 const net::CompletionCallback
& callback
,
129 callback
.Run(result
);
132 } // namespace content