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 struct Message; | 23 struct Message; |
23 class ScriptContextSet; | 24 class ScriptContextSet; |
24 | 25 |
25 // Manually implements JavaScript bindings for extension messaging. | 26 // Manually implements JavaScript bindings for extension messaging. |
26 class MessagingBindings : public ObjectBackedNativeHandler { | 27 class MessagingBindings : public ObjectBackedNativeHandler { |
27 public: | 28 public: |
28 explicit MessagingBindings(ScriptContext* script_context); | 29 explicit MessagingBindings(ScriptContext* script_context); |
29 ~MessagingBindings() override; | 30 ~MessagingBindings() override; |
30 | 31 |
31 // Checks whether the port exists in the given frame. If it does not, a reply | 32 // Checks whether the port exists in the given frame. If it does not, a reply |
(...skipping 21 matching lines...) Expand all Loading... |
53 const Message& message, | 54 const Message& message, |
54 content::RenderFrame* restrict_to_render_frame); | 55 content::RenderFrame* restrict_to_render_frame); |
55 | 56 |
56 // Dispatches the onDisconnect event in response to the channel being closed. | 57 // Dispatches the onDisconnect event in response to the channel being closed. |
57 static void DispatchOnDisconnect( | 58 static void DispatchOnDisconnect( |
58 const ScriptContextSet& context_set, | 59 const ScriptContextSet& context_set, |
59 int port_id, | 60 int port_id, |
60 const std::string& error_message, | 61 const std::string& error_message, |
61 content::RenderFrame* restrict_to_render_frame); | 62 content::RenderFrame* restrict_to_render_frame); |
62 | 63 |
| 64 // Returns an existing port with the given |global_id|, or null. |
| 65 ExtensionPort* GetPortWithGlobalId(int global_id); |
| 66 |
| 67 // Creates a new port with the given |global_id|. MessagingBindings owns the |
| 68 // returned port. |
| 69 ExtensionPort* CreateNewPortWithGlobalId(int global_id); |
| 70 |
| 71 // Removes the port with the given |local_id|. |
| 72 void RemovePortWithLocalId(int local_id); |
| 73 |
| 74 base::WeakPtr<MessagingBindings> GetWeakPtr(); |
| 75 |
63 private: | 76 private: |
| 77 using PortMap = std::map<int, std::unique_ptr<ExtensionPort>>; |
| 78 |
64 // JS Exposed Function: Sends a message along the given channel. | 79 // JS Exposed Function: Sends a message along the given channel. |
65 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args); | 80 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args); |
66 | 81 |
67 // JS Exposed Function: Close a port, optionally forcefully (i.e. close the | 82 // JS Exposed Function: Close a port, optionally forcefully (i.e. close the |
68 // whole channel instead of just the given port). | 83 // whole channel instead of just the given port). |
69 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args); | 84 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args); |
70 | 85 |
71 // JS Exposed Function: Binds |callback| to be invoked *sometime after* | 86 // JS Exposed Function: Binds |callback| to be invoked *sometime after* |
72 // |object| is garbage collected. We don't call the method re-entrantly so as | 87 // |object| is garbage collected. We don't call the method re-entrantly so as |
73 // to avoid executing JS in some bizarro undefined mid-GC state, nor do we | 88 // to avoid executing JS in some bizarro undefined mid-GC state, nor do we |
74 // then call into the script context if it's been invalidated. | 89 // then call into the script context if it's been invalidated. |
75 void BindToGC(const v8::FunctionCallbackInfo<v8::Value>& args); | 90 void BindToGC(const v8::FunctionCallbackInfo<v8::Value>& args); |
76 | 91 |
| 92 // JS Exposed Function: Opens a new channel to an extension. |
| 93 void OpenChannelToExtension(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 94 |
| 95 // JS Exposed Function: Opens a new channel to a native application. |
| 96 void OpenChannelToNativeApp(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 97 |
| 98 // JS Exposed Function: Opens a new channel to a tab. |
| 99 void OpenChannelToTab(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 100 |
77 // Helper function to close a port. See CloseChannel() for |force_close| | 101 // Helper function to close a port. See CloseChannel() for |force_close| |
78 // documentation. | 102 // documentation. |
79 void ClosePort(int port_id, bool force_close); | 103 void ClosePort(int port_id, bool force_close); |
80 | 104 |
| 105 // Sets the global id for the port with |local_id|. |
| 106 void SetGlobalPortId(int local_id, int global_id); |
| 107 |
| 108 int GetNextLocalId(); |
| 109 |
| 110 // Active ports, mapped by local port id. |
| 111 PortMap ports_; |
| 112 |
| 113 // Ports which are disconnected, but haven't been fully initialized. Once |
| 114 // initialized and any pending messages are sent, these ports are removed. |
| 115 PortMap disconnected_ports_; |
| 116 |
| 117 // The next available local id for a port. |
| 118 int next_local_id_ = 0; |
| 119 |
81 base::WeakPtrFactory<MessagingBindings> weak_ptr_factory_; | 120 base::WeakPtrFactory<MessagingBindings> weak_ptr_factory_; |
82 | 121 |
83 DISALLOW_COPY_AND_ASSIGN(MessagingBindings); | 122 DISALLOW_COPY_AND_ASSIGN(MessagingBindings); |
84 }; | 123 }; |
85 | 124 |
86 } // namespace extensions | 125 } // namespace extensions |
87 | 126 |
88 #endif // EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ | 127 #endif // EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ |
OLD | NEW |