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