summaryrefslogtreecommitdiff
path: root/chromium/extensions/renderer/extension_port.cc
blob: 47a472e870ce64456ea6c256edb9f727c2a10d43 (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
// 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.

#include "extensions/renderer/extension_port.h"

#include "content/public/renderer/render_frame.h"
#include "extensions/common/api/messaging/message.h"
#include "extensions/common/api/messaging/port_id.h"
#include "extensions/common/extension_messages.h"
#include "extensions/renderer/script_context.h"

namespace extensions {

ExtensionPort::ExtensionPort(ScriptContext* script_context,
                             const PortId& id,
                             int js_id)
    : script_context_(script_context), id_(id), js_id_(js_id) {}

ExtensionPort::~ExtensionPort() {}

void ExtensionPort::PostExtensionMessage(std::unique_ptr<Message> message) {
  content::RenderFrame* render_frame = script_context_->GetRenderFrame();
  // TODO(devlin): What should we do if there's no render frame? Up until now,
  // we've always just dropped the messages, but we might need to figure this
  // out for service workers.
  if (!render_frame)
    return;
  render_frame->Send(new ExtensionHostMsg_PostMessage(
      render_frame->GetRoutingID(), id_, *message));
}

void ExtensionPort::Close(bool close_channel) {
  content::RenderFrame* render_frame = script_context_->GetRenderFrame();
  if (!render_frame)
    return;
  render_frame->Send(new ExtensionHostMsg_CloseMessagePort(
      render_frame->GetRoutingID(), id_, close_channel));
}

}  // namespace extensions