[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" |
[email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame^] | 33 | #include "v8/include/v8.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 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) |
| 52 | shared_mem_cache_[i] = NULL; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 53 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 54 | #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] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 65 | if (gdi_init_lpk) { |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 66 | gdi_init_lpk(0); |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 67 | } |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 68 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 69 | #endif |
| 70 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 71 | // Out of process dev tools rely upon auto break behavior. |
[email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame^] | 72 | 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] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 75 | |
[email protected] | 987422f | 2013-10-01 10:33:31 | [diff] [blame] | 76 | #if defined(OS_ANDROID) |
[email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame^] | 77 | 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] | 987422f | 2013-10-01 10:33:31 | [diff] [blame] | 82 | #endif |
| 83 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 84 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 85 | if (command_line.HasSwitch(switches::kJavaScriptFlags)) { |
[email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame^] | 86 | std::string flags( |
[email protected] | 95edc39 | 2010-07-30 22:00:38 | [diff] [blame] | 87 | command_line.GetSwitchValueASCII(switches::kJavaScriptFlags)); |
[email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame^] | 88 | v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size())); |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 89 | } |
[email protected] | 55dd933 | 2013-09-04 17:17:50 | [diff] [blame] | 90 | |
| 91 | // Turn on cross-site document blocking for renderer processes. |
| 92 | SiteIsolationPolicy::SetPolicyEnabled( |
| 93 | GetContentClient()->renderer()->ShouldEnableSiteIsolationPolicy()); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 96 | RenderProcessImpl::~RenderProcessImpl() { |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 97 | #ifndef NDEBUG |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 98 | int count = blink::WebFrame::instanceCount(); |
[email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 99 | if (count) |
| 100 | DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES"; |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 101 | #endif |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 102 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 103 | GetShutDownEvent()->Signal(); |
| 104 | ClearTransportDIBCache(); |
| 105 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 106 | |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame] | 107 | void RenderProcessImpl::AddBindings(int bindings) { |
| 108 | enabled_bindings_ |= bindings; |
| 109 | } |
| 110 | |
| 111 | int RenderProcessImpl::GetEnabledBindings() const { |
| 112 | return enabled_bindings_; |
| 113 | } |
| 114 | |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 115 | // ----------------------------------------------------------------------------- |
| 116 | // Platform specific code for dealing with bitmap transport... |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 117 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 118 | TransportDIB* RenderProcessImpl::CreateTransportDIB(size_t size) { |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 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 |
[email protected] | ada848b1 | 2010-04-08 22:35:38 | [diff] [blame] | 121 | // get one. The TransportDIB is cached in the browser. |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 122 | TransportDIB::Handle handle; |
[email protected] | ada848b1 | 2010-04-08 22:35:38 | [diff] [blame] | 123 | IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, true, &handle); |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 124 | if (!main_thread()->Send(msg)) |
| 125 | return NULL; |
| 126 | if (handle.fd < 0) |
| 127 | return NULL; |
| 128 | return TransportDIB::Map(handle); |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 129 | #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] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 136 | void RenderProcessImpl::FreeTransportDIB(TransportDIB* dib) { |
| 137 | if (!dib) |
| 138 | return; |
| 139 | |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 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 |
[email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 142 | // shared memory. |
| 143 | IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id()); |
| 144 | main_thread()->Send(msg); |
| 145 | #endif |
| 146 | |
| 147 | delete dib; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | } |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 149 | |
| 150 | // ----------------------------------------------------------------------------- |
| 151 | |
| 152 | |
| 153 | skia::PlatformCanvas* RenderProcessImpl::GetDrawingCanvas( |
| 154 | TransportDIB** memory, const gfx::Rect& rect) { |
| 155 | int width = rect.width(); |
| 156 | int height = rect.height(); |
[email protected] | 9d611ca | 2012-11-14 13:46:56 | [diff] [blame] | 157 | const size_t stride = skia::PlatformCanvasStrideForWidth(rect.width()); |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 158 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 159 | 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] | a866cc2 | 2010-06-15 17:34:59 | [diff] [blame] | 175 | return NULL; |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | return (*memory)->GetPlatformCanvas(width, height); |
| 179 | } |
| 180 | |
| 181 | void 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] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 190 | bool 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 | |
| 205 | int 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 | |
| 233 | bool 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 | |
| 242 | void 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] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 250 | |
| 251 | } // namespace content |