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_
11 #include "base/basictypes.h"
12 #include "base/id_map.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "content/child/webmessageportchannel_impl.h"
16 #include "content/common/service_worker/service_worker_types.h"
17 #include "content/renderer/service_worker/service_worker_cache_storage_dispatcher.h"
18 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h"
19 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
20 #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h"
23 class WebServiceWorkerContextProxy
;
32 class EmbeddedWorkerContextClient
;
34 // TODO(kinuko): This should implement WebServiceWorkerContextClient
35 // rather than having EmbeddedWorkerContextClient implement it.
36 // See the header comment in embedded_worker_context_client.h for the
37 // potential EW/SW layering concerns.
38 class ServiceWorkerScriptContext
{
40 ServiceWorkerScriptContext(
41 EmbeddedWorkerContextClient
* embedded_context
,
42 blink::WebServiceWorkerContextProxy
* proxy
);
43 ~ServiceWorkerScriptContext();
45 void OnMessageReceived(const IPC::Message
& message
);
47 void DidHandleActivateEvent(int request_id
,
48 blink::WebServiceWorkerEventResult
);
49 void DidHandleInstallEvent(int request_id
,
50 blink::WebServiceWorkerEventResult result
);
51 void DidHandleFetchEvent(int request_id
,
52 ServiceWorkerFetchEventResult result
,
53 const ServiceWorkerResponse
& response
);
54 void DidHandleSyncEvent(int request_id
);
55 void GetClientDocuments(
56 blink::WebServiceWorkerClientsCallbacks
* callbacks
);
57 void PostMessageToDocument(
59 const base::string16
& message
,
60 scoped_ptr
<blink::WebMessagePortChannelArray
> channels
);
62 // Send a message to the browser. Takes ownership of |message|.
63 void Send(IPC::Message
* message
);
65 // Get routing_id for sending message to the ServiceWorkerVersion
66 // in the browser process.
67 int GetRoutingID() const;
69 blink::WebServiceWorkerCacheStorage
* cache_storage() {
70 return cache_storage_dispatcher_
.get();
74 typedef IDMap
<blink::WebServiceWorkerClientsCallbacks
, IDMapOwnPointer
>
78 void OnActivateEvent(int request_id
);
79 void OnInstallEvent(int request_id
, int active_version_id
);
80 void OnFetchEvent(int request_id
, const ServiceWorkerFetchRequest
& request
);
81 void OnSyncEvent(int request_id
);
82 void OnPushEvent(int request_id
, const std::string
& data
);
83 void OnPostMessage(const base::string16
& message
,
84 const std::vector
<int>& sent_message_port_ids
,
85 const std::vector
<int>& new_routing_ids
);
86 void OnDidGetClientDocuments(
87 int request_id
, const std::vector
<int>& client_ids
);
89 scoped_ptr
<ServiceWorkerCacheStorageDispatcher
> cache_storage_dispatcher_
;
91 // Not owned; embedded_context_ owns this.
92 EmbeddedWorkerContextClient
* embedded_context_
;
94 // Not owned; this object is destroyed when proxy_ becomes invalid.
95 blink::WebServiceWorkerContextProxy
* proxy_
;
97 // Used for incoming messages from the browser for which an outgoing response
98 // back to the browser is expected, the id must be sent back with the
100 int current_request_id_
;
102 // Pending callbacks for GetClientDocuments().
103 ClientsCallbacksMap pending_clients_callbacks_
;
105 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptContext
);
108 } // namespace content
110 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_