1 // Copyright (c) 2012 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.
5 #include "content/renderer/render_process_impl.h"
7 #include "build/build_config.h"
15 #include "base/basictypes.h"
16 #include "base/command_line.h"
17 #include "base/compiler_specific.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/sys_info.h"
21 #include "content/child/child_thread.h"
22 #include "content/child/npapi/plugin_instance.h"
23 #include "content/child/npapi/plugin_lib.h"
24 #include "content/child/site_isolation_policy.h"
25 #include "content/common/view_messages.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/renderer/content_renderer_client.h"
28 #include "ipc/ipc_channel.h"
29 #include "ipc/ipc_message_utils.h"
30 #include "skia/ext/platform_canvas.h"
31 #include "third_party/WebKit/public/web/WebFrame.h"
32 #include "ui/surface/transport_dib.h"
33 #include "webkit/glue/webkit_glue.h"
35 #if defined(OS_MACOSX)
36 #include "base/mac/mac_util.h"
41 RenderProcessImpl::RenderProcessImpl()
42 : shared_mem_cache_cleaner_(
43 FROM_HERE
, base::TimeDelta::FromSeconds(5),
44 this, &RenderProcessImpl::ClearTransportDIBCache
),
45 transport_dib_next_sequence_number_(0),
46 enabled_bindings_(0) {
47 in_process_plugins_
= InProcessPlugins();
48 for (size_t i
= 0; i
< arraysize(shared_mem_cache_
); ++i
)
49 shared_mem_cache_
[i
] = NULL
;
52 // HACK: See http://b/issue?id=1024307 for rationale.
53 if (GetModuleHandle(L
"LPK.DLL") == NULL
) {
54 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
55 // when buffering into a EMF buffer for printing.
56 typedef BOOL (__stdcall
*GdiInitializeLanguagePack
)(int LoadedShapingDLLs
);
57 GdiInitializeLanguagePack gdi_init_lpk
=
58 reinterpret_cast<GdiInitializeLanguagePack
>(GetProcAddress(
59 GetModuleHandle(L
"GDI32.DLL"),
60 "GdiInitializeLanguagePack"));
68 // Out of process dev tools rely upon auto break behavior.
69 webkit_glue::SetJavaScriptFlags("--debugger-auto-break");
71 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
72 if (command_line
.HasSwitch(switches::kJavaScriptFlags
)) {
73 webkit_glue::SetJavaScriptFlags(
74 command_line
.GetSwitchValueASCII(switches::kJavaScriptFlags
));
77 // Turn on cross-site document blocking for renderer processes.
78 SiteIsolationPolicy::SetPolicyEnabled(
79 GetContentClient()->renderer()->ShouldEnableSiteIsolationPolicy());
82 RenderProcessImpl::~RenderProcessImpl() {
84 int count
= WebKit::WebFrame::instanceCount();
86 DLOG(ERROR
) << "WebFrame LEAKED " << count
<< " TIMES";
89 GetShutDownEvent()->Signal();
90 ClearTransportDIBCache();
93 bool RenderProcessImpl::InProcessPlugins() {
94 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
95 #if defined(OS_LINUX) || defined(OS_OPENBSD)
96 // Plugin processes require a UI message loop, and the Linux message loop
97 // implementation only allows one UI loop per process.
98 if (command_line
.HasSwitch(switches::kInProcessPlugins
))
99 NOTIMPLEMENTED() << ": in process plugins not supported on Linux";
100 return command_line
.HasSwitch(switches::kInProcessPlugins
);
102 return command_line
.HasSwitch(switches::kInProcessPlugins
) ||
103 command_line
.HasSwitch(switches::kSingleProcess
);
107 void RenderProcessImpl::AddBindings(int bindings
) {
108 enabled_bindings_
|= bindings
;
111 int RenderProcessImpl::GetEnabledBindings() const {
112 return enabled_bindings_
;
115 // -----------------------------------------------------------------------------
116 // Platform specific code for dealing with bitmap transport...
118 TransportDIB
* RenderProcessImpl::CreateTransportDIB(size_t size
) {
119 #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
121 // get one. The TransportDIB is cached in the browser.
122 TransportDIB::Handle handle
;
123 IPC::Message
* msg
= new ViewHostMsg_AllocTransportDIB(size
, true, &handle
);
124 if (!main_thread()->Send(msg
))
128 return TransportDIB::Map(handle
);
130 // Windows, legacy GTK and Android create transport DIBs inside the
132 return TransportDIB::Create(size
, transport_dib_next_sequence_number_
++);
136 void RenderProcessImpl::FreeTransportDIB(TransportDIB
* dib
) {
140 #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
143 IPC::Message
* msg
= new ViewHostMsg_FreeTransportDIB(dib
->id());
144 main_thread()->Send(msg
);
150 // -----------------------------------------------------------------------------
153 skia::PlatformCanvas
* RenderProcessImpl::GetDrawingCanvas(
154 TransportDIB
** memory
, const gfx::Rect
& rect
) {
155 int width
= rect
.width();
156 int height
= rect
.height();
157 const size_t stride
= skia::PlatformCanvasStrideForWidth(rect
.width());
158 #if defined(OS_LINUX) || defined(OS_OPENBSD)
159 const size_t max_size
= base::SysInfo::MaxSharedMemorySize();
161 const size_t max_size
= 0;
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
;
170 const size_t size
= height
* stride
;
172 if (!GetTransportDIBFromCache(memory
, size
)) {
173 *memory
= CreateTransportDIB(size
);
178 return (*memory
)->GetPlatformCanvas(width
, height
);
181 void RenderProcessImpl::ReleaseTransportDIB(TransportDIB
* mem
) {
182 if (PutSharedMemInCache(mem
)) {
183 shared_mem_cache_cleaner_
.Reset();
187 FreeTransportDIB(mem
);
190 bool RenderProcessImpl::UseInProcessPlugins() const {
191 return in_process_plugins_
;
194 bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB
** mem
,
196 // look for a cached object that is suitable for the requested size.
197 for (size_t i
= 0; i
< arraysize(shared_mem_cache_
); ++i
) {
198 if (shared_mem_cache_
[i
] &&
199 size
<= shared_mem_cache_
[i
]->size()) {
200 *mem
= shared_mem_cache_
[i
];
201 shared_mem_cache_
[i
] = NULL
;
209 int RenderProcessImpl::FindFreeCacheSlot(size_t size
) {
211 // - look for an empty slot to store mem, or
212 // - if full, then replace smallest entry which is smaller than |size|
213 for (size_t i
= 0; i
< arraysize(shared_mem_cache_
); ++i
) {
214 if (shared_mem_cache_
[i
] == NULL
)
218 size_t smallest_size
= size
;
219 int smallest_index
= -1;
221 for (size_t i
= 1; i
< arraysize(shared_mem_cache_
); ++i
) {
222 const size_t entry_size
= shared_mem_cache_
[i
]->size();
223 if (entry_size
< smallest_size
) {
224 smallest_size
= entry_size
;
229 if (smallest_index
!= -1) {
230 FreeTransportDIB(shared_mem_cache_
[smallest_index
]);
231 shared_mem_cache_
[smallest_index
] = NULL
;
234 return smallest_index
;
237 bool RenderProcessImpl::PutSharedMemInCache(TransportDIB
* mem
) {
238 const int slot
= FindFreeCacheSlot(mem
->size());
242 shared_mem_cache_
[slot
] = mem
;
246 void RenderProcessImpl::ClearTransportDIBCache() {
247 for (size_t i
= 0; i
< arraysize(shared_mem_cache_
); ++i
) {
248 if (shared_mem_cache_
[i
]) {
249 FreeTransportDIB(shared_mem_cache_
[i
]);
250 shared_mem_cache_
[i
] = NULL
;
255 } // namespace content