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