OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 // This file provides the embedder's side of random webkit glue functions. | |
6 | |
7 #include "build/build_config.h" | |
8 | |
9 #if defined(OS_WIN) | |
10 #include <windows.h> | |
11 #endif | |
12 | |
13 #include <vector> | |
14 | |
15 #include "base/command_line.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/string_util.h" | |
18 #include "base/utf_string_conversions.h" | |
19 #include "chrome/common/chrome_switches.h" | |
20 #include "chrome/common/chrome_version_info.h" | |
21 #include "chrome/common/render_messages.h" | |
22 #include "chrome/common/url_constants.h" | |
23 #include "content/common/clipboard_messages.h" | |
24 #include "content/common/socket_stream_dispatcher.h" | |
25 #include "content/common/view_messages.h" | |
26 #include "content/plugin/npobject_util.h" | |
27 #include "content/renderer/render_thread.h" | |
28 #include "googleurl/src/url_util.h" | |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h" | |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | |
32 #include "third_party/skia/include/core/SkBitmap.h" | |
33 #include "ui/base/clipboard/clipboard.h" | |
34 #include "ui/base/resource/resource_bundle.h" | |
35 #include "webkit/glue/scoped_clipboard_writer_glue.h" | |
36 #include "webkit/glue/webkit_glue.h" | |
37 #include "webkit/glue/websocketstreamhandle_bridge.h" | |
38 | |
39 #if !defined(DISABLE_NACL) | |
40 #include "native_client/src/shared/imc/nacl_imc.h" | |
41 #include "native_client/src/trusted/plugin/nacl_entry_points.h" | |
42 #endif | |
43 | |
44 #if defined(OS_LINUX) | |
45 #include "content/renderer/renderer_sandbox_support_linux.h" | |
46 #endif | |
47 | |
48 // This definition of WriteBitmapFromPixels uses shared memory to communicate | |
49 // across processes. | |
50 void ScopedClipboardWriterGlue::WriteBitmapFromPixels(const void* pixels, | |
51 const gfx::Size& size) { | |
52 // Do not try to write a bitmap more than once | |
53 if (shared_buf_) | |
54 return; | |
55 | |
56 uint32 buf_size = 4 * size.width() * size.height(); | |
57 | |
58 // Allocate a shared memory buffer to hold the bitmap bits. | |
59 #if defined(OS_POSIX) | |
60 // On POSIX, we need to ask the browser to create the shared memory for us, | |
61 // since this is blocked by the sandbox. | |
62 base::SharedMemoryHandle shared_mem_handle; | |
63 ViewHostMsg_AllocateSharedMemoryBuffer *msg = | |
64 new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size, | |
65 &shared_mem_handle); | |
66 if (RenderThread::current()->Send(msg)) { | |
67 if (base::SharedMemory::IsHandleValid(shared_mem_handle)) { | |
68 shared_buf_ = new base::SharedMemory(shared_mem_handle, false); | |
69 if (!shared_buf_ || !shared_buf_->Map(buf_size)) { | |
70 NOTREACHED() << "Map failed"; | |
71 return; | |
72 } | |
73 } else { | |
74 NOTREACHED() << "Browser failed to allocate shared memory"; | |
75 return; | |
76 } | |
77 } else { | |
78 NOTREACHED() << "Browser allocation request message failed"; | |
79 return; | |
80 } | |
81 #else // !OS_POSIX | |
82 shared_buf_ = new base::SharedMemory; | |
83 if (!shared_buf_->CreateAndMapAnonymous(buf_size)) { | |
84 NOTREACHED(); | |
85 return; | |
86 } | |
87 #endif | |
88 | |
89 // Copy the bits into shared memory | |
90 memcpy(shared_buf_->memory(), pixels, buf_size); | |
91 shared_buf_->Unmap(); | |
92 | |
93 ui::Clipboard::ObjectMapParam size_param; | |
94 const char* size_data = reinterpret_cast<const char*>(&size); | |
95 for (size_t i = 0; i < sizeof(gfx::Size); ++i) | |
96 size_param.push_back(size_data[i]); | |
97 | |
98 ui::Clipboard::ObjectMapParams params; | |
99 | |
100 // The first parameter is replaced on the receiving end with a pointer to | |
101 // a shared memory object containing the bitmap. We reserve space for it here. | |
102 ui::Clipboard::ObjectMapParam place_holder_param; | |
103 params.push_back(place_holder_param); | |
104 params.push_back(size_param); | |
105 objects_[ui::Clipboard::CBF_SMBITMAP] = params; | |
106 } | |
107 | |
108 // Define a destructor that makes IPCs to flush the contents to the | |
109 // system clipboard. | |
110 ScopedClipboardWriterGlue::~ScopedClipboardWriterGlue() { | |
111 if (objects_.empty()) | |
112 return; | |
113 | |
114 if (shared_buf_) { | |
115 RenderThread::current()->Send( | |
116 new ClipboardHostMsg_WriteObjectsSync(objects_, | |
117 shared_buf_->handle())); | |
118 delete shared_buf_; | |
119 return; | |
120 } | |
121 | |
122 RenderThread::current()->Send( | |
123 new ClipboardHostMsg_WriteObjectsAsync(objects_)); | |
124 } | |
125 | |
126 namespace webkit_glue { | |
127 | |
128 void AppendToLog(const char* file, int line, const char* msg) { | |
129 logging::LogMessage(file, line).stream() << msg; | |
130 } | |
131 | |
132 base::StringPiece GetDataResource(int resource_id) { | |
133 return ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | |
134 } | |
135 | |
136 #if defined(OS_WIN) | |
137 HCURSOR LoadCursor(int cursor_id) { | |
138 return ResourceBundle::GetSharedInstance().LoadCursor(cursor_id); | |
139 } | |
140 #endif | |
141 | |
142 // Clipboard glue | |
143 | |
144 ui::Clipboard* ClipboardGetClipboard() { | |
145 return NULL; | |
146 } | |
147 | |
148 bool ClipboardIsFormatAvailable(const ui::Clipboard::FormatType& format, | |
149 ui::Clipboard::Buffer buffer) { | |
150 bool result; | |
151 RenderThread::current()->Send( | |
152 new ClipboardHostMsg_IsFormatAvailable(format, buffer, &result)); | |
153 return result; | |
154 } | |
155 | |
156 void ClipboardReadAvailableTypes(ui::Clipboard::Buffer buffer, | |
157 std::vector<string16>* types, | |
158 bool* contains_filenames) { | |
159 RenderThread::current()->Send(new ClipboardHostMsg_ReadAvailableTypes( | |
160 buffer, types, contains_filenames)); | |
161 } | |
162 | |
163 void ClipboardReadText(ui::Clipboard::Buffer buffer, string16* result) { | |
164 RenderThread::current()->Send(new ClipboardHostMsg_ReadText(buffer, result)); | |
165 } | |
166 | |
167 void ClipboardReadAsciiText(ui::Clipboard::Buffer buffer, std::string* result) { | |
168 RenderThread::current()->Send( | |
169 new ClipboardHostMsg_ReadAsciiText(buffer, result)); | |
170 } | |
171 | |
172 void ClipboardReadHTML(ui::Clipboard::Buffer buffer, string16* markup, | |
173 GURL* url) { | |
174 RenderThread::current()->Send( | |
175 new ClipboardHostMsg_ReadHTML(buffer, markup, url)); | |
176 } | |
177 | |
178 void ClipboardReadImage(ui::Clipboard::Buffer buffer, std::string* data) { | |
179 RenderThread::current()->Send(new ClipboardHostMsg_ReadImage(buffer, data)); | |
180 } | |
181 | |
182 bool ClipboardReadData(ui::Clipboard::Buffer buffer, const string16& type, | |
183 string16* data, string16* metadata) { | |
184 bool result = false; | |
185 RenderThread::current()->Send(new ClipboardHostMsg_ReadData( | |
186 buffer, type, &result, data, metadata)); | |
187 return result; | |
188 } | |
189 | |
190 bool ClipboardReadFilenames(ui::Clipboard::Buffer buffer, | |
191 std::vector<string16>* filenames) { | |
192 bool result; | |
193 RenderThread::current()->Send(new ClipboardHostMsg_ReadFilenames( | |
194 buffer, &result, filenames)); | |
195 return result; | |
196 } | |
197 | |
198 void GetPlugins(bool refresh, | |
199 std::vector<webkit::npapi::WebPluginInfo>* plugins) { | |
200 if (!RenderThread::current()->plugin_refresh_allowed()) | |
201 refresh = false; | |
202 RenderThread::current()->Send(new ViewHostMsg_GetPlugins(refresh, plugins)); | |
203 } | |
204 | |
205 bool IsProtocolSupportedForMedia(const GURL& url) { | |
206 // If new protocol is to be added here, we need to make sure the response is | |
207 // validated accordingly in the media engine. | |
208 if (url.SchemeIsFile() || url.SchemeIs(chrome::kHttpScheme) || | |
209 url.SchemeIs(chrome::kHttpsScheme) || | |
210 url.SchemeIs(chrome::kDataScheme) || | |
211 url.SchemeIs(chrome::kExtensionScheme) || | |
212 url.SchemeIs(chrome::kFileSystemScheme) || | |
213 url.SchemeIs(chrome::kBlobScheme)) | |
214 return true; | |
215 return false; | |
216 } | |
217 | |
218 // static factory function | |
219 ResourceLoaderBridge* ResourceLoaderBridge::Create( | |
220 const ResourceLoaderBridge::RequestInfo& request_info) { | |
221 return ChildThread::current()->CreateBridge(request_info); | |
222 } | |
223 | |
224 // static factory function | |
225 WebSocketStreamHandleBridge* WebSocketStreamHandleBridge::Create( | |
226 WebKit::WebSocketStreamHandle* handle, | |
227 WebSocketStreamHandleDelegate* delegate) { | |
228 SocketStreamDispatcher* dispatcher = | |
229 ChildThread::current()->socket_stream_dispatcher(); | |
230 return dispatcher->CreateBridge(handle, delegate); | |
231 } | |
232 | |
233 void CloseCurrentConnections() { | |
234 RenderThread::current()->CloseCurrentConnections(); | |
235 } | |
236 | |
237 void SetCacheMode(bool enabled) { | |
238 RenderThread::current()->SetCacheMode(enabled); | |
239 } | |
240 | |
241 void ClearCache(bool preserve_ssl_host_info) { | |
242 RenderThread::current()->ClearCache(preserve_ssl_host_info); | |
243 } | |
244 | |
245 void ClearHostResolverCache() { | |
246 RenderThread::current()->ClearHostResolverCache(); | |
247 } | |
248 | |
249 void ClearPredictorCache() { | |
250 RenderThread::current()->ClearPredictorCache(); | |
251 } | |
252 | |
253 std::string GetProductVersion() { | |
254 chrome::VersionInfo version_info; | |
255 std::string product("Chrome/"); | |
256 product += version_info.is_valid() ? version_info.Version() | |
257 : "0.0.0.0"; | |
258 return product; | |
259 } | |
260 | |
261 bool IsSingleProcess() { | |
262 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess); | |
263 } | |
264 | |
265 void EnableSpdy(bool enable) { | |
266 RenderThread::current()->EnableSpdy(enable); | |
267 } | |
268 | |
269 void UserMetricsRecordAction(const std::string& action) { | |
270 RenderThread::current()->Send( | |
271 new ViewHostMsg_UserMetricsRecordAction(action)); | |
272 } | |
273 | |
274 #if !defined(DISABLE_NACL) | |
275 bool LaunchSelLdr(const char* alleged_url, int socket_count, void* imc_handles, | |
276 void* nacl_process_handle, int* nacl_process_id) { | |
277 std::vector<nacl::FileDescriptor> sockets; | |
278 base::ProcessHandle nacl_process; | |
279 if (!RenderThread::current()->Send( | |
280 new ViewHostMsg_LaunchNaCl( | |
281 ASCIIToWide(alleged_url), | |
282 socket_count, | |
283 &sockets, | |
284 &nacl_process, | |
285 reinterpret_cast<base::ProcessId*>(nacl_process_id)))) { | |
286 return false; | |
287 } | |
288 CHECK(static_cast<int>(sockets.size()) == socket_count); | |
289 for (int i = 0; i < socket_count; i++) { | |
290 static_cast<nacl::Handle*>(imc_handles)[i] = | |
291 nacl::ToNativeHandle(sockets[i]); | |
292 } | |
293 *static_cast<nacl::Handle*>(nacl_process_handle) = nacl_process; | |
294 return true; | |
295 } | |
296 #endif | |
297 | |
298 #if defined(OS_LINUX) | |
299 int MatchFontWithFallback(const std::string& face, bool bold, | |
300 bool italic, int charset) { | |
301 return renderer_sandbox_support::MatchFontWithFallback( | |
302 face, bold, italic, charset); | |
303 } | |
304 | |
305 bool GetFontTable(int fd, uint32_t table, uint8_t* output, | |
306 size_t* output_length) { | |
307 return renderer_sandbox_support::GetFontTable( | |
308 fd, table, output, output_length); | |
309 } | |
310 #endif | |
311 | |
312 } // namespace webkit_glue | |
OLD | NEW |