summaryrefslogtreecommitdiff
path: root/chromium/extensions/renderer/extension_port.h
blob: aa584a97b5f02484e4f32cb2a5ae3396ac26fac9 (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
// Copyright 2016 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_EXTENSION_PORT_H_
#define EXTENSIONS_RENDERER_EXTENSION_PORT_H_

#include <memory>
#include <utility>
#include <vector>

#include "base/macros.h"
#include "extensions/common/api/messaging/port_id.h"

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

// A class representing information about a specific extension message port that
// handles sending related IPCs to the browser. This consists of a port id and
// a separate js_id which is exposed only to the JavaScript context.
class ExtensionPort {
 public:
  ExtensionPort(ScriptContext* script_context, const PortId& id, int js_id);
  ~ExtensionPort();

  // Posts a new message to the port. If the port is not initialized, the
  // message will be queued until it is.
  void PostExtensionMessage(std::unique_ptr<Message> message);

  // Closes the port. If there are pending messages, they will still be sent
  // assuming initialization completes (after which, the port will close).
  void Close(bool close_channel);

  const PortId& id() const { return id_; }
  int js_id() const { return js_id_; }

 private:
  // The associated ScriptContext for this port. Since these objects are owned
  // by a NativeHandler, this should always be valid.
  ScriptContext* script_context_ = nullptr;

  const PortId id_;

  // The id used in the JS bindings. If this is a receiver port, this is not the
  // same as the port_number in the PortId. This should be used only as an
  // identifier in the JS context this port is from.
  int js_id_ = 0;

  DISALLOW_COPY_AND_ASSIGN(ExtensionPort);
};

}  // namespace extensions

#endif  // EXTENSIONS_RENDERER_EXTENSION_PORT_H_