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