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