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 "third_party/WebKit/public/platform/WebGeofencingEventType.h"
20 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h"
21 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsClaimCallbacks.h"
22 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
23 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
24 #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h"
25 #include "third_party/WebKit/public/platform/WebServiceWorkerSkipWaitingCallbacks.h"
28 struct WebCircularGeofencingRegion
;
29 struct WebCrossOriginServiceWorkerClient
;
30 struct WebServiceWorkerClientQueryOptions
;
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
);
76 const blink::WebServiceWorkerClientQueryOptions
& options
,
77 blink::WebServiceWorkerClientsCallbacks
* callbacks
);
78 void OpenWindow(const GURL
& url
,
79 blink::WebServiceWorkerClientCallbacks
* callbacks
);
80 void SetCachedMetadata(const GURL
& url
, const char* data
, size_t size
);
81 void ClearCachedMetadata(const GURL
& url
);
82 void PostMessageToClient(
83 const base::string16
& uuid
,
84 const base::string16
& message
,
85 scoped_ptr
<blink::WebMessagePortChannelArray
> channels
);
86 void PostCrossOriginMessageToClient(
87 const blink::WebCrossOriginServiceWorkerClient
& client
,
88 const base::string16
& message
,
89 scoped_ptr
<blink::WebMessagePortChannelArray
> channels
);
90 void FocusClient(const base::string16
& uuid
,
91 blink::WebServiceWorkerClientCallbacks
* callback
);
92 void SkipWaiting(blink::WebServiceWorkerSkipWaitingCallbacks
* callbacks
);
93 void ClaimClients(blink::WebServiceWorkerClientsClaimCallbacks
* callbacks
);
95 // Send a message to the browser. Takes ownership of |message|.
96 void Send(IPC::Message
* message
);
98 // Get routing_id for sending message to the ServiceWorkerVersion
99 // in the browser process.
100 int GetRoutingID() const;
103 typedef IDMap
<blink::WebServiceWorkerClientsCallbacks
, IDMapOwnPointer
>
105 typedef IDMap
<blink::WebServiceWorkerClientsClaimCallbacks
, IDMapOwnPointer
>
106 ClaimClientsCallbacksMap
;
107 typedef IDMap
<blink::WebServiceWorkerClientCallbacks
, IDMapOwnPointer
>
109 typedef IDMap
<blink::WebServiceWorkerSkipWaitingCallbacks
, IDMapOwnPointer
>
110 SkipWaitingCallbacksMap
;
112 void OnActivateEvent(int request_id
);
113 void OnInstallEvent(int request_id
);
114 void OnFetchEvent(int request_id
, const ServiceWorkerFetchRequest
& request
);
115 void OnSyncEvent(int request_id
);
116 void OnNotificationClickEvent(
118 const std::string
& notification_id
,
119 const PlatformNotificationData
& notification_data
);
120 void OnPushEvent(int request_id
, const std::string
& data
);
121 void OnGeofencingEvent(int request_id
,
122 blink::WebGeofencingEventType event_type
,
123 const std::string
& region_id
,
124 const blink::WebCircularGeofencingRegion
& region
);
125 void OnCrossOriginConnectEvent(int request_id
,
126 const NavigatorConnectClient
& client
);
128 const base::string16
& message
,
129 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
130 const std::vector
<int>& new_routing_ids
);
131 void OnCrossOriginMessageToWorker(
132 const NavigatorConnectClient
& client
,
133 const base::string16
& message
,
134 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
135 const std::vector
<int>& new_routing_ids
);
136 void OnDidGetClients(
137 int request_id
, const std::vector
<ServiceWorkerClientInfo
>& clients
);
138 void OnOpenWindowResponse(int request_id
,
139 const ServiceWorkerClientInfo
& client
);
140 void OnOpenWindowError(int request_id
, const std::string
& message
);
141 void OnFocusClientResponse(int request_id
,
142 const ServiceWorkerClientInfo
& client
);
143 void OnDidSkipWaiting(int request_id
);
144 void OnDidClaimClients(int request_id
);
145 void OnClaimClientsError(int request_id
,
146 blink::WebServiceWorkerError::ErrorType error_type
,
147 const base::string16
& message
);
150 // Not owned; embedded_context_ owns this.
151 EmbeddedWorkerContextClient
* embedded_context_
;
153 // Not owned; this object is destroyed when proxy_ becomes invalid.
154 blink::WebServiceWorkerContextProxy
* proxy_
;
156 // Used for incoming messages from the browser for which an outgoing response
157 // back to the browser is expected, the id must be sent back with the
159 int current_request_id_
;
161 // Pending callbacks for GetClientDocuments().
162 ClientsCallbacksMap pending_clients_callbacks_
;
164 // Pending callbacks for OpenWindow() and FocusClient().
165 ClientCallbacksMap pending_client_callbacks_
;
167 // Pending callbacks for SkipWaiting().
168 SkipWaitingCallbacksMap pending_skip_waiting_callbacks_
;
170 // Pending callbacks for ClaimClients().
171 ClaimClientsCallbacksMap pending_claim_clients_callbacks_
;
173 // Capture timestamps for UMA
174 std::map
<int, base::TimeTicks
> activate_start_timings_
;
175 std::map
<int, base::TimeTicks
> fetch_start_timings_
;
176 std::map
<int, base::TimeTicks
> install_start_timings_
;
177 std::map
<int, base::TimeTicks
> notification_click_start_timings_
;
178 std::map
<int, base::TimeTicks
> push_start_timings_
;
180 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptContext
);
183 } // namespace content
185 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_