blob: a508e779cf116b4cf4beece2d6eab1604ed66d54 [file] [log] [blame]
[email protected]2ac1e7c2010-03-02 20:50:561// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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
[email protected]00c39612010-03-06 02:53:2813#include "chrome/renderer/render_process_impl.h"
14
initial.commit09911bf2008-07-26 23:55:2915#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]00c39612010-03-06 02:53:2834#include "media/base/media_switches.h"
[email protected]d032f492009-09-29 00:33:4635#include "native_client/src/trusted/plugin/nacl_entry_points.h"
initial.commit09911bf2008-07-26 23:55:2936#include "webkit/glue/webkit_glue.h"
37
[email protected]0c3a16b2009-10-08 23:17:1438#if defined(OS_MACOSX)
39#include "base/mac_util.h"
40#endif
41
[email protected]396c3a462010-03-03 05:03:2242namespace {
initial.commit09911bf2008-07-26 23:55:2943
[email protected]396c3a462010-03-03 05:03:2244bool LaunchNaClProcess(const char* url,
45 int imc_fd,
46 nacl::Handle* imc_handle,
47 nacl::Handle* nacl_process_handle,
48 int* nacl_process_id) {
[email protected]d032f492009-09-29 00:33:4649 // TODO(gregoryd): nacl::FileDescriptor will be soon merged with
50 // base::FileDescriptor
[email protected]f08e47d2009-10-15 21:46:1551 nacl::FileDescriptor imc_descriptor;
[email protected]2461cf0a2009-10-22 17:42:3252 base::ProcessHandle nacl_process;
[email protected]d032f492009-09-29 00:33:4653 if (!RenderThread::current()->Send(
[email protected]f08e47d2009-10-15 21:46:1554 new ViewHostMsg_LaunchNaCl(ASCIIToWide(url),
[email protected]2461cf0a2009-10-22 17:42:3255 imc_fd,
56 &imc_descriptor,
57 &nacl_process,
58 reinterpret_cast<base::ProcessId*>(nacl_process_id)))) {
[email protected]d032f492009-09-29 00:33:4659 return false;
60 }
[email protected]103607e2010-02-01 18:57:0961 *imc_handle = nacl::ToNativeHandle(imc_descriptor);
[email protected]2461cf0a2009-10-22 17:42:3262 *nacl_process_handle = nacl_process;
[email protected]d032f492009-09-29 00:33:4663 return true;
64}
65
[email protected]396c3a462010-03-03 05:03:2266} // namespace
[email protected]e68e62fa2009-02-20 02:00:0467
[email protected]396c3a462010-03-03 05:03:2268//-----------------------------------------------------------------------------
[email protected]e68e62fa2009-02-20 02:00:0469
[email protected]396c3a462010-03-03 05:03:2270RenderProcessImpl::RenderProcessImpl()
[email protected]00c39612010-03-06 02:53:2871 : ALLOW_THIS_IN_INITIALIZER_LIST(shared_mem_cache_cleaner_(
[email protected]396c3a462010-03-03 05:03:2272 base::TimeDelta::FromSeconds(5),
73 this, &RenderProcessImpl::ClearTransportDIBCache)),
74 transport_dib_next_sequence_number_(0) {
75 in_process_plugins_ = InProcessPlugins();
76 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i)
77 shared_mem_cache_[i] = NULL;
[email protected]e68e62fa2009-02-20 02:00:0478
[email protected]396c3a462010-03-03 05:03:2279#if defined(OS_WIN)
80 // HACK: See http://b/issue?id=1024307 for rationale.
81 if (GetModuleHandle(L"LPK.DLL") == NULL) {
82 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
83 // when buffering into a EMF buffer for printing.
84 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
85 GdiInitializeLanguagePack gdi_init_lpk =
86 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
87 GetModuleHandle(L"GDI32.DLL"),
88 "GdiInitializeLanguagePack"));
89 DCHECK(gdi_init_lpk);
[email protected]00c39612010-03-06 02:53:2890 if (gdi_init_lpk) {
[email protected]396c3a462010-03-03 05:03:2291 gdi_init_lpk(0);
[email protected]00c39612010-03-06 02:53:2892 }
[email protected]396c3a462010-03-03 05:03:2293 }
[email protected]e68e62fa2009-02-20 02:00:0494#endif
95
[email protected]396c3a462010-03-03 05:03:2296 // Out of process dev tools rely upon auto break behavior.
97 webkit_glue::SetJavaScriptFlags(
98 L"--debugger-auto-break"
99 // Enable lazy in-memory profiling.
100 L" --prof --prof-lazy --logfile=* --compress-log");
101
102 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
103 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
104 webkit_glue::SetJavaScriptFlags(
[email protected]00c39612010-03-06 02:53:28105 command_line.GetSwitchValue(switches::kJavaScriptFlags));
[email protected]396c3a462010-03-03 05:03:22106 }
107
108 if (command_line.HasSwitch(switches::kEnableWatchdog)) {
109 // TODO(JAR): Need to implement renderer IO msgloop watchdog.
110 }
111
112 if (command_line.HasSwitch(switches::kDumpHistogramsOnExit)) {
113 StatisticsRecorder::set_dump_on_exit(true);
114 }
115
116#ifndef DISABLE_NACL
117 if (command_line.HasSwitch(switches::kInternalNaCl))
118 RegisterInternalNaClPlugin(LaunchNaClProcess);
119#endif
120
[email protected]00c39612010-03-06 02:53:28121 if (!command_line.HasSwitch(switches::kDisableByteRangeSupport)) {
[email protected]396c3a462010-03-03 05:03:22122 webkit_glue::SetMediaCacheEnabled(true);
[email protected]00c39612010-03-06 02:53:28123 }
[email protected]396c3a462010-03-03 05:03:22124
125#if defined(OS_MACOSX)
126 FilePath bundle_path = mac_util::MainAppBundlePath();
127
128 initialized_media_library_ =
129 media::InitializeMediaLibrary(bundle_path.Append("Libraries"));
130#else
131 FilePath module_path;
132 initialized_media_library_ =
133 PathService::Get(base::DIR_MODULE, &module_path) &&
134 media::InitializeMediaLibrary(module_path);
[email protected]00c39612010-03-06 02:53:28135
136 // TODO(hclam): Add more checks here. Currently this is not used.
137 if (CommandLine::ForCurrentProcess()->HasSwitch(
138 switches::kEnableOpenMax)) {
139 media::InitializeOpenMaxLibrary(module_path);
140 }
[email protected]396c3a462010-03-03 05:03:22141#endif
[email protected]e68e62fa2009-02-20 02:00:04142}
143
[email protected]396c3a462010-03-03 05:03:22144RenderProcessImpl::~RenderProcessImpl() {
145 // TODO(port): Try and limit what we pull in for our non-Win unit test bundle.
146#ifndef NDEBUG
147 // log important leaked objects
148 webkit_glue::CheckForLeaks();
149#endif
[email protected]e68e62fa2009-02-20 02:00:04150
[email protected]396c3a462010-03-03 05:03:22151 GetShutDownEvent()->Signal();
152 ClearTransportDIBCache();
153}
[email protected]e68e62fa2009-02-20 02:00:04154
[email protected]396c3a462010-03-03 05:03:22155bool RenderProcessImpl::InProcessPlugins() {
156 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
157#if defined(OS_LINUX)
158 // Plugin processes require a UI message loop, and the Linux message loop
159 // implementation only allows one UI loop per process.
160 if (command_line.HasSwitch(switches::kInProcessPlugins))
161 NOTIMPLEMENTED() << ": in process plugins not supported on Linux";
162 return command_line.HasSwitch(switches::kInProcessPlugins);
163#else
164 return command_line.HasSwitch(switches::kInProcessPlugins) ||
165 command_line.HasSwitch(switches::kSingleProcess);
166#endif
167}
168
[email protected]00c39612010-03-06 02:53:28169// -----------------------------------------------------------------------------
170// Platform specific code for dealing with bitmap transport...
[email protected]e68e62fa2009-02-20 02:00:04171
[email protected]396c3a462010-03-03 05:03:22172TransportDIB* RenderProcessImpl::CreateTransportDIB(size_t size) {
173#if defined(OS_WIN) || defined(OS_LINUX)
174 // Windows and Linux create transport DIBs inside the renderer
175 return TransportDIB::Create(size, transport_dib_next_sequence_number_++);
176#elif defined(OS_MACOSX) // defined(OS_WIN) || defined(OS_LINUX)
177 // Mac creates transport DIBs in the browser, so we need to do a sync IPC to
178 // get one.
179 TransportDIB::Handle handle;
180 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, &handle);
181 if (!main_thread()->Send(msg))
182 return NULL;
183 if (handle.fd < 0)
184 return NULL;
185 return TransportDIB::Map(handle);
186#endif // defined(OS_MACOSX)
[email protected]e68e62fa2009-02-20 02:00:04187}
188
[email protected]396c3a462010-03-03 05:03:22189void RenderProcessImpl::FreeTransportDIB(TransportDIB* dib) {
190 if (!dib)
191 return;
192
193#if defined(OS_MACOSX)
194 // On Mac we need to tell the browser that it can drop a reference to the
195 // shared memory.
196 IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id());
197 main_thread()->Send(msg);
198#endif
199
200 delete dib;
initial.commit09911bf2008-07-26 23:55:29201}
[email protected]00c39612010-03-06 02:53:28202
203// -----------------------------------------------------------------------------
204
205
206skia::PlatformCanvas* RenderProcessImpl::GetDrawingCanvas(
207 TransportDIB** memory, const gfx::Rect& rect) {
208 int width = rect.width();
209 int height = rect.height();
210 const size_t stride = skia::PlatformCanvas::StrideForWidth(rect.width());
211#if defined(OS_LINUX)
212 const size_t max_size = base::SysInfo::MaxSharedMemorySize();
213#else
214 const size_t max_size = 0;
215#endif
216
217 // If the requested size is too big, reduce the height. Ideally we might like
218 // to reduce the width as well to make the size reduction more "balanced", but
219 // it rarely comes up in practice.
220 if ((max_size != 0) && (height * stride > max_size))
221 height = max_size / stride;
222
223 const size_t size = height * stride;
224
225 if (!GetTransportDIBFromCache(memory, size)) {
226 *memory = CreateTransportDIB(size);
227 if (!*memory)
228 return false;
229 }
230
231 return (*memory)->GetPlatformCanvas(width, height);
232}
233
234void RenderProcessImpl::ReleaseTransportDIB(TransportDIB* mem) {
235 if (PutSharedMemInCache(mem)) {
236 shared_mem_cache_cleaner_.Reset();
237 return;
238 }
239
240 FreeTransportDIB(mem);
241}
242
243bool RenderProcessImpl::UseInProcessPlugins() const {
244 return in_process_plugins_;
245}
246
247bool RenderProcessImpl::HasInitializedMediaLibrary() const {
248 return initialized_media_library_;
249}
250
251bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB** mem,
252 size_t size) {
253 // look for a cached object that is suitable for the requested size.
254 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
255 if (shared_mem_cache_[i] &&
256 size <= shared_mem_cache_[i]->size()) {
257 *mem = shared_mem_cache_[i];
258 shared_mem_cache_[i] = NULL;
259 return true;
260 }
261 }
262
263 return false;
264}
265
266int RenderProcessImpl::FindFreeCacheSlot(size_t size) {
267 // simple algorithm:
268 // - look for an empty slot to store mem, or
269 // - if full, then replace smallest entry which is smaller than |size|
270 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
271 if (shared_mem_cache_[i] == NULL)
272 return i;
273 }
274
275 size_t smallest_size = size;
276 int smallest_index = -1;
277
278 for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) {
279 const size_t entry_size = shared_mem_cache_[i]->size();
280 if (entry_size < smallest_size) {
281 smallest_size = entry_size;
282 smallest_index = i;
283 }
284 }
285
286 if (smallest_index != -1) {
287 FreeTransportDIB(shared_mem_cache_[smallest_index]);
288 shared_mem_cache_[smallest_index] = NULL;
289 }
290
291 return smallest_index;
292}
293
294bool RenderProcessImpl::PutSharedMemInCache(TransportDIB* mem) {
295 const int slot = FindFreeCacheSlot(mem->size());
296 if (slot == -1)
297 return false;
298
299 shared_mem_cache_[slot] = mem;
300 return true;
301}
302
303void RenderProcessImpl::ClearTransportDIBCache() {
304 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
305 if (shared_mem_cache_[i]) {
306 FreeTransportDIB(shared_mem_cache_[i]);
307 shared_mem_cache_[i] = NULL;
308 }
309 }
310}