OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_MESSAGING_BINDINGS_H_ | 5 #ifndef EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ |
6 #define EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ | 6 #define EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/unguessable_token.h" |
12 #include "extensions/renderer/object_backed_native_handler.h" | 13 #include "extensions/renderer/object_backed_native_handler.h" |
13 | 14 |
14 struct ExtensionMsg_ExternalConnectionInfo; | 15 struct ExtensionMsg_ExternalConnectionInfo; |
15 struct ExtensionMsg_TabConnectionInfo; | 16 struct ExtensionMsg_TabConnectionInfo; |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 class RenderFrame; | 19 class RenderFrame; |
19 } | 20 } |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 class ExtensionPort; | 23 class ExtensionPort; |
23 struct Message; | 24 struct Message; |
| 25 struct PortId; |
24 class ScriptContextSet; | 26 class ScriptContextSet; |
25 | 27 |
26 // Manually implements JavaScript bindings for extension messaging. | 28 // Manually implements JavaScript bindings for extension messaging. |
27 class MessagingBindings : public ObjectBackedNativeHandler { | 29 class MessagingBindings : public ObjectBackedNativeHandler { |
28 public: | 30 public: |
29 explicit MessagingBindings(ScriptContext* script_context); | 31 explicit MessagingBindings(ScriptContext* script_context); |
30 ~MessagingBindings() override; | 32 ~MessagingBindings() override; |
31 | 33 |
32 // Checks whether the port exists in the given frame. If it does not, a reply | 34 // Checks whether the port exists in the given frame. If it does not, a reply |
33 // is sent back to the browser. | 35 // is sent back to the browser. |
34 static void ValidateMessagePort(const ScriptContextSet& context_set, | 36 static void ValidateMessagePort(const ScriptContextSet& context_set, |
35 int port_id, | 37 const PortId& port_id, |
36 content::RenderFrame* render_frame); | 38 content::RenderFrame* render_frame); |
37 | 39 |
38 // Dispatches the onConnect content script messaging event to some contexts | 40 // Dispatches the onConnect content script messaging event to some contexts |
39 // in |context_set|. If |restrict_to_render_frame| is specified, only contexts | 41 // in |context_set|. If |restrict_to_render_frame| is specified, only contexts |
40 // in that render frame will receive the message. | 42 // in that render frame will receive the message. |
41 static void DispatchOnConnect(const ScriptContextSet& context_set, | 43 static void DispatchOnConnect(const ScriptContextSet& context_set, |
42 int target_port_id, | 44 const PortId& target_port_id, |
43 const std::string& channel_name, | 45 const std::string& channel_name, |
44 const ExtensionMsg_TabConnectionInfo& source, | 46 const ExtensionMsg_TabConnectionInfo& source, |
45 const ExtensionMsg_ExternalConnectionInfo& info, | 47 const ExtensionMsg_ExternalConnectionInfo& info, |
46 const std::string& tls_channel_id, | 48 const std::string& tls_channel_id, |
47 content::RenderFrame* restrict_to_render_frame); | 49 content::RenderFrame* restrict_to_render_frame); |
48 | 50 |
49 // Delivers a message sent using content script messaging to some of the | 51 // Delivers a message sent using content script messaging to some of the |
50 // contexts in |bindings_context_set|. If |restrict_to_render_frame| is | 52 // contexts in |bindings_context_set|. If |restrict_to_render_frame| is |
51 // specified, only contexts in that render view will receive the message. | 53 // specified, only contexts in that render view will receive the message. |
52 static void DeliverMessage(const ScriptContextSet& context_set, | 54 static void DeliverMessage(const ScriptContextSet& context_set, |
53 int target_port_id, | 55 const PortId& target_port_id, |
54 const Message& message, | 56 const Message& message, |
55 content::RenderFrame* restrict_to_render_frame); | 57 content::RenderFrame* restrict_to_render_frame); |
56 | 58 |
57 // Dispatches the onDisconnect event in response to the channel being closed. | 59 // Dispatches the onDisconnect event in response to the channel being closed. |
58 static void DispatchOnDisconnect( | 60 static void DispatchOnDisconnect( |
59 const ScriptContextSet& context_set, | 61 const ScriptContextSet& context_set, |
60 int port_id, | 62 const PortId& port_id, |
61 const std::string& error_message, | 63 const std::string& error_message, |
62 content::RenderFrame* restrict_to_render_frame); | 64 content::RenderFrame* restrict_to_render_frame); |
63 | 65 |
64 // Returns an existing port with the given |global_id|, or null. | 66 // Returns an existing port with the given |id|, or null. |
65 ExtensionPort* GetPortWithGlobalId(int global_id); | 67 ExtensionPort* GetPortWithId(const PortId& id); |
66 | 68 |
67 // Creates a new port with the given |global_id|. MessagingBindings owns the | 69 // Creates a new port with the given |id|. MessagingBindings owns the |
68 // returned port. | 70 // returned port. |
69 ExtensionPort* CreateNewPortWithGlobalId(int global_id); | 71 ExtensionPort* CreateNewPortWithId(const PortId& id); |
70 | 72 |
71 // Removes the port with the given |local_id|. | 73 // Removes the port with the given |js_id|. |
72 void RemovePortWithLocalId(int local_id); | 74 void RemovePortWithJsId(int js_id); |
| 75 |
| 76 const base::UnguessableToken& context_id() const { return context_id_; } |
73 | 77 |
74 base::WeakPtr<MessagingBindings> GetWeakPtr(); | 78 base::WeakPtr<MessagingBindings> GetWeakPtr(); |
75 | 79 |
76 private: | 80 private: |
77 using PortMap = std::map<int, std::unique_ptr<ExtensionPort>>; | 81 using PortMap = std::map<int, std::unique_ptr<ExtensionPort>>; |
78 | 82 |
79 // JS Exposed Function: Sends a message along the given channel. | 83 // JS Exposed Function: Sends a message along the given channel. |
80 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args); | 84 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args); |
81 | 85 |
82 // JS Exposed Function: Close a port, optionally forcefully (i.e. close the | 86 // JS Exposed Function: Close a port, optionally forcefully (i.e. close the |
(...skipping 10 matching lines...) Expand all Loading... |
93 void OpenChannelToExtension(const v8::FunctionCallbackInfo<v8::Value>& args); | 97 void OpenChannelToExtension(const v8::FunctionCallbackInfo<v8::Value>& args); |
94 | 98 |
95 // JS Exposed Function: Opens a new channel to a native application. | 99 // JS Exposed Function: Opens a new channel to a native application. |
96 void OpenChannelToNativeApp(const v8::FunctionCallbackInfo<v8::Value>& args); | 100 void OpenChannelToNativeApp(const v8::FunctionCallbackInfo<v8::Value>& args); |
97 | 101 |
98 // JS Exposed Function: Opens a new channel to a tab. | 102 // JS Exposed Function: Opens a new channel to a tab. |
99 void OpenChannelToTab(const v8::FunctionCallbackInfo<v8::Value>& args); | 103 void OpenChannelToTab(const v8::FunctionCallbackInfo<v8::Value>& args); |
100 | 104 |
101 // Helper function to close a port. See CloseChannel() for |force_close| | 105 // Helper function to close a port. See CloseChannel() for |force_close| |
102 // documentation. | 106 // documentation. |
103 void ClosePort(int port_id, bool force_close); | 107 void ClosePort(int local_port_id, bool force_close); |
104 | 108 |
105 // Sets the global id for the port with |local_id|. | 109 int GetNextJsId(); |
106 void SetGlobalPortId(int local_id, int global_id); | |
107 | |
108 int GetNextLocalId(); | |
109 | 110 |
110 // Active ports, mapped by local port id. | 111 // Active ports, mapped by local port id. |
111 PortMap ports_; | 112 PortMap ports_; |
112 | 113 |
113 // Ports which are disconnected, but haven't been fully initialized. Once | 114 // The next available js id for a port. |
114 // initialized and any pending messages are sent, these ports are removed. | 115 size_t next_js_id_ = 0; |
115 PortMap disconnected_ports_; | |
116 | 116 |
117 // The next available local id for a port. | 117 // The number of extension ports created. |
118 size_t next_local_id_ = 0; | 118 size_t num_extension_ports_ = 0; |
119 | 119 |
120 // The number of ports created in the 'beforeunload' event handler. | 120 // A unique identifier for this JS context. |
121 size_t ports_created_in_before_unload_ = 0; | 121 const base::UnguessableToken context_id_; |
122 | |
123 // The number of ports created in the 'unload' event handler. | |
124 size_t ports_created_in_unload_ = 0; | |
125 | |
126 // The number of ports created during during any time that isn't in the unload | |
127 // or beforeunload handlers. | |
128 int ports_created_normal_ = 0; | |
129 | 122 |
130 base::WeakPtrFactory<MessagingBindings> weak_ptr_factory_; | 123 base::WeakPtrFactory<MessagingBindings> weak_ptr_factory_; |
131 | 124 |
132 DISALLOW_COPY_AND_ASSIGN(MessagingBindings); | 125 DISALLOW_COPY_AND_ASSIGN(MessagingBindings); |
133 }; | 126 }; |
134 | 127 |
135 } // namespace extensions | 128 } // namespace extensions |
136 | 129 |
137 #endif // EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ | 130 #endif // EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ |
OLD | NEW |