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