Chromium Code Reviews| 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..3d4c3891e5c1734310b507c2ac6cde6485a4d464 |
| --- /dev/null |
| +++ b/content/renderer/web_ui_extension.cc |
| @@ -0,0 +1,174 @@ |
| +// 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 "content/public/renderer/v8_value_converter.h" |
| +#include "content/renderer/web_ui_extension_data.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 "third_party/WebKit/Source/WebKit/chromium/public/WebView.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); |
| + return UTF16ToUTF8(string16(reinterpret_cast<const char16*>(*s), s.length())); |
| +} |
| + |
| +// Converts string16 to V8 String. |
| +v8::Handle<v8::String> UTF16ToV8String(const string16& s) { |
| + return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size()); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace content { |
| + |
| +static const char* const kWebUIExtensionName = "v8/WebUI"; |
| + |
| +static const char* const kWebUIExtensionJS = |
|
Evan Stade
2012/11/07 23:09:39
document
Shishir
2012/11/08 00:10:38
Done.
|
| + "var chrome;" |
| + "if (!chrome)" |
| + " chrome = {};" |
| + "chrome.send = function(name, data) {" |
| + " native function Send();" |
| + " Send(name, data);" |
| + "};" |
| + "chrome.getVariableValue = function(name) {" |
| + " native function GetVariableValue();" |
| + " return GetVariableValue(name);" |
| + "};"; |
| + |
|
Evan Stade
2012/11/07 23:09:39
there's an extra newline here
Shishir
2012/11/08 00:10:38
Done.
|
| + |
| +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); |
| + static v8::Handle<v8::Value> GetVariableValue(const v8::Arguments& args); |
| + |
| + private: |
| + static bool ShouldRespondToRequest(WebKit::WebFrame** frame_ptr, |
| + RenderView** render_view_ptr); |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebUIExtensionWrapper); |
| +}; |
| + |
| +WebUIExtensionWrapper::WebUIExtensionWrapper() |
| + : v8::Extension(kWebUIExtensionName, kWebUIExtensionJS) { } |
|
Evan Stade
2012/11/07 23:09:39
nit: {}
Shishir
2012/11/08 00:10:38
Done.
|
| + |
| +WebUIExtensionWrapper::~WebUIExtensionWrapper() { } |
| + |
| +v8::Handle<v8::FunctionTemplate> WebUIExtensionWrapper::GetNativeFunction( |
| + v8::Handle<v8::String> name) { |
| + if (name->Equals(v8::String::New("Send"))) { |
|
Evan Stade
2012/11/07 23:09:39
nit: don't think these conditions need curlies...
Shishir
2012/11/08 00:10:38
Done.
|
| + return v8::FunctionTemplate::New(Send); |
| + } else if (name->Equals(v8::String::New("GetVariableValue"))) { |
| + return v8::FunctionTemplate::New(GetVariableValue); |
| + } else { |
| + return v8::Handle<v8::FunctionTemplate>(); |
| + } |
| +} |
| + |
| +// static |
| +bool WebUIExtensionWrapper::ShouldRespondToRequest( |
| + WebKit::WebFrame** frame_ptr, |
| + RenderView** render_view_ptr) { |
| + WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext(); |
| + if (!frame || !frame->view()) |
| + return false; |
| + |
| + RenderView* render_view = RenderView::FromWebView(frame->view()); |
| + if (!render_view) |
| + return false; |
| + |
| + GURL frame_url = frame->document().url(); |
| + |
| + bool webui_enabled = |
| + (render_view->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI) && |
| + (frame_url.SchemeIs(chrome::kChromeUIScheme) || |
| + frame_url.SchemeIs(chrome::kDataScheme)); |
| + |
| + if (!webui_enabled) |
| + return false; |
| + |
| + *frame_ptr = frame; |
| + *render_view_ptr = render_view; |
| + return true; |
| +} |
| + |
| +// static |
| +v8::Handle<v8::Value> WebUIExtensionWrapper::Send(const v8::Arguments& args) { |
| + WebKit::WebFrame* frame; |
| + RenderView* render_view; |
| + if (!ShouldRespondToRequest(&frame, &render_view)) |
| + 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(); |
| + |
| + scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| + content.reset(converter->FromV8Value(args[1], |
| + frame->mainWorldScriptContext())); |
| + CHECK(content->IsType(Value::TYPE_LIST)); |
|
Evan Stade
2012/11/07 23:09:39
imo, use GetAsListValue, make content a scoped_ptr
Shishir
2012/11/08 00:10:38
Done.
|
| + } |
| + |
| + // Send the message up to the browser. |
| + render_view->Send(new ViewHostMsg_WebUISend( |
| + render_view->GetRoutingID(), |
| + frame->document().url(), |
| + message, |
| + *(static_cast<ListValue*>(content.get())))); |
| + return v8::Undefined(); |
| +} |
| + |
| +v8::Handle<v8::Value> WebUIExtensionWrapper::GetVariableValue( |
| + const v8::Arguments& args) { |
| + WebKit::WebFrame* frame; |
| + RenderView* render_view; |
| + if (!ShouldRespondToRequest(&frame, &render_view)) |
| + return v8::Undefined(); |
| + |
| + if (!args.Length() || !args[0]->IsString()) |
| + return v8::Undefined(); |
| + |
| + std::string key = V8ValueToUTF8(args[0]); |
| + |
| + return UTF16ToV8String(UTF8ToUTF16( |
| + WebUIExtensionData::Get(render_view)->GetValue(key))); |
|
Evan Stade
2012/11/07 23:09:39
can't you use v8::String::New on utf8?
Shishir
2012/11/08 00:10:38
Done.
|
| +} |
| + |
| +v8::Extension* WebUIExtension::Get() { |
| + return new WebUIExtensionWrapper(); |
| +} |
| + |
| +} // namespace content |