Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Unified Diff: extensions/renderer/extension_port.cc

Issue 2300453002: [Extensions] Begin making Extension port initialization asynchronous (Closed)
Patch Set: Nasko's Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/renderer/extension_port.h ('k') | extensions/renderer/messaging_bindings.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/renderer/extension_port.cc
diff --git a/extensions/renderer/extension_port.cc b/extensions/renderer/extension_port.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7ba4571ddc3cd0546cdbdb613f42b7d72dcce24a
--- /dev/null
+++ b/extensions/renderer/extension_port.cc
@@ -0,0 +1,64 @@
+// 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/extension_messages.h"
+#include "extensions/renderer/script_context.h"
+
+namespace extensions {
+
+ExtensionPort::ExtensionPort(ScriptContext* script_context, int local_id)
+ : script_context_(script_context), local_id_(local_id) {}
+
+ExtensionPort::~ExtensionPort() {}
+
+void ExtensionPort::SetGlobalId(int id) {
+ global_id_ = id;
+ content::RenderFrame* render_frame = script_context_->GetRenderFrame();
+ for (const auto& message : pending_messages_)
+ PostMessageImpl(render_frame, *message);
+ if (is_disconnected_)
+ SendDisconnected(render_frame);
+ pending_messages_.clear();
+}
+
+void ExtensionPort::PostExtensionMessage(std::unique_ptr<Message> message) {
+ if (!initialized()) {
+ pending_messages_.push_back(std::move(message));
+ return;
+ }
+ PostMessageImpl(script_context_->GetRenderFrame(), *message);
+}
+
+void ExtensionPort::Close(bool close_channel) {
+ is_disconnected_ = true;
+ close_channel_ = close_channel;
+ if (!initialized())
+ return;
+
+ SendDisconnected(script_context_->GetRenderFrame());
+}
+
+void ExtensionPort::PostMessageImpl(content::RenderFrame* render_frame,
+ const Message& message) {
+ // 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(), global_id_, message));
+}
+
+void ExtensionPort::SendDisconnected(content::RenderFrame* render_frame) {
+ if (!render_frame)
+ return;
+ render_frame->Send(new ExtensionHostMsg_CloseMessagePort(
+ render_frame->GetRoutingID(), global_id_, close_channel_));
+}
+
+} // namespace extensions
« no previous file with comments | « extensions/renderer/extension_port.h ('k') | extensions/renderer/messaging_bindings.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698