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 EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_
6#define EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_
7
8#include <string>
9
10#include "extensions/renderer/script_context_set.h"
11
12struct ExtensionMsg_ExternalConnectionInfo;
13
14namespace base {
15class DictionaryValue;
16}
17
18namespace content {
19class RenderView;
20}
21
22namespace v8 {
23class Extension;
24}
25
26namespace extensions {
27class Dispatcher;
28struct Message;
29class ObjectBackedNativeHandler;
30class ScriptContextSet;
31
32// Manually implements JavaScript bindings for extension messaging.
33//
34// TODO(aa): This should all get re-implemented using SchemaGeneratedBindings.
35// If anything needs to be manual for some reason, it should be implemented in
36// its own class.
37class MessagingBindings {
38 public:
39  // Creates an instance of the extension.
40  static ObjectBackedNativeHandler* Get(Dispatcher* dispatcher,
41                                        ScriptContext* context);
42
43  // Dispatches the onConnect content script messaging event to some contexts
44  // in |context_set|. If |restrict_to_render_view| is specified, only contexts
45  // in that render view will receive the message.
46  static void DispatchOnConnect(const ScriptContextSet& context_set,
47                                int target_port_id,
48                                const std::string& channel_name,
49                                const base::DictionaryValue& source_tab,
50                                const ExtensionMsg_ExternalConnectionInfo& info,
51                                const std::string& tls_channel_id,
52                                content::RenderView* restrict_to_render_view);
53
54  // Delivers a message sent using content script messaging to some of the
55  // contexts in |bindings_context_set|. If |restrict_to_render_view| is
56  // specified, only contexts in that render view will receive the message.
57  static void DeliverMessage(const ScriptContextSet& context_set,
58                             int target_port_id,
59                             const Message& message,
60                             content::RenderView* restrict_to_render_view);
61
62  // Dispatches the onDisconnect event in response to the channel being closed.
63  static void DispatchOnDisconnect(
64      const ScriptContextSet& context_set,
65      int port_id,
66      const std::string& error_message,
67      content::RenderView* restrict_to_render_view);
68};
69
70}  // namespace extensions
71
72#endif  // EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_
73