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