OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/web_ui_extension.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "base/values.h" |
| 10 #include "content/common/view_messages.h" |
| 11 #include "content/public/common/bindings_policy.h" |
| 12 #include "content/public/common/url_constants.h" |
| 13 #include "content/public/renderer/render_thread.h" |
| 14 #include "content/public/renderer/render_view.h" |
| 15 #include "content/public/renderer/v8_value_converter.h" |
| 16 #include "content/renderer/web_ui_extension_data.h" |
| 17 #include "googleurl/src/gurl.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 21 #include "v8/include/v8.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Converts a V8 value to a string16. |
| 26 string16 V8ValueToUTF16(v8::Handle<v8::Value> v) { |
| 27 v8::String::Value s(v); |
| 28 return string16(reinterpret_cast<const char16*>(*s), s.length()); |
| 29 } |
| 30 |
| 31 // Converts a V8 value to a UTF8 string. |
| 32 std::string V8ValueToUTF8(v8::Handle<v8::Value> v) { |
| 33 return UTF16ToUTF8(V8ValueToUTF16(v)); |
| 34 } |
| 35 |
| 36 // Converts string16 to V8 String. |
| 37 v8::Handle<v8::String> UTF16ToV8String(const string16& s) { |
| 38 return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size()); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 namespace content { |
| 44 |
| 45 static const char* const kWebUIExtensionName = "v8/WebUI"; |
| 46 |
| 47 static const char* const kWebUIExtensionJS = |
| 48 "var chrome;" |
| 49 "if (!chrome)" |
| 50 " chrome = {};" |
| 51 "chrome.send = function(name, data) {" |
| 52 " native function Send();" |
| 53 " Send(name, data);" |
| 54 "};" |
| 55 "chrome.getVariableValue = function(name) {" |
| 56 " native function GetVariableValue();" |
| 57 " return GetVariableValue(name);" |
| 58 "};"; |
| 59 |
| 60 |
| 61 class WebUIExtensionWrapper : public v8::Extension { |
| 62 public: |
| 63 WebUIExtensionWrapper(); |
| 64 virtual ~WebUIExtensionWrapper(); |
| 65 |
| 66 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 67 v8::Handle<v8::String> name); |
| 68 static v8::Handle<v8::Value> Send(const v8::Arguments& args); |
| 69 static v8::Handle<v8::Value> GetVariableValue(const v8::Arguments& args); |
| 70 |
| 71 private: |
| 72 static bool ShouldRespondToRequest(WebKit::WebFrame** frame_ptr, |
| 73 RenderView** render_view_ptr); |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(WebUIExtensionWrapper); |
| 76 }; |
| 77 |
| 78 WebUIExtensionWrapper::WebUIExtensionWrapper() |
| 79 : v8::Extension(kWebUIExtensionName, kWebUIExtensionJS) { } |
| 80 |
| 81 WebUIExtensionWrapper::~WebUIExtensionWrapper() { } |
| 82 |
| 83 v8::Handle<v8::FunctionTemplate> WebUIExtensionWrapper::GetNativeFunction( |
| 84 v8::Handle<v8::String> name) { |
| 85 if (name->Equals(v8::String::New("Send"))) { |
| 86 return v8::FunctionTemplate::New(Send); |
| 87 } else if (name->Equals(v8::String::New("GetVariableValue"))) { |
| 88 return v8::FunctionTemplate::New(GetVariableValue); |
| 89 } else { |
| 90 return v8::Handle<v8::FunctionTemplate>(); |
| 91 } |
| 92 } |
| 93 |
| 94 // static |
| 95 bool WebUIExtensionWrapper::ShouldRespondToRequest( |
| 96 WebKit::WebFrame** frame_ptr, |
| 97 RenderView** render_view_ptr) { |
| 98 WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext(); |
| 99 if (!frame || !frame->view()) |
| 100 return false; |
| 101 |
| 102 RenderView* render_view = RenderView::FromWebView(frame->view()); |
| 103 if (!render_view) |
| 104 return false; |
| 105 |
| 106 GURL frame_url = frame->document().url(); |
| 107 |
| 108 bool webui_enabled = |
| 109 (render_view->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI) && |
| 110 (frame_url.SchemeIs(chrome::kChromeUIScheme) || |
| 111 frame_url.SchemeIs(chrome::kDataScheme)); |
| 112 |
| 113 if (!webui_enabled) |
| 114 return false; |
| 115 |
| 116 *frame_ptr = frame; |
| 117 *render_view_ptr = render_view; |
| 118 return true; |
| 119 } |
| 120 |
| 121 // static |
| 122 v8::Handle<v8::Value> WebUIExtensionWrapper::Send(const v8::Arguments& args) { |
| 123 WebKit::WebFrame* frame; |
| 124 RenderView* render_view; |
| 125 if (!ShouldRespondToRequest(&frame, &render_view)) |
| 126 return v8::Undefined(); |
| 127 |
| 128 // We expect at least two parameters - a string message identifier, and |
| 129 // an object parameter. The object param can be undefined. |
| 130 if (args.Length() != 2 || !args[0]->IsString()) |
| 131 return v8::Undefined(); |
| 132 |
| 133 const std::string message = V8ValueToUTF8(args[0]); |
| 134 |
| 135 // If they've provided an optional message parameter, convert that into a |
| 136 // Value to send to the browser process. |
| 137 scoped_ptr<Value> content; |
| 138 if (args[1]->IsUndefined()) { |
| 139 content.reset(new ListValue()); |
| 140 } else { |
| 141 if (!args[1]->IsObject()) |
| 142 return v8::Undefined(); |
| 143 |
| 144 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 145 content.reset(converter->FromV8Value(args[1], |
| 146 frame->mainWorldScriptContext())); |
| 147 CHECK(content->IsType(Value::TYPE_LIST)); |
| 148 } |
| 149 |
| 150 // Send the message up to the browser. |
| 151 render_view->Send(new ViewHostMsg_WebUISend( |
| 152 render_view->GetRoutingID(), |
| 153 frame->document().url(), |
| 154 message, |
| 155 *(static_cast<ListValue*>(content.get())))); |
| 156 return v8::Undefined(); |
| 157 } |
| 158 |
| 159 v8::Handle<v8::Value> WebUIExtensionWrapper::GetVariableValue( |
| 160 const v8::Arguments& args) { |
| 161 WebKit::WebFrame* frame; |
| 162 RenderView* render_view; |
| 163 if (!ShouldRespondToRequest(&frame, &render_view)) |
| 164 return v8::Undefined(); |
| 165 |
| 166 if (!args.Length() || !args[0]->IsString()) |
| 167 return v8::Undefined(); |
| 168 |
| 169 string16 key = V8ValueToUTF16(args[0]); |
| 170 |
| 171 return UTF16ToV8String(WebUIExtensionData::Get(render_view)->GetValue(key)); |
| 172 } |
| 173 |
| 174 v8::Extension* WebUIExtension::Get() { |
| 175 return new WebUIExtensionWrapper(); |
| 176 } |
| 177 |
| 178 } // namespace content |
OLD | NEW |