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/WebMessagePortChannel.h"
21 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
22 #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h"
25 class WebServiceWorkerContextProxy
;
34 class EmbeddedWorkerContextClient
;
36 // TODO(kinuko): This should implement WebServiceWorkerContextClient
37 // rather than having EmbeddedWorkerContextClient implement it.
38 // See the header comment in embedded_worker_context_client.h for the
39 // potential EW/SW layering concerns.
40 class ServiceWorkerScriptContext
{
42 ServiceWorkerScriptContext(
43 EmbeddedWorkerContextClient
* embedded_context
,
44 blink::WebServiceWorkerContextProxy
* proxy
);
45 ~ServiceWorkerScriptContext();
47 void OnMessageReceived(const IPC::Message
& message
);
49 void DidHandleActivateEvent(int request_id
,
50 blink::WebServiceWorkerEventResult
);
51 void DidHandleInstallEvent(int request_id
,
52 blink::WebServiceWorkerEventResult result
);
53 void DidHandleFetchEvent(int request_id
,
54 ServiceWorkerFetchEventResult result
,
55 const ServiceWorkerResponse
& response
);
56 void DidHandleSyncEvent(int request_id
);
57 void GetClientDocuments(
58 blink::WebServiceWorkerClientsCallbacks
* callbacks
);
59 void PostMessageToDocument(
61 const base::string16
& message
,
62 scoped_ptr
<blink::WebMessagePortChannelArray
> channels
);
64 // Send a message to the browser. Takes ownership of |message|.
65 void Send(IPC::Message
* message
);
67 // Get routing_id for sending message to the ServiceWorkerVersion
68 // in the browser process.
69 int GetRoutingID() const;
71 blink::WebServiceWorkerCacheStorage
* cache_storage() {
72 return cache_storage_dispatcher_
.get();
76 typedef IDMap
<blink::WebServiceWorkerClientsCallbacks
, IDMapOwnPointer
>
80 void OnActivateEvent(int request_id
);
81 void OnInstallEvent(int request_id
, int active_version_id
);
82 void OnFetchEvent(int request_id
, const ServiceWorkerFetchRequest
& request
);
83 void OnSyncEvent(int request_id
);
84 void OnPushEvent(int request_id
, const std::string
& data
);
85 void OnPostMessage(const base::string16
& message
,
86 const std::vector
<int>& sent_message_port_ids
,
87 const std::vector
<int>& new_routing_ids
);
88 void OnDidGetClientDocuments(
89 int request_id
, const std::vector
<int>& client_ids
);
91 scoped_ptr
<ServiceWorkerCacheStorageDispatcher
> cache_storage_dispatcher_
;
93 // Not owned; embedded_context_ owns this.
94 EmbeddedWorkerContextClient
* embedded_context_
;
96 // Not owned; this object is destroyed when proxy_ becomes invalid.
97 blink::WebServiceWorkerContextProxy
* proxy_
;
99 // Used for incoming messages from the browser for which an outgoing response
100 // back to the browser is expected, the id must be sent back with the
102 int current_request_id_
;
104 // Pending callbacks for GetClientDocuments().
105 ClientsCallbacksMap pending_clients_callbacks_
;
107 // Capture timestamps for UMA
108 std::map
<int, base::TimeTicks
> activate_start_timings_
;
109 std::map
<int, base::TimeTicks
> fetch_start_timings_
;
110 std::map
<int, base::TimeTicks
> install_start_timings_
;
112 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptContext
);
115 } // namespace content
117 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_