1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#include <objidl.h>
#include <mlang.h>
#endif
#include "chrome/renderer/render_process_impl.h"
#include "app/surface/transport_dib.h"
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/path_service.h"
#include "base/sys_info.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/nacl_types.h"
#include "chrome/renderer/render_thread.h"
#include "chrome/renderer/render_view.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_message_utils.h"
#include "media/base/media.h"
#include "media/base/media_switches.h"
#include "native_client/src/shared/imc/nacl_imc.h"
#include "native_client/src/trusted/plugin/nacl_entry_points.h"
#include "skia/ext/platform_canvas.h"
#include "webkit/glue/plugins/plugin_instance.h"
#include "webkit/glue/plugins/plugin_lib.h"
#include "webkit/glue/webkit_glue.h"
#if defined(OS_MACOSX)
#include "base/mac_util.h"
#elif defined(OS_WIN)
#include "app/win/iat_patch_function.h"
#endif
#if defined(OS_LINUX)
#include "chrome/renderer/renderer_sandbox_support_linux.h"
#endif
namespace {
// TODO(abarth): Remove this function in favor of webkit_glue::LaunchSelLdr.
#if !defined(DISABLE_NACL)
bool LaunchNaClProcessMultiFD(const char* alleged_url,
int socket_count,
nacl::Handle* imc_handles,
nacl::Handle* nacl_process_handle,
int* nacl_process_id) {
// TODO(gregoryd): nacl::FileDescriptor will be soon merged with
// base::FileDescriptor
std::vector<nacl::FileDescriptor> sockets;
base::ProcessHandle nacl_process;
if (!RenderThread::current()->Send(
new ViewHostMsg_LaunchNaCl(
ASCIIToWide(alleged_url),
socket_count,
&sockets,
&nacl_process,
reinterpret_cast<base::ProcessId*>(nacl_process_id)))) {
return false;
}
CHECK(static_cast<int>(sockets.size()) == socket_count);
for (int i = 0; i < socket_count; i++) {
imc_handles[i] = nacl::ToNativeHandle(sockets[i]);
}
*nacl_process_handle = nacl_process;
return true;
}
#endif
} // namespace
//-----------------------------------------------------------------------------
#if defined(OS_WIN)
static app::win::IATPatchFunction g_iat_patch_createdca;
HDC WINAPI CreateDCAPatch(LPCSTR driver_name,
LPCSTR device_name,
LPCSTR output,
const void* init_data) {
DCHECK(std::string("DISPLAY") == std::string(driver_name));
DCHECK(!device_name);
DCHECK(!output);
DCHECK(!init_data);
// CreateDC fails behind the sandbox, but not CreateCompatibleDC.
return CreateCompatibleDC(NULL);
}
static app::win::IATPatchFunction g_iat_patch_get_font_data;
DWORD WINAPI GetFontDataPatch(HDC hdc,
DWORD table,
DWORD offset,
LPVOID buffer,
DWORD length) {
int rv = GetFontData(hdc, table, offset, buffer, length);
if (rv == GDI_ERROR && hdc) {
HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT));
LOGFONT logfont;
if (GetObject(font, sizeof(LOGFONT), &logfont)) {
std::vector<char> font_data;
if (RenderThread::current()->Send(new ViewHostMsg_PreCacheFont(logfont)))
rv = GetFontData(hdc, table, offset, buffer, length);
}
}
return rv;
}
#endif
RenderProcessImpl::RenderProcessImpl()
: ALLOW_THIS_IN_INITIALIZER_LIST(shared_mem_cache_cleaner_(
base::TimeDelta::FromSeconds(5),
this, &RenderProcessImpl::ClearTransportDIBCache)),
transport_dib_next_sequence_number_(0) {
in_process_plugins_ = InProcessPlugins();
for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i)
shared_mem_cache_[i] = NULL;
#if defined(OS_WIN)
// HACK: See http://b/issue?id=1024307 for rationale.
if (GetModuleHandle(L"LPK.DLL") == NULL) {
// Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
// when buffering into a EMF buffer for printing.
typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
GdiInitializeLanguagePack gdi_init_lpk =
reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
GetModuleHandle(L"GDI32.DLL"),
"GdiInitializeLanguagePack"));
DCHECK(gdi_init_lpk);
if (gdi_init_lpk) {
gdi_init_lpk(0);
}
}
#endif
// Out of process dev tools rely upon auto break behavior.
webkit_glue::SetJavaScriptFlags(
"--debugger-auto-break"
// Enable lazy in-memory profiling.
" --prof --prof-lazy --logfile=*");
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
webkit_glue::SetJavaScriptFlags(
command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
}
if (command_line.HasSwitch(switches::kEnableWatchdog)) {
// TODO(JAR): Need to implement renderer IO msgloop watchdog.
}
if (command_line.HasSwitch(switches::kDumpHistogramsOnExit)) {
base::StatisticsRecorder::set_dump_on_exit(true);
}
#if !defined(DISABLE_NACL)
if (command_line.HasSwitch(switches::kInternalNaCl)) {
std::map<std::string, uintptr_t> funcs;
funcs["launch_nacl_process_multi_fd"] =
reinterpret_cast<uintptr_t>(LaunchNaClProcessMultiFD);
RegisterInternalNaClPlugin(funcs);
#if defined(OS_LINUX)
nacl::SetCreateMemoryObjectFunc(
renderer_sandbox_support::MakeSharedMemorySegmentViaIPC);
#endif
}
#endif
#if defined(OS_MACOSX)
FilePath bundle_path = mac_util::MainAppBundlePath();
initialized_media_library_ =
media::InitializeMediaLibrary(bundle_path.Append("Libraries"));
#else
FilePath module_path;
initialized_media_library_ =
PathService::Get(base::DIR_MODULE, &module_path) &&
media::InitializeMediaLibrary(module_path);
// TODO(hclam): Add more checks here. Currently this is not used.
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableOpenMax)) {
media::InitializeOpenMaxLibrary(module_path);
}
#endif
#if defined(OS_WIN)
// Need to patch a few functions for font loading to work correctly.
FilePath pdf;
if (PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf) &&
file_util::PathExists(pdf)) {
g_iat_patch_createdca.Patch(
pdf.value().c_str(), "gdi32.dll", "CreateDCA", CreateDCAPatch);
g_iat_patch_get_font_data.Patch(
pdf.value().c_str(), "gdi32.dll", "GetFontData", GetFontDataPatch);
}
#endif
}
RenderProcessImpl::~RenderProcessImpl() {
// TODO(port): Try and limit what we pull in for our non-Win unit test bundle.
#ifndef NDEBUG
// log important leaked objects
webkit_glue::CheckForLeaks();
#endif
GetShutDownEvent()->Signal();
ClearTransportDIBCache();
}
bool RenderProcessImpl::InProcessPlugins() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
#if defined(OS_LINUX)
// Plugin processes require a UI message loop, and the Linux message loop
// implementation only allows one UI loop per process.
if (command_line.HasSwitch(switches::kInProcessPlugins))
NOTIMPLEMENTED() << ": in process plugins not supported on Linux";
return command_line.HasSwitch(switches::kInProcessPlugins);
#else
return command_line.HasSwitch(switches::kInProcessPlugins) ||
command_line.HasSwitch(switches::kSingleProcess);
#endif
}
// -----------------------------------------------------------------------------
// Platform specific code for dealing with bitmap transport...
TransportDIB* RenderProcessImpl::CreateTransportDIB(size_t size) {
#if defined(OS_WIN) || defined(OS_LINUX)
// Windows and Linux create transport DIBs inside the renderer
return TransportDIB::Create(size, transport_dib_next_sequence_number_++);
#elif defined(OS_MACOSX) // defined(OS_WIN) || defined(OS_LINUX)
// Mac creates transport DIBs in the browser, so we need to do a sync IPC to
// get one. The TransportDIB is cached in the browser.
TransportDIB::Handle handle;
IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, true, &handle);
if (!main_thread()->Send(msg))
return NULL;
if (handle.fd < 0)
return NULL;
return TransportDIB::Map(handle);
#endif // defined(OS_MACOSX)
}
void RenderProcessImpl::FreeTransportDIB(TransportDIB* dib) {
if (!dib)
return;
#if defined(OS_MACOSX)
// On Mac we need to tell the browser that it can drop a reference to the
// shared memory.
IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id());
main_thread()->Send(msg);
#endif
delete dib;
}
// -----------------------------------------------------------------------------
skia::PlatformCanvas* RenderProcessImpl::GetDrawingCanvas(
TransportDIB** memory, const gfx::Rect& rect) {
int width = rect.width();
int height = rect.height();
const size_t stride = skia::PlatformCanvas::StrideForWidth(rect.width());
#if defined(OS_LINUX)
const size_t max_size = base::SysInfo::MaxSharedMemorySize();
#else
const size_t max_size = 0;
#endif
// If the requested size is too big, reduce the height. Ideally we might like
// to reduce the width as well to make the size reduction more "balanced", but
// it rarely comes up in practice.
if ((max_size != 0) && (height * stride > max_size))
height = max_size / stride;
const size_t size = height * stride;
if (!GetTransportDIBFromCache(memory, size)) {
*memory = CreateTransportDIB(size);
if (!*memory)
return NULL;
}
return (*memory)->GetPlatformCanvas(width, height);
}
void RenderProcessImpl::ReleaseTransportDIB(TransportDIB* mem) {
if (PutSharedMemInCache(mem)) {
shared_mem_cache_cleaner_.Reset();
return;
}
FreeTransportDIB(mem);
}
bool RenderProcessImpl::UseInProcessPlugins() const {
return in_process_plugins_;
}
bool RenderProcessImpl::HasInitializedMediaLibrary() const {
return initialized_media_library_;
}
bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB** mem,
size_t size) {
// look for a cached object that is suitable for the requested size.
for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
if (shared_mem_cache_[i] &&
size <= shared_mem_cache_[i]->size()) {
*mem = shared_mem_cache_[i];
shared_mem_cache_[i] = NULL;
return true;
}
}
return false;
}
int RenderProcessImpl::FindFreeCacheSlot(size_t size) {
// simple algorithm:
// - look for an empty slot to store mem, or
// - if full, then replace smallest entry which is smaller than |size|
for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
if (shared_mem_cache_[i] == NULL)
return i;
}
size_t smallest_size = size;
int smallest_index = -1;
for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) {
const size_t entry_size = shared_mem_cache_[i]->size();
if (entry_size < smallest_size) {
smallest_size = entry_size;
smallest_index = i;
}
}
if (smallest_index != -1) {
FreeTransportDIB(shared_mem_cache_[smallest_index]);
shared_mem_cache_[smallest_index] = NULL;
}
return smallest_index;
}
bool RenderProcessImpl::PutSharedMemInCache(TransportDIB* mem) {
const int slot = FindFreeCacheSlot(mem->size());
if (slot == -1)
return false;
shared_mem_cache_[slot] = mem;
return true;
}
void RenderProcessImpl::ClearTransportDIBCache() {
for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
if (shared_mem_cache_[i]) {
FreeTransportDIB(shared_mem_cache_[i]);
shared_mem_cache_[i] = NULL;
}
}
}
|