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_
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 "third_party/WebKit/public/platform/WebGeofencingEventType.h"
21 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h"
22 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsClaimCallbacks.h"
23 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
24 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
25 #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h"
26 #include "third_party/WebKit/public/platform/WebServiceWorkerSkipWaitingCallbacks.h"
29 struct WebCircularGeofencingRegion
;
30 struct WebCrossOriginServiceWorkerClient
;
31 class WebServiceWorkerContextProxy
;
40 class EmbeddedWorkerContextClient
;
41 class WebServiceWorkerRegistrationImpl
;
42 struct NavigatorConnectClient
;
43 struct PlatformNotificationData
;
44 struct ServiceWorkerClientInfo
;
46 // TODO(kinuko): This should implement WebServiceWorkerContextClient
47 // rather than having EmbeddedWorkerContextClient implement it.
48 // See the header comment in embedded_worker_context_client.h for the
49 // potential EW/SW layering concerns.
50 class ServiceWorkerScriptContext
{
52 ServiceWorkerScriptContext(
53 EmbeddedWorkerContextClient
* embedded_context
,
54 blink::WebServiceWorkerContextProxy
* proxy
);
55 ~ServiceWorkerScriptContext();
57 void OnMessageReceived(const IPC::Message
& message
);
59 void SetRegistrationInServiceWorkerGlobalScope(
60 scoped_ptr
<WebServiceWorkerRegistrationImpl
> registration
);
61 void DidHandleActivateEvent(int request_id
,
62 blink::WebServiceWorkerEventResult
);
63 void DidHandleInstallEvent(int request_id
,
64 blink::WebServiceWorkerEventResult result
);
65 void DidHandleFetchEvent(int request_id
,
66 ServiceWorkerFetchEventResult result
,
67 const ServiceWorkerResponse
& response
);
68 void DidHandleNotificationClickEvent(
70 blink::WebServiceWorkerEventResult result
);
71 void DidHandlePushEvent(int request_id
,
72 blink::WebServiceWorkerEventResult result
);
73 void DidHandleSyncEvent(int request_id
);
74 void DidHandleCrossOriginConnectEvent(int request_id
, bool accept_connection
);
75 void GetClientDocuments(
76 blink::WebServiceWorkerClientsCallbacks
* callbacks
);
77 void OpenWindow(const GURL
& url
,
78 blink::WebServiceWorkerClientCallbacks
* callbacks
);
79 void SetCachedMetadata(const GURL
& url
, const char* data
, size_t size
);
80 void ClearCachedMetadata(const GURL
& url
);
81 void PostMessageToDocument(
83 const base::string16
& message
,
84 scoped_ptr
<blink::WebMessagePortChannelArray
> channels
);
85 void PostCrossOriginMessageToClient(
86 const blink::WebCrossOriginServiceWorkerClient
& client
,
87 const base::string16
& message
,
88 scoped_ptr
<blink::WebMessagePortChannelArray
> channels
);
89 void FocusClient(int client_id
,
90 blink::WebServiceWorkerClientCallbacks
* callback
);
91 void SkipWaiting(blink::WebServiceWorkerSkipWaitingCallbacks
* callbacks
);
92 void ClaimClients(blink::WebServiceWorkerClientsClaimCallbacks
* callbacks
);
94 // Send a message to the browser. Takes ownership of |message|.
95 void Send(IPC::Message
* message
);
97 // Get routing_id for sending message to the ServiceWorkerVersion
98 // in the browser process.
99 int GetRoutingID() const;
101 blink::WebServiceWorkerCacheStorage
* cache_storage() {
102 return cache_storage_dispatcher_
.get();
106 typedef IDMap
<blink::WebServiceWorkerClientsCallbacks
, IDMapOwnPointer
>
108 typedef IDMap
<blink::WebServiceWorkerClientsClaimCallbacks
, IDMapOwnPointer
>
109 ClaimClientsCallbacksMap
;
110 typedef IDMap
<blink::WebServiceWorkerClientCallbacks
, IDMapOwnPointer
>
112 typedef IDMap
<blink::WebServiceWorkerSkipWaitingCallbacks
, IDMapOwnPointer
>
113 SkipWaitingCallbacksMap
;
115 void OnActivateEvent(int request_id
);
116 void OnInstallEvent(int request_id
, int active_version_id
);
117 void OnFetchEvent(int request_id
, const ServiceWorkerFetchRequest
& request
);
118 void OnSyncEvent(int request_id
);
119 void OnNotificationClickEvent(
121 const std::string
& notification_id
,
122 const PlatformNotificationData
& notification_data
);
123 void OnPushEvent(int request_id
, const std::string
& data
);
124 void OnGeofencingEvent(int request_id
,
125 blink::WebGeofencingEventType event_type
,
126 const std::string
& region_id
,
127 const blink::WebCircularGeofencingRegion
& region
);
128 void OnCrossOriginConnectEvent(int request_id
,
129 const NavigatorConnectClient
& client
);
131 const base::string16
& message
,
132 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
133 const std::vector
<int>& new_routing_ids
);
134 void OnCrossOriginMessageToWorker(
135 const NavigatorConnectClient
& client
,
136 const base::string16
& message
,
137 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
138 const std::vector
<int>& new_routing_ids
);
139 void OnDidGetClientDocuments(
140 int request_id
, const std::vector
<ServiceWorkerClientInfo
>& clients
);
141 void OnOpenWindowResponse(int request_id
,
142 const ServiceWorkerClientInfo
& client
);
143 void OnOpenWindowError(int request_id
);
144 void OnFocusClientResponse(int request_id
,
145 const ServiceWorkerClientInfo
& client
);
146 void OnDidSkipWaiting(int request_id
);
147 void OnDidClaimClients(int request_id
);
148 void OnClaimClientsError(int request_id
,
149 blink::WebServiceWorkerError::ErrorType error_type
,
150 const base::string16
& message
);
153 scoped_ptr
<ServiceWorkerCacheStorageDispatcher
> cache_storage_dispatcher_
;
155 // Not owned; embedded_context_ owns this.
156 EmbeddedWorkerContextClient
* embedded_context_
;
158 // Not owned; this object is destroyed when proxy_ becomes invalid.
159 blink::WebServiceWorkerContextProxy
* proxy_
;
161 // Used for incoming messages from the browser for which an outgoing response
162 // back to the browser is expected, the id must be sent back with the
164 int current_request_id_
;
166 // Pending callbacks for GetClientDocuments().
167 ClientsCallbacksMap pending_clients_callbacks_
;
169 // Pending callbacks for OpenWindow() and FocusClient().
170 ClientCallbacksMap pending_client_callbacks_
;
172 // Pending callbacks for SkipWaiting().
173 SkipWaitingCallbacksMap pending_skip_waiting_callbacks_
;
175 // Pending callbacks for ClaimClients().
176 ClaimClientsCallbacksMap pending_claim_clients_callbacks_
;
178 // Capture timestamps for UMA
179 std::map
<int, base::TimeTicks
> activate_start_timings_
;
180 std::map
<int, base::TimeTicks
> fetch_start_timings_
;
181 std::map
<int, base::TimeTicks
> install_start_timings_
;
182 std::map
<int, base::TimeTicks
> notification_click_start_timings_
;
183 std::map
<int, base::TimeTicks
> push_start_timings_
;
185 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptContext
);
188 } // namespace content
190 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_