Update To 11.40.268.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 <map>
9 #include <string>
10 #include <vector>
11
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"
24
25 namespace blink {
26 struct WebCircularGeofencingRegion;
27 class WebServiceWorkerContextProxy;
28 }
29
30 namespace IPC {
31 class Message;
32 }
33
34 namespace content {
35
36 class EmbeddedWorkerContextClient;
37
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 {
43  public:
44   ServiceWorkerScriptContext(
45       EmbeddedWorkerContextClient* embedded_context,
46       blink::WebServiceWorkerContextProxy* proxy);
47   ~ServiceWorkerScriptContext();
48
49   void OnMessageReceived(const IPC::Message& message);
50
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(
62       int client_id,
63       const base::string16& message,
64       scoped_ptr<blink::WebMessagePortChannelArray> channels);
65
66   // Send a message to the browser. Takes ownership of |message|.
67   void Send(IPC::Message* message);
68
69   // Get routing_id for sending message to the ServiceWorkerVersion
70   // in the browser process.
71   int GetRoutingID() const;
72
73   blink::WebServiceWorkerCacheStorage* cache_storage() {
74     return cache_storage_dispatcher_.get();
75   }
76
77  private:
78   typedef IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer>
79       ClientsCallbacksMap;
80
81
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);
96
97   scoped_ptr<ServiceWorkerCacheStorageDispatcher> cache_storage_dispatcher_;
98
99   // Not owned; embedded_context_ owns this.
100   EmbeddedWorkerContextClient* embedded_context_;
101
102   // Not owned; this object is destroyed when proxy_ becomes invalid.
103   blink::WebServiceWorkerContextProxy* proxy_;
104
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
107   // response.
108   int current_request_id_;
109
110   // Pending callbacks for GetClientDocuments().
111   ClientsCallbacksMap pending_clients_callbacks_;
112
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_;
117
118   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptContext);
119 };
120
121 }  // namespace content
122
123 #endif  // CONTENT_RENDERER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CONTEXT_H_