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.
4
5#ifndef CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_DISPATCHER_H_
6#define CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_DISPATCHER_H_
7
8#include <vector>
9
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 "content/public/renderer/render_process_observer.h"
15#include "third_party/WebKit/public/platform/WebServiceWorkerCache.h"
16#include "third_party/WebKit/public/platform/WebServiceWorkerCacheError.h"
17#include "third_party/WebKit/public/platform/WebServiceWorkerCacheStorage.h"
18
19namespace content {
20
21struct ServiceWorkerFetchRequest;
22class ServiceWorkerScriptContext;
23struct ServiceWorkerResponse;
24
25// There is one ServiceWorkerCacheStorageDispatcher per
26// ServiceWorkerScriptContext. It handles CacheStorage operations with messages
27// to/from the browser, creating Cache objects (for which it also handles
28// messages) as it goes.
29
30class ServiceWorkerCacheStorageDispatcher
31    : public blink::WebServiceWorkerCacheStorage {
32 public:
33  explicit ServiceWorkerCacheStorageDispatcher(
34      ServiceWorkerScriptContext* script_context);
35  virtual ~ServiceWorkerCacheStorageDispatcher();
36
37  // ServiceWorkerScriptContext calls our OnMessageReceived directly.
38  bool OnMessageReceived(const IPC::Message& message);
39
40  // Message handlers for CacheStorage messages from the browser process.
41  void OnCacheStorageGetSuccess(int request_id, int cache_id);
42  void OnCacheStorageHasSuccess(int request_id);
43  void OnCacheStorageCreateSuccess(int request_id, int cache_id);
44  void OnCacheStorageDeleteSuccess(int request_id);
45  void OnCacheStorageKeysSuccess(int request_id,
46                                 const std::vector<base::string16>& keys);
47
48  void OnCacheStorageGetError(int request_id,
49                              blink::WebServiceWorkerCacheError reason);
50  void OnCacheStorageHasError(int request_id,
51                              blink::WebServiceWorkerCacheError reason);
52  void OnCacheStorageCreateError(int request_id,
53                                 blink::WebServiceWorkerCacheError reason);
54  void OnCacheStorageDeleteError(int request_id,
55                                 blink::WebServiceWorkerCacheError reason);
56  void OnCacheStorageKeysError(int request_id,
57                               blink::WebServiceWorkerCacheError reason);
58
59  // Message handlers for Cache messages from the browser process.
60  void OnCacheMatchSuccess(int request_id,
61                           const ServiceWorkerResponse& response);
62  void OnCacheMatchAllSuccess(
63      int request_id,
64      const std::vector<ServiceWorkerResponse>& response);
65  void OnCacheKeysSuccess(
66      int request_id,
67      const std::vector<ServiceWorkerFetchRequest>& response);
68  void OnCacheBatchSuccess(int request_id,
69                           const std::vector<ServiceWorkerResponse>& response);
70
71  void OnCacheMatchError(int request_id,
72                         blink::WebServiceWorkerCacheError reason);
73  void OnCacheMatchAllError(int request_id,
74                            blink::WebServiceWorkerCacheError reason);
75  void OnCacheKeysError(int request_id,
76                        blink::WebServiceWorkerCacheError reason);
77  void OnCacheBatchError(int request_id,
78                         blink::WebServiceWorkerCacheError reason);
79
80  // From WebServiceWorkerCacheStorage:
81  virtual void dispatchGet(CacheStorageWithCacheCallbacks* callbacks,
82                           const blink::WebString& cacheName);
83  virtual void dispatchHas(CacheStorageCallbacks* callbacks,
84                           const blink::WebString& cacheName);
85  virtual void dispatchCreate(CacheStorageWithCacheCallbacks* callbacks,
86                              const blink::WebString& cacheName);
87  virtual void dispatchDelete(CacheStorageCallbacks* callbacks,
88                              const blink::WebString& cacheName);
89  virtual void dispatchKeys(CacheStorageKeysCallbacks* callbacks);
90
91  // These methods are used by WebCache to forward events to the browser
92  // process.
93  void dispatchMatchForCache(
94      int cache_id,
95      blink::WebServiceWorkerCache::CacheMatchCallbacks* callbacks,
96      const blink::WebServiceWorkerRequest& request,
97      const blink::WebServiceWorkerCache::QueryParams& query_params);
98  void dispatchMatchAllForCache(
99      int cache_id,
100      blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks,
101      const blink::WebServiceWorkerRequest& request,
102      const blink::WebServiceWorkerCache::QueryParams& query_params);
103  void dispatchKeysForCache(
104      int cache_id,
105      blink::WebServiceWorkerCache::CacheWithRequestsCallbacks* callbacks,
106      const blink::WebServiceWorkerRequest* request,
107      const blink::WebServiceWorkerCache::QueryParams& query_params);
108  void dispatchBatchForCache(
109      int cache_id,
110      blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks,
111      const blink::WebVector<blink::WebServiceWorkerCache::BatchOperation>&
112          batch_operations);
113
114  void OnWebCacheDestruction(int cache_id);
115
116 private:
117  class WebCache;
118
119  typedef IDMap<CacheStorageCallbacks, IDMapOwnPointer> CallbacksMap;
120  typedef IDMap<CacheStorageWithCacheCallbacks, IDMapOwnPointer>
121      WithCacheCallbacksMap;
122  typedef IDMap<CacheStorageKeysCallbacks, IDMapOwnPointer>
123      KeysCallbacksMap;
124
125  typedef IDMap<blink::WebServiceWorkerCache::CacheMatchCallbacks,
126                IDMapOwnPointer> MatchCallbacksMap;
127  typedef IDMap<blink::WebServiceWorkerCache::CacheWithResponsesCallbacks,
128                IDMapOwnPointer> WithResponsesCallbacksMap;
129  typedef IDMap<blink::WebServiceWorkerCache::CacheWithRequestsCallbacks,
130                IDMapOwnPointer> WithRequestsCallbacksMap;
131
132  // Not owned. The script context containing this object.
133  ServiceWorkerScriptContext* script_context_;
134
135  WithCacheCallbacksMap get_callbacks_;
136  CallbacksMap has_callbacks_;
137  WithCacheCallbacksMap create_callbacks_;
138  CallbacksMap delete_callbacks_;
139  KeysCallbacksMap keys_callbacks_;
140
141  // The individual caches created under this CacheStorage object.
142  IDMap<WebCache, IDMapExternalPointer> web_caches_;
143
144  // These ID maps are held in the CacheStorage object rather than the Cache
145  // object to ensure that the IDs are unique.
146  MatchCallbacksMap cache_match_callbacks_;
147  WithResponsesCallbacksMap cache_match_all_callbacks_;
148  WithRequestsCallbacksMap cache_keys_callbacks_;
149  WithResponsesCallbacksMap cache_batch_callbacks_;
150
151  base::WeakPtrFactory<ServiceWorkerCacheStorageDispatcher> weak_factory_;
152
153  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorageDispatcher);
154};
155
156}  // namespace content
157
158#endif  // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_DISPATCHER_H_
159