Decouple Cache Storage messaging from Service Worker/Embedded Worker
[chromium-blink-merge.git] / content / renderer / service_worker / service_worker_script_context.h
blob07b58ca050776d8beb1cdda57360a23c409af18a
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_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_
6 #define CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/id_map.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/time/time.h"
17 #include "content/child/webmessageportchannel_impl.h"
18 #include "content/common/service_worker/service_worker_types.h"
19 #include "content/renderer/service_worker/service_worker_cache_storage_dispatcher.h"
20 #include "content/renderer/service_worker/webserviceworkercachestorage_impl.h"
21 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h"
22 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h"
23 #include "third_party/WebKit/public/platform/WebServiceWorkerCacheStorage.h"
24 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsClaimCallbacks.h"
25 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
26 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
27 #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h"
28 #include "third_party/WebKit/public/platform/WebServiceWorkerSkipWaitingCallbacks.h"
30 namespace blink {
31 struct WebCircularGeofencingRegion;
32 struct WebCrossOriginServiceWorkerClient;
33 struct WebServiceWorkerClientQueryOptions;
34 class WebServiceWorkerContextProxy;
37 namespace IPC {
38 class Message;
41 namespace content {
43 class EmbeddedWorkerContextClient;
44 class WebServiceWorkerRegistrationImpl;
45 struct NavigatorConnectClient;
46 struct PlatformNotificationData;
47 struct ServiceWorkerClientInfo;
49 // TODO(kinuko): This should implement WebServiceWorkerContextClient
50 // rather than having EmbeddedWorkerContextClient implement it.
51 // See the header comment in embedded_worker_context_client.h for the
52 // potential EW/SW layering concerns.
53 class ServiceWorkerScriptContext {
54 public:
55 ServiceWorkerScriptContext(
56 EmbeddedWorkerContextClient* embedded_context,
57 blink::WebServiceWorkerContextProxy* proxy);
58 ~ServiceWorkerScriptContext();
60 void OnMessageReceived(const IPC::Message& message);
62 void SetRegistrationInServiceWorkerGlobalScope(
63 scoped_ptr<WebServiceWorkerRegistrationImpl> registration);
64 void DidHandleActivateEvent(int request_id,
65 blink::WebServiceWorkerEventResult);
66 void DidHandleInstallEvent(int request_id,
67 blink::WebServiceWorkerEventResult result);
68 void DidHandleFetchEvent(int request_id,
69 ServiceWorkerFetchEventResult result,
70 const ServiceWorkerResponse& response);
71 void DidHandleNotificationClickEvent(
72 int request_id,
73 blink::WebServiceWorkerEventResult result);
74 void DidHandlePushEvent(int request_id,
75 blink::WebServiceWorkerEventResult result);
76 void DidHandleSyncEvent(int request_id);
77 void DidHandleCrossOriginConnectEvent(int request_id, bool accept_connection);
78 void GetClients(
79 const blink::WebServiceWorkerClientQueryOptions& options,
80 blink::WebServiceWorkerClientsCallbacks* callbacks);
81 void OpenWindow(const GURL& url,
82 blink::WebServiceWorkerClientCallbacks* callbacks);
83 void SetCachedMetadata(const GURL& url, const char* data, size_t size);
84 void ClearCachedMetadata(const GURL& url);
85 void PostMessageToClient(
86 const base::string16& uuid,
87 const base::string16& message,
88 scoped_ptr<blink::WebMessagePortChannelArray> channels);
89 void PostCrossOriginMessageToClient(
90 const blink::WebCrossOriginServiceWorkerClient& client,
91 const base::string16& message,
92 scoped_ptr<blink::WebMessagePortChannelArray> channels);
93 void FocusClient(const base::string16& uuid,
94 blink::WebServiceWorkerClientCallbacks* callback);
95 void SkipWaiting(blink::WebServiceWorkerSkipWaitingCallbacks* callbacks);
96 void ClaimClients(blink::WebServiceWorkerClientsClaimCallbacks* callbacks);
98 // Send a message to the browser. Takes ownership of |message|.
99 void Send(IPC::Message* message);
101 // Get routing_id for sending message to the ServiceWorkerVersion
102 // in the browser process.
103 int GetRoutingID() const;
105 // TODO(jsbell): Remove when CacheStorage creation is moved. crbug.com/439389
106 blink::WebServiceWorkerCacheStorage* cache_storage() const {
107 return cache_storage_.get();
110 private:
111 typedef IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer>
112 ClientsCallbacksMap;
113 typedef IDMap<blink::WebServiceWorkerClientsClaimCallbacks, IDMapOwnPointer>
114 ClaimClientsCallbacksMap;
115 typedef IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>
116 ClientCallbacksMap;
117 typedef IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>
118 SkipWaitingCallbacksMap;
120 void OnActivateEvent(int request_id);
121 void OnInstallEvent(int request_id);
122 void OnFetchEvent(int request_id, const ServiceWorkerFetchRequest& request);
123 void OnSyncEvent(int request_id);
124 void OnNotificationClickEvent(
125 int request_id,
126 const std::string& notification_id,
127 const PlatformNotificationData& notification_data);
128 void OnPushEvent(int request_id, const std::string& data);
129 void OnGeofencingEvent(int request_id,
130 blink::WebGeofencingEventType event_type,
131 const std::string& region_id,
132 const blink::WebCircularGeofencingRegion& region);
133 void OnCrossOriginConnectEvent(int request_id,
134 const NavigatorConnectClient& client);
135 void OnPostMessage(
136 const base::string16& message,
137 const std::vector<TransferredMessagePort>& sent_message_ports,
138 const std::vector<int>& new_routing_ids);
139 void OnCrossOriginMessageToWorker(
140 const NavigatorConnectClient& client,
141 const base::string16& message,
142 const std::vector<TransferredMessagePort>& sent_message_ports,
143 const std::vector<int>& new_routing_ids);
144 void OnDidGetClients(
145 int request_id, const std::vector<ServiceWorkerClientInfo>& clients);
146 void OnOpenWindowResponse(int request_id,
147 const ServiceWorkerClientInfo& client);
148 void OnOpenWindowError(int request_id, const std::string& message);
149 void OnFocusClientResponse(int request_id,
150 const ServiceWorkerClientInfo& client);
151 void OnDidSkipWaiting(int request_id);
152 void OnDidClaimClients(int request_id);
153 void OnClaimClientsError(int request_id,
154 blink::WebServiceWorkerError::ErrorType error_type,
155 const base::string16& message);
156 void OnPing();
158 // Not owned; embedded_context_ owns this.
159 EmbeddedWorkerContextClient* embedded_context_;
161 scoped_ptr<WebServiceWorkerCacheStorageImpl> cache_storage_;
163 // Not owned; this object is destroyed when proxy_ becomes invalid.
164 blink::WebServiceWorkerContextProxy* proxy_;
166 // Used for incoming messages from the browser for which an outgoing response
167 // back to the browser is expected, the id must be sent back with the
168 // response.
169 int current_request_id_;
171 // Pending callbacks for GetClientDocuments().
172 ClientsCallbacksMap pending_clients_callbacks_;
174 // Pending callbacks for OpenWindow() and FocusClient().
175 ClientCallbacksMap pending_client_callbacks_;
177 // Pending callbacks for SkipWaiting().
178 SkipWaitingCallbacksMap pending_skip_waiting_callbacks_;
180 // Pending callbacks for ClaimClients().
181 ClaimClientsCallbacksMap pending_claim_clients_callbacks_;
183 // Capture timestamps for UMA
184 std::map<int, base::TimeTicks> activate_start_timings_;
185 std::map<int, base::TimeTicks> fetch_start_timings_;
186 std::map<int, base::TimeTicks> install_start_timings_;
187 std::map<int, base::TimeTicks> notification_click_start_timings_;
188 std::map<int, base::TimeTicks> push_start_timings_;
190 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptContext);
193 } // namespace content
195 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_