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 "chrome/browser/extensions/api/messaging/message_service.h" | 9 #include "chrome/browser/extensions/api/messaging/message_service.h" |
9 | 10 |
10 class GURL; | 11 class GURL; |
11 | 12 |
12 namespace content { | 13 namespace content { |
| 14 class BrowserContext; |
| 15 class RenderFrameHost; |
13 class RenderProcessHost; | 16 class RenderProcessHost; |
14 } // namespace content | 17 } // namespace content |
15 | 18 |
| 19 namespace IPC { |
| 20 class Message; |
| 21 } // namespace IPC |
| 22 |
16 namespace extensions { | 23 namespace extensions { |
17 | 24 |
18 // A port that manages communication with an extension. | 25 // A port that manages communication with an extension. |
| 26 // The port's lifetime will end when either all receivers close the port, or |
| 27 // when the opener / receiver explicitly closes the channel. |
19 class ExtensionMessagePort : public MessageService::MessagePort { | 28 class ExtensionMessagePort : public MessageService::MessagePort { |
20 public: | 29 public: |
21 ExtensionMessagePort(content::RenderProcessHost* process, | 30 // Create a port that is tied to frame(s) in a single tab. |
22 int routing_id, | 31 ExtensionMessagePort(base::WeakPtr<MessageService> message_service, |
23 const std::string& extension_id); | 32 int port_id, |
24 void DispatchOnConnect(int dest_port_id, | 33 const std::string& extension_id, |
25 const std::string& channel_name, | 34 content::RenderFrameHost* rfh, |
| 35 bool include_child_frames); |
| 36 // 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 ExtensionMessagePort(base::WeakPtr<MessageService> message_service, |
| 39 int port_id, |
| 40 const std::string& extension_id, |
| 41 content::RenderProcessHost* extension_process); |
| 42 ~ExtensionMessagePort() override; |
| 43 |
| 44 bool IsValidPort() override; |
| 45 |
| 46 // MessageService::MessagePort: |
| 47 void DispatchOnConnect(const std::string& channel_name, |
26 scoped_ptr<base::DictionaryValue> source_tab, | 48 scoped_ptr<base::DictionaryValue> source_tab, |
27 int source_frame_id, | 49 int source_frame_id, |
28 int target_tab_id, | |
29 int target_frame_id, | |
30 int guest_process_id, | 50 int guest_process_id, |
31 int guest_render_frame_routing_id, | 51 int guest_render_frame_routing_id, |
32 const std::string& source_extension_id, | 52 const std::string& source_extension_id, |
33 const std::string& target_extension_id, | 53 const std::string& target_extension_id, |
34 const GURL& source_url, | 54 const GURL& source_url, |
35 const std::string& tls_channel_id) override; | 55 const std::string& tls_channel_id) override; |
36 void DispatchOnDisconnect(int source_port_id, | 56 void DispatchOnDisconnect(const std::string& error_message) override; |
37 const std::string& error_message) override; | 57 void DispatchOnMessage(const Message& message) override; |
38 void DispatchOnMessage(const Message& message, int target_port_id) override; | |
39 void IncrementLazyKeepaliveCount() override; | 58 void IncrementLazyKeepaliveCount() override; |
40 void DecrementLazyKeepaliveCount() override; | 59 void DecrementLazyKeepaliveCount() override; |
41 content::RenderProcessHost* GetRenderProcessHost() override; | 60 void OpenPort(int process_id, int routing_id) override; |
| 61 void ClosePort(int process_id, int routing_id) override; |
42 | 62 |
43 private: | 63 private: |
44 content::RenderProcessHost* process_; | 64 class FrameTracker; |
45 int routing_id_; | 65 |
| 66 // Registers a frame as a receiver / sender. |
| 67 void RegisterFrame(content::RenderFrameHost* rfh); |
| 68 |
| 69 // Unregisters a frame as a receiver / sender. When there are no registered |
| 70 // frames any more, the port closes via CloseChannel(). |
| 71 void UnregisterFrame(content::RenderFrameHost* rfh); |
| 72 |
| 73 // Immediately close the port and its associated channel. |
| 74 void CloseChannel(); |
| 75 |
| 76 // Send a IPC message to the renderer for all registered frames. |
| 77 void SendToPort(scoped_ptr<IPC::Message> msg); |
| 78 |
| 79 base::WeakPtr<MessageService> weak_message_service_; |
| 80 |
| 81 int port_id_; |
46 std::string extension_id_; | 82 std::string extension_id_; |
47 void* background_host_ptr_; // used in IncrementLazyKeepaliveCount | 83 content::BrowserContext* browser_context_; |
| 84 // Only for receivers in an extension process. |
| 85 content::RenderProcessHost* extension_process_; |
| 86 |
| 87 // When the port is used as a sender, this set contains only one element. |
| 88 // If used as a receiver, it may contain any number of frames. |
| 89 // This set is populated before the first message is sent to the destination, |
| 90 // and shrinks over time when the port is rejected by the recipient frame, or |
| 91 // when the frame is removed or unloaded. |
| 92 std::set<content::RenderFrameHost*> frames_; |
| 93 |
| 94 // Whether the renderer acknowledged creation of the port. This is used to |
| 95 // distinguish abnormal port closure (e.g. no receivers) from explicit port |
| 96 // closure (e.g. by the port.disconnect() JavaScript method in the renderer). |
| 97 bool did_create_port_; |
| 98 |
| 99 ExtensionHost* background_host_ptr_; // used in IncrementLazyKeepaliveCount |
| 100 scoped_ptr<FrameTracker> frame_tracker_; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(ExtensionMessagePort); |
48 }; | 103 }; |
49 | 104 |
50 } // namespace extensions | 105 } // namespace extensions |
51 | 106 |
52 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ | 107 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_EXTENSION_MESSAGE_PORT_H_ |
OLD | NEW |