blob: 1b8509f4d8de7fa41444eb0225c6d7109faa4275 [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]e68e62fa2009-02-20 02:00:0428#include "chrome/common/transport_dib.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/renderer/render_view.h"
[email protected]946d1b22009-07-22 23:57:2130#include "ipc/ipc_channel.h"
31#include "ipc/ipc_message_utils.h"
[email protected]10000d62009-04-18 00:36:2232#include "media/base/media.h"
initial.commit09911bf2008-07-26 23:55:2933#include "webkit/glue/webkit_glue.h"
34
[email protected]955ee6d62009-06-17 21:53:0335static size_t GetMaxSharedMemorySize() {
36 static int size = 0;
37#if defined(OS_LINUX)
38 if (size == 0) {
39 std::string contents;
40 file_util::ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents);
41 size = strtoul(contents.c_str(), NULL, 0);
42 }
43#endif
44 return size;
45}
46
[email protected]bf557e12009-02-25 20:50:2947//-----------------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:2948
[email protected]8930d472009-02-21 08:05:2849RenderProcess::RenderProcess()
[email protected]421d1cd2009-07-21 21:06:2350 : ChildProcess(new RenderThread()),
51 ALLOW_THIS_IN_INITIALIZER_LIST(shared_mem_cache_cleaner_(
[email protected]514e7112009-02-20 05:23:3652 base::TimeDelta::FromSeconds(5),
53 this, &RenderProcess::ClearTransportDIBCache)),
54 sequence_number_(0) {
[email protected]421d1cd2009-07-21 21:06:2355 Init();
56}
57
58RenderProcess::RenderProcess(const std::string& channel_name)
59 : ChildProcess(new RenderThread(channel_name)),
60 ALLOW_THIS_IN_INITIALIZER_LIST(shared_mem_cache_cleaner_(
61 base::TimeDelta::FromSeconds(5),
62 this, &RenderProcess::ClearTransportDIBCache)),
63 sequence_number_(0) {
64 Init();
65}
66
67RenderProcess::~RenderProcess() {
68 // TODO(port)
69 // Try and limit what we pull in for our non-Win unit test bundle
70#ifndef NDEBUG
71 // log important leaked objects
72 webkit_glue::CheckForLeaks();
73#endif
74
75 GetShutDownEvent()->Signal();
76
77 // We need to stop the RenderThread as the clearer_factory_
78 // member could be in use while the object itself is destroyed,
79 // as a result of the containing RenderProcess object being destroyed.
80 // This race condition causes a crash when the renderer process is shutting
81 // down.
82 child_thread()->Stop();
83 ClearTransportDIBCache();
84}
85
86void RenderProcess::Init() {
[email protected]8930d472009-02-21 08:05:2887 in_process_plugins_ = InProcessPlugins();
[email protected]8930d472009-02-21 08:05:2888 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i)
89 shared_mem_cache_[i] = NULL;
90
[email protected]037fce02009-01-22 01:42:1591#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2992 // HACK: See http://b/issue?id=1024307 for rationale.
93 if (GetModuleHandle(L"LPK.DLL") == NULL) {
94 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
95 // when buffering into a EMF buffer for printing.
96 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
97 GdiInitializeLanguagePack gdi_init_lpk =
98 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
99 GetModuleHandle(L"GDI32.DLL"),
100 "GdiInitializeLanguagePack"));
101 DCHECK(gdi_init_lpk);
102 if (gdi_init_lpk) {
103 gdi_init_lpk(0);
104 }
105 }
[email protected]037fce02009-01-22 01:42:15106#endif
initial.commit09911bf2008-07-26 23:55:29107
[email protected]bb975362009-01-21 01:00:22108 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
initial.commit09911bf2008-07-26 23:55:29109 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
110 webkit_glue::SetJavaScriptFlags(
111 command_line.GetSwitchValue(switches::kJavaScriptFlags));
112 }
initial.commit09911bf2008-07-26 23:55:29113
[email protected]58bfc6b2009-06-24 09:45:02114 // Out of process dev tools rely upon auto break behavior.
[email protected]e9609bd2009-06-24 12:17:03115 webkit_glue::SetJavaScriptFlags(
116 L"--debugger-auto-break"
117 // Enable lazy in-memory profiling.
118 L" --prof --prof-lazy --logfile=* --compress-log");
[email protected]2ef9c452009-05-13 13:03:14119
initial.commit09911bf2008-07-26 23:55:29120 if (command_line.HasSwitch(switches::kEnableWatchdog)) {
121 // TODO(JAR): Need to implement renderer IO msgloop watchdog.
122 }
123
124 if (command_line.HasSwitch(switches::kDumpHistogramsOnExit)) {
125 StatisticsRecorder::set_dump_on_exit(true);
126 }
127
[email protected]10000d62009-04-18 00:36:22128 FilePath module_path;
[email protected]b08eebe2009-05-18 22:37:21129 initialized_media_library_ =
130 PathService::Get(base::DIR_MODULE, &module_path) &&
131 media::InitializeMediaLibrary(module_path);
initial.commit09911bf2008-07-26 23:55:29132}
133
[email protected]8930d472009-02-21 08:05:28134bool RenderProcess::InProcessPlugins() {
135 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]0e2f8752009-04-27 20:34:31136#if defined(OS_LINUX)
137 // Plugin processes require a UI message loop, and the Linux message loop
138 // implementation only allows one UI loop per process.
139 if (command_line.HasSwitch(switches::kInProcessPlugins))
140 NOTIMPLEMENTED() << ": in process plugins not supported on Linux";
141 return command_line.HasSwitch(switches::kInProcessPlugins);
142#else
[email protected]8930d472009-02-21 08:05:28143 return command_line.HasSwitch(switches::kInProcessPlugins) ||
144 command_line.HasSwitch(switches::kSingleProcess);
[email protected]0e2f8752009-04-27 20:34:31145#endif
initial.commit09911bf2008-07-26 23:55:29146}
147
[email protected]e68e62fa2009-02-20 02:00:04148// -----------------------------------------------------------------------------
149// Platform specific code for dealing with bitmap transport...
150
[email protected]e68e62fa2009-02-20 02:00:04151TransportDIB* RenderProcess::CreateTransportDIB(size_t size) {
152#if defined(OS_WIN) || defined(OS_LINUX)
153 // Windows and Linux create transport DIBs inside the renderer
154 return TransportDIB::Create(size, sequence_number_++);
155#elif defined(OS_MACOSX) // defined(OS_WIN) || defined(OS_LINUX)
156 // Mac creates transport DIBs in the browser, so we need to do a sync IPC to
157 // get one.
[email protected]2749885f2009-03-05 21:40:11158 TransportDIB::Handle handle;
159 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, &handle);
[email protected]421d1cd2009-07-21 21:06:23160 if (!child_thread()->Send(msg))
[email protected]e68e62fa2009-02-20 02:00:04161 return NULL;
[email protected]2749885f2009-03-05 21:40:11162 if (handle.fd < 0)
[email protected]e68e62fa2009-02-20 02:00:04163 return NULL;
[email protected]2749885f2009-03-05 21:40:11164 return TransportDIB::Map(handle);
[email protected]e68e62fa2009-02-20 02:00:04165#endif // defined(OS_MACOSX)
166}
167
168void RenderProcess::FreeTransportDIB(TransportDIB* dib) {
169 if (!dib)
170 return;
171
172#if defined(OS_MACOSX)
173 // On Mac we need to tell the browser that it can drop a reference to the
174 // shared memory.
175 IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id());
[email protected]421d1cd2009-07-21 21:06:23176 child_thread()->Send(msg);
[email protected]e68e62fa2009-02-20 02:00:04177#endif
178
179 delete dib;
180}
181
182// -----------------------------------------------------------------------------
183
184
[email protected]e68e62fa2009-02-20 02:00:04185skia::PlatformCanvas* RenderProcess::GetDrawingCanvas(
186 TransportDIB** memory, const gfx::Rect& rect) {
[email protected]955ee6d62009-06-17 21:53:03187 int width = rect.width();
188 int height = rect.height();
[email protected]e68e62fa2009-02-20 02:00:04189 const size_t stride = skia::PlatformCanvas::StrideForWidth(rect.width());
[email protected]955ee6d62009-06-17 21:53:03190 const size_t max_size = GetMaxSharedMemorySize();
191
192 // If the requested size is too big, reduce the height. Ideally we might like
193 // to reduce the width as well to make the size reduction more "balanced", but
194 // it rarely comes up in practice.
195 if ((max_size != 0) && (height * stride > max_size))
196 height = max_size / stride;
197
198 const size_t size = height * stride;
initial.commit09911bf2008-07-26 23:55:29199
[email protected]8930d472009-02-21 08:05:28200 if (!GetTransportDIBFromCache(memory, size)) {
201 *memory = CreateTransportDIB(size);
[email protected]e68e62fa2009-02-20 02:00:04202 if (!*memory)
203 return false;
initial.commit09911bf2008-07-26 23:55:29204 }
205
[email protected]955ee6d62009-06-17 21:53:03206 return (*memory)->GetPlatformCanvas(width, height);
initial.commit09911bf2008-07-26 23:55:29207}
208
[email protected]e68e62fa2009-02-20 02:00:04209void RenderProcess::ReleaseTransportDIB(TransportDIB* mem) {
[email protected]8930d472009-02-21 08:05:28210 if (PutSharedMemInCache(mem)) {
211 shared_mem_cache_cleaner_.Reset();
initial.commit09911bf2008-07-26 23:55:29212 return;
213 }
[email protected]e68e62fa2009-02-20 02:00:04214
[email protected]8930d472009-02-21 08:05:28215 FreeTransportDIB(mem);
initial.commit09911bf2008-07-26 23:55:29216}
217
[email protected]e68e62fa2009-02-20 02:00:04218bool RenderProcess::GetTransportDIBFromCache(TransportDIB** mem,
219 size_t size) {
initial.commit09911bf2008-07-26 23:55:29220 // look for a cached object that is suitable for the requested size.
[email protected]037fce02009-01-22 01:42:15221 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
[email protected]e68e62fa2009-02-20 02:00:04222 if (shared_mem_cache_[i] &&
223 size <= shared_mem_cache_[i]->size()) {
224 *mem = shared_mem_cache_[i];
initial.commit09911bf2008-07-26 23:55:29225 shared_mem_cache_[i] = NULL;
[email protected]e68e62fa2009-02-20 02:00:04226 return true;
initial.commit09911bf2008-07-26 23:55:29227 }
228 }
initial.commit09911bf2008-07-26 23:55:29229
initial.commit09911bf2008-07-26 23:55:29230 return false;
231}
232
[email protected]e68e62fa2009-02-20 02:00:04233int RenderProcess::FindFreeCacheSlot(size_t size) {
234 // simple algorithm:
235 // - look for an empty slot to store mem, or
236 // - if full, then replace smallest entry which is smaller than |size|
237 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
238 if (shared_mem_cache_[i] == NULL)
239 return i;
240 }
241
242 size_t smallest_size = size;
243 int smallest_index = -1;
244
245 for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) {
246 const size_t entry_size = shared_mem_cache_[i]->size();
247 if (entry_size < smallest_size) {
248 smallest_size = entry_size;
249 smallest_index = i;
250 }
251 }
252
253 if (smallest_index != -1) {
254 FreeTransportDIB(shared_mem_cache_[smallest_index]);
255 shared_mem_cache_[smallest_index] = NULL;
256 }
257
258 return smallest_index;
259}
260
261bool RenderProcess::PutSharedMemInCache(TransportDIB* mem) {
262 const int slot = FindFreeCacheSlot(mem->size());
263 if (slot == -1)
264 return false;
265
266 shared_mem_cache_[slot] = mem;
267 return true;
268}
269
270void RenderProcess::ClearTransportDIBCache() {
[email protected]037fce02009-01-22 01:42:15271 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
initial.commit09911bf2008-07-26 23:55:29272 if (shared_mem_cache_[i]) {
[email protected]e68e62fa2009-02-20 02:00:04273 FreeTransportDIB(shared_mem_cache_[i]);
initial.commit09911bf2008-07-26 23:55:29274 shared_mem_cache_[i] = NULL;
275 }
276 }
277}