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())); | |
abarth-chromium
2012/11/09 18:19:43
I don't think this is quite right. V8 strings are
Shishir
2012/11/09 19:20:08
Done.
sreeram
2012/11/12 19:42:44
For the record, I think the earlier code was fine
| |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 namespace content { | |
34 | |
35 static const char* const kWebUIExtensionName = "v8/WebUI"; | |
36 | |
37 // Javascript that gets executed when the extension loads into all frames. | |
38 // Exposes two methods: | |
39 // - chrome.send: Used to send messages to the browser. Requires the message | |
40 // name as the first argument and can have other optional arguments. | |
Evan Stade
2012/11/08 01:37:59
nit: there is only one other argument, which is an
Shishir
2012/11/09 19:20:08
Done.
| |
41 // - chrome.getVariableValue: Returns value for the input variable name if such | |
42 // a value was set by the browser. Else will return an empty string. | |
43 static const char* const kWebUIExtensionJS = | |
44 "var chrome;" | |
45 "if (!chrome)" | |
46 " chrome = {};" | |
47 "chrome.send = function(name, data) {" | |
48 " native function Send();" | |
49 " Send(name, data);" | |
50 "};" | |
51 "chrome.getVariableValue = function(name) {" | |
52 " native function GetVariableValue();" | |
53 " return GetVariableValue(name);" | |
54 "};"; | |
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) {} | |
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"))) | |
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 // static | |
89 bool WebUIExtensionWrapper::ShouldRespondToRequest( | |
abarth-chromium
2012/11/09 18:19:43
It looks like the main difference is now we're exp
Shishir
2012/11/09 19:20:08
Done.
| |
90 WebKit::WebFrame** frame_ptr, | |
91 RenderView** render_view_ptr) { | |
92 WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext(); | |
93 if (!frame || !frame->view()) | |
94 return false; | |
95 | |
96 RenderView* render_view = RenderView::FromWebView(frame->view()); | |
97 if (!render_view) | |
98 return false; | |
99 | |
100 GURL frame_url = frame->document().url(); | |
101 | |
102 bool webui_enabled = | |
103 (render_view->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI) && | |
104 (frame_url.SchemeIs(chrome::kChromeUIScheme) || | |
105 frame_url.SchemeIs(chrome::kDataScheme)); | |
106 | |
107 if (!webui_enabled) | |
108 return false; | |
109 | |
110 *frame_ptr = frame; | |
111 *render_view_ptr = render_view; | |
112 return true; | |
113 } | |
114 | |
115 // static | |
116 v8::Handle<v8::Value> WebUIExtensionWrapper::Send(const v8::Arguments& args) { | |
117 WebKit::WebFrame* frame; | |
118 RenderView* render_view; | |
119 if (!ShouldRespondToRequest(&frame, &render_view)) | |
120 return v8::Undefined(); | |
121 | |
122 // We expect at least two parameters - a string message identifier, and | |
123 // an object parameter. The object param can be undefined. | |
124 if (args.Length() != 2 || !args[0]->IsString()) | |
125 return v8::Undefined(); | |
126 | |
127 const std::string message = V8ValueToUTF8(args[0]); | |
128 | |
129 // If they've provided an optional message parameter, convert that into a | |
130 // Value to send to the browser process. | |
131 scoped_ptr<ListValue> content; | |
132 if (args[1]->IsUndefined()) { | |
133 content.reset(new ListValue()); | |
134 } else { | |
135 if (!args[1]->IsObject()) | |
136 return v8::Undefined(); | |
137 | |
138 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
139 | |
140 base::Value* value = converter->FromV8Value( | |
141 args[1], frame->mainWorldScriptContext()); | |
142 base::ListValue* list = NULL; | |
143 value->GetAsList(&list); | |
144 DCHECK(list); | |
145 content.reset(list); | |
146 } | |
147 | |
148 // Send the message up to the browser. | |
149 render_view->Send(new ViewHostMsg_WebUISend(render_view->GetRoutingID(), | |
150 frame->document().url(), | |
151 message, | |
152 *content)); | |
153 return v8::Undefined(); | |
154 } | |
155 | |
156 v8::Handle<v8::Value> WebUIExtensionWrapper::GetVariableValue( | |
157 const v8::Arguments& args) { | |
158 WebKit::WebFrame* frame; | |
159 RenderView* render_view; | |
160 if (!ShouldRespondToRequest(&frame, &render_view)) | |
161 return v8::Undefined(); | |
162 | |
163 if (!args.Length() || !args[0]->IsString()) | |
164 return v8::Undefined(); | |
165 | |
166 std::string key = V8ValueToUTF8(args[0]); | |
167 | |
168 const std::string& value = | |
169 WebUIExtensionData::Get(render_view)->GetValue(key); | |
170 return v8::String::New(value.c_str(), value.length()); | |
171 } | |
172 | |
173 v8::Extension* WebUIExtension::Get() { | |
174 return new WebUIExtensionWrapper(); | |
175 } | |
176 | |
177 } // namespace content | |
OLD | NEW |