Index: extensions/renderer/extension_port.h |
diff --git a/extensions/renderer/extension_port.h b/extensions/renderer/extension_port.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c48ddc17741163bd570a4b678a87ac39367fce1c |
--- /dev/null |
+++ b/extensions/renderer/extension_port.h |
@@ -0,0 +1,78 @@ |
+// 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 <vector> |
+ |
+#include "base/macros.h" |
+ |
+namespace content { |
+class RenderFrame; |
+} |
+ |
+namespace extensions { |
+ |
+struct Message; |
+class ScriptContext; |
+ |
+// A class to handle sending message port-related IPCs to the browser. Since |
lazyboy
2016/09/08 00:05:40
Also point out what this class represents, "info a
Devlin
2016/09/08 19:15:30
Done.
|
+// global port IDs are assigned asynchronously, this object caches pending |
+// messages. |
+class ExtensionPort { |
+ public: |
+ ExtensionPort(ScriptContext* script_context, int local_id); |
+ ~ExtensionPort(); |
+ |
+ // Sets the global id of the port (that is, the id used in the browser |
+ // process to send/receive messages). Any pending messages will be sent. |
+ void SetGlobalId(int id); |
+ |
+ // 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 ClosePort(bool close_channel); |
lazyboy
2016/09/08 00:05:40
"Port" prefix seems extraneous given |this| is a E
Devlin
2016/09/08 19:15:30
Fair enough, done.
|
+ |
+ int local_id() const { return local_id_; } |
+ int global_id() const { return global_id_; } |
+ bool initialized() const { return global_id_ != -1; } |
lazyboy
2016/09/08 00:05:40
IsInitialized() and move definition to cc file.
Devlin
2016/09/08 19:15:30
Why? This is basically a simple accessor (it does
lazyboy
2016/09/08 21:59:16
I guess I simplified the meaning of simple accesso
|
+ |
+ private: |
+ // Helper function to send the post message IPC. |
+ void PostMessageImpl(content::RenderFrame* render_frame, |
+ const Message& message); |
+ |
+ // Sends the message to the browser that this port has been disconnected. |
+ void SendDisconnected(content::RenderFrame* render_frame); |
+ |
+ // The associated ScriptContext for this port. Since these objects are owned |
+ // by a NativeHandler, this should always be valid. |
+ ScriptContext* script_context_ = nullptr; |
lazyboy
2016/09/08 00:05:40
Here and for local_id_: Do you need default member
Devlin
2016/09/08 19:15:30
I think yes, because we want a fallback if the cto
lazyboy
2016/09/08 21:59:16
Acknowledged.
|
+ |
+ // The local id of the port (used within JS bindings). |
+ int local_id_ = -1; |
+ |
+ // The global id of the port (used by the browser process). |
+ int global_id_ = -1; |
+ |
+ // The queue of any pending messages to be sent once the port is initialized. |
+ std::vector<std::unique_ptr<Message>> pending_messages_; |
+ |
+ // Whether or not the port is disconnected. |
+ bool is_disconnected_ = false; |
+ |
+ // Whether to close the full message channel. |
+ bool close_channel_ = false; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ExtensionPort); |
+}; |
+ |
+} // namespace extensions |
+ |
+#endif // EXTENSIONS_RENDERER_EXTENSION_PORT_H_ |