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