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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CACHE_MAP_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CACHE_MAP_H_
11 #include "base/basictypes.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/browser/service_worker/service_worker_database.h"
14 #include "content/common/content_export.h"
15 #include "net/base/completion_callback.h"
16 #include "net/url_request/url_request_status.h"
22 class ServiceWorkerContextCore
;
23 class ServiceWorkerVersion
;
24 class ServiceWorkerResponseMetadataWriter
;
26 // Class that maintains the mapping between urls and a resource id
27 // for a particular version's implicit script resources.
28 class CONTENT_EXPORT ServiceWorkerScriptCacheMap
{
30 int64
LookupResourceId(const GURL
& url
);
31 // A size of -1 means that we don't know the size yet
32 // (it has not finished caching).
33 int64
LookupResourceSize(const GURL
& url
);
35 // Used during the initial run of a new version to build the map
37 void NotifyStartedCaching(const GURL
& url
, int64 resource_id
);
38 void NotifyFinishedCaching(const GURL
& url
,
40 const net::URLRequestStatus
& status
,
41 const std::string
& status_message
);
43 // Used to retrieve the results of the initial run of a new version.
45 std::vector
<ServiceWorkerDatabase::ResourceRecord
>* resources
);
47 // Used when loading an existing version.
49 const std::vector
<ServiceWorkerDatabase::ResourceRecord
>& resources
);
51 // Writes the metadata of the existing script.
52 void WriteMetadata(const GURL
& url
,
53 const std::vector
<char>& data
,
54 const net::CompletionCallback
& callback
);
55 // Clears the metadata of the existing script.
56 void ClearMetadata(const GURL
& url
, const net::CompletionCallback
& callback
);
58 size_t size() const { return resource_map_
.size(); }
60 const net::URLRequestStatus
& main_script_status() const {
61 return main_script_status_
;
64 const std::string
& main_script_status_message() const {
65 return main_script_status_message_
;
69 typedef std::map
<GURL
, ServiceWorkerDatabase::ResourceRecord
> ResourceMap
;
71 // The version objects owns its script cache and provides a rawptr to it.
72 friend class ServiceWorkerVersion
;
73 ServiceWorkerScriptCacheMap(
74 ServiceWorkerVersion
* owner
,
75 base::WeakPtr
<ServiceWorkerContextCore
> context
);
76 ~ServiceWorkerScriptCacheMap();
78 void OnMetadataWritten(scoped_ptr
<ServiceWorkerResponseMetadataWriter
> writer
,
79 const net::CompletionCallback
& callback
,
82 ServiceWorkerVersion
* owner_
;
83 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
84 ResourceMap resource_map_
;
85 net::URLRequestStatus main_script_status_
;
86 std::string main_script_status_message_
;
88 base::WeakPtrFactory
<ServiceWorkerScriptCacheMap
> weak_factory_
;
90 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptCacheMap
);
93 } // namespace content
95 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CACHE_MAP_H_