blob: 5a5bfe797cdd1e5f3be2f266f6ec4dabf8fb147b [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
[email protected]037fce02009-01-22 01:42:155#include "build/build_config.h"
6
7#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:298#include <windows.h>
9#include <objidl.h>
10#include <mlang.h>
[email protected]037fce02009-01-22 01:42:1511#endif
initial.commit09911bf2008-07-26 23:55:2912
13#include "chrome/renderer/render_process.h"
14
15#include "base/basictypes.h"
16#include "base/command_line.h"
[email protected]037fce02009-01-22 01:42:1517#include "base/compiler_specific.h"
[email protected]955ee6d62009-06-17 21:53:0318#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/message_loop.h"
20#include "base/histogram.h"
[email protected]8ee8189e2008-10-08 19:35:2121#include "base/path_service.h"
[email protected]037fce02009-01-22 01:42:1522#include "base/sys_info.h"
[email protected]f09c7182009-03-10 12:54:0423// TODO(jar): DNS calls should be renderer specific, not including browser.
24#include "chrome/browser/net/dns_global.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/common/chrome_switches.h"
[email protected]8ee8189e2008-10-08 19:35:2126#include "chrome/common/chrome_paths.h"
initial.commit09911bf2008-07-26 23:55:2927#include "chrome/common/render_messages.h"
[email protected]d032f492009-09-29 00:33:4628#include "chrome/common/nacl_types.h"
[email protected]e68e62fa2009-02-20 02:00:0429#include "chrome/common/transport_dib.h"
initial.commit09911bf2008-07-26 23:55:2930#include "chrome/renderer/render_view.h"
[email protected]946d1b22009-07-22 23:57:2131#include "ipc/ipc_channel.h"
32#include "ipc/ipc_message_utils.h"
[email protected]10000d62009-04-18 00:36:2233#include "media/base/media.h"
[email protected]d032f492009-09-29 00:33:4634#include "native_client/src/trusted/plugin/nacl_entry_points.h"
initial.commit09911bf2008-07-26 23:55:2935#include "webkit/glue/webkit_glue.h"
36
[email protected]0c3a16b2009-10-08 23:17:1437#if defined(OS_MACOSX)
38#include "base/mac_util.h"
39#endif
40
[email protected]bf557e12009-02-25 20:50:2941//-----------------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:2942
[email protected]8930d472009-02-21 08:05:2843RenderProcess::RenderProcess()
[email protected]42f1d7822009-07-23 18:17:5544 : ALLOW_THIS_IN_INITIALIZER_LIST(shared_mem_cache_cleaner_(
[email protected]514e7112009-02-20 05:23:3645 base::TimeDelta::FromSeconds(5),
46 this, &RenderProcess::ClearTransportDIBCache)),
47 sequence_number_(0) {
[email protected]8930d472009-02-21 08:05:2848 in_process_plugins_ = InProcessPlugins();
[email protected]8930d472009-02-21 08:05:2849 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i)
50 shared_mem_cache_[i] = NULL;
51
[email protected]037fce02009-01-22 01:42:1552#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2953 // HACK: See http://b/issue?id=1024307 for rationale.
54 if (GetModuleHandle(L"LPK.DLL") == NULL) {
55 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
56 // when buffering into a EMF buffer for printing.
57 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
58 GdiInitializeLanguagePack gdi_init_lpk =
59 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
60 GetModuleHandle(L"GDI32.DLL"),
61 "GdiInitializeLanguagePack"));
62 DCHECK(gdi_init_lpk);
63 if (gdi_init_lpk) {
64 gdi_init_lpk(0);
65 }
66 }
[email protected]037fce02009-01-22 01:42:1567#endif
initial.commit09911bf2008-07-26 23:55:2968
[email protected]58bfc6b2009-06-24 09:45:0269 // Out of process dev tools rely upon auto break behavior.
[email protected]e9609bd2009-06-24 12:17:0370 webkit_glue::SetJavaScriptFlags(
71 L"--debugger-auto-break"
72 // Enable lazy in-memory profiling.
73 L" --prof --prof-lazy --logfile=* --compress-log");
[email protected]2ef9c452009-05-13 13:03:1474
[email protected]82a24512009-07-23 15:40:4575 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
76 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
77 webkit_glue::SetJavaScriptFlags(
78 command_line.GetSwitchValue(switches::kJavaScriptFlags));
79 }
80
initial.commit09911bf2008-07-26 23:55:2981 if (command_line.HasSwitch(switches::kEnableWatchdog)) {
82 // TODO(JAR): Need to implement renderer IO msgloop watchdog.
83 }
84
85 if (command_line.HasSwitch(switches::kDumpHistogramsOnExit)) {
86 StatisticsRecorder::set_dump_on_exit(true);
87 }
88
[email protected]d8c7cbcc2009-10-02 19:00:3189#ifndef DISABLE_NACL
[email protected]d032f492009-09-29 00:33:4690 if (command_line.HasSwitch(switches::kInternalNaCl))
91 RegisterInternalNaClPlugin(RenderProcess::LaunchNaClProcess);
[email protected]d8c7cbcc2009-10-02 19:00:3192#endif
[email protected]d032f492009-09-29 00:33:4693
[email protected]a6decd192009-10-16 22:07:3894 if (!command_line.HasSwitch(switches::kDisableByteRangeSupport)) {
[email protected]3979b7d2009-09-30 17:49:4195 webkit_glue::SetMediaCacheEnabled(true);
96 }
97
[email protected]0c3a16b2009-10-08 23:17:1498#if defined(OS_MACOSX)
99 FilePath bundle_path = mac_util::MainAppBundlePath();
100
101 initialized_media_library_ =
102 media::InitializeMediaLibrary(bundle_path.Append("Libraries"));
[email protected]0c3a16b2009-10-08 23:17:14103#else
[email protected]10000d62009-04-18 00:36:22104 FilePath module_path;
[email protected]b08eebe2009-05-18 22:37:21105 initialized_media_library_ =
106 PathService::Get(base::DIR_MODULE, &module_path) &&
107 media::InitializeMediaLibrary(module_path);
[email protected]0c3a16b2009-10-08 23:17:14108#endif
initial.commit09911bf2008-07-26 23:55:29109}
110
[email protected]42f1d7822009-07-23 18:17:55111RenderProcess::~RenderProcess() {
[email protected]86230b92009-11-23 20:38:38112 // TODO(port): Try and limit what we pull in for our non-Win unit test bundle.
[email protected]42f1d7822009-07-23 18:17:55113#ifndef NDEBUG
114 // log important leaked objects
115 webkit_glue::CheckForLeaks();
116#endif
117
118 GetShutDownEvent()->Signal();
119 ClearTransportDIBCache();
120}
121
[email protected]8930d472009-02-21 08:05:28122bool RenderProcess::InProcessPlugins() {
123 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]0e2f8752009-04-27 20:34:31124#if defined(OS_LINUX)
125 // Plugin processes require a UI message loop, and the Linux message loop
126 // implementation only allows one UI loop per process.
127 if (command_line.HasSwitch(switches::kInProcessPlugins))
128 NOTIMPLEMENTED() << ": in process plugins not supported on Linux";
129 return command_line.HasSwitch(switches::kInProcessPlugins);
130#else
[email protected]8930d472009-02-21 08:05:28131 return command_line.HasSwitch(switches::kInProcessPlugins) ||
132 command_line.HasSwitch(switches::kSingleProcess);
[email protected]0e2f8752009-04-27 20:34:31133#endif
initial.commit09911bf2008-07-26 23:55:29134}
135
[email protected]f08e47d2009-10-15 21:46:15136bool RenderProcess::LaunchNaClProcess(const char* url,
137 int imc_fd,
138 nacl::Handle* imc_handle,
139 nacl::Handle* nacl_process_handle,
140 int* nacl_process_id) {
[email protected]d032f492009-09-29 00:33:46141 // TODO(gregoryd): nacl::FileDescriptor will be soon merged with
142 // base::FileDescriptor
[email protected]f08e47d2009-10-15 21:46:15143 nacl::FileDescriptor imc_descriptor;
[email protected]2461cf0a2009-10-22 17:42:32144 base::ProcessHandle nacl_process;
[email protected]d032f492009-09-29 00:33:46145 if (!RenderThread::current()->Send(
[email protected]f08e47d2009-10-15 21:46:15146 new ViewHostMsg_LaunchNaCl(ASCIIToWide(url),
[email protected]2461cf0a2009-10-22 17:42:32147 imc_fd,
148 &imc_descriptor,
149 &nacl_process,
150 reinterpret_cast<base::ProcessId*>(nacl_process_id)))) {
[email protected]d032f492009-09-29 00:33:46151 return false;
152 }
[email protected]103607e2010-02-01 18:57:09153 *imc_handle = nacl::ToNativeHandle(imc_descriptor);
[email protected]2461cf0a2009-10-22 17:42:32154 *nacl_process_handle = nacl_process;
[email protected]d032f492009-09-29 00:33:46155 return true;
156}
157
[email protected]e68e62fa2009-02-20 02:00:04158// -----------------------------------------------------------------------------
159// Platform specific code for dealing with bitmap transport...
160
[email protected]e68e62fa2009-02-20 02:00:04161TransportDIB* RenderProcess::CreateTransportDIB(size_t size) {
162#if defined(OS_WIN) || defined(OS_LINUX)
163 // Windows and Linux create transport DIBs inside the renderer
164 return TransportDIB::Create(size, sequence_number_++);
165#elif defined(OS_MACOSX) // defined(OS_WIN) || defined(OS_LINUX)
166 // Mac creates transport DIBs in the browser, so we need to do a sync IPC to
167 // get one.
[email protected]2749885f2009-03-05 21:40:11168 TransportDIB::Handle handle;
169 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, &handle);
[email protected]42f1d7822009-07-23 18:17:55170 if (!main_thread()->Send(msg))
[email protected]e68e62fa2009-02-20 02:00:04171 return NULL;
[email protected]2749885f2009-03-05 21:40:11172 if (handle.fd < 0)
[email protected]e68e62fa2009-02-20 02:00:04173 return NULL;
[email protected]2749885f2009-03-05 21:40:11174 return TransportDIB::Map(handle);
[email protected]e68e62fa2009-02-20 02:00:04175#endif // defined(OS_MACOSX)
176}
177
178void RenderProcess::FreeTransportDIB(TransportDIB* dib) {
179 if (!dib)
180 return;
181
182#if defined(OS_MACOSX)
183 // On Mac we need to tell the browser that it can drop a reference to the
184 // shared memory.
185 IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id());
[email protected]42f1d7822009-07-23 18:17:55186 main_thread()->Send(msg);
[email protected]e68e62fa2009-02-20 02:00:04187#endif
188
189 delete dib;
190}
191
192// -----------------------------------------------------------------------------
193
194
[email protected]e68e62fa2009-02-20 02:00:04195skia::PlatformCanvas* RenderProcess::GetDrawingCanvas(
196 TransportDIB** memory, const gfx::Rect& rect) {
[email protected]955ee6d62009-06-17 21:53:03197 int width = rect.width();
198 int height = rect.height();
[email protected]e68e62fa2009-02-20 02:00:04199 const size_t stride = skia::PlatformCanvas::StrideForWidth(rect.width());
[email protected]bae93ec2009-08-04 17:59:20200#if defined(OS_LINUX)
[email protected]80a086c52009-08-04 17:52:04201 const size_t max_size = base::SysInfo::MaxSharedMemorySize();
[email protected]bae93ec2009-08-04 17:59:20202#else
203 const size_t max_size = 0;
204#endif
[email protected]955ee6d62009-06-17 21:53:03205
206 // If the requested size is too big, reduce the height. Ideally we might like
207 // to reduce the width as well to make the size reduction more "balanced", but
208 // it rarely comes up in practice.
209 if ((max_size != 0) && (height * stride > max_size))
210 height = max_size / stride;
211
212 const size_t size = height * stride;
initial.commit09911bf2008-07-26 23:55:29213
[email protected]8930d472009-02-21 08:05:28214 if (!GetTransportDIBFromCache(memory, size)) {
215 *memory = CreateTransportDIB(size);
[email protected]e68e62fa2009-02-20 02:00:04216 if (!*memory)
217 return false;
initial.commit09911bf2008-07-26 23:55:29218 }
219
[email protected]955ee6d62009-06-17 21:53:03220 return (*memory)->GetPlatformCanvas(width, height);
initial.commit09911bf2008-07-26 23:55:29221}
222
[email protected]e68e62fa2009-02-20 02:00:04223void RenderProcess::ReleaseTransportDIB(TransportDIB* mem) {
[email protected]8930d472009-02-21 08:05:28224 if (PutSharedMemInCache(mem)) {
225 shared_mem_cache_cleaner_.Reset();
initial.commit09911bf2008-07-26 23:55:29226 return;
227 }
[email protected]e68e62fa2009-02-20 02:00:04228
[email protected]8930d472009-02-21 08:05:28229 FreeTransportDIB(mem);
initial.commit09911bf2008-07-26 23:55:29230}
231
[email protected]e68e62fa2009-02-20 02:00:04232bool RenderProcess::GetTransportDIBFromCache(TransportDIB** mem,
233 size_t size) {
initial.commit09911bf2008-07-26 23:55:29234 // look for a cached object that is suitable for the requested size.
[email protected]037fce02009-01-22 01:42:15235 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
[email protected]e68e62fa2009-02-20 02:00:04236 if (shared_mem_cache_[i] &&
237 size <= shared_mem_cache_[i]->size()) {
238 *mem = shared_mem_cache_[i];
initial.commit09911bf2008-07-26 23:55:29239 shared_mem_cache_[i] = NULL;
[email protected]e68e62fa2009-02-20 02:00:04240 return true;
initial.commit09911bf2008-07-26 23:55:29241 }
242 }
initial.commit09911bf2008-07-26 23:55:29243
initial.commit09911bf2008-07-26 23:55:29244 return false;
245}
246
[email protected]e68e62fa2009-02-20 02:00:04247int RenderProcess::FindFreeCacheSlot(size_t size) {
248 // simple algorithm:
249 // - look for an empty slot to store mem, or
250 // - if full, then replace smallest entry which is smaller than |size|
251 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
252 if (shared_mem_cache_[i] == NULL)
253 return i;
254 }
255
256 size_t smallest_size = size;
257 int smallest_index = -1;
258
259 for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) {
260 const size_t entry_size = shared_mem_cache_[i]->size();
261 if (entry_size < smallest_size) {
262 smallest_size = entry_size;
263 smallest_index = i;
264 }
265 }
266
267 if (smallest_index != -1) {
268 FreeTransportDIB(shared_mem_cache_[smallest_index]);
269 shared_mem_cache_[smallest_index] = NULL;
270 }
271
272 return smallest_index;
273}
274
275bool RenderProcess::PutSharedMemInCache(TransportDIB* mem) {
276 const int slot = FindFreeCacheSlot(mem->size());
277 if (slot == -1)
278 return false;
279
280 shared_mem_cache_[slot] = mem;
281 return true;
282}
283
284void RenderProcess::ClearTransportDIBCache() {
[email protected]037fce02009-01-22 01:42:15285 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
initial.commit09911bf2008-07-26 23:55:29286 if (shared_mem_cache_[i]) {
[email protected]e68e62fa2009-02-20 02:00:04287 FreeTransportDIB(shared_mem_cache_[i]);
initial.commit09911bf2008-07-26 23:55:29288 shared_mem_cache_[i] = NULL;
289 }
290 }
291}