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. 4 5 #ifndef CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_ 6 #define CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_ 7 8 #include <string> 9 #include <vector> 10 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" 21 22 namespace blink { 23 class WebServiceWorkerContextProxy; 24 } 25 26 namespace IPC { 27 class Message; 28 } 29 30 namespace content { 31 32 class EmbeddedWorkerContextClient; 33 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 { 39 public: 40 ServiceWorkerScriptContext( 41 EmbeddedWorkerContextClient* embedded_context, 42 blink::WebServiceWorkerContextProxy* proxy); 43 ~ServiceWorkerScriptContext(); 44 45 void OnMessageReceived(const IPC::Message& message); 46 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( 58 int client_id, 59 const base::string16& message, 60 scoped_ptr<blink::WebMessagePortChannelArray> channels); 61 62 // Send a message to the browser. Takes ownership of |message|. 63 void Send(IPC::Message* message); 64 65 // Get routing_id for sending message to the ServiceWorkerVersion 66 // in the browser process. 67 int GetRoutingID() const; 68 69 blink::WebServiceWorkerCacheStorage* cache_storage() { 70 return cache_storage_dispatcher_.get(); 71 } 72 73 private: 74 typedef IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer> 75 ClientsCallbacksMap; 76 77 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); 88 89 scoped_ptr<ServiceWorkerCacheStorageDispatcher> cache_storage_dispatcher_; 90 91 // Not owned; embedded_context_ owns this. 92 EmbeddedWorkerContextClient* embedded_context_; 93 94 // Not owned; this object is destroyed when proxy_ becomes invalid. 95 blink::WebServiceWorkerContextProxy* proxy_; 96 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 99 // response. 100 int current_request_id_; 101 102 // Pending callbacks for GetClientDocuments(). 103 ClientsCallbacksMap pending_clients_callbacks_; 104 105 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptContext); 106 }; 107 108 } // namespace content 109 110 #endif // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_ 111