Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / service_worker / service_worker_script_context.h
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 <vector>
9
10 #include "base/basictypes.h"
11 #include "base/id_map.h"
12 #include "base/strings/string16.h"
13 #include "content/child/webmessageportchannel_impl.h"
14 #include "content/common/service_worker/service_worker_types.h"
15 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
16 #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h"
17
18 namespace blink {
19 class WebServiceWorkerContextProxy;
20 }
21
22 namespace IPC {
23 class Message;
24 }
25
26 namespace content {
27
28 class EmbeddedWorkerContextClient;
29
30 // TODO(kinuko): This should implement WebServiceWorkerContextClient
31 // rather than having EmbeddedWorkerContextClient implement it.
32 // See the header comment in embedded_worker_context_client.h for the
33 // potential EW/SW layering concerns.
34 class ServiceWorkerScriptContext {
35  public:
36   ServiceWorkerScriptContext(
37       EmbeddedWorkerContextClient* embedded_context,
38       blink::WebServiceWorkerContextProxy* proxy);
39   ~ServiceWorkerScriptContext();
40
41   void OnMessageReceived(const IPC::Message& message);
42
43   void DidHandleActivateEvent(int request_id,
44                               blink::WebServiceWorkerEventResult);
45   void DidHandleInstallEvent(int request_id,
46                              blink::WebServiceWorkerEventResult result);
47   void DidHandleFetchEvent(int request_id,
48                            ServiceWorkerFetchEventResult result,
49                            const ServiceWorkerResponse& response);
50   void DidHandleSyncEvent(int request_id);
51   void GetClientDocuments(
52       blink::WebServiceWorkerClientsCallbacks* callbacks);
53   void PostMessageToDocument(int client_id,
54                              const base::string16& message,
55                              const std::vector<int>& message_port_ids);
56
57  private:
58   typedef IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer>
59       ClientsCallbacksMap;
60
61   // Send a message to the browser.
62   void Send(IPC::Message* message);
63
64   void OnActivateEvent(int request_id);
65   void OnInstallEvent(int request_id, int active_version_id);
66   void OnFetchEvent(int request_id, const ServiceWorkerFetchRequest& request);
67   void OnSyncEvent(int request_id);
68   void OnPostMessage(const base::string16& message,
69                      const std::vector<int>& sent_message_port_ids,
70                      const std::vector<int>& new_routing_ids);
71   void OnDidGetClientDocuments(
72       int request_id, const std::vector<int>& client_ids);
73
74   // Get routing_id for sending message to the ServiceWorkerVersion
75   // in the browser process.
76   int GetRoutingID() const;
77
78   // Not owned; embedded_context_ owns this.
79   EmbeddedWorkerContextClient* embedded_context_;
80
81   // Not owned; this object is destroyed when proxy_ becomes invalid.
82   blink::WebServiceWorkerContextProxy* proxy_;
83
84   // Used for incoming messages from the browser for which an outgoing response
85   // back to the browser is expected, the id must be sent back with the
86   // response.
87   int current_request_id_;
88
89   // Pending callbacks for GetClientDocuments().
90   ClientsCallbacksMap pending_clients_callbacks_;
91
92   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptContext);
93 };
94
95 }  // namespace content
96
97 #endif  // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_