Index: content/renderer/web_ui_extension.cc |
diff --git a/content/renderer/web_ui_extension.cc b/content/renderer/web_ui_extension.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0dedd8fd04362d62056c2b3d92f68e39579551d9 |
--- /dev/null |
+++ b/content/renderer/web_ui_extension.cc |
@@ -0,0 +1,149 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/renderer/web_ui_extension.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/utf_string_conversions.h" |
+#include "base/values.h" |
+#include "content/common/view_messages.h" |
+#include "content/public/common/bindings_policy.h" |
+#include "content/public/common/url_constants.h" |
+#include "content/public/renderer/render_thread.h" |
+#include "content/public/renderer/render_view.h" |
+#include "googleurl/src/gurl.h" |
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
+#include "v8/include/v8.h" |
+ |
+namespace { |
+ |
+// Converts a V8 value to a UTF8 string. |
+std::string V8ValueToUTF8(v8::Handle<v8::Value> v) { |
+ v8::String::Value s(v); |
+ string16 str16(reinterpret_cast<const char16*>(*s), s.length()); |
+ return UTF16ToUTF8(str16); |
+} |
abarth-chromium
2012/08/23 20:13:24
We must already have this code elsewhere. Can we
Shishir
2012/11/06 20:08:38
I cant find a common libaray that has these. I had
|
+ |
+Value* ConvertV8Value(v8::Handle<v8::Value> value) { |
+ if (value->IsBoolean()) |
+ return Value::CreateBooleanValue(value->BooleanValue()); |
+ if (value->IsInt32()) |
+ return Value::CreateIntegerValue(value->Int32Value()); |
+ if (value->IsNumber()) |
+ return Value::CreateDoubleValue(value->NumberValue()); |
+ if (value->IsString() || value->IsStringObject()) |
+ return Value::CreateStringValue(V8ValueToUTF8(value)); |
+ |
+ if (value->IsObject() || value->IsArray()) { |
+ // We currently assume all objects are arrays. |
+ DCHECK(value->IsArray()); |
+ |
+ v8::Handle<v8::Array> arr = value.As<v8::Array>(); |
+ ListValue* list = new ListValue(); |
+ for (size_t i = 0; i < arr->Length(); ++i) { |
+ v8::Handle<v8::Value> v = arr->Get(i); |
+ list->Append(ConvertV8Value(v)); |
+ } |
+ return list; |
+ } |
+ |
+ // Covers null and undefined. |
+ return Value::CreateNullValue(); |
+} |
abarth-chromium
2012/08/23 20:13:24
ditto
Shishir
2012/11/06 20:08:38
Found a converter that does this and replaced this
|
+ |
+} // namespace |
+ |
+namespace content { |
+ |
+static const char* const kWebUIExtensionName = "v8/WebUI"; |
+ |
+static const char* const kWebUIExtensionJS = |
+ "var chrome;" |
+ "if (!chrome)" |
+ " chrome = {};" |
+ "chrome.send = function(name, data) {" |
+ " native function Send();" |
+ " Send(name, data);" |
+ "};"; |
+ |
+ |
+class WebUIExtensionWrapper : public v8::Extension { |
+ public: |
+ WebUIExtensionWrapper(); |
+ virtual ~WebUIExtensionWrapper(); |
+ |
+ virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
+ v8::Handle<v8::String> name); |
+ static v8::Handle<v8::Value> Send(const v8::Arguments& args); |
+ |
+ private: |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WebUIExtensionWrapper); |
+}; |
+ |
+WebUIExtensionWrapper::WebUIExtensionWrapper() |
+ : v8::Extension(kWebUIExtensionName, kWebUIExtensionJS) { } |
+ |
+WebUIExtensionWrapper::~WebUIExtensionWrapper() { } |
+ |
+v8::Handle<v8::FunctionTemplate> WebUIExtensionWrapper::GetNativeFunction( |
+ v8::Handle<v8::String> name) { |
+ return name->Equals(v8::String::New("Send")) ? |
+ v8::FunctionTemplate::New(Send) : v8::Handle<v8::FunctionTemplate>(); |
+} |
+ |
+// static |
+v8::Handle<v8::Value> WebUIExtensionWrapper::Send(const v8::Arguments& args) { |
+ // The send function is only available to internal webui pages. |
+ WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext(); |
+ if (!frame || !frame->view()) |
+ return v8::Undefined(); |
+ |
+ content::RenderView* render_view = |
+ content::RenderView::FromWebView(frame->view()); |
+ GURL frame_url = frame->document().url(); |
+ |
+ bool webui_enabled = |
+ (render_view->GetEnabledBindings() & content::BINDINGS_POLICY_WEB_UI) && |
+ (frame_url.SchemeIs(chrome::kChromeUIScheme) || |
+ frame_url.SchemeIs(chrome::kDataScheme)); |
abarth-chromium
2012/08/23 20:13:24
I see, the check went here. That's not as good be
Shishir
2012/11/06 20:08:38
The webuis all have this along with one exception
|
+ |
+ if (!webui_enabled) |
+ return v8::Undefined(); |
+ |
+ // We expect at least two parameters - a string message identifier, and |
+ // an object parameter. The object param can be undefined. |
+ if (args.Length() != 2 || !args[0]->IsString()) |
+ return v8::Undefined(); |
+ |
+ const std::string message = V8ValueToUTF8(args[0]); |
+ |
+ // If they've provided an optional message parameter, convert that into a |
+ // Value to send to the browser process. |
+ scoped_ptr<Value> content; |
+ if (args[1]->IsUndefined()) { |
+ content.reset(new ListValue()); |
+ } else { |
+ if (!args[1]->IsObject()) |
+ return v8::Undefined(); |
+ |
+ content.reset(ConvertV8Value(args[1])); |
+ CHECK(content->IsType(Value::TYPE_LIST)); |
+ } |
+ |
+ // Send the message up to the browser. |
+ render_view->Send(new ViewHostMsg_WebUISend( |
+ render_view->GetRoutingID(), |
+ frame_url, |
+ message, |
+ *(static_cast<ListValue*>(content.get())))); |
+ return v8::Undefined(); |
+} |
+ |
+v8::Extension* WebUIExtension::Get() { |
+ return new WebUIExtensionWrapper(); |
+} |
+ |
+} // namespace content |