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"
39 #if defined(OS_ANDROID)
40 #include "base/android/sys_utils.h"
45 RenderProcessImpl::RenderProcessImpl()
46 : shared_mem_cache_cleaner_(
47 FROM_HERE
, base::TimeDelta::FromSeconds(5),
48 this, &RenderProcessImpl::ClearTransportDIBCache
),
49 transport_dib_next_sequence_number_(0),
50 enabled_bindings_(0) {
51 for (size_t i
= 0; i
< arraysize(shared_mem_cache_
); ++i
)
52 shared_mem_cache_
[i
] = NULL
;
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"));
71 // Out of process dev tools rely upon auto break behavior.
72 webkit_glue::SetJavaScriptFlags("--debugger-auto-break");
74 #if defined(OS_ANDROID)
75 if (base::android::SysUtils::IsLowEndDevice())
76 webkit_glue::SetJavaScriptFlags("--optimize-for-size");
79 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
80 if (command_line
.HasSwitch(switches::kJavaScriptFlags
)) {
81 webkit_glue::SetJavaScriptFlags(
82 command_line
.GetSwitchValueASCII(switches::kJavaScriptFlags
));
85 // Turn on cross-site document blocking for renderer processes.
86 SiteIsolationPolicy::SetPolicyEnabled(
87 GetContentClient()->renderer()->ShouldEnableSiteIsolationPolicy());
90 RenderProcessImpl::~RenderProcessImpl() {
92 int count
= blink::WebFrame::instanceCount();
94 DLOG(ERROR
) << "WebFrame LEAKED " << count
<< " TIMES";
97 GetShutDownEvent()->Signal();
98 ClearTransportDIBCache();
101 void RenderProcessImpl::AddBindings(int bindings
) {
102 enabled_bindings_
|= bindings
;
105 int RenderProcessImpl::GetEnabledBindings() const {
106 return enabled_bindings_
;
109 // -----------------------------------------------------------------------------
110 // Platform specific code for dealing with bitmap transport...
112 TransportDIB
* RenderProcessImpl::CreateTransportDIB(size_t size
) {
113 #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
114 // POSIX creates transport DIBs in the browser, so we need to do a sync IPC to
115 // get one. The TransportDIB is cached in the browser.
116 TransportDIB::Handle handle
;
117 IPC::Message
* msg
= new ViewHostMsg_AllocTransportDIB(size
, true, &handle
);
118 if (!main_thread()->Send(msg
))
122 return TransportDIB::Map(handle
);
124 // Windows, legacy GTK and Android create transport DIBs inside the
126 return TransportDIB::Create(size
, transport_dib_next_sequence_number_
++);
130 void RenderProcessImpl::FreeTransportDIB(TransportDIB
* dib
) {
134 #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
135 // On POSIX we need to tell the browser that it can drop a reference to the
137 IPC::Message
* msg
= new ViewHostMsg_FreeTransportDIB(dib
->id());
138 main_thread()->Send(msg
);
144 // -----------------------------------------------------------------------------
147 skia::PlatformCanvas
* RenderProcessImpl::GetDrawingCanvas(
148 TransportDIB
** memory
, const gfx::Rect
& rect
) {
149 int width
= rect
.width();
150 int height
= rect
.height();
151 const size_t stride
= skia::PlatformCanvasStrideForWidth(rect
.width());
152 #if defined(OS_LINUX) || defined(OS_OPENBSD)
153 const size_t max_size
= base::SysInfo::MaxSharedMemorySize();
155 const size_t max_size
= 0;
158 // If the requested size is too big, reduce the height. Ideally we might like
159 // to reduce the width as well to make the size reduction more "balanced", but
160 // it rarely comes up in practice.
161 if ((max_size
!= 0) && (height
* stride
> max_size
))
162 height
= max_size
/ stride
;
164 const size_t size
= height
* stride
;
166 if (!GetTransportDIBFromCache(memory
, size
)) {
167 *memory
= CreateTransportDIB(size
);
172 return (*memory
)->GetPlatformCanvas(width
, height
);
175 void RenderProcessImpl::ReleaseTransportDIB(TransportDIB
* mem
) {
176 if (PutSharedMemInCache(mem
)) {
177 shared_mem_cache_cleaner_
.Reset();
181 FreeTransportDIB(mem
);
184 bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB
** mem
,
186 // look for a cached object that is suitable for the requested size.
187 for (size_t i
= 0; i
< arraysize(shared_mem_cache_
); ++i
) {
188 if (shared_mem_cache_
[i
] &&
189 size
<= shared_mem_cache_
[i
]->size()) {
190 *mem
= shared_mem_cache_
[i
];
191 shared_mem_cache_
[i
] = NULL
;
199 int RenderProcessImpl::FindFreeCacheSlot(size_t size
) {
201 // - look for an empty slot to store mem, or
202 // - if full, then replace smallest entry which is smaller than |size|
203 for (size_t i
= 0; i
< arraysize(shared_mem_cache_
); ++i
) {
204 if (shared_mem_cache_
[i
] == NULL
)
208 size_t smallest_size
= size
;
209 int smallest_index
= -1;
211 for (size_t i
= 1; i
< arraysize(shared_mem_cache_
); ++i
) {
212 const size_t entry_size
= shared_mem_cache_
[i
]->size();
213 if (entry_size
< smallest_size
) {
214 smallest_size
= entry_size
;
219 if (smallest_index
!= -1) {
220 FreeTransportDIB(shared_mem_cache_
[smallest_index
]);
221 shared_mem_cache_
[smallest_index
] = NULL
;
224 return smallest_index
;
227 bool RenderProcessImpl::PutSharedMemInCache(TransportDIB
* mem
) {
228 const int slot
= FindFreeCacheSlot(mem
->size());
232 shared_mem_cache_
[slot
] = mem
;
236 void RenderProcessImpl::ClearTransportDIBCache() {
237 for (size_t i
= 0; i
< arraysize(shared_mem_cache_
); ++i
) {
238 if (shared_mem_cache_
[i
]) {
239 FreeTransportDIB(shared_mem_cache_
[i
]);
240 shared_mem_cache_
[i
] = NULL
;
245 } // namespace content