| 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/extension_custom_bindings.h" | 5 #include "chrome/renderer/extensions/extension_custom_bindings.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/common/chrome_view_type.h" | 10 #include "chrome/common/chrome_view_type.h" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 std::string extension_id_; | 104 std::string extension_id_; |
| 105 int browser_window_id_; | 105 int browser_window_id_; |
| 106 content::ViewType view_type_; | 106 content::ViewType view_type_; |
| 107 v8::Local<v8::Array> views_; | 107 v8::Local<v8::Array> views_; |
| 108 int index_; | 108 int index_; |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 } // namespace | 111 } // namespace |
| 112 | 112 |
| 113 ExtensionCustomBindings::ExtensionCustomBindings( | 113 ExtensionCustomBindings::ExtensionCustomBindings( |
| 114 int dependency_count, |
| 115 const char** dependencies, |
| 114 ExtensionDispatcher* extension_dispatcher) | 116 ExtensionDispatcher* extension_dispatcher) |
| 115 : ChromeV8Extension(extension_dispatcher) { | 117 : ChromeV8Extension( |
| 116 RouteStaticFunction("GetExtensionViews", &GetExtensionViews); | 118 "extensions/extension_custom_bindings.js", |
| 117 RouteStaticFunction("OpenChannelToExtension", &OpenChannelToExtension); | 119 IDR_EXTENSION_CUSTOM_BINDINGS_JS, |
| 118 } | 120 dependency_count, |
| 121 dependencies, |
| 122 extension_dispatcher) {} |
| 119 | 123 |
| 120 // static | 124 // static |
| 121 v8::Handle<v8::Value> ExtensionCustomBindings::GetExtensionViews( | 125 v8::Handle<v8::Value> ExtensionCustomBindings::GetExtensionViews( |
| 122 const v8::Arguments& args) { | 126 const v8::Arguments& args) { |
| 123 if (args.Length() != 2) | 127 if (args.Length() != 2) |
| 124 return v8::Undefined(); | 128 return v8::Undefined(); |
| 125 | 129 |
| 126 if (!args[0]->IsInt32() || !args[1]->IsString()) | 130 if (!args[0]->IsInt32() || !args[1]->IsString()) |
| 127 return v8::Undefined(); | 131 return v8::Undefined(); |
| 128 | 132 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 v8_extension->GetExtensionForCurrentRenderView(); | 165 v8_extension->GetExtensionForCurrentRenderView(); |
| 162 if (!extension) | 166 if (!extension) |
| 163 return v8::Undefined(); | 167 return v8::Undefined(); |
| 164 | 168 |
| 165 ExtensionViewAccumulator accumulator(extension->id(), browser_window_id, | 169 ExtensionViewAccumulator accumulator(extension->id(), browser_window_id, |
| 166 view_type); | 170 view_type); |
| 167 content::RenderView::ForEach(&accumulator); | 171 content::RenderView::ForEach(&accumulator); |
| 168 return accumulator.views(); | 172 return accumulator.views(); |
| 169 } | 173 } |
| 170 | 174 |
| 175 v8::Handle<v8::FunctionTemplate> ExtensionCustomBindings::GetNativeFunction( |
| 176 v8::Handle<v8::String> name) { |
| 177 if (name->Equals(v8::String::New("GetExtensionViews"))) { |
| 178 return v8::FunctionTemplate::New(GetExtensionViews, |
| 179 v8::External::New(this)); |
| 180 } else if (name->Equals(v8::String::New("OpenChannelToExtension"))) { |
| 181 return v8::FunctionTemplate::New(OpenChannelToExtension); |
| 182 } |
| 183 |
| 184 return ChromeV8Extension::GetNativeFunction(name); |
| 185 } |
| 186 |
| 171 // static | 187 // static |
| 172 v8::Handle<v8::Value> ExtensionCustomBindings::OpenChannelToExtension( | 188 v8::Handle<v8::Value> ExtensionCustomBindings::OpenChannelToExtension( |
| 173 const v8::Arguments& args) { | 189 const v8::Arguments& args) { |
| 174 // Get the current RenderView so that we can send a routed IPC message from | 190 // Get the current RenderView so that we can send a routed IPC message from |
| 175 // the correct source. | 191 // the correct source. |
| 176 content::RenderView* renderview = GetCurrentRenderView(); | 192 content::RenderView* renderview = GetCurrentRenderView(); |
| 177 if (!renderview) | 193 if (!renderview) |
| 178 return v8::Undefined(); | 194 return v8::Undefined(); |
| 179 | 195 |
| 180 // The Javascript code should validate/fill the arguments. | 196 // The Javascript code should validate/fill the arguments. |
| 181 CHECK(args.Length() >= 3 && | 197 CHECK(args.Length() >= 3 && |
| 182 args[0]->IsString() && | 198 args[0]->IsString() && |
| 183 args[1]->IsString() && | 199 args[1]->IsString() && |
| 184 args[2]->IsString()); | 200 args[2]->IsString()); |
| 185 | 201 |
| 186 std::string source_id = *v8::String::Utf8Value(args[0]->ToString()); | 202 std::string source_id = *v8::String::Utf8Value(args[0]->ToString()); |
| 187 std::string target_id = *v8::String::Utf8Value(args[1]->ToString()); | 203 std::string target_id = *v8::String::Utf8Value(args[1]->ToString()); |
| 188 std::string channel_name = *v8::String::Utf8Value(args[2]->ToString()); | 204 std::string channel_name = *v8::String::Utf8Value(args[2]->ToString()); |
| 189 int port_id = -1; | 205 int port_id = -1; |
| 190 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension( | 206 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension( |
| 191 renderview->GetRoutingID(), | 207 renderview->GetRoutingID(), |
| 192 source_id, | 208 source_id, |
| 193 target_id, | 209 target_id, |
| 194 channel_name, | 210 channel_name, |
| 195 &port_id)); | 211 &port_id)); |
| 196 return v8::Integer::New(port_id); | 212 return v8::Integer::New(port_id); |
| 197 } | 213 } |
| 198 | 214 |
| 199 } // namespace extensions | 215 } // namespace extensions |
| OLD | NEW |