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 "googleurl/src/gurl.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
18 #include "v8/include/v8.h" | |
19 | |
20 namespace { | |
21 | |
22 // Converts a V8 value to a UTF8 string. | |
23 std::string V8ValueToUTF8(v8::Handle<v8::Value> v) { | |
24 v8::String::Value s(v); | |
25 string16 str16(reinterpret_cast<const char16*>(*s), s.length()); | |
26 return UTF16ToUTF8(str16); | |
27 } | |
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
| |
28 | |
29 Value* ConvertV8Value(v8::Handle<v8::Value> value) { | |
30 if (value->IsBoolean()) | |
31 return Value::CreateBooleanValue(value->BooleanValue()); | |
32 if (value->IsInt32()) | |
33 return Value::CreateIntegerValue(value->Int32Value()); | |
34 if (value->IsNumber()) | |
35 return Value::CreateDoubleValue(value->NumberValue()); | |
36 if (value->IsString() || value->IsStringObject()) | |
37 return Value::CreateStringValue(V8ValueToUTF8(value)); | |
38 | |
39 if (value->IsObject() || value->IsArray()) { | |
40 // We currently assume all objects are arrays. | |
41 DCHECK(value->IsArray()); | |
42 | |
43 v8::Handle<v8::Array> arr = value.As<v8::Array>(); | |
44 ListValue* list = new ListValue(); | |
45 for (size_t i = 0; i < arr->Length(); ++i) { | |
46 v8::Handle<v8::Value> v = arr->Get(i); | |
47 list->Append(ConvertV8Value(v)); | |
48 } | |
49 return list; | |
50 } | |
51 | |
52 // Covers null and undefined. | |
53 return Value::CreateNullValue(); | |
54 } | |
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
| |
55 | |
56 } // namespace | |
57 | |
58 namespace content { | |
59 | |
60 static const char* const kWebUIExtensionName = "v8/WebUI"; | |
61 | |
62 static const char* const kWebUIExtensionJS = | |
63 "var chrome;" | |
64 "if (!chrome)" | |
65 " chrome = {};" | |
66 "chrome.send = function(name, data) {" | |
67 " native function Send();" | |
68 " Send(name, data);" | |
69 "};"; | |
70 | |
71 | |
72 class WebUIExtensionWrapper : public v8::Extension { | |
73 public: | |
74 WebUIExtensionWrapper(); | |
75 virtual ~WebUIExtensionWrapper(); | |
76 | |
77 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | |
78 v8::Handle<v8::String> name); | |
79 static v8::Handle<v8::Value> Send(const v8::Arguments& args); | |
80 | |
81 private: | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(WebUIExtensionWrapper); | |
84 }; | |
85 | |
86 WebUIExtensionWrapper::WebUIExtensionWrapper() | |
87 : v8::Extension(kWebUIExtensionName, kWebUIExtensionJS) { } | |
88 | |
89 WebUIExtensionWrapper::~WebUIExtensionWrapper() { } | |
90 | |
91 v8::Handle<v8::FunctionTemplate> WebUIExtensionWrapper::GetNativeFunction( | |
92 v8::Handle<v8::String> name) { | |
93 return name->Equals(v8::String::New("Send")) ? | |
94 v8::FunctionTemplate::New(Send) : v8::Handle<v8::FunctionTemplate>(); | |
95 } | |
96 | |
97 // static | |
98 v8::Handle<v8::Value> WebUIExtensionWrapper::Send(const v8::Arguments& args) { | |
99 // The send function is only available to internal webui pages. | |
100 WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext(); | |
101 if (!frame || !frame->view()) | |
102 return v8::Undefined(); | |
103 | |
104 content::RenderView* render_view = | |
105 content::RenderView::FromWebView(frame->view()); | |
106 GURL frame_url = frame->document().url(); | |
107 | |
108 bool webui_enabled = | |
109 (render_view->GetEnabledBindings() & content::BINDINGS_POLICY_WEB_UI) && | |
110 (frame_url.SchemeIs(chrome::kChromeUIScheme) || | |
111 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
| |
112 | |
113 if (!webui_enabled) | |
114 return v8::Undefined(); | |
115 | |
116 // We expect at least two parameters - a string message identifier, and | |
117 // an object parameter. The object param can be undefined. | |
118 if (args.Length() != 2 || !args[0]->IsString()) | |
119 return v8::Undefined(); | |
120 | |
121 const std::string message = V8ValueToUTF8(args[0]); | |
122 | |
123 // If they've provided an optional message parameter, convert that into a | |
124 // Value to send to the browser process. | |
125 scoped_ptr<Value> content; | |
126 if (args[1]->IsUndefined()) { | |
127 content.reset(new ListValue()); | |
128 } else { | |
129 if (!args[1]->IsObject()) | |
130 return v8::Undefined(); | |
131 | |
132 content.reset(ConvertV8Value(args[1])); | |
133 CHECK(content->IsType(Value::TYPE_LIST)); | |
134 } | |
135 | |
136 // Send the message up to the browser. | |
137 render_view->Send(new ViewHostMsg_WebUISend( | |
138 render_view->GetRoutingID(), | |
139 frame_url, | |
140 message, | |
141 *(static_cast<ListValue*>(content.get())))); | |
142 return v8::Undefined(); | |
143 } | |
144 | |
145 v8::Extension* WebUIExtension::Get() { | |
146 return new WebUIExtensionWrapper(); | |
147 } | |
148 | |
149 } // namespace content | |
OLD | NEW |