OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "extensions/renderer/extension_port.h" |
| 6 |
| 7 #include "content/public/renderer/render_frame.h" |
| 8 #include "extensions/common/api/messaging/message.h" |
| 9 #include "extensions/common/extension_messages.h" |
| 10 #include "extensions/renderer/script_context.h" |
| 11 |
| 12 namespace extensions { |
| 13 |
| 14 ExtensionPort::ExtensionPort(ScriptContext* script_context, int local_id) |
| 15 : script_context_(script_context), local_id_(local_id) {} |
| 16 |
| 17 ExtensionPort::~ExtensionPort() {} |
| 18 |
| 19 void ExtensionPort::SetGlobalId(int id) { |
| 20 global_id_ = id; |
| 21 content::RenderFrame* render_frame = script_context_->GetRenderFrame(); |
| 22 for (const auto& message : pending_messages_) |
| 23 PostMessageImpl(render_frame, *message); |
| 24 if (is_disconnected_) |
| 25 SendDisconnected(render_frame); |
| 26 pending_messages_.clear(); |
| 27 } |
| 28 |
| 29 void ExtensionPort::PostExtensionMessage(std::unique_ptr<Message> message) { |
| 30 if (!initialized()) { |
| 31 pending_messages_.push_back(std::move(message)); |
| 32 return; |
| 33 } |
| 34 PostMessageImpl(script_context_->GetRenderFrame(), *message); |
| 35 } |
| 36 |
| 37 void ExtensionPort::Close(bool close_channel) { |
| 38 is_disconnected_ = true; |
| 39 close_channel_ = close_channel; |
| 40 if (!initialized()) |
| 41 return; |
| 42 |
| 43 SendDisconnected(script_context_->GetRenderFrame()); |
| 44 } |
| 45 |
| 46 void ExtensionPort::PostMessageImpl(content::RenderFrame* render_frame, |
| 47 const Message& message) { |
| 48 // TODO(devlin): What should we do if there's no render frame? Up until now, |
| 49 // we've always just dropped the messages, but we might need to figure this |
| 50 // out for service workers. |
| 51 if (!render_frame) |
| 52 return; |
| 53 render_frame->Send(new ExtensionHostMsg_PostMessage( |
| 54 render_frame->GetRoutingID(), global_id_, message)); |
| 55 } |
| 56 |
| 57 void ExtensionPort::SendDisconnected(content::RenderFrame* render_frame) { |
| 58 if (!render_frame) |
| 59 return; |
| 60 render_frame->Send(new ExtensionHostMsg_CloseMessagePort( |
| 61 render_frame->GetRoutingID(), global_id_, close_channel_)); |
| 62 } |
| 63 |
| 64 } // namespace extensions |
OLD | NEW |