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_RENDERER_CACHE_STORAGE_CACHE_STORAGE_DISPATCHER_H_
6 #define CONTENT_RENDERER_CACHE_STORAGE_CACHE_STORAGE_DISPATCHER_H_
10 #include "base/id_map.h"
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/time/time.h"
15 #include "content/child/worker_task_runner.h"
16 #include "content/public/renderer/render_process_observer.h"
17 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCache.h"
18 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCacheError.h"
19 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCacheStorage.h"
23 class ThreadSafeSender
;
24 struct ServiceWorkerFetchRequest
;
25 struct ServiceWorkerResponse
;
27 // Handle the Cache Storage messaging for this context thread. The
28 // main thread and each worker thread have their own instances.
29 class CacheStorageDispatcher
: public WorkerTaskRunner::Observer
{
31 explicit CacheStorageDispatcher(ThreadSafeSender
* thread_safe_sender
);
32 ~CacheStorageDispatcher() override
;
34 // |thread_safe_sender| needs to be passed in because if the call leads to
35 // construction it will be needed.
36 static CacheStorageDispatcher
* ThreadSpecificInstance(
37 ThreadSafeSender
* thread_safe_sender
);
39 // WorkerTaskRunner::Observer implementation.
40 void OnWorkerRunLoopStopped() override
;
42 bool Send(IPC::Message
* msg
);
44 // ServiceWorkerScriptContext calls our OnMessageReceived directly.
45 bool OnMessageReceived(const IPC::Message
& message
);
47 // Message handlers for CacheStorage messages from the browser process.
48 void OnCacheStorageHasSuccess(int thread_id
, int request_id
);
49 void OnCacheStorageOpenSuccess(int thread_id
, int request_id
, int cache_id
);
50 void OnCacheStorageDeleteSuccess(int thread_id
, int request_id
);
51 void OnCacheStorageKeysSuccess(int thread_id
,
53 const std::vector
<base::string16
>& keys
);
54 void OnCacheStorageMatchSuccess(int thread_id
,
56 const ServiceWorkerResponse
& response
);
58 void OnCacheStorageHasError(int thread_id
,
60 blink::WebServiceWorkerCacheError reason
);
61 void OnCacheStorageOpenError(int thread_id
,
63 blink::WebServiceWorkerCacheError reason
);
64 void OnCacheStorageDeleteError(int thread_id
,
66 blink::WebServiceWorkerCacheError reason
);
67 void OnCacheStorageKeysError(int thread_id
,
69 blink::WebServiceWorkerCacheError reason
);
70 void OnCacheStorageMatchError(int thread_id
,
72 blink::WebServiceWorkerCacheError reason
);
74 // Message handlers for Cache messages from the browser process.
75 void OnCacheMatchSuccess(int thread_id
,
77 const ServiceWorkerResponse
& response
);
78 void OnCacheMatchAllSuccess(
81 const std::vector
<ServiceWorkerResponse
>& response
);
82 void OnCacheKeysSuccess(
85 const std::vector
<ServiceWorkerFetchRequest
>& response
);
86 void OnCacheBatchSuccess(int thread_id
,
89 void OnCacheMatchError(int thread_id
,
91 blink::WebServiceWorkerCacheError reason
);
92 void OnCacheMatchAllError(int thread_id
,
94 blink::WebServiceWorkerCacheError reason
);
95 void OnCacheKeysError(int thread_id
,
97 blink::WebServiceWorkerCacheError reason
);
98 void OnCacheBatchError(int thread_id
,
100 blink::WebServiceWorkerCacheError reason
);
102 // TODO(jsbell): These are only called by WebServiceWorkerCacheStorageImpl
103 // and should be renamed to match Chromium conventions. crbug.com/439389
105 blink::WebServiceWorkerCacheStorage::CacheStorageCallbacks
* callbacks
,
107 const blink::WebString
& cacheName
);
109 blink::WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks
*
112 const blink::WebString
& cacheName
);
114 blink::WebServiceWorkerCacheStorage::CacheStorageCallbacks
* callbacks
,
116 const blink::WebString
& cacheName
);
118 blink::WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks
* callbacks
,
121 blink::WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks
*
124 const blink::WebServiceWorkerRequest
& request
,
125 const blink::WebServiceWorkerCache::QueryParams
& query_params
);
127 // These methods are used by WebCache to forward events to the browser
129 void dispatchMatchForCache(
131 blink::WebServiceWorkerCache::CacheMatchCallbacks
* callbacks
,
132 const blink::WebServiceWorkerRequest
& request
,
133 const blink::WebServiceWorkerCache::QueryParams
& query_params
);
134 void dispatchMatchAllForCache(
136 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks
* callbacks
,
137 const blink::WebServiceWorkerRequest
& request
,
138 const blink::WebServiceWorkerCache::QueryParams
& query_params
);
139 void dispatchKeysForCache(
141 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks
* callbacks
,
142 const blink::WebServiceWorkerRequest
* request
,
143 const blink::WebServiceWorkerCache::QueryParams
& query_params
);
144 void dispatchBatchForCache(
146 blink::WebServiceWorkerCache::CacheBatchCallbacks
* callbacks
,
147 const blink::WebVector
<blink::WebServiceWorkerCache::BatchOperation
>&
150 void OnWebCacheDestruction(int cache_id
);
155 typedef IDMap
<blink::WebServiceWorkerCacheStorage::CacheStorageCallbacks
,
156 IDMapOwnPointer
> CallbacksMap
;
158 blink::WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks
,
159 IDMapOwnPointer
> WithCacheCallbacksMap
;
160 typedef IDMap
<blink::WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks
,
161 IDMapOwnPointer
> KeysCallbacksMap
;
162 typedef IDMap
<blink::WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks
,
163 IDMapOwnPointer
> StorageMatchCallbacksMap
;
165 typedef base::hash_map
<int32
, base::TimeTicks
> TimeMap
;
167 typedef IDMap
<blink::WebServiceWorkerCache::CacheMatchCallbacks
,
168 IDMapOwnPointer
> MatchCallbacksMap
;
169 typedef IDMap
<blink::WebServiceWorkerCache::CacheWithResponsesCallbacks
,
170 IDMapOwnPointer
> WithResponsesCallbacksMap
;
171 typedef IDMap
<blink::WebServiceWorkerCache::CacheWithRequestsCallbacks
,
172 IDMapOwnPointer
> WithRequestsCallbacksMap
;
173 using BatchCallbacksMap
=
174 IDMap
<blink::WebServiceWorkerCache::CacheBatchCallbacks
, IDMapOwnPointer
>;
176 static int32
CurrentWorkerId() {
177 return WorkerTaskRunner::Instance()->CurrentWorkerId();
180 void PopulateWebResponseFromResponse(
181 const ServiceWorkerResponse
& response
,
182 blink::WebServiceWorkerResponse
* web_response
);
184 blink::WebVector
<blink::WebServiceWorkerResponse
> WebResponsesFromResponses(
185 const std::vector
<ServiceWorkerResponse
>& responses
);
187 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
189 CallbacksMap has_callbacks_
;
190 WithCacheCallbacksMap open_callbacks_
;
191 CallbacksMap delete_callbacks_
;
192 KeysCallbacksMap keys_callbacks_
;
193 StorageMatchCallbacksMap match_callbacks_
;
197 TimeMap delete_times_
;
199 TimeMap match_times_
;
201 // The individual caches created under this CacheStorage object.
202 IDMap
<WebCache
, IDMapExternalPointer
> web_caches_
;
204 // These ID maps are held in the CacheStorage object rather than the Cache
205 // object to ensure that the IDs are unique.
206 MatchCallbacksMap cache_match_callbacks_
;
207 WithResponsesCallbacksMap cache_match_all_callbacks_
;
208 WithRequestsCallbacksMap cache_keys_callbacks_
;
209 BatchCallbacksMap cache_batch_callbacks_
;
211 TimeMap cache_match_times_
;
212 TimeMap cache_match_all_times_
;
213 TimeMap cache_keys_times_
;
214 TimeMap cache_batch_times_
;
216 base::WeakPtrFactory
<CacheStorageDispatcher
> weak_factory_
;
218 DISALLOW_COPY_AND_ASSIGN(CacheStorageDispatcher
);
221 } // namespace content
223 #endif // CONTENT_RENDERER_CACHE_STORAGE_CACHE_STORAGE_DISPATCHER_H_