Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / renderer / cache_storage / cache_storage_dispatcher.h
blobf08712ce41700be51f76769a67ee6bfcc48780be
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_
8 #include <vector>
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"
21 namespace content {
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 {
30 public:
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,
52 int request_id,
53 const std::vector<base::string16>& keys);
54 void OnCacheStorageMatchSuccess(int thread_id,
55 int request_id,
56 const ServiceWorkerResponse& response);
58 void OnCacheStorageHasError(int thread_id,
59 int request_id,
60 blink::WebServiceWorkerCacheError reason);
61 void OnCacheStorageOpenError(int thread_id,
62 int request_id,
63 blink::WebServiceWorkerCacheError reason);
64 void OnCacheStorageDeleteError(int thread_id,
65 int request_id,
66 blink::WebServiceWorkerCacheError reason);
67 void OnCacheStorageKeysError(int thread_id,
68 int request_id,
69 blink::WebServiceWorkerCacheError reason);
70 void OnCacheStorageMatchError(int thread_id,
71 int request_id,
72 blink::WebServiceWorkerCacheError reason);
74 // Message handlers for Cache messages from the browser process.
75 void OnCacheMatchSuccess(int thread_id,
76 int request_id,
77 const ServiceWorkerResponse& response);
78 void OnCacheMatchAllSuccess(
79 int thread_id,
80 int request_id,
81 const std::vector<ServiceWorkerResponse>& response);
82 void OnCacheKeysSuccess(
83 int thread_id,
84 int request_id,
85 const std::vector<ServiceWorkerFetchRequest>& response);
86 void OnCacheBatchSuccess(int thread_id,
87 int request_id);
89 void OnCacheMatchError(int thread_id,
90 int request_id,
91 blink::WebServiceWorkerCacheError reason);
92 void OnCacheMatchAllError(int thread_id,
93 int request_id,
94 blink::WebServiceWorkerCacheError reason);
95 void OnCacheKeysError(int thread_id,
96 int request_id,
97 blink::WebServiceWorkerCacheError reason);
98 void OnCacheBatchError(int thread_id,
99 int request_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
104 void dispatchHas(
105 blink::WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks,
106 const GURL& origin,
107 const blink::WebString& cacheName);
108 void dispatchOpen(
109 blink::WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks*
110 callbacks,
111 const GURL& origin,
112 const blink::WebString& cacheName);
113 void dispatchDelete(
114 blink::WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks,
115 const GURL& origin,
116 const blink::WebString& cacheName);
117 void dispatchKeys(
118 blink::WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks* callbacks,
119 const GURL& origin);
120 void dispatchMatch(
121 blink::WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks*
122 callbacks,
123 const GURL& origin,
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
128 // process.
129 void dispatchMatchForCache(
130 int cache_id,
131 blink::WebServiceWorkerCache::CacheMatchCallbacks* callbacks,
132 const blink::WebServiceWorkerRequest& request,
133 const blink::WebServiceWorkerCache::QueryParams& query_params);
134 void dispatchMatchAllForCache(
135 int cache_id,
136 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks,
137 const blink::WebServiceWorkerRequest& request,
138 const blink::WebServiceWorkerCache::QueryParams& query_params);
139 void dispatchKeysForCache(
140 int cache_id,
141 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks* callbacks,
142 const blink::WebServiceWorkerRequest* request,
143 const blink::WebServiceWorkerCache::QueryParams& query_params);
144 void dispatchBatchForCache(
145 int cache_id,
146 blink::WebServiceWorkerCache::CacheBatchCallbacks* callbacks,
147 const blink::WebVector<blink::WebServiceWorkerCache::BatchOperation>&
148 batch_operations);
150 void OnWebCacheDestruction(int cache_id);
152 private:
153 class WebCache;
155 typedef IDMap<blink::WebServiceWorkerCacheStorage::CacheStorageCallbacks,
156 IDMapOwnPointer> CallbacksMap;
157 typedef IDMap<
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_;
193 TimeMap has_times_;
194 TimeMap open_times_;
195 TimeMap delete_times_;
196 TimeMap keys_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_