| 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 } | 74 } |
| 75 |
| 75 ~ExtensionImpl() {} | 76 ~ExtensionImpl() {} |
| 76 | 77 |
| 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 } | |
| 88 return ChromeV8Extension::GetNativeFunction(name); | |
| 89 } | |
| 90 | |
| 91 // Sends a message along the given channel. | 78 // Sends a message along the given channel. |
| 92 static v8::Handle<v8::Value> PostMessage(const v8::Arguments& args) { | 79 static v8::Handle<v8::Value> PostMessage(const v8::Arguments& args) { |
| 93 content::RenderView* renderview = GetCurrentRenderView(); | 80 content::RenderView* renderview = GetCurrentRenderView(); |
| 94 if (!renderview) | 81 if (!renderview) |
| 95 return v8::Undefined(); | 82 return v8::Undefined(); |
| 96 | 83 |
| 97 if (args.Length() >= 2 && args[0]->IsInt32() && args[1]->IsString()) { | 84 if (args.Length() >= 2 && args[0]->IsInt32() && args[1]->IsString()) { |
| 98 int port_id = args[0]->Int32Value(); | 85 int port_id = args[0]->Int32Value(); |
| 99 if (!HasPortData(port_id)) { | 86 if (!HasPortData(port_id)) { |
| 100 return v8::ThrowException(v8::Exception::Error( | 87 return v8::ThrowException(v8::Exception::Error( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 135 } |
| 149 } | 136 } |
| 150 return v8::Undefined(); | 137 return v8::Undefined(); |
| 151 } | 138 } |
| 152 }; | 139 }; |
| 153 | 140 |
| 154 } // namespace | 141 } // namespace |
| 155 | 142 |
| 156 namespace extensions { | 143 namespace extensions { |
| 157 | 144 |
| 158 v8::Extension* MiscellaneousBindings::Get(ExtensionDispatcher* dispatcher) { | 145 ChromeV8Extension* MiscellaneousBindings::Get(ExtensionDispatcher* dispatcher) { |
| 159 static v8::Extension* extension = new ExtensionImpl(dispatcher); | 146 return new ExtensionImpl(dispatcher); |
| 160 return extension; | |
| 161 } | 147 } |
| 162 | 148 |
| 163 void MiscellaneousBindings::DeliverMessage( | 149 void MiscellaneousBindings::DeliverMessage( |
| 164 const ChromeV8ContextSet::ContextSet& contexts, | 150 const ChromeV8ContextSet::ContextSet& contexts, |
| 165 int target_port_id, | 151 int target_port_id, |
| 166 const std::string& message, | 152 const std::string& message, |
| 167 content::RenderView* restrict_to_render_view) { | 153 content::RenderView* restrict_to_render_view) { |
| 168 v8::HandleScope handle_scope; | 154 v8::HandleScope handle_scope; |
| 169 | 155 |
| 170 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); | 156 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 192 arguments.push_back(v8::String::New(message.c_str(), message.size())); | 178 arguments.push_back(v8::String::New(message.c_str(), message.size())); |
| 193 arguments.push_back(port_id_handle); | 179 arguments.push_back(port_id_handle); |
| 194 CHECK((*it)->CallChromeHiddenMethod("Port.dispatchOnMessage", | 180 CHECK((*it)->CallChromeHiddenMethod("Port.dispatchOnMessage", |
| 195 arguments.size(), | 181 arguments.size(), |
| 196 &arguments[0], | 182 &arguments[0], |
| 197 NULL)); | 183 NULL)); |
| 198 } | 184 } |
| 199 } | 185 } |
| 200 | 186 |
| 201 } // namespace extension | 187 } // namespace extension |
| OLD | NEW |