summaryrefslogtreecommitdiff
path: root/chromium/extensions/renderer/renderer_messaging_service.h
blob: 5d060dee7225c40a5c3d89fcf5d24c835f85ecae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_RENDERER_RENDERER_MESSAGING_SERVICE_H_
#define EXTENSIONS_RENDERER_RENDERER_MESSAGING_SERVICE_H_

#include <string>

#include "base/macros.h"
#include "extensions/common/extension_id.h"

struct ExtensionMsg_ExternalConnectionInfo;
struct ExtensionMsg_TabConnectionInfo;

namespace content {
class RenderFrame;
}

namespace extensions {
class ExtensionBindingsSystem;
class ScriptContext;
class ScriptContextSet;
struct Message;
struct PortId;

class RendererMessagingService {
 public:
  explicit RendererMessagingService(ExtensionBindingsSystem* bindings_system);
  virtual ~RendererMessagingService();

  // Checks whether the port exists in the given frame. If it does not, a reply
  // is sent back to the browser.
  void ValidateMessagePort(const ScriptContextSet& context_set,
                           const PortId& port_id,
                           content::RenderFrame* render_frame);

  // Dispatches the onConnect content script messaging event to some contexts
  // in |context_set|. If |restrict_to_render_frame| is specified, only contexts
  // in that render frame will receive the message.
  void DispatchOnConnect(const ScriptContextSet& context_set,
                         const PortId& target_port_id,
                         const std::string& channel_name,
                         const ExtensionMsg_TabConnectionInfo& source,
                         const ExtensionMsg_ExternalConnectionInfo& info,
                         const std::string& tls_channel_id,
                         content::RenderFrame* restrict_to_render_frame);

  // Delivers a message sent using content script messaging to some of the
  // contexts in |bindings_context_set|. If |restrict_to_render_frame| is
  // specified, only contexts in that render view will receive the message.
  void DeliverMessage(const ScriptContextSet& context_set,
                      const PortId& target_port_id,
                      const Message& message,
                      content::RenderFrame* restrict_to_render_frame);

  // Dispatches the onDisconnect event in response to the channel being closed.
  void DispatchOnDisconnect(const ScriptContextSet& context_set,
                            const PortId& port_id,
                            const std::string& error_message,
                            content::RenderFrame* restrict_to_render_frame);

 private:
  // Helpers for the public methods to perform the action in a single
  // ScriptContext.
  void ValidateMessagePortInContext(const PortId& port_id,
                                    bool* has_port,
                                    ScriptContext* script_context);
  void DispatchOnConnectToScriptContext(
      const PortId& target_port_id,
      const std::string& channel_name,
      const ExtensionMsg_TabConnectionInfo* source,
      const ExtensionMsg_ExternalConnectionInfo& info,
      const std::string& tls_channel_id,
      bool* port_created,
      ScriptContext* script_context);
  void DeliverMessageToScriptContext(const Message& message,
                                     const PortId& target_port_id,
                                     ScriptContext* script_context);
  void DispatchOnDisconnectToScriptContext(const PortId& port_id,
                                           const std::string& error_message,
                                           ScriptContext* script_context);

  // Returns true if the given |script_context| has a port with the given
  // |port_id|.
  virtual bool ContextHasMessagePort(ScriptContext* script_context,
                                     const PortId& port_id) = 0;

  // Dispatches the onConnect event to listeners in the given |script_context|.
  virtual void DispatchOnConnectToListeners(
      ScriptContext* script_context,
      const PortId& target_port_id,
      const ExtensionId& target_extension_id,
      const std::string& channel_name,
      const ExtensionMsg_TabConnectionInfo* source,
      const ExtensionMsg_ExternalConnectionInfo& info,
      const std::string& tls_channel_id,
      const std::string& event_name) = 0;

  // Dispatches the onMessage event to listeners in the given |script_context|.
  // This will only be called if the context has a port with the given id.
  virtual void DispatchOnMessageToListeners(ScriptContext* script_context,
                                            const Message& message,
                                            const PortId& target_port_id) = 0;

  // Dispatches the onDisconnect event to listeners in the given
  // |script_context|. This will only be called if the context has a port
  // with the given id.
  virtual void DispatchOnDisconnectToListeners(ScriptContext* script_context,
                                               const PortId& port_id,
                                               const std::string& error) = 0;

  // The associated ExtensionBindingsSystem; guaranteed to outlive this object.
  ExtensionBindingsSystem* const bindings_system_;

  DISALLOW_COPY_AND_ASSIGN(RendererMessagingService);
};

}  // namespace extensions

#endif  // EXTENSIONS_RENDERER_RENDERER_MESSAGING_SERVICE_H_