blob: 54a9e5f8dc615eedf1443691bf3e7df4f1c7507d [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]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]8704f89b2011-04-15 00:30:0513#include "content/renderer/render_process_impl.h"
[email protected]00c39612010-03-06 02:53:2814
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]b9b751f22011-03-25 14:04:1228#include "ui/gfx/surface/transport_dib.h"
[email protected]191eb3f72010-12-21 06:27:5029#include "webkit/plugins/npapi/plugin_instance.h"
30#include "webkit/plugins/npapi/plugin_lib.h"
initial.commit09911bf2008-07-26 23:55:2931#include "webkit/glue/webkit_glue.h"
32
[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]396c3a462010-03-03 05:03:2237RenderProcessImpl::RenderProcessImpl()
[email protected]00c39612010-03-06 02:53:2838 : ALLOW_THIS_IN_INITIALIZER_LIST(shared_mem_cache_cleaner_(
[email protected]d323a172011-09-02 18:23:0239 FROM_HERE, base::TimeDelta::FromSeconds(5),
[email protected]396c3a462010-03-03 05:03:2240 this, &RenderProcessImpl::ClearTransportDIBCache)),
41 transport_dib_next_sequence_number_(0) {
42 in_process_plugins_ = InProcessPlugins();
43 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i)
44 shared_mem_cache_[i] = NULL;
[email protected]e68e62fa2009-02-20 02:00:0445
[email protected]396c3a462010-03-03 05:03:2246#if defined(OS_WIN)
47 // HACK: See http://b/issue?id=1024307 for rationale.
48 if (GetModuleHandle(L"LPK.DLL") == NULL) {
49 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
50 // when buffering into a EMF buffer for printing.
51 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
52 GdiInitializeLanguagePack gdi_init_lpk =
53 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
54 GetModuleHandle(L"GDI32.DLL"),
55 "GdiInitializeLanguagePack"));
56 DCHECK(gdi_init_lpk);
[email protected]00c39612010-03-06 02:53:2857 if (gdi_init_lpk) {
[email protected]396c3a462010-03-03 05:03:2258 gdi_init_lpk(0);
[email protected]00c39612010-03-06 02:53:2859 }
[email protected]396c3a462010-03-03 05:03:2260 }
[email protected]e68e62fa2009-02-20 02:00:0461#endif
62
[email protected]396c3a462010-03-03 05:03:2263 // Out of process dev tools rely upon auto break behavior.
64 webkit_glue::SetJavaScriptFlags(
[email protected]95edc392010-07-30 22:00:3865 "--debugger-auto-break"
[email protected]6b4adc7d62011-07-21 16:32:1566 // Enable on-demand profiling.
67 " --prof --prof-lazy");
[email protected]396c3a462010-03-03 05:03:2268
69 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
70 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
71 webkit_glue::SetJavaScriptFlags(
[email protected]95edc392010-07-30 22:00:3872 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
[email protected]396c3a462010-03-03 05:03:2273 }
[email protected]407dfa632011-12-23 11:59:3574
75 if (command_line.HasSwitch(switches::kDartFlags)) {
76 webkit_glue::SetDartFlags(
77 command_line.GetSwitchValueASCII(switches::kDartFlags));
78 }
[email protected]e68e62fa2009-02-20 02:00:0479}
80
[email protected]396c3a462010-03-03 05:03:2281RenderProcessImpl::~RenderProcessImpl() {
82 // TODO(port): Try and limit what we pull in for our non-Win unit test bundle.
83#ifndef NDEBUG
84 // log important leaked objects
85 webkit_glue::CheckForLeaks();
86#endif
[email protected]e68e62fa2009-02-20 02:00:0487
[email protected]396c3a462010-03-03 05:03:2288 GetShutDownEvent()->Signal();
89 ClearTransportDIBCache();
90}
[email protected]e68e62fa2009-02-20 02:00:0491
[email protected]396c3a462010-03-03 05:03:2292bool RenderProcessImpl::InProcessPlugins() {
93 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]e60c0232011-11-11 19:56:3594#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]396c3a462010-03-03 05:03:2295 // Plugin processes require a UI message loop, and the Linux message loop
96 // implementation only allows one UI loop per process.
97 if (command_line.HasSwitch(switches::kInProcessPlugins))
98 NOTIMPLEMENTED() << ": in process plugins not supported on Linux";
99 return command_line.HasSwitch(switches::kInProcessPlugins);
100#else
101 return command_line.HasSwitch(switches::kInProcessPlugins) ||
102 command_line.HasSwitch(switches::kSingleProcess);
103#endif
104}
105
[email protected]00c39612010-03-06 02:53:28106// -----------------------------------------------------------------------------
107// Platform specific code for dealing with bitmap transport...
[email protected]e68e62fa2009-02-20 02:00:04108
[email protected]396c3a462010-03-03 05:03:22109TransportDIB* RenderProcessImpl::CreateTransportDIB(size_t size) {
[email protected]fd911dd2012-01-27 01:57:10110#if defined(OS_WIN) || defined(OS_LINUX) || \
111 defined(OS_OPENBSD) || defined(OS_ANDROID)
[email protected]396c3a462010-03-03 05:03:22112 // Windows and Linux create transport DIBs inside the renderer
113 return TransportDIB::Create(size, transport_dib_next_sequence_number_++);
[email protected]e60c0232011-11-11 19:56:35114#elif defined(OS_MACOSX)
[email protected]396c3a462010-03-03 05:03:22115 // Mac creates transport DIBs in the browser, so we need to do a sync IPC to
[email protected]ada848b12010-04-08 22:35:38116 // get one. The TransportDIB is cached in the browser.
[email protected]396c3a462010-03-03 05:03:22117 TransportDIB::Handle handle;
[email protected]ada848b12010-04-08 22:35:38118 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, true, &handle);
[email protected]396c3a462010-03-03 05:03:22119 if (!main_thread()->Send(msg))
120 return NULL;
121 if (handle.fd < 0)
122 return NULL;
123 return TransportDIB::Map(handle);
124#endif // defined(OS_MACOSX)
[email protected]e68e62fa2009-02-20 02:00:04125}
126
[email protected]396c3a462010-03-03 05:03:22127void RenderProcessImpl::FreeTransportDIB(TransportDIB* dib) {
128 if (!dib)
129 return;
130
131#if defined(OS_MACOSX)
132 // On Mac we need to tell the browser that it can drop a reference to the
133 // shared memory.
134 IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id());
135 main_thread()->Send(msg);
136#endif
137
138 delete dib;
initial.commit09911bf2008-07-26 23:55:29139}
[email protected]00c39612010-03-06 02:53:28140
141// -----------------------------------------------------------------------------
142
143
144skia::PlatformCanvas* RenderProcessImpl::GetDrawingCanvas(
145 TransportDIB** memory, const gfx::Rect& rect) {
146 int width = rect.width();
147 int height = rect.height();
148 const size_t stride = skia::PlatformCanvas::StrideForWidth(rect.width());
[email protected]e60c0232011-11-11 19:56:35149#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]00c39612010-03-06 02:53:28150 const size_t max_size = base::SysInfo::MaxSharedMemorySize();
151#else
152 const size_t max_size = 0;
153#endif
154
155 // If the requested size is too big, reduce the height. Ideally we might like
156 // to reduce the width as well to make the size reduction more "balanced", but
157 // it rarely comes up in practice.
158 if ((max_size != 0) && (height * stride > max_size))
159 height = max_size / stride;
160
161 const size_t size = height * stride;
162
163 if (!GetTransportDIBFromCache(memory, size)) {
164 *memory = CreateTransportDIB(size);
165 if (!*memory)
[email protected]a866cc22010-06-15 17:34:59166 return NULL;
[email protected]00c39612010-03-06 02:53:28167 }
168
169 return (*memory)->GetPlatformCanvas(width, height);
170}
171
172void RenderProcessImpl::ReleaseTransportDIB(TransportDIB* mem) {
173 if (PutSharedMemInCache(mem)) {
174 shared_mem_cache_cleaner_.Reset();
175 return;
176 }
177
178 FreeTransportDIB(mem);
179}
180
181bool RenderProcessImpl::UseInProcessPlugins() const {
182 return in_process_plugins_;
183}
184
[email protected]00c39612010-03-06 02:53:28185bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB** mem,
186 size_t size) {
187 // look for a cached object that is suitable for the requested size.
188 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
189 if (shared_mem_cache_[i] &&
190 size <= shared_mem_cache_[i]->size()) {
191 *mem = shared_mem_cache_[i];
192 shared_mem_cache_[i] = NULL;
193 return true;
194 }
195 }
196
197 return false;
198}
199
200int RenderProcessImpl::FindFreeCacheSlot(size_t size) {
201 // simple algorithm:
202 // - look for an empty slot to store mem, or
203 // - if full, then replace smallest entry which is smaller than |size|
204 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
205 if (shared_mem_cache_[i] == NULL)
206 return i;
207 }
208
209 size_t smallest_size = size;
210 int smallest_index = -1;
211
212 for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) {
213 const size_t entry_size = shared_mem_cache_[i]->size();
214 if (entry_size < smallest_size) {
215 smallest_size = entry_size;
216 smallest_index = i;
217 }
218 }
219
220 if (smallest_index != -1) {
221 FreeTransportDIB(shared_mem_cache_[smallest_index]);
222 shared_mem_cache_[smallest_index] = NULL;
223 }
224
225 return smallest_index;
226}
227
228bool RenderProcessImpl::PutSharedMemInCache(TransportDIB* mem) {
229 const int slot = FindFreeCacheSlot(mem->size());
230 if (slot == -1)
231 return false;
232
233 shared_mem_cache_[slot] = mem;
234 return true;
235}
236
237void RenderProcessImpl::ClearTransportDIBCache() {
238 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
239 if (shared_mem_cache_[i]) {
240 FreeTransportDIB(shared_mem_cache_[i]);
241 shared_mem_cache_[i] = NULL;
242 }
243 }
244}