| 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/runtime_custom_bindings.h" | 5 #include "chrome/renderer/extensions/runtime_custom_bindings.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "chrome/common/extensions/extension_messages.h" | 11 #include "chrome/common/extensions/extension_messages.h" |
| 12 #include "chrome/common/extensions/manifest.h" | 12 #include "chrome/common/extensions/manifest.h" |
| 13 #include "chrome/renderer/extensions/chrome_v8_context.h" | 13 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 14 #include "content/public/renderer/render_view.h" | 14 #include "content/public/renderer/render_view.h" |
| 15 #include "content/public/renderer/v8_value_converter.h" | 15 #include "content/public/renderer/v8_value_converter.h" |
| 16 | 16 |
| 17 using content::V8ValueConverter; | 17 using content::V8ValueConverter; |
| 18 | 18 |
| 19 namespace extensions { | 19 namespace extensions { |
| 20 | 20 |
| 21 RuntimeCustomBindings::RuntimeCustomBindings(Dispatcher* dispatcher, | 21 RuntimeCustomBindings::RuntimeCustomBindings(ChromeV8Context* context) |
| 22 ChromeV8Context* context) | 22 : ChromeV8Extension(NULL), context_(context) { |
| 23 : ChromeV8Extension(dispatcher, context->v8_context()), | |
| 24 context_(context) { | |
| 25 RouteFunction("GetManifest", | 23 RouteFunction("GetManifest", |
| 26 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this))); | 24 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this))); |
| 27 RouteStaticFunction("OpenChannelToExtension", &OpenChannelToExtension); | 25 RouteStaticFunction("OpenChannelToExtension", &OpenChannelToExtension); |
| 28 RouteStaticFunction("OpenChannelToNativeApp", &OpenChannelToNativeApp); | 26 RouteStaticFunction("OpenChannelToNativeApp", &OpenChannelToNativeApp); |
| 29 } | 27 } |
| 30 | 28 |
| 31 RuntimeCustomBindings::~RuntimeCustomBindings() {} | 29 RuntimeCustomBindings::~RuntimeCustomBindings() {} |
| 32 | 30 |
| 33 // static | 31 // static |
| 34 v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToExtension( | 32 v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToExtension( |
| 35 const v8::Arguments& args) { | 33 const v8::Arguments& args) { |
| 36 // Get the current RenderView so that we can send a routed IPC message from | 34 // Get the current RenderView so that we can send a routed IPC message from |
| 37 // the correct source. | 35 // the correct source. |
| 38 RuntimeCustomBindings* self = GetFromArguments<RuntimeCustomBindings>(args); | 36 content::RenderView* renderview = GetCurrentRenderView(); |
| 39 content::RenderView* renderview = self->GetRenderView(); | |
| 40 if (!renderview) | 37 if (!renderview) |
| 41 return v8::Undefined(); | 38 return v8::Undefined(); |
| 42 | 39 |
| 43 // The Javascript code should validate/fill the arguments. | 40 // The Javascript code should validate/fill the arguments. |
| 44 CHECK(args.Length() >= 3 && | 41 CHECK(args.Length() >= 3 && |
| 45 args[0]->IsString() && | 42 args[0]->IsString() && |
| 46 args[1]->IsString() && | 43 args[1]->IsString() && |
| 47 args[2]->IsString()); | 44 args[2]->IsString()); |
| 48 | 45 |
| 49 std::string source_id = *v8::String::Utf8Value(args[0]->ToString()); | 46 std::string source_id = *v8::String::Utf8Value(args[0]->ToString()); |
| 50 std::string target_id = *v8::String::Utf8Value(args[1]->ToString()); | 47 std::string target_id = *v8::String::Utf8Value(args[1]->ToString()); |
| 51 std::string channel_name = *v8::String::Utf8Value(args[2]->ToString()); | 48 std::string channel_name = *v8::String::Utf8Value(args[2]->ToString()); |
| 52 int port_id = -1; | 49 int port_id = -1; |
| 53 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension( | 50 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension( |
| 54 renderview->GetRoutingID(), | 51 renderview->GetRoutingID(), |
| 55 source_id, | 52 source_id, |
| 56 target_id, | 53 target_id, |
| 57 channel_name, | 54 channel_name, |
| 58 &port_id)); | 55 &port_id)); |
| 59 return v8::Integer::New(port_id); | 56 return v8::Integer::New(port_id); |
| 60 } | 57 } |
| 61 | 58 |
| 62 // static | 59 // static |
| 63 v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToNativeApp( | 60 v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToNativeApp( |
| 64 const v8::Arguments& args) { | 61 const v8::Arguments& args) { |
| 65 // Get the current RenderView so that we can send a routed IPC message from | 62 // Get the current RenderView so that we can send a routed IPC message from |
| 66 // the correct source. | 63 // the correct source. |
| 67 RuntimeCustomBindings* self = GetFromArguments<RuntimeCustomBindings>(args); | 64 content::RenderView* renderview = GetCurrentRenderView(); |
| 68 content::RenderView* renderview = self->GetRenderView(); | |
| 69 if (!renderview) | 65 if (!renderview) |
| 70 return v8::Undefined(); | 66 return v8::Undefined(); |
| 71 | 67 |
| 72 // The Javascript code should validate/fill the arguments. | 68 // The Javascript code should validate/fill the arguments. |
| 73 CHECK(args.Length() >= 2 && | 69 CHECK(args.Length() >= 2 && |
| 74 args[0]->IsString() && | 70 args[0]->IsString() && |
| 75 args[1]->IsString()); | 71 args[1]->IsString()); |
| 76 | 72 |
| 77 std::string extension_id = *v8::String::Utf8Value(args[0]->ToString()); | 73 std::string extension_id = *v8::String::Utf8Value(args[0]->ToString()); |
| 78 std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString()); | 74 std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 89 v8::Handle<v8::Value> RuntimeCustomBindings::GetManifest( | 85 v8::Handle<v8::Value> RuntimeCustomBindings::GetManifest( |
| 90 const v8::Arguments& args) { | 86 const v8::Arguments& args) { |
| 91 CHECK(context_->extension()); | 87 CHECK(context_->extension()); |
| 92 | 88 |
| 93 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 89 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 94 return converter->ToV8Value(context_->extension()->manifest()->value(), | 90 return converter->ToV8Value(context_->extension()->manifest()->value(), |
| 95 context_->v8_context()); | 91 context_->v8_context()); |
| 96 } | 92 } |
| 97 | 93 |
| 98 } // extensions | 94 } // extensions |
| OLD | NEW |