Chromium Code Reviews| 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|. | |
|
lazyboy
2016/09/08 00:05:40
MessagingBindings owns the port...
Devlin
2016/09/08 19:15:31
Done.
| |
| 68 ExtensionPort* CreateNewPortWithGlobalId(int global_id); | |
| 69 | |
| 70 // Removes the port with the given |local_id|. | |
| 71 void RemovePortWithLocalId(int local_id); | |
| 72 | |
| 73 base::WeakPtr<MessagingBindings> GetWeakPtr(); | |
| 74 | |
| 63 private: | 75 private: |
| 76 using PortMap = std::map<int, std::unique_ptr<ExtensionPort>>; | |
|
lazyboy
2016/09/08 00:05:40
Description: port local id to port info map, etc.
Devlin
2016/09/08 19:15:31
Done, but doc'd over |ports_| (since the typedef i
| |
| 77 | |
| 64 // JS Exposed Function: Sends a message along the given channel. | 78 // JS Exposed Function: Sends a message along the given channel. |
| 65 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args); | 79 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 66 | 80 |
| 67 // JS Exposed Function: Close a port, optionally forcefully (i.e. close the | 81 // JS Exposed Function: Close a port, optionally forcefully (i.e. close the |
| 68 // whole channel instead of just the given port). | 82 // whole channel instead of just the given port). |
| 69 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args); | 83 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 70 | 84 |
| 71 // JS Exposed Function: Binds |callback| to be invoked *sometime after* | 85 // 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 | 86 // |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 | 87 // 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. | 88 // then call into the script context if it's been invalidated. |
| 75 void BindToGC(const v8::FunctionCallbackInfo<v8::Value>& args); | 89 void BindToGC(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 76 | 90 |
| 91 // JS Exposed Function: Opens a new channel to an extension. | |
| 92 void OpenChannelToExtension(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 93 | |
| 94 // JS Exposed Function: Opens a new channel to a native application. | |
| 95 void OpenChannelToNativeApp(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 96 | |
| 97 // JS Exposed Function: Opens a new channel to a tab. | |
| 98 void OpenChannelToTab(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 99 | |
| 77 // Helper function to close a port. See CloseChannel() for |force_close| | 100 // Helper function to close a port. See CloseChannel() for |force_close| |
| 78 // documentation. | 101 // documentation. |
| 79 void ClosePort(int port_id, bool force_close); | 102 void ClosePort(int port_id, bool force_close); |
| 80 | 103 |
| 104 // Sets the global id for the port with |local_id|. | |
| 105 void SetGlobalPortId(int local_id, int global_id); | |
| 106 | |
| 107 // Active ports. | |
| 108 PortMap ports_; | |
| 109 | |
| 110 // Ports which are disconnected, but haven't been fully initialized. Once | |
| 111 // initialized and any pending messages are sent, these ports are removed. | |
| 112 PortMap disconnected_ports_; | |
| 113 | |
| 114 // The next available local id for a port. | |
| 115 int next_local_id_ = 0; | |
| 116 | |
| 81 base::WeakPtrFactory<MessagingBindings> weak_ptr_factory_; | 117 base::WeakPtrFactory<MessagingBindings> weak_ptr_factory_; |
| 82 | 118 |
| 83 DISALLOW_COPY_AND_ASSIGN(MessagingBindings); | 119 DISALLOW_COPY_AND_ASSIGN(MessagingBindings); |
| 84 }; | 120 }; |
| 85 | 121 |
| 86 } // namespace extensions | 122 } // namespace extensions |
| 87 | 123 |
| 88 #endif // EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ | 124 #endif // EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ |
| OLD | NEW |