| 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 "extensions/renderer/script_context_set.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "extensions/renderer/object_backed_native_handler.h" |
| 11 | 13 |
| 12 struct ExtensionMsg_ExternalConnectionInfo; | 14 struct ExtensionMsg_ExternalConnectionInfo; |
| 13 struct ExtensionMsg_TabConnectionInfo; | 15 struct ExtensionMsg_TabConnectionInfo; |
| 14 | 16 |
| 15 namespace base { | |
| 16 class DictionaryValue; | |
| 17 } | |
| 18 | |
| 19 namespace content { | 17 namespace content { |
| 20 class RenderFrame; | 18 class RenderFrame; |
| 21 } | 19 } |
| 22 | 20 |
| 23 namespace v8 { | |
| 24 class Extension; | |
| 25 } | |
| 26 | |
| 27 namespace extensions { | 21 namespace extensions { |
| 28 struct Message; | 22 struct Message; |
| 29 class ObjectBackedNativeHandler; | |
| 30 class ScriptContextSet; | 23 class ScriptContextSet; |
| 31 | 24 |
| 32 // Manually implements JavaScript bindings for extension messaging. | 25 // Manually implements JavaScript bindings for extension messaging. |
| 33 // | 26 class MessagingBindings : public ObjectBackedNativeHandler { |
| 34 // TODO(aa): This should all get re-implemented using SchemaGeneratedBindings. | |
| 35 // If anything needs to be manual for some reason, it should be implemented in | |
| 36 // its own class. | |
| 37 class MessagingBindings { | |
| 38 public: | 27 public: |
| 39 // Creates an instance of the extension. | 28 explicit MessagingBindings(ScriptContext* script_context); |
| 40 static ObjectBackedNativeHandler* Get(ScriptContext* context); | 29 ~MessagingBindings() override; |
| 41 | 30 |
| 42 // Checks whether the port exists in the given frame. If it does not, a reply | 31 // Checks whether the port exists in the given frame. If it does not, a reply |
| 43 // is sent back to the browser. | 32 // is sent back to the browser. |
| 44 static void ValidateMessagePort(const ScriptContextSet& context_set, | 33 static void ValidateMessagePort(const ScriptContextSet& context_set, |
| 45 int port_id, | 34 int port_id, |
| 46 content::RenderFrame* render_frame); | 35 content::RenderFrame* render_frame); |
| 47 | 36 |
| 48 // Dispatches the onConnect content script messaging event to some contexts | 37 // Dispatches the onConnect content script messaging event to some contexts |
| 49 // in |context_set|. If |restrict_to_render_frame| is specified, only contexts | 38 // in |context_set|. If |restrict_to_render_frame| is specified, only contexts |
| 50 // in that render frame will receive the message. | 39 // in that render frame will receive the message. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 63 int target_port_id, | 52 int target_port_id, |
| 64 const Message& message, | 53 const Message& message, |
| 65 content::RenderFrame* restrict_to_render_frame); | 54 content::RenderFrame* restrict_to_render_frame); |
| 66 | 55 |
| 67 // Dispatches the onDisconnect event in response to the channel being closed. | 56 // Dispatches the onDisconnect event in response to the channel being closed. |
| 68 static void DispatchOnDisconnect( | 57 static void DispatchOnDisconnect( |
| 69 const ScriptContextSet& context_set, | 58 const ScriptContextSet& context_set, |
| 70 int port_id, | 59 int port_id, |
| 71 const std::string& error_message, | 60 const std::string& error_message, |
| 72 content::RenderFrame* restrict_to_render_frame); | 61 content::RenderFrame* restrict_to_render_frame); |
| 62 |
| 63 private: |
| 64 // JS Exposed Function: Sends a message along the given channel. |
| 65 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 66 |
| 67 // JS Exposed Function: Close a port, optionally forcefully (i.e. close the |
| 68 // whole channel instead of just the given port). |
| 69 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 70 |
| 71 // 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 |
| 73 // 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. |
| 75 void BindToGC(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 76 |
| 77 // Helper function to close a port. See CloseChannel() for |force_close| |
| 78 // documentation. |
| 79 void ClosePort(int port_id, bool force_close); |
| 80 |
| 81 base::WeakPtrFactory<MessagingBindings> weak_ptr_factory_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(MessagingBindings); |
| 73 }; | 84 }; |
| 74 | 85 |
| 75 } // namespace extensions | 86 } // namespace extensions |
| 76 | 87 |
| 77 #endif // EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ | 88 #endif // EXTENSIONS_RENDERER_MESSAGING_BINDINGS_H_ |
| OLD | NEW |