| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "chrome/renderer/extensions/renderer_extension_bindings.h" | 5 #include "chrome/renderer/extensions/renderer_extension_bindings.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/common/extensions/extension_message_bundle.h" | 13 #include "chrome/common/extensions/extension_message_bundle.h" |
| 14 #include "chrome/common/render_messages.h" | 14 #include "chrome/common/extensions/extension_messages.h" |
| 15 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 16 #include "chrome/renderer/extensions/bindings_utils.h" | 16 #include "chrome/renderer/extensions/bindings_utils.h" |
| 17 #include "chrome/renderer/extensions/event_bindings.h" | 17 #include "chrome/renderer/extensions/event_bindings.h" |
| 18 #include "chrome/renderer/render_thread.h" | 18 #include "chrome/renderer/render_thread.h" |
| 19 #include "content/renderer/render_view.h" | 19 #include "content/renderer/render_view.h" |
| 20 #include "grit/renderer_resources.h" | 20 #include "grit/renderer_resources.h" |
| 21 | 21 |
| 22 using bindings_utils::GetStringResource; | 22 using bindings_utils::GetStringResource; |
| 23 using bindings_utils::ExtensionBase; | 23 using bindings_utils::ExtensionBase; |
| 24 | 24 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); | 97 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); |
| 98 if (!renderview) | 98 if (!renderview) |
| 99 return v8::Undefined(); | 99 return v8::Undefined(); |
| 100 | 100 |
| 101 if (args.Length() >= 3 && args[0]->IsString() && args[1]->IsString() && | 101 if (args.Length() >= 3 && args[0]->IsString() && args[1]->IsString() && |
| 102 args[2]->IsString()) { | 102 args[2]->IsString()) { |
| 103 std::string source_id = *v8::String::Utf8Value(args[0]->ToString()); | 103 std::string source_id = *v8::String::Utf8Value(args[0]->ToString()); |
| 104 std::string target_id = *v8::String::Utf8Value(args[1]->ToString()); | 104 std::string target_id = *v8::String::Utf8Value(args[1]->ToString()); |
| 105 std::string channel_name = *v8::String::Utf8Value(args[2]->ToString()); | 105 std::string channel_name = *v8::String::Utf8Value(args[2]->ToString()); |
| 106 int port_id = -1; | 106 int port_id = -1; |
| 107 renderview->Send(new ViewHostMsg_OpenChannelToExtension( | 107 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension( |
| 108 renderview->routing_id(), source_id, target_id, | 108 renderview->routing_id(), source_id, target_id, |
| 109 channel_name, &port_id)); | 109 channel_name, &port_id)); |
| 110 return v8::Integer::New(port_id); | 110 return v8::Integer::New(port_id); |
| 111 } | 111 } |
| 112 return v8::Undefined(); | 112 return v8::Undefined(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 // Sends a message along the given channel. | 115 // Sends a message along the given channel. |
| 116 static v8::Handle<v8::Value> PostMessage(const v8::Arguments& args) { | 116 static v8::Handle<v8::Value> PostMessage(const v8::Arguments& args) { |
| 117 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); | 117 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); |
| 118 if (!renderview) | 118 if (!renderview) |
| 119 return v8::Undefined(); | 119 return v8::Undefined(); |
| 120 | 120 |
| 121 if (args.Length() >= 2 && args[0]->IsInt32() && args[1]->IsString()) { | 121 if (args.Length() >= 2 && args[0]->IsInt32() && args[1]->IsString()) { |
| 122 int port_id = args[0]->Int32Value(); | 122 int port_id = args[0]->Int32Value(); |
| 123 if (!HasPortData(port_id)) { | 123 if (!HasPortData(port_id)) { |
| 124 return v8::ThrowException(v8::Exception::Error( | 124 return v8::ThrowException(v8::Exception::Error( |
| 125 v8::String::New(kPortClosedError))); | 125 v8::String::New(kPortClosedError))); |
| 126 } | 126 } |
| 127 std::string message = *v8::String::Utf8Value(args[1]->ToString()); | 127 std::string message = *v8::String::Utf8Value(args[1]->ToString()); |
| 128 renderview->Send(new ViewHostMsg_ExtensionPostMessage( | 128 renderview->Send(new ExtensionHostMsg_PostMessage( |
| 129 renderview->routing_id(), port_id, message)); | 129 renderview->routing_id(), port_id, message)); |
| 130 } | 130 } |
| 131 return v8::Undefined(); | 131 return v8::Undefined(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 // Forcefully disconnects a port. | 134 // Forcefully disconnects a port. |
| 135 static v8::Handle<v8::Value> CloseChannel(const v8::Arguments& args) { | 135 static v8::Handle<v8::Value> CloseChannel(const v8::Arguments& args) { |
| 136 if (args.Length() >= 2 && args[0]->IsInt32() && args[1]->IsBoolean()) { | 136 if (args.Length() >= 2 && args[0]->IsInt32() && args[1]->IsBoolean()) { |
| 137 int port_id = args[0]->Int32Value(); | 137 int port_id = args[0]->Int32Value(); |
| 138 if (!HasPortData(port_id)) { | 138 if (!HasPortData(port_id)) { |
| 139 return v8::Undefined(); | 139 return v8::Undefined(); |
| 140 } | 140 } |
| 141 // Send via the RenderThread because the RenderView might be closing. | 141 // Send via the RenderThread because the RenderView might be closing. |
| 142 bool notify_browser = args[1]->BooleanValue(); | 142 bool notify_browser = args[1]->BooleanValue(); |
| 143 if (notify_browser) | 143 if (notify_browser) |
| 144 EventBindings::GetRenderThread()->Send( | 144 EventBindings::GetRenderThread()->Send( |
| 145 new ViewHostMsg_ExtensionCloseChannel(port_id)); | 145 new ExtensionHostMsg_CloseChannel(port_id)); |
| 146 ClearPortData(port_id); | 146 ClearPortData(port_id); |
| 147 } | 147 } |
| 148 return v8::Undefined(); | 148 return v8::Undefined(); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // A new port has been created for a context. This occurs both when script | 151 // A new port has been created for a context. This occurs both when script |
| 152 // opens a connection, and when a connection is opened to this script. | 152 // opens a connection, and when a connection is opened to this script. |
| 153 static v8::Handle<v8::Value> PortAddRef(const v8::Arguments& args) { | 153 static v8::Handle<v8::Value> PortAddRef(const v8::Arguments& args) { |
| 154 if (args.Length() >= 1 && args[0]->IsInt32()) { | 154 if (args.Length() >= 1 && args[0]->IsInt32()) { |
| 155 int port_id = args[0]->Int32Value(); | 155 int port_id = args[0]->Int32Value(); |
| 156 ++GetPortData(port_id).ref_count; | 156 ++GetPortData(port_id).ref_count; |
| 157 } | 157 } |
| 158 return v8::Undefined(); | 158 return v8::Undefined(); |
| 159 } | 159 } |
| 160 | 160 |
| 161 // The frame a port lived in has been destroyed. When there are no more | 161 // The frame a port lived in has been destroyed. When there are no more |
| 162 // frames with a reference to a given port, we will disconnect it and notify | 162 // frames with a reference to a given port, we will disconnect it and notify |
| 163 // the other end of the channel. | 163 // the other end of the channel. |
| 164 static v8::Handle<v8::Value> PortRelease(const v8::Arguments& args) { | 164 static v8::Handle<v8::Value> PortRelease(const v8::Arguments& args) { |
| 165 if (args.Length() >= 1 && args[0]->IsInt32()) { | 165 if (args.Length() >= 1 && args[0]->IsInt32()) { |
| 166 int port_id = args[0]->Int32Value(); | 166 int port_id = args[0]->Int32Value(); |
| 167 if (HasPortData(port_id) && --GetPortData(port_id).ref_count == 0) { | 167 if (HasPortData(port_id) && --GetPortData(port_id).ref_count == 0) { |
| 168 // Send via the RenderThread because the RenderView might be closing. | 168 // Send via the RenderThread because the RenderView might be closing. |
| 169 EventBindings::GetRenderThread()->Send( | 169 EventBindings::GetRenderThread()->Send( |
| 170 new ViewHostMsg_ExtensionCloseChannel(port_id)); | 170 new ExtensionHostMsg_CloseChannel(port_id)); |
| 171 ClearPortData(port_id); | 171 ClearPortData(port_id); |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 return v8::Undefined(); | 174 return v8::Undefined(); |
| 175 } | 175 } |
| 176 | 176 |
| 177 static v8::Handle<v8::Value> GetL10nMessage(const v8::Arguments& args) { | 177 static v8::Handle<v8::Value> GetL10nMessage(const v8::Arguments& args) { |
| 178 if (args.Length() != 3 || !args[0]->IsString()) { | 178 if (args.Length() != 3 || !args[0]->IsString()) { |
| 179 NOTREACHED() << "Bad arguments"; | 179 NOTREACHED() << "Bad arguments"; |
| 180 return v8::Undefined(); | 180 return v8::Undefined(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 192 L10nMessagesMap* l10n_messages = GetL10nMessagesMap(extension_id); | 192 L10nMessagesMap* l10n_messages = GetL10nMessagesMap(extension_id); |
| 193 if (!l10n_messages) { | 193 if (!l10n_messages) { |
| 194 // Get the current RenderView so that we can send a routed IPC message | 194 // Get the current RenderView so that we can send a routed IPC message |
| 195 // from the correct source. | 195 // from the correct source. |
| 196 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); | 196 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); |
| 197 if (!renderview) | 197 if (!renderview) |
| 198 return v8::Undefined(); | 198 return v8::Undefined(); |
| 199 | 199 |
| 200 L10nMessagesMap messages; | 200 L10nMessagesMap messages; |
| 201 // A sync call to load message catalogs for current extension. | 201 // A sync call to load message catalogs for current extension. |
| 202 renderview->Send(new ViewHostMsg_GetExtensionMessageBundle( | 202 renderview->Send(new ExtensionHostMsg_GetMessageBundle( |
| 203 extension_id, &messages)); | 203 extension_id, &messages)); |
| 204 | 204 |
| 205 // Save messages we got. | 205 // Save messages we got. |
| 206 ExtensionToL10nMessagesMap& l10n_messages_map = | 206 ExtensionToL10nMessagesMap& l10n_messages_map = |
| 207 *GetExtensionToL10nMessagesMap(); | 207 *GetExtensionToL10nMessagesMap(); |
| 208 l10n_messages_map[extension_id] = messages; | 208 l10n_messages_map[extension_id] = messages; |
| 209 | 209 |
| 210 l10n_messages = GetL10nMessagesMap(extension_id); | 210 l10n_messages = GetL10nMessagesMap(extension_id); |
| 211 } | 211 } |
| 212 | 212 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 const GURL& event_url) { | 307 const GURL& event_url) { |
| 308 v8::HandleScope handle_scope; | 308 v8::HandleScope handle_scope; |
| 309 std::vector< v8::Handle<v8::Value> > argv = ListValueToV8(args); | 309 std::vector< v8::Handle<v8::Value> > argv = ListValueToV8(args); |
| 310 EventBindings::CallFunction(extension_id, | 310 EventBindings::CallFunction(extension_id, |
| 311 function_name, | 311 function_name, |
| 312 argv.size(), | 312 argv.size(), |
| 313 &argv[0], | 313 &argv[0], |
| 314 renderview, | 314 renderview, |
| 315 event_url); | 315 event_url); |
| 316 } | 316 } |
| OLD | NEW |