| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "chrome/browser/extensions/api/messaging/message_service.h" | 9 #include "chrome/browser/extensions/api/messaging/message_service.h" |
| 10 #include "extensions/common/api/messaging/port_id.h" |
| 10 | 11 |
| 11 class GURL; | 12 class GURL; |
| 12 | 13 |
| 13 namespace content { | 14 namespace content { |
| 14 class BrowserContext; | 15 class BrowserContext; |
| 15 class RenderFrameHost; | 16 class RenderFrameHost; |
| 16 class RenderProcessHost; | 17 class RenderProcessHost; |
| 17 } // namespace content | 18 } // namespace content |
| 18 | 19 |
| 19 namespace IPC { | 20 namespace IPC { |
| 20 class Message; | 21 class Message; |
| 21 } // namespace IPC | 22 } // namespace IPC |
| 22 | 23 |
| 23 namespace extensions { | 24 namespace extensions { |
| 24 | 25 |
| 25 // A port that manages communication with an extension. | 26 // A port that manages communication with an extension. |
| 26 // The port's lifetime will end when either all receivers close the port, or | 27 // The port's lifetime will end when either all receivers close the port, or |
| 27 // when the opener / receiver explicitly closes the channel. | 28 // when the opener / receiver explicitly closes the channel. |
| 28 class ExtensionMessagePort : public MessageService::MessagePort { | 29 class ExtensionMessagePort : public MessageService::MessagePort { |
| 29 public: | 30 public: |
| 30 // Create a port that is tied to frame(s) in a single tab. | 31 // Create a port that is tied to frame(s) in a single tab. |
| 31 ExtensionMessagePort(base::WeakPtr<MessageService> message_service, | 32 ExtensionMessagePort(base::WeakPtr<MessageService> message_service, |
| 32 int port_id, | 33 const PortId& port_id, |
| 33 const std::string& extension_id, | 34 const std::string& extension_id, |
| 34 content::RenderFrameHost* rfh, | 35 content::RenderFrameHost* rfh, |
| 35 bool include_child_frames); | 36 bool include_child_frames); |
| 36 // Create a port that is tied to all frames of an extension, possibly spanning | 37 // Create a port that is tied to all frames of an extension, possibly spanning |
| 37 // multiple tabs, including the invisible background page, popups, etc. | 38 // multiple tabs, including the invisible background page, popups, etc. |
| 38 ExtensionMessagePort(base::WeakPtr<MessageService> message_service, | 39 ExtensionMessagePort(base::WeakPtr<MessageService> message_service, |
| 39 int port_id, | 40 const PortId& port_id, |
| 40 const std::string& extension_id, | 41 const std::string& extension_id, |
| 41 content::RenderProcessHost* extension_process); | 42 content::RenderProcessHost* extension_process); |
| 42 ~ExtensionMessagePort() override; | 43 ~ExtensionMessagePort() override; |
| 43 | 44 |
| 44 // Checks whether the frames to which this port is tied at its construction | 45 // Checks whether the frames to which this port is tied at its construction |
| 45 // are still aware of this port's existence. Frames that don't know about | 46 // are still aware of this port's existence. Frames that don't know about |
| 46 // the port are removed from the set of frames. This should be used for opener | 47 // the port are removed from the set of frames. This should be used for opener |
| 47 // ports because the frame may be navigated before the port was initialized. | 48 // ports because the frame may be navigated before the port was initialized. |
| 48 void RevalidatePort(); | 49 void RevalidatePort(); |
| 49 | 50 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 78 void UnregisterFrame(content::RenderFrameHost* rfh); | 79 void UnregisterFrame(content::RenderFrameHost* rfh); |
| 79 | 80 |
| 80 // Immediately close the port and its associated channel. | 81 // Immediately close the port and its associated channel. |
| 81 void CloseChannel(); | 82 void CloseChannel(); |
| 82 | 83 |
| 83 // Send a IPC message to the renderer for all registered frames. | 84 // Send a IPC message to the renderer for all registered frames. |
| 84 void SendToPort(std::unique_ptr<IPC::Message> msg); | 85 void SendToPort(std::unique_ptr<IPC::Message> msg); |
| 85 | 86 |
| 86 base::WeakPtr<MessageService> weak_message_service_; | 87 base::WeakPtr<MessageService> weak_message_service_; |
| 87 | 88 |
| 88 int port_id_; | 89 const PortId port_id_; |
| 89 std::string extension_id_; | 90 std::string extension_id_; |
| 90 content::BrowserContext* browser_context_; | 91 content::BrowserContext* browser_context_; |
| 91 // Only for receivers in an extension process. | 92 // Only for receivers in an extension process. |
| 92 content::RenderProcessHost* extension_process_; | 93 content::RenderProcessHost* extension_process_; |
| 93 | 94 |
| 94 // When the port is used as a sender, this set contains only one element. | 95 // When the port is used as a sender, this set contains only one element. |
| 95 // If used as a receiver, it may contain any number of frames. | 96 // If used as a receiver, it may contain any number of frames. |
| 96 // This set is populated before the first message is sent to the destination, | 97 // This set is populated before the first message is sent to the destination, |
| 97 // and shrinks over time when the port is rejected by the recipient frame, or | 98 // and shrinks over time when the port is rejected by the recipient frame, or |
| 98 // when the frame is removed or unloaded. | 99 // when the frame is removed or unloaded. |
| 99 std::set<content::RenderFrameHost*> frames_; | 100 std::set<content::RenderFrameHost*> frames_; |
| 100 | 101 |
| 101 // Whether the renderer acknowledged creation of the port. This is used to | 102 // Whether the renderer acknowledged creation of the port. This is used to |
| 102 // distinguish abnormal port closure (e.g. no receivers) from explicit port | 103 // distinguish abnormal port closure (e.g. no receivers) from explicit port |
| 103 // closure (e.g. by the port.disconnect() JavaScript method in the renderer). | 104 // closure (e.g. by the port.disconnect() JavaScript method in the renderer). |
| 104 bool did_create_port_; | 105 bool did_create_port_; |
| 105 | 106 |
| 106 ExtensionHost* background_host_ptr_; // used in IncrementLazyKeepaliveCount | 107 ExtensionHost* background_host_ptr_; // used in IncrementLazyKeepaliveCount |
| 107 std::unique_ptr<FrameTracker> frame_tracker_; | 108 std::unique_ptr<FrameTracker> frame_tracker_; |
| 108 | 109 |
| 109 DISALLOW_COPY_AND_ASSIGN(ExtensionMessagePort); | 110 DISALLOW_COPY_AND_ASSIGN(ExtensionMessagePort); |
| 110 }; | 111 }; |
| 111 | 112 |
| 112 } // namespace extensions | 113 } // namespace extensions |
| 113 | 114 |
| 114 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ | 115 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ |
| OLD | NEW |