| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_RENDERER_EXTENSION_PORT_H_ | 5 #ifndef EXTENSIONS_RENDERER_EXTENSION_PORT_H_ |
| 6 #define EXTENSIONS_RENDERER_EXTENSION_PORT_H_ | 6 #define EXTENSIONS_RENDERER_EXTENSION_PORT_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 | 13 #include "extensions/common/api/messaging/port_id.h" |
| 13 namespace content { | |
| 14 class RenderFrame; | |
| 15 } | |
| 16 | 14 |
| 17 namespace extensions { | 15 namespace extensions { |
| 18 | |
| 19 struct Message; | 16 struct Message; |
| 17 struct PortId; |
| 20 class ScriptContext; | 18 class ScriptContext; |
| 21 | 19 |
| 22 // A class representing information about a specific extension message port that | 20 // A class representing information about a specific extension message port that |
| 23 // handles sending related IPCs to the browser. Since global port IDs are | 21 // handles sending related IPCs to the browser. Since global port IDs are |
| 24 // assigned asynchronously, this object caches pending messages. | 22 // assigned asynchronously, this object caches pending messages. |
| 25 class ExtensionPort { | 23 class ExtensionPort { |
| 26 public: | 24 public: |
| 27 ExtensionPort(ScriptContext* script_context, int local_id); | 25 ExtensionPort(ScriptContext* script_context, const PortId& id, int js_id); |
| 28 ~ExtensionPort(); | 26 ~ExtensionPort(); |
| 29 | 27 |
| 30 // Sets the global id of the port (that is, the id used in the browser | |
| 31 // process to send/receive messages). Any pending messages will be sent. | |
| 32 void SetGlobalId(int id); | |
| 33 | |
| 34 // Posts a new message to the port. If the port is not initialized, the | 28 // Posts a new message to the port. If the port is not initialized, the |
| 35 // message will be queued until it is. | 29 // message will be queued until it is. |
| 36 void PostExtensionMessage(std::unique_ptr<Message> message); | 30 void PostExtensionMessage(std::unique_ptr<Message> message); |
| 37 | 31 |
| 38 // Closes the port. If there are pending messages, they will still be sent | 32 // Closes the port. If there are pending messages, they will still be sent |
| 39 // assuming initialization completes (after which, the port will close). | 33 // assuming initialization completes (after which, the port will close). |
| 40 void Close(bool close_channel); | 34 void Close(bool close_channel); |
| 41 | 35 |
| 42 int local_id() const { return local_id_; } | 36 const PortId& id() const { return id_; } |
| 43 int global_id() const { return global_id_; } | 37 int js_id() const { return js_id_; } |
| 44 bool initialized() const { return global_id_ != -1; } | |
| 45 | 38 |
| 46 private: | 39 private: |
| 47 // Helper function to send the post message IPC. | |
| 48 void PostMessageImpl(content::RenderFrame* render_frame, | |
| 49 const Message& message); | |
| 50 | |
| 51 // Sends the message to the browser that this port has been disconnected. | |
| 52 void SendDisconnected(content::RenderFrame* render_frame); | |
| 53 | |
| 54 // The associated ScriptContext for this port. Since these objects are owned | 40 // The associated ScriptContext for this port. Since these objects are owned |
| 55 // by a NativeHandler, this should always be valid. | 41 // by a NativeHandler, this should always be valid. |
| 56 ScriptContext* script_context_ = nullptr; | 42 ScriptContext* script_context_ = nullptr; |
| 57 | 43 |
| 58 // The local id of the port (used within JS bindings). | 44 const PortId id_; |
| 59 int local_id_ = -1; | |
| 60 | 45 |
| 61 // The global id of the port (used by the browser process). | 46 // The id used in the JS bindings. If this is a receiver port, this is not the |
| 62 int global_id_ = -1; | 47 // same as the port_number in the PortId. This should be used only as an |
| 63 | 48 // identifier in the JS context this port is from. |
| 64 // The queue of any pending messages to be sent once the port is initialized. | 49 int js_id_ = 0; |
| 65 std::vector<std::unique_ptr<Message>> pending_messages_; | |
| 66 | |
| 67 // Whether or not the port is disconnected. | |
| 68 bool is_disconnected_ = false; | |
| 69 | |
| 70 // Whether to close the full message channel. | |
| 71 bool close_channel_ = false; | |
| 72 | 50 |
| 73 DISALLOW_COPY_AND_ASSIGN(ExtensionPort); | 51 DISALLOW_COPY_AND_ASSIGN(ExtensionPort); |
| 74 }; | 52 }; |
| 75 | 53 |
| 76 } // namespace extensions | 54 } // namespace extensions |
| 77 | 55 |
| 78 #endif // EXTENSIONS_RENDERER_EXTENSION_PORT_H_ | 56 #endif // EXTENSIONS_RENDERER_EXTENSION_PORT_H_ |
| OLD | NEW |