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/public/child/worker_thread.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 WorkerThread::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 // WorkerThread::Observer implementation.
40 void WillStopCurrentWorkerThread() 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() { return WorkerThread::GetCurrentId(); }
178 void PopulateWebResponseFromResponse(
179 const ServiceWorkerResponse
& response
,
180 blink::WebServiceWorkerResponse
* web_response
);
182 blink::WebVector
<blink::WebServiceWorkerResponse
> WebResponsesFromResponses(
183 const std::vector
<ServiceWorkerResponse
>& responses
);
185 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
187 CallbacksMap has_callbacks_
;
188 WithCacheCallbacksMap open_callbacks_
;
189 CallbacksMap delete_callbacks_
;
190 KeysCallbacksMap keys_callbacks_
;
191 StorageMatchCallbacksMap match_callbacks_
;
195 TimeMap delete_times_
;
197 TimeMap match_times_
;
199 // The individual caches created under this CacheStorage object.
200 IDMap
<WebCache
, IDMapExternalPointer
> web_caches_
;
202 // These ID maps are held in the CacheStorage object rather than the Cache
203 // object to ensure that the IDs are unique.
204 MatchCallbacksMap cache_match_callbacks_
;
205 WithResponsesCallbacksMap cache_match_all_callbacks_
;
206 WithRequestsCallbacksMap cache_keys_callbacks_
;
207 BatchCallbacksMap cache_batch_callbacks_
;
209 TimeMap cache_match_times_
;
210 TimeMap cache_match_all_times_
;
211 TimeMap cache_keys_times_
;
212 TimeMap cache_batch_times_
;
214 base::WeakPtrFactory
<CacheStorageDispatcher
> weak_factory_
;
216 DISALLOW_COPY_AND_ASSIGN(CacheStorageDispatcher
);
219 } // namespace content
221 #endif // CONTENT_RENDERER_CACHE_STORAGE_CACHE_STORAGE_DISPATCHER_H_