[email protected] | fd911dd | 2012-01-27 01:57:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | d353541f | 2012-05-03 22:45:41 | [diff] [blame] | 5 | #include "content/renderer/render_process_impl.h" |
| 6 | |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 7 | #include "build/build_config.h" |
| 8 | |
| 9 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include <windows.h> |
| 11 | #include <objidl.h> |
| 12 | #include <mlang.h> |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 13 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/basictypes.h" |
| 16 | #include "base/command_line.h" |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 17 | #include "base/compiler_specific.h" |
[email protected] | aaf6889 | 2013-07-18 00:11:30 | [diff] [blame] | 18 | #include "base/message_loop/message_loop.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 20 | #include "base/sys_info.h" |
[email protected] | 10208ea | 2013-06-06 20:08:03 | [diff] [blame] | 21 | #include "content/child/child_thread.h" |
[email protected] | 29e2fb4 | 2013-07-19 01:13:47 | [diff] [blame] | 22 | #include "content/child/npapi/plugin_instance.h" |
| 23 | #include "content/child/npapi/plugin_lib.h" |
[email protected] | 55dd933 | 2013-09-04 17:17:50 | [diff] [blame] | 24 | #include "content/child/site_isolation_policy.h" |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 25 | #include "content/common/view_messages.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 26 | #include "content/public/common/content_switches.h" |
[email protected] | d344114c | 2011-10-01 01:24:34 | [diff] [blame] | 27 | #include "content/public/renderer/content_renderer_client.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 28 | #include "ipc/ipc_channel.h" |
| 29 | #include "ipc/ipc_message_utils.h" |
[email protected] | 46f36a49 | 2010-07-28 19:36:41 | [diff] [blame] | 30 | #include "skia/ext/platform_canvas.h" |
[email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 31 | #include "third_party/WebKit/public/web/WebFrame.h" |
[email protected] | d353541f | 2012-05-03 22:45:41 | [diff] [blame] | 32 | #include "ui/surface/transport_dib.h" |
| 33 | #include "webkit/glue/webkit_glue.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
[email protected] | 0c3a16b | 2009-10-08 23:17:14 | [diff] [blame] | 35 | #if defined(OS_MACOSX) |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 36 | #include "base/mac/mac_util.h" |
[email protected] | 0c3a16b | 2009-10-08 23:17:14 | [diff] [blame] | 37 | #endif |
| 38 | |
[email protected] | 987422f | 2013-10-01 10:33:31 | [diff] [blame] | 39 | #if defined(OS_ANDROID) |
| 40 | #include "base/android/sys_utils.h" |
| 41 | #endif |
| 42 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 43 | namespace content { |
| 44 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 45 | RenderProcessImpl::RenderProcessImpl() |
[email protected] | 69e797f | 2013-04-30 01:10:22 | [diff] [blame] | 46 | : shared_mem_cache_cleaner_( |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 47 | FROM_HERE, base::TimeDelta::FromSeconds(5), |
[email protected] | 69e797f | 2013-04-30 01:10:22 | [diff] [blame] | 48 | this, &RenderProcessImpl::ClearTransportDIBCache), |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame] | 49 | transport_dib_next_sequence_number_(0), |
| 50 | enabled_bindings_(0) { |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 51 | in_process_plugins_ = InProcessPlugins(); |
| 52 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) |
| 53 | shared_mem_cache_[i] = NULL; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 54 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 55 | #if defined(OS_WIN) |
| 56 | // HACK: See http://b/issue?id=1024307 for rationale. |
| 57 | if (GetModuleHandle(L"LPK.DLL") == NULL) { |
| 58 | // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works |
| 59 | // when buffering into a EMF buffer for printing. |
| 60 | typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs); |
| 61 | GdiInitializeLanguagePack gdi_init_lpk = |
| 62 | reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress( |
| 63 | GetModuleHandle(L"GDI32.DLL"), |
| 64 | "GdiInitializeLanguagePack")); |
| 65 | DCHECK(gdi_init_lpk); |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 66 | if (gdi_init_lpk) { |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 67 | gdi_init_lpk(0); |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 68 | } |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 69 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 70 | #endif |
| 71 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 72 | // Out of process dev tools rely upon auto break behavior. |
[email protected] | 218c503 | 2013-08-29 08:47:42 | [diff] [blame] | 73 | webkit_glue::SetJavaScriptFlags("--debugger-auto-break"); |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 74 | |
[email protected] | 987422f | 2013-10-01 10:33:31 | [diff] [blame] | 75 | #if defined(OS_ANDROID) |
| 76 | if (base::android::SysUtils::IsLowEndDevice()) |
| 77 | webkit_glue::SetJavaScriptFlags("--optimize-for-size"); |
| 78 | #endif |
| 79 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 80 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 81 | if (command_line.HasSwitch(switches::kJavaScriptFlags)) { |
| 82 | webkit_glue::SetJavaScriptFlags( |
[email protected] | 95edc39 | 2010-07-30 22:00:38 | [diff] [blame] | 83 | command_line.GetSwitchValueASCII(switches::kJavaScriptFlags)); |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 84 | } |
[email protected] | 55dd933 | 2013-09-04 17:17:50 | [diff] [blame] | 85 | |
| 86 | // Turn on cross-site document blocking for renderer processes. |
| 87 | SiteIsolationPolicy::SetPolicyEnabled( |
| 88 | GetContentClient()->renderer()->ShouldEnableSiteIsolationPolicy()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 91 | RenderProcessImpl::~RenderProcessImpl() { |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 92 | #ifndef NDEBUG |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame^] | 93 | int count = blink::WebFrame::instanceCount(); |
[email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 94 | if (count) |
| 95 | DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES"; |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 96 | #endif |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 97 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 98 | GetShutDownEvent()->Signal(); |
| 99 | ClearTransportDIBCache(); |
| 100 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 101 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 102 | bool RenderProcessImpl::InProcessPlugins() { |
| 103 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 104 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 105 | // Plugin processes require a UI message loop, and the Linux message loop |
| 106 | // implementation only allows one UI loop per process. |
| 107 | if (command_line.HasSwitch(switches::kInProcessPlugins)) |
| 108 | NOTIMPLEMENTED() << ": in process plugins not supported on Linux"; |
| 109 | return command_line.HasSwitch(switches::kInProcessPlugins); |
| 110 | #else |
| 111 | return command_line.HasSwitch(switches::kInProcessPlugins) || |
| 112 | command_line.HasSwitch(switches::kSingleProcess); |
| 113 | #endif |
| 114 | } |
| 115 | |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame] | 116 | void RenderProcessImpl::AddBindings(int bindings) { |
| 117 | enabled_bindings_ |= bindings; |
| 118 | } |
| 119 | |
| 120 | int RenderProcessImpl::GetEnabledBindings() const { |
| 121 | return enabled_bindings_; |
| 122 | } |
| 123 | |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 124 | // ----------------------------------------------------------------------------- |
| 125 | // Platform specific code for dealing with bitmap transport... |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 126 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 127 | TransportDIB* RenderProcessImpl::CreateTransportDIB(size_t size) { |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 128 | #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID) |
| 129 | // POSIX creates transport DIBs in the browser, so we need to do a sync IPC to |
[email protected] | ada848b1 | 2010-04-08 22:35:38 | [diff] [blame] | 130 | // get one. The TransportDIB is cached in the browser. |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 131 | TransportDIB::Handle handle; |
[email protected] | ada848b1 | 2010-04-08 22:35:38 | [diff] [blame] | 132 | IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, true, &handle); |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 133 | if (!main_thread()->Send(msg)) |
| 134 | return NULL; |
| 135 | if (handle.fd < 0) |
| 136 | return NULL; |
| 137 | return TransportDIB::Map(handle); |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 138 | #else |
| 139 | // Windows, legacy GTK and Android create transport DIBs inside the |
| 140 | // renderer. |
| 141 | return TransportDIB::Create(size, transport_dib_next_sequence_number_++); |
| 142 | #endif |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 145 | void RenderProcessImpl::FreeTransportDIB(TransportDIB* dib) { |
| 146 | if (!dib) |
| 147 | return; |
| 148 | |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 149 | #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID) |
| 150 | // On POSIX we need to tell the browser that it can drop a reference to the |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 151 | // shared memory. |
| 152 | IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id()); |
| 153 | main_thread()->Send(msg); |
| 154 | #endif |
| 155 | |
| 156 | delete dib; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | } |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 158 | |
| 159 | // ----------------------------------------------------------------------------- |
| 160 | |
| 161 | |
| 162 | skia::PlatformCanvas* RenderProcessImpl::GetDrawingCanvas( |
| 163 | TransportDIB** memory, const gfx::Rect& rect) { |
| 164 | int width = rect.width(); |
| 165 | int height = rect.height(); |
[email protected] | 9d611ca | 2012-11-14 13:46:56 | [diff] [blame] | 166 | const size_t stride = skia::PlatformCanvasStrideForWidth(rect.width()); |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 167 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 168 | const size_t max_size = base::SysInfo::MaxSharedMemorySize(); |
| 169 | #else |
| 170 | const size_t max_size = 0; |
| 171 | #endif |
| 172 | |
| 173 | // If the requested size is too big, reduce the height. Ideally we might like |
| 174 | // to reduce the width as well to make the size reduction more "balanced", but |
| 175 | // it rarely comes up in practice. |
| 176 | if ((max_size != 0) && (height * stride > max_size)) |
| 177 | height = max_size / stride; |
| 178 | |
| 179 | const size_t size = height * stride; |
| 180 | |
| 181 | if (!GetTransportDIBFromCache(memory, size)) { |
| 182 | *memory = CreateTransportDIB(size); |
| 183 | if (!*memory) |
[email protected] | a866cc2 | 2010-06-15 17:34:59 | [diff] [blame] | 184 | return NULL; |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return (*memory)->GetPlatformCanvas(width, height); |
| 188 | } |
| 189 | |
| 190 | void RenderProcessImpl::ReleaseTransportDIB(TransportDIB* mem) { |
| 191 | if (PutSharedMemInCache(mem)) { |
| 192 | shared_mem_cache_cleaner_.Reset(); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | FreeTransportDIB(mem); |
| 197 | } |
| 198 | |
| 199 | bool RenderProcessImpl::UseInProcessPlugins() const { |
| 200 | return in_process_plugins_; |
| 201 | } |
| 202 | |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 203 | bool RenderProcessImpl::GetTransportDIBFromCache(TransportDIB** mem, |
| 204 | size_t size) { |
| 205 | // look for a cached object that is suitable for the requested size. |
| 206 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
| 207 | if (shared_mem_cache_[i] && |
| 208 | size <= shared_mem_cache_[i]->size()) { |
| 209 | *mem = shared_mem_cache_[i]; |
| 210 | shared_mem_cache_[i] = NULL; |
| 211 | return true; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | int RenderProcessImpl::FindFreeCacheSlot(size_t size) { |
| 219 | // simple algorithm: |
| 220 | // - look for an empty slot to store mem, or |
| 221 | // - if full, then replace smallest entry which is smaller than |size| |
| 222 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
| 223 | if (shared_mem_cache_[i] == NULL) |
| 224 | return i; |
| 225 | } |
| 226 | |
| 227 | size_t smallest_size = size; |
| 228 | int smallest_index = -1; |
| 229 | |
| 230 | for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) { |
| 231 | const size_t entry_size = shared_mem_cache_[i]->size(); |
| 232 | if (entry_size < smallest_size) { |
| 233 | smallest_size = entry_size; |
| 234 | smallest_index = i; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (smallest_index != -1) { |
| 239 | FreeTransportDIB(shared_mem_cache_[smallest_index]); |
| 240 | shared_mem_cache_[smallest_index] = NULL; |
| 241 | } |
| 242 | |
| 243 | return smallest_index; |
| 244 | } |
| 245 | |
| 246 | bool RenderProcessImpl::PutSharedMemInCache(TransportDIB* mem) { |
| 247 | const int slot = FindFreeCacheSlot(mem->size()); |
| 248 | if (slot == -1) |
| 249 | return false; |
| 250 | |
| 251 | shared_mem_cache_[slot] = mem; |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | void RenderProcessImpl::ClearTransportDIBCache() { |
| 256 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
| 257 | if (shared_mem_cache_[i]) { |
| 258 | FreeTransportDIB(shared_mem_cache_[i]); |
| 259 | shared_mem_cache_[i] = NULL; |
| 260 | } |
| 261 | } |
| 262 | } |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 263 | |
| 264 | } // namespace content |