blob: 8256947034bd071493091c61575626cdbb754394 [file] [log] [blame]
[email protected]fd911dd2012-01-27 01:57:101// Copyright (c) 2012 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]d353541f2012-05-03 22:45:415#include "content/renderer/render_process_impl.h"
6
[email protected]037fce02009-01-22 01:42:157#include "build/build_config.h"
8
9#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2910#include <windows.h>
11#include <objidl.h>
12#include <mlang.h>
[email protected]037fce02009-01-22 01:42:1513#endif
initial.commit09911bf2008-07-26 23:55:2914
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"
initial.commit09911bf2008-07-26 23:55:2918#include "base/message_loop.h"
[email protected]037fce02009-01-22 01:42:1519#include "base/sys_info.h"
[email protected]be1ce6a72010-08-03 14:35:2220#include "base/utf_string_conversions.h"
[email protected]f1a29a02011-10-06 23:08:4421#include "content/common/child_thread.h"
[email protected]0aed2f52011-03-23 18:06:3622#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2923#include "content/public/common/content_switches.h"
[email protected]d344114c2011-10-01 01:24:3424#include "content/public/renderer/content_renderer_client.h"
[email protected]946d1b22009-07-22 23:57:2125#include "ipc/ipc_channel.h"
26#include "ipc/ipc_message_utils.h"
[email protected]46f36a492010-07-28 19:36:4127#include "skia/ext/platform_canvas.h"
[email protected]d353541f2012-05-03 22:45:4128#include "ui/surface/transport_dib.h"
29#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5030#include "webkit/plugins/npapi/plugin_instance.h"
31#include "webkit/plugins/npapi/plugin_lib.h"
initial.commit09911bf2008-07-26 23:55:2932
[email protected]0c3a16b2009-10-08 23:17:1433#if defined(OS_MACOSX)
[email protected]0378bf42011-01-01 18:20:1434#include "base/mac/mac_util.h"
[email protected]0c3a16b2009-10-08 23:17:1435#endif
36
[email protected]eb398192012-10-22 20:16:1937namespace content {
38
[email protected]396c3a462010-03-03 05:03:2239RenderProcessImpl::RenderProcessImpl()
[email protected]69e797f2013-04-30 01:10:2240 : shared_mem_cache_cleaner_(
[email protected]d323a172011-09-02 18:23:0241 FROM_HERE, base::TimeDelta::FromSeconds(5),
[email protected]69e797f2013-04-30 01:10:2242 this, &RenderProcessImpl::ClearTransportDIBCache),
[email protected]744c2a22012-03-15 18:42:0443 transport_dib_next_sequence_number_(0),
44 enabled_bindings_(0) {
[email protected]396c3a462010-03-03 05:03:2245 in_process_plugins_ = InProcessPlugins();
46 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i)
47 shared_mem_cache_[i] = NULL;
[email protected]e68e62fa2009-02-20 02:00:0448
[email protected]396c3a462010-03-03 05:03:2249#if defined(OS_WIN)
50 // HACK: See http://b/issue?id=1024307 for rationale.
51 if (GetModuleHandle(L"LPK.DLL") == NULL) {
52 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
53 // when buffering into a EMF buffer for printing.
54 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
55 GdiInitializeLanguagePack gdi_init_lpk =
56 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
57 GetModuleHandle(L"GDI32.DLL"),
58 "GdiInitializeLanguagePack"));
59 DCHECK(gdi_init_lpk);
[email protected]00c39612010-03-06 02:53:2860 if (gdi_init_lpk) {
[email protected]396c3a462010-03-03 05:03:2261 gdi_init_lpk(0);
[email protected]00c39612010-03-06 02:53:2862 }
[email protected]396c3a462010-03-03 05:03:2263 }
[email protected]e68e62fa2009-02-20 02:00:0464#endif
65
[email protected]396c3a462010-03-03 05:03:2266 // Out of process dev tools rely upon auto break behavior.
67 webkit_glue::SetJavaScriptFlags(
[email protected]95edc392010-07-30 22:00:3868 "--debugger-auto-break"
[email protected]6b4adc7d62011-07-21 16:32:1569 // Enable on-demand profiling.
70 " --prof --prof-lazy");
[email protected]396c3a462010-03-03 05:03:2271
72 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
73 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
74 webkit_glue::SetJavaScriptFlags(
[email protected]95edc392010-07-30 22:00:3875 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
[email protected]396c3a462010-03-03 05:03:2276 }
[email protected]e68e62fa2009-02-20 02:00:0477}
78
[email protected]396c3a462010-03-03 05:03:2279RenderProcessImpl::~RenderProcessImpl() {
80 // TODO(port): Try and limit what we pull in for our non-Win unit test bundle.
81#ifndef NDEBUG
82 // log important leaked objects
83 webkit_glue::CheckForLeaks();
84#endif
[email protected]e68e62fa2009-02-20 02:00:0485
[email protected]396c3a462010-03-03 05:03:2286 GetShutDownEvent()->Signal();
87 ClearTransportDIBCache();
88}
[email protected]e68e62fa2009-02-20 02:00:0489
[email protected]396c3a462010-03-03 05:03:2290bool RenderProcessImpl::InProcessPlugins() {
91 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]e60c0232011-11-11 19:56:3592#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]396c3a462010-03-03 05:03:2293 // Plugin processes require a UI message loop, and the Linux message loop
94 // implementation only allows one UI loop per process.
95 if (command_line.HasSwitch(switches::kInProcessPlugins))
96 NOTIMPLEMENTED() << ": in process plugins not supported on Linux";
97 return command_line.HasSwitch(switches::kInProcessPlugins);
98#else
99 return command_line.HasSwitch(switches::kInProcessPlugins) ||
100 command_line.HasSwitch(switches::kSingleProcess);
101#endif
102}
103
[email protected]744c2a22012-03-15 18:42:04104void RenderProcessImpl::AddBindings(int bindings) {
105 enabled_bindings_ |= bindings;
106}
107
108int RenderProcessImpl::GetEnabledBindings() const {
109 return enabled_bindings_;
110}
111
[email protected]00c39612010-03-06 02:53:28112// -----------------------------------------------------------------------------
113// Platform specific code for dealing with bitmap transport...
[email protected]e68e62fa2009-02-20 02:00:04114
[email protected]396c3a462010-03-03 05:03:22115TransportDIB* RenderProcessImpl::CreateTransportDIB(size_t size) {
[email protected]fd911dd2012-01-27 01:57:10116#if defined(OS_WIN) || defined(OS_LINUX) || \
117 defined(OS_OPENBSD) || defined(OS_ANDROID)
[email protected]396c3a462010-03-03 05:03:22118 // Windows and Linux create transport DIBs inside the renderer
119 return TransportDIB::Create(size, transport_dib_next_sequence_number_++);
[email protected]e60c0232011-11-11 19:56:35120#elif defined(OS_MACOSX)
[email protected]396c3a462010-03-03 05:03:22121 // Mac creates transport DIBs in the browser, so we need to do a sync IPC to
[email protected]ada848b12010-04-08 22:35:38122 // get one. The TransportDIB is cached in the browser.
[email protected]396c3a462010-03-03 05:03:22123 TransportDIB::Handle handle;
[email protected]ada848b12010-04-08 22:35:38124 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, true, &handle);
[email protected]396c3a462010-03-03 05:03:22125 if (!main_thread()->Send(msg))
126 return NULL;
127 if (handle.fd < 0)
128 return NULL;
129 return TransportDIB::Map(handle);
130#endif // defined(OS_MACOSX)
[email protected]e68e62fa2009-02-20 02:00:04131}
132
[email protected]396c3a462010-03-03 05:03:22133void RenderProcessImpl::FreeTransportDIB(TransportDIB* dib) {
134 if (!dib)
135 return;
136
137#if defined(OS_MACOSX)
138 // On Mac we need to tell the browser that it can drop a reference to the
139 // shared memory.
140 IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id());
141 main_thread()->Send(msg);
142#endif
143
144 delete dib;
initial.commit09911bf2008-07-26 23:55:29145}
[email protected]00c39612010-03-06 02:53:28146
147// -----------------------------------------------------------------------------
148
149
150skia::PlatformCanvas* RenderProcessImpl::GetDrawingCanvas(
151 TransportDIB** memory, const gfx::Rect& rect) {
152 int width = rect.width();
153 int height = rect.height();
[email protected]9d611ca2012-11-14 13:46:56154 const size_t stride = skia::PlatformCanvasStrideForWidth(rect.width());
[email protected]e60c0232011-11-11 19:56:35155#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]00c39612010-03-06 02:53:28156 const size_t max_size = base::SysInfo::MaxSharedMemorySize();
157#else
158 const size_t max_size = 0;
159#endif
160
161 // If the requested size is too big, reduce the height. Ideally we might like
162 // to reduce the width as well to make the size reduction more "balanced", but
163 // it rarely comes up in practice.
164 if ((max_size != 0) && (height * stride > max_size))
165 height = max_size / stride;
166
167 const size_t size = height * stride;
168
169 if (!GetTransportDIBFromCache(memory, size)) {
170 *memory = CreateTransportDIB(size);
171 if (!*memory)
[email protected]a866cc22010-06-15 17:34:59172 return NULL;
[email protected]00c39612010-03-06 02:53:28173 }
174
175 return (*memory)->GetPlatformCanvas(width, height);
176}
177
178void RenderProcessImpl::ReleaseTransportDIB(TransportDIB* mem) {
179 if (PutSharedMemInCache(mem)) {
180 shared_mem_cache_cleaner_.Reset();
181 return;
182 }
183
184 FreeTransportDIB(mem);
185}
186
187bool RenderProcessImpl::UseInProcessPlugins() const {
188 return in_process_plugins_;
189}
190
[email protected]00c39612010-03-06 02:53:28191bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB** mem,
192 size_t size) {
193 // look for a cached object that is suitable for the requested size.
194 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
195 if (shared_mem_cache_[i] &&
196 size <= shared_mem_cache_[i]->size()) {
197 *mem = shared_mem_cache_[i];
198 shared_mem_cache_[i] = NULL;
199 return true;
200 }
201 }
202
203 return false;
204}
205
206int RenderProcessImpl::FindFreeCacheSlot(size_t size) {
207 // simple algorithm:
208 // - look for an empty slot to store mem, or
209 // - if full, then replace smallest entry which is smaller than |size|
210 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
211 if (shared_mem_cache_[i] == NULL)
212 return i;
213 }
214
215 size_t smallest_size = size;
216 int smallest_index = -1;
217
218 for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) {
219 const size_t entry_size = shared_mem_cache_[i]->size();
220 if (entry_size < smallest_size) {
221 smallest_size = entry_size;
222 smallest_index = i;
223 }
224 }
225
226 if (smallest_index != -1) {
227 FreeTransportDIB(shared_mem_cache_[smallest_index]);
228 shared_mem_cache_[smallest_index] = NULL;
229 }
230
231 return smallest_index;
232}
233
234bool RenderProcessImpl::PutSharedMemInCache(TransportDIB* mem) {
235 const int slot = FindFreeCacheSlot(mem->size());
236 if (slot == -1)
237 return false;
238
239 shared_mem_cache_[slot] = mem;
240 return true;
241}
242
243void RenderProcessImpl::ClearTransportDIBCache() {
244 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
245 if (shared_mem_cache_[i]) {
246 FreeTransportDIB(shared_mem_cache_[i]);
247 shared_mem_cache_[i] = NULL;
248 }
249 }
250}
[email protected]eb398192012-10-22 20:16:19251
252} // namespace content