blob: 1ad3d5ec4b28fccf3fdccae4501f853c7099afe8 [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"
[email protected]aaf68892013-07-18 00:11:3018#include "base/message_loop/message_loop.h"
[email protected]74ebfb12013-06-07 20:48:0019#include "base/strings/utf_string_conversions.h"
[email protected]037fce02009-01-22 01:42:1520#include "base/sys_info.h"
[email protected]10208ea2013-06-06 20:08:0321#include "content/child/child_thread.h"
[email protected]29e2fb42013-07-19 01:13:4722#include "content/child/npapi/plugin_instance.h"
23#include "content/child/npapi/plugin_lib.h"
[email protected]55dd9332013-09-04 17:17:5024#include "content/child/site_isolation_policy.h"
[email protected]0aed2f52011-03-23 18:06:3625#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2926#include "content/public/common/content_switches.h"
[email protected]d344114c2011-10-01 01:24:3427#include "content/public/renderer/content_renderer_client.h"
[email protected]946d1b22009-07-22 23:57:2128#include "ipc/ipc_channel.h"
29#include "ipc/ipc_message_utils.h"
[email protected]46f36a492010-07-28 19:36:4130#include "skia/ext/platform_canvas.h"
[email protected]6bd867b2013-07-24 22:10:2031#include "third_party/WebKit/public/web/WebFrame.h"
[email protected]d353541f2012-05-03 22:45:4132#include "ui/surface/transport_dib.h"
[email protected]067f5192014-01-29 05:22:0933#include "v8/include/v8.h"
initial.commit09911bf2008-07-26 23:55:2934
[email protected]0c3a16b2009-10-08 23:17:1435#if defined(OS_MACOSX)
[email protected]0378bf42011-01-01 18:20:1436#include "base/mac/mac_util.h"
[email protected]0c3a16b2009-10-08 23:17:1437#endif
38
[email protected]987422f2013-10-01 10:33:3139#if defined(OS_ANDROID)
40#include "base/android/sys_utils.h"
41#endif
42
[email protected]eb398192012-10-22 20:16:1943namespace content {
44
[email protected]396c3a462010-03-03 05:03:2245RenderProcessImpl::RenderProcessImpl()
[email protected]69e797f2013-04-30 01:10:2246 : shared_mem_cache_cleaner_(
[email protected]d323a172011-09-02 18:23:0247 FROM_HERE, base::TimeDelta::FromSeconds(5),
[email protected]69e797f2013-04-30 01:10:2248 this, &RenderProcessImpl::ClearTransportDIBCache),
[email protected]744c2a22012-03-15 18:42:0449 transport_dib_next_sequence_number_(0),
50 enabled_bindings_(0) {
[email protected]396c3a462010-03-03 05:03:2251 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i)
52 shared_mem_cache_[i] = NULL;
[email protected]e68e62fa2009-02-20 02:00:0453
[email protected]396c3a462010-03-03 05:03:2254#if defined(OS_WIN)
55 // HACK: See http://b/issue?id=1024307 for rationale.
56 if (GetModuleHandle(L"LPK.DLL") == NULL) {
57 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
58 // when buffering into a EMF buffer for printing.
59 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
60 GdiInitializeLanguagePack gdi_init_lpk =
61 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
62 GetModuleHandle(L"GDI32.DLL"),
63 "GdiInitializeLanguagePack"));
64 DCHECK(gdi_init_lpk);
[email protected]00c39612010-03-06 02:53:2865 if (gdi_init_lpk) {
[email protected]396c3a462010-03-03 05:03:2266 gdi_init_lpk(0);
[email protected]00c39612010-03-06 02:53:2867 }
[email protected]396c3a462010-03-03 05:03:2268 }
[email protected]e68e62fa2009-02-20 02:00:0469#endif
70
[email protected]396c3a462010-03-03 05:03:2271 // Out of process dev tools rely upon auto break behavior.
[email protected]067f5192014-01-29 05:22:0972 std::string auto_break_flag("--debugger-auto-break");
73 v8::V8::SetFlagsFromString(auto_break_flag.c_str(),
74 static_cast<int>(auto_break_flag.size()));
[email protected]396c3a462010-03-03 05:03:2275
[email protected]987422f2013-10-01 10:33:3176#if defined(OS_ANDROID)
[email protected]067f5192014-01-29 05:22:0977 if (base::android::SysUtils::IsLowEndDevice()) {
78 std::string optimize_flag("--optimize-for-size");
79 v8::V8::SetFlagsFromString(optimize_flag.c_str(),
80 static_cast<int>(optimize_flag.size()));
81 }
[email protected]987422f2013-10-01 10:33:3182#endif
83
[email protected]396c3a462010-03-03 05:03:2284 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
85 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
[email protected]067f5192014-01-29 05:22:0986 std::string flags(
[email protected]95edc392010-07-30 22:00:3887 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
[email protected]067f5192014-01-29 05:22:0988 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
[email protected]396c3a462010-03-03 05:03:2289 }
[email protected]55dd9332013-09-04 17:17:5090
91 // Turn on cross-site document blocking for renderer processes.
92 SiteIsolationPolicy::SetPolicyEnabled(
93 GetContentClient()->renderer()->ShouldEnableSiteIsolationPolicy());
[email protected]e68e62fa2009-02-20 02:00:0494}
95
[email protected]396c3a462010-03-03 05:03:2296RenderProcessImpl::~RenderProcessImpl() {
[email protected]396c3a462010-03-03 05:03:2297#ifndef NDEBUG
[email protected]180ef242013-11-07 06:50:4698 int count = blink::WebFrame::instanceCount();
[email protected]6bd867b2013-07-24 22:10:2099 if (count)
100 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
[email protected]396c3a462010-03-03 05:03:22101#endif
[email protected]e68e62fa2009-02-20 02:00:04102
[email protected]396c3a462010-03-03 05:03:22103 GetShutDownEvent()->Signal();
104 ClearTransportDIBCache();
105}
[email protected]e68e62fa2009-02-20 02:00:04106
[email protected]744c2a22012-03-15 18:42:04107void RenderProcessImpl::AddBindings(int bindings) {
108 enabled_bindings_ |= bindings;
109}
110
111int RenderProcessImpl::GetEnabledBindings() const {
112 return enabled_bindings_;
113}
114
[email protected]00c39612010-03-06 02:53:28115// -----------------------------------------------------------------------------
116// Platform specific code for dealing with bitmap transport...
[email protected]e68e62fa2009-02-20 02:00:04117
[email protected]396c3a462010-03-03 05:03:22118TransportDIB* RenderProcessImpl::CreateTransportDIB(size_t size) {
[email protected]1dfb8bef2013-05-17 07:31:44119#if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
120 // POSIX creates transport DIBs in the browser, so we need to do a sync IPC to
[email protected]ada848b12010-04-08 22:35:38121 // get one. The TransportDIB is cached in the browser.
[email protected]396c3a462010-03-03 05:03:22122 TransportDIB::Handle handle;
[email protected]ada848b12010-04-08 22:35:38123 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, true, &handle);
[email protected]396c3a462010-03-03 05:03:22124 if (!main_thread()->Send(msg))
125 return NULL;
126 if (handle.fd < 0)
127 return NULL;
128 return TransportDIB::Map(handle);
[email protected]1dfb8bef2013-05-17 07:31:44129#else
130 // Windows, legacy GTK and Android create transport DIBs inside the
131 // renderer.
132 return TransportDIB::Create(size, transport_dib_next_sequence_number_++);
133#endif
[email protected]e68e62fa2009-02-20 02:00:04134}
135
[email protected]396c3a462010-03-03 05:03:22136void RenderProcessImpl::FreeTransportDIB(TransportDIB* dib) {
137 if (!dib)
138 return;
139
[email protected]1dfb8bef2013-05-17 07:31:44140#if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
141 // On POSIX we need to tell the browser that it can drop a reference to the
[email protected]396c3a462010-03-03 05:03:22142 // shared memory.
143 IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id());
144 main_thread()->Send(msg);
145#endif
146
147 delete dib;
initial.commit09911bf2008-07-26 23:55:29148}
[email protected]00c39612010-03-06 02:53:28149
150// -----------------------------------------------------------------------------
151
152
153skia::PlatformCanvas* RenderProcessImpl::GetDrawingCanvas(
154 TransportDIB** memory, const gfx::Rect& rect) {
155 int width = rect.width();
156 int height = rect.height();
[email protected]9d611ca2012-11-14 13:46:56157 const size_t stride = skia::PlatformCanvasStrideForWidth(rect.width());
[email protected]e60c0232011-11-11 19:56:35158#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]00c39612010-03-06 02:53:28159 const size_t max_size = base::SysInfo::MaxSharedMemorySize();
160#else
161 const size_t max_size = 0;
162#endif
163
164 // If the requested size is too big, reduce the height. Ideally we might like
165 // to reduce the width as well to make the size reduction more "balanced", but
166 // it rarely comes up in practice.
167 if ((max_size != 0) && (height * stride > max_size))
168 height = max_size / stride;
169
170 const size_t size = height * stride;
171
172 if (!GetTransportDIBFromCache(memory, size)) {
173 *memory = CreateTransportDIB(size);
174 if (!*memory)
[email protected]a866cc22010-06-15 17:34:59175 return NULL;
[email protected]00c39612010-03-06 02:53:28176 }
177
178 return (*memory)->GetPlatformCanvas(width, height);
179}
180
181void RenderProcessImpl::ReleaseTransportDIB(TransportDIB* mem) {
182 if (PutSharedMemInCache(mem)) {
183 shared_mem_cache_cleaner_.Reset();
184 return;
185 }
186
187 FreeTransportDIB(mem);
188}
189
[email protected]00c39612010-03-06 02:53:28190bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB** mem,
191 size_t size) {
192 // look for a cached object that is suitable for the requested size.
193 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
194 if (shared_mem_cache_[i] &&
195 size <= shared_mem_cache_[i]->size()) {
196 *mem = shared_mem_cache_[i];
197 shared_mem_cache_[i] = NULL;
198 return true;
199 }
200 }
201
202 return false;
203}
204
205int RenderProcessImpl::FindFreeCacheSlot(size_t size) {
206 // simple algorithm:
207 // - look for an empty slot to store mem, or
208 // - if full, then replace smallest entry which is smaller than |size|
209 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
210 if (shared_mem_cache_[i] == NULL)
211 return i;
212 }
213
214 size_t smallest_size = size;
215 int smallest_index = -1;
216
217 for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) {
218 const size_t entry_size = shared_mem_cache_[i]->size();
219 if (entry_size < smallest_size) {
220 smallest_size = entry_size;
221 smallest_index = i;
222 }
223 }
224
225 if (smallest_index != -1) {
226 FreeTransportDIB(shared_mem_cache_[smallest_index]);
227 shared_mem_cache_[smallest_index] = NULL;
228 }
229
230 return smallest_index;
231}
232
233bool RenderProcessImpl::PutSharedMemInCache(TransportDIB* mem) {
234 const int slot = FindFreeCacheSlot(mem->size());
235 if (slot == -1)
236 return false;
237
238 shared_mem_cache_[slot] = mem;
239 return true;
240}
241
242void RenderProcessImpl::ClearTransportDIBCache() {
243 for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) {
244 if (shared_mem_cache_[i]) {
245 FreeTransportDIB(shared_mem_cache_[i]);
246 shared_mem_cache_[i] = NULL;
247 }
248 }
249}
[email protected]eb398192012-10-22 20:16:19250
251} // namespace content