| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/miscellaneous_bindings.h" | 5 #include "chrome/renderer/extensions/miscellaneous_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" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 static ExtensionData::PortData& GetPortData(int port_id) { | 56 static ExtensionData::PortData& GetPortData(int port_id) { |
| 57 return g_extension_data.Get().ports[port_id]; | 57 return g_extension_data.Get().ports[port_id]; |
| 58 } | 58 } |
| 59 | 59 |
| 60 static void ClearPortData(int port_id) { | 60 static void ClearPortData(int port_id) { |
| 61 g_extension_data.Get().ports.erase(port_id); | 61 g_extension_data.Get().ports.erase(port_id); |
| 62 } | 62 } |
| 63 | 63 |
| 64 const char kPortClosedError[] = "Attempting to use a disconnected port object"; | 64 const char kPortClosedError[] = "Attempting to use a disconnected port object"; |
| 65 const char* kExtensionDeps[] = { "extensions/event.js" }; | |
| 66 | 65 |
| 67 class ExtensionImpl : public ChromeV8Extension { | 66 class ExtensionImpl : public ChromeV8Extension { |
| 68 public: | 67 public: |
| 69 explicit ExtensionImpl(ExtensionDispatcher* dispatcher) | 68 explicit ExtensionImpl(ExtensionDispatcher* dispatcher) |
| 70 : ChromeV8Extension("extensions/miscellaneous_bindings.js", | 69 : ChromeV8Extension(dispatcher) { |
| 71 IDR_MISCELLANEOUS_BINDINGS_JS, | 70 RouteStaticFunction("CloseChannel", &CloseChannel); |
| 72 arraysize(kExtensionDeps), kExtensionDeps, | 71 RouteStaticFunction("PortAddRef", &PortAddRef); |
| 73 dispatcher) { | 72 RouteStaticFunction("PortRelease", &PortRelease); |
| 73 RouteStaticFunction("PostMessage", &PostMessage); |
| 74 RouteStaticFunction("BindToGC", &BindToGC); |
| 74 } | 75 } |
| 76 |
| 75 ~ExtensionImpl() {} | 77 ~ExtensionImpl() {} |
| 76 | 78 |
| 77 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | |
| 78 v8::Handle<v8::String> name) { | |
| 79 if (name->Equals(v8::String::New("PostMessage"))) { | |
| 80 return v8::FunctionTemplate::New(PostMessage); | |
| 81 } else if (name->Equals(v8::String::New("CloseChannel"))) { | |
| 82 return v8::FunctionTemplate::New(CloseChannel); | |
| 83 } else if (name->Equals(v8::String::New("PortAddRef"))) { | |
| 84 return v8::FunctionTemplate::New(PortAddRef); | |
| 85 } else if (name->Equals(v8::String::New("PortRelease"))) { | |
| 86 return v8::FunctionTemplate::New(PortRelease); | |
| 87 } else if (name->Equals(v8::String::New("BindToGC"))) { | |
| 88 return v8::FunctionTemplate::New(BindToGC); | |
| 89 } | |
| 90 return ChromeV8Extension::GetNativeFunction(name); | |
| 91 } | |
| 92 | |
| 93 // Sends a message along the given channel. | 79 // Sends a message along the given channel. |
| 94 static v8::Handle<v8::Value> PostMessage(const v8::Arguments& args) { | 80 static v8::Handle<v8::Value> PostMessage(const v8::Arguments& args) { |
| 95 content::RenderView* renderview = GetCurrentRenderView(); | 81 content::RenderView* renderview = GetCurrentRenderView(); |
| 96 if (!renderview) | 82 if (!renderview) |
| 97 return v8::Undefined(); | 83 return v8::Undefined(); |
| 98 | 84 |
| 99 if (args.Length() >= 2 && args[0]->IsInt32() && args[1]->IsString()) { | 85 if (args.Length() >= 2 && args[0]->IsInt32() && args[1]->IsString()) { |
| 100 int port_id = args[0]->Int32Value(); | 86 int port_id = args[0]->Int32Value(); |
| 101 if (!HasPortData(port_id)) { | 87 if (!HasPortData(port_id)) { |
| 102 return v8::ThrowException(v8::Exception::Error( | 88 return v8::ThrowException(v8::Exception::Error( |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 NOTREACHED(); | 165 NOTREACHED(); |
| 180 } | 166 } |
| 181 return v8::Undefined(); | 167 return v8::Undefined(); |
| 182 } | 168 } |
| 183 }; | 169 }; |
| 184 | 170 |
| 185 } // namespace | 171 } // namespace |
| 186 | 172 |
| 187 namespace extensions { | 173 namespace extensions { |
| 188 | 174 |
| 189 v8::Extension* MiscellaneousBindings::Get(ExtensionDispatcher* dispatcher) { | 175 ChromeV8Extension* MiscellaneousBindings::Get(ExtensionDispatcher* dispatcher) { |
| 190 static v8::Extension* extension = new ExtensionImpl(dispatcher); | 176 return new ExtensionImpl(dispatcher); |
| 191 return extension; | |
| 192 } | 177 } |
| 193 | 178 |
| 194 void MiscellaneousBindings::DeliverMessage( | 179 void MiscellaneousBindings::DeliverMessage( |
| 195 const ChromeV8ContextSet::ContextSet& contexts, | 180 const ChromeV8ContextSet::ContextSet& contexts, |
| 196 int target_port_id, | 181 int target_port_id, |
| 197 const std::string& message, | 182 const std::string& message, |
| 198 content::RenderView* restrict_to_render_view) { | 183 content::RenderView* restrict_to_render_view) { |
| 199 v8::HandleScope handle_scope; | 184 v8::HandleScope handle_scope; |
| 200 | 185 |
| 201 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); | 186 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 223 arguments.push_back(v8::String::New(message.c_str(), message.size())); | 208 arguments.push_back(v8::String::New(message.c_str(), message.size())); |
| 224 arguments.push_back(port_id_handle); | 209 arguments.push_back(port_id_handle); |
| 225 CHECK((*it)->CallChromeHiddenMethod("Port.dispatchOnMessage", | 210 CHECK((*it)->CallChromeHiddenMethod("Port.dispatchOnMessage", |
| 226 arguments.size(), | 211 arguments.size(), |
| 227 &arguments[0], | 212 &arguments[0], |
| 228 NULL)); | 213 NULL)); |
| 229 } | 214 } |
| 230 } | 215 } |
| 231 | 216 |
| 232 } // namespace extension | 217 } // namespace extension |
| OLD | NEW |