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