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.h"
19 #include "base/sys_info.h"
20 #include "base/utf_string_conversions.h"
21 #include "content/common/child_thread.h"
22 #include "content/common/view_messages.h"
23 #include "content/public/common/content_switches.h"
24 #include "content/public/renderer/content_renderer_client.h"
25 #include "ipc/ipc_channel.h"
26 #include "ipc/ipc_message_utils.h"
27 #include "skia/ext/platform_canvas.h"
28 #include "ui/surface/transport_dib.h"
29 #include "webkit/glue/webkit_glue.h"
30 #include "webkit/plugins/npapi/plugin_instance.h"
31 #include "webkit/plugins/npapi/plugin_lib.h"
33 #if defined(OS_MACOSX)
34 #include "base/mac/mac_util.h"
39 RenderProcessImpl::RenderProcessImpl()
40 : ALLOW_THIS_IN_INITIALIZER_LIST(shared_mem_cache_cleaner_(
41 FROM_HERE
, base::TimeDelta::FromSeconds(5),
42 this, &RenderProcessImpl::ClearTransportDIBCache
)),
43 transport_dib_next_sequence_number_(0),
44 enabled_bindings_(0) {
45 in_process_plugins_
= InProcessPlugins();
46 for (size_t i
= 0; i
< arraysize(shared_mem_cache_
); ++i
)
47 shared_mem_cache_
[i
] = NULL
;
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"));
66 // Out of process dev tools rely upon auto break behavior.
67 webkit_glue::SetJavaScriptFlags(
68 "--debugger-auto-break"
69 // Enable on-demand profiling.
70 " --prof --prof-lazy");
72 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
73 if (command_line
.HasSwitch(switches::kJavaScriptFlags
)) {
74 webkit_glue::SetJavaScriptFlags(
75 command_line
.GetSwitchValueASCII(switches::kJavaScriptFlags
));
79 RenderProcessImpl::~RenderProcessImpl() {
80 // TODO(port): Try and limit what we pull in for our non-Win unit test bundle.
82 // log important leaked objects
83 webkit_glue::CheckForLeaks();
86 GetShutDownEvent()->Signal();
87 ClearTransportDIBCache();
90 bool RenderProcessImpl::InProcessPlugins() {
91 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
92 #if defined(OS_LINUX) || defined(OS_OPENBSD)
93 // 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
);
99 return command_line
.HasSwitch(switches::kInProcessPlugins
) ||
100 command_line
.HasSwitch(switches::kSingleProcess
);
104 void RenderProcessImpl::AddBindings(int bindings
) {
105 enabled_bindings_
|= bindings
;
108 int RenderProcessImpl::GetEnabledBindings() const {
109 return enabled_bindings_
;
112 // -----------------------------------------------------------------------------
113 // Platform specific code for dealing with bitmap transport...
115 TransportDIB
* RenderProcessImpl::CreateTransportDIB(size_t size
) {
116 #if defined(OS_WIN) || defined(OS_LINUX) || \
117 defined(OS_OPENBSD) || defined(OS_ANDROID)
118 // Windows and Linux create transport DIBs inside the renderer
119 return TransportDIB::Create(size
, transport_dib_next_sequence_number_
++);
120 #elif defined(OS_MACOSX)
121 // Mac creates transport DIBs in the browser, so we need to do a sync IPC to
122 // get one. The TransportDIB is cached in the browser.
123 TransportDIB::Handle handle
;
124 IPC::Message
* msg
= new ViewHostMsg_AllocTransportDIB(size
, true, &handle
);
125 if (!main_thread()->Send(msg
))
129 return TransportDIB::Map(handle
);
130 #endif // defined(OS_MACOSX)
133 void RenderProcessImpl::FreeTransportDIB(TransportDIB
* dib
) {
137 #if defined(OS_MACOSX)
138 // On Mac we need to tell the browser that it can drop a reference to the
140 IPC::Message
* msg
= new ViewHostMsg_FreeTransportDIB(dib
->id());
141 main_thread()->Send(msg
);
147 // -----------------------------------------------------------------------------
150 skia::PlatformCanvas
* RenderProcessImpl::GetDrawingCanvas(
151 TransportDIB
** memory
, const gfx::Rect
& rect
) {
152 int width
= rect
.width();
153 int height
= rect
.height();
154 const size_t stride
= skia::PlatformCanvasStrideForWidth(rect
.width());
155 #if defined(OS_LINUX) || defined(OS_OPENBSD)
156 const size_t max_size
= base::SysInfo::MaxSharedMemorySize();
158 const size_t max_size
= 0;
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
;
167 const size_t size
= height
* stride
;
169 if (!GetTransportDIBFromCache(memory
, size
)) {
170 *memory
= CreateTransportDIB(size
);
175 return (*memory
)->GetPlatformCanvas(width
, height
);
178 void RenderProcessImpl::ReleaseTransportDIB(TransportDIB
* mem
) {
179 if (PutSharedMemInCache(mem
)) {
180 shared_mem_cache_cleaner_
.Reset();
184 FreeTransportDIB(mem
);
187 bool RenderProcessImpl::UseInProcessPlugins() const {
188 return in_process_plugins_
;
191 bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB
** mem
,
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
;
206 int RenderProcessImpl::FindFreeCacheSlot(size_t size
) {
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
)
215 size_t smallest_size
= size
;
216 int smallest_index
= -1;
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
;
226 if (smallest_index
!= -1) {
227 FreeTransportDIB(shared_mem_cache_
[smallest_index
]);
228 shared_mem_cache_
[smallest_index
] = NULL
;
231 return smallest_index
;
234 bool RenderProcessImpl::PutSharedMemInCache(TransportDIB
* mem
) {
235 const int slot
= FindFreeCacheSlot(mem
->size());
239 shared_mem_cache_
[slot
] = mem
;
243 void 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
;
252 } // namespace content