license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
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 | |
| 13 | #include "chrome/renderer/render_process.h" |
| 14 | |
| 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" |
| 19 | #include "base/histogram.h" |
[email protected] | 8ee8189e | 2008-10-08 19:35:21 | [diff] [blame] | 20 | #include "base/path_service.h" |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 21 | #include "base/sys_info.h" |
[email protected] | f09c718 | 2009-03-10 12:54:04 | [diff] [blame] | 22 | // TODO(jar): DNS calls should be renderer specific, not including browser. |
| 23 | #include "chrome/browser/net/dns_global.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | #include "chrome/common/chrome_switches.h" |
[email protected] | 8ee8189e | 2008-10-08 19:35:21 | [diff] [blame] | 25 | #include "chrome/common/chrome_paths.h" |
[email protected] | 82e5ee8 | 2009-04-03 02:29:45 | [diff] [blame] | 26 | #include "chrome/common/ipc_channel.h" |
| 27 | #include "chrome/common/ipc_message_utils.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | #include "chrome/common/render_messages.h" |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 29 | #include "chrome/common/transport_dib.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | #include "chrome/renderer/render_view.h" |
[email protected] | 10000d6 | 2009-04-18 00:36:22 | [diff] [blame] | 31 | #include "media/base/media.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | #include "webkit/glue/webkit_glue.h" |
| 33 | |
[email protected] | bf557e1 | 2009-02-25 20:50:29 | [diff] [blame] | 34 | //----------------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 36 | RenderProcess::RenderProcess() |
| 37 | : ChildProcess(new RenderThread()), |
[email protected] | 514e711 | 2009-02-20 05:23:36 | [diff] [blame] | 38 | ALLOW_THIS_IN_INITIALIZER_LIST(shared_mem_cache_cleaner_( |
| 39 | base::TimeDelta::FromSeconds(5), |
| 40 | this, &RenderProcess::ClearTransportDIBCache)), |
| 41 | sequence_number_(0) { |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 42 | Init(); |
| 43 | } |
| 44 | |
| 45 | RenderProcess::RenderProcess(const std::wstring& channel_name) |
| 46 | : ChildProcess(new RenderThread(channel_name)), |
| 47 | ALLOW_THIS_IN_INITIALIZER_LIST(shared_mem_cache_cleaner_( |
| 48 | base::TimeDelta::FromSeconds(5), |
| 49 | this, &RenderProcess::ClearTransportDIBCache)), |
| 50 | sequence_number_(0) { |
| 51 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | RenderProcess::~RenderProcess() { |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 55 | // TODO(port) |
| 56 | // Try and limit what we pull in for our non-Win unit test bundle |
| 57 | #ifndef NDEBUG |
| 58 | // log important leaked objects |
| 59 | webkit_glue::CheckForLeaks(); |
| 60 | #endif |
| 61 | |
| 62 | GetShutDownEvent()->Signal(); |
| 63 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | // We need to stop the RenderThread as the clearer_factory_ |
| 65 | // member could be in use while the object itself is destroyed, |
| 66 | // as a result of the containing RenderProcess object being destroyed. |
| 67 | // This race condition causes a crash when the renderer process is shutting |
| 68 | // down. |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 69 | child_thread()->Stop(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 70 | ClearTransportDIBCache(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 73 | void RenderProcess::Init() { |
| 74 | in_process_plugins_ = InProcessPlugins(); |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 75 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) |
| 76 | shared_mem_cache_[i] = NULL; |
| 77 | |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 78 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | // HACK: See http://b/issue?id=1024307 for rationale. |
| 80 | if (GetModuleHandle(L"LPK.DLL") == NULL) { |
| 81 | // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works |
| 82 | // when buffering into a EMF buffer for printing. |
| 83 | typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs); |
| 84 | GdiInitializeLanguagePack gdi_init_lpk = |
| 85 | reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress( |
| 86 | GetModuleHandle(L"GDI32.DLL"), |
| 87 | "GdiInitializeLanguagePack")); |
| 88 | DCHECK(gdi_init_lpk); |
| 89 | if (gdi_init_lpk) { |
| 90 | gdi_init_lpk(0); |
| 91 | } |
| 92 | } |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 93 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 95 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | if (command_line.HasSwitch(switches::kJavaScriptFlags)) { |
| 97 | webkit_glue::SetJavaScriptFlags( |
| 98 | command_line.GetSwitchValue(switches::kJavaScriptFlags)); |
| 99 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | |
[email protected] | 2ef9c45 | 2009-05-13 13:03:14 | [diff] [blame^] | 101 | if (!command_line.HasSwitch(switches::kDisableOutOfProcessDevTools)) { |
| 102 | // Out of process dev tools rely upon auto break behavior. |
| 103 | webkit_glue::SetJavaScriptFlags(L"--debugger-auto-break"); |
| 104 | } |
| 105 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | if (command_line.HasSwitch(switches::kEnableWatchdog)) { |
| 107 | // TODO(JAR): Need to implement renderer IO msgloop watchdog. |
| 108 | } |
| 109 | |
| 110 | if (command_line.HasSwitch(switches::kDumpHistogramsOnExit)) { |
| 111 | StatisticsRecorder::set_dump_on_exit(true); |
| 112 | } |
| 113 | |
[email protected] | 10000d6 | 2009-04-18 00:36:22 | [diff] [blame] | 114 | FilePath module_path; |
| 115 | if (PathService::Get(base::DIR_MODULE, &module_path)) { |
| 116 | if (media::InitializeMediaLibrary(module_path)) { |
| 117 | webkit_glue::SetMediaPlayerAvailable(true); |
| 118 | } |
[email protected] | c3e2817 | 2009-02-11 23:38:32 | [diff] [blame] | 119 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 122 | bool RenderProcess::InProcessPlugins() { |
| 123 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
[email protected] | 0e2f875 | 2009-04-27 20:34:31 | [diff] [blame] | 124 | #if defined(OS_LINUX) |
| 125 | // Plugin processes require a UI message loop, and the Linux message loop |
| 126 | // implementation only allows one UI loop per process. |
| 127 | if (command_line.HasSwitch(switches::kInProcessPlugins)) |
| 128 | NOTIMPLEMENTED() << ": in process plugins not supported on Linux"; |
| 129 | return command_line.HasSwitch(switches::kInProcessPlugins); |
| 130 | #else |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 131 | return command_line.HasSwitch(switches::kInProcessPlugins) || |
| 132 | command_line.HasSwitch(switches::kSingleProcess); |
[email protected] | 0e2f875 | 2009-04-27 20:34:31 | [diff] [blame] | 133 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 136 | // ----------------------------------------------------------------------------- |
| 137 | // Platform specific code for dealing with bitmap transport... |
| 138 | |
| 139 | // ----------------------------------------------------------------------------- |
| 140 | // Create a platform canvas object which renders into the given transport |
| 141 | // memory. |
| 142 | // ----------------------------------------------------------------------------- |
| 143 | static skia::PlatformCanvas* CanvasFromTransportDIB( |
| 144 | TransportDIB* dib, const gfx::Rect& rect) { |
| 145 | #if defined(OS_WIN) |
| 146 | return new skia::PlatformCanvas(rect.width(), rect.height(), true, |
| 147 | dib->handle()); |
| 148 | #elif defined(OS_LINUX) || defined(OS_MACOSX) |
| 149 | return new skia::PlatformCanvas(rect.width(), rect.height(), true, |
| 150 | reinterpret_cast<uint8_t*>(dib->memory())); |
| 151 | #endif |
| 152 | } |
| 153 | |
| 154 | TransportDIB* RenderProcess::CreateTransportDIB(size_t size) { |
| 155 | #if defined(OS_WIN) || defined(OS_LINUX) |
| 156 | // Windows and Linux create transport DIBs inside the renderer |
| 157 | return TransportDIB::Create(size, sequence_number_++); |
| 158 | #elif defined(OS_MACOSX) // defined(OS_WIN) || defined(OS_LINUX) |
| 159 | // Mac creates transport DIBs in the browser, so we need to do a sync IPC to |
| 160 | // get one. |
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 161 | TransportDIB::Handle handle; |
| 162 | IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, &handle); |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 163 | if (!child_thread()->Send(msg)) |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 164 | return NULL; |
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 165 | if (handle.fd < 0) |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 166 | return NULL; |
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 167 | return TransportDIB::Map(handle); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 168 | #endif // defined(OS_MACOSX) |
| 169 | } |
| 170 | |
| 171 | void RenderProcess::FreeTransportDIB(TransportDIB* dib) { |
| 172 | if (!dib) |
| 173 | return; |
| 174 | |
| 175 | #if defined(OS_MACOSX) |
| 176 | // On Mac we need to tell the browser that it can drop a reference to the |
| 177 | // shared memory. |
| 178 | IPC::Message* msg = new ViewHostMsg_FreeTransportDIB(dib->id()); |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 179 | child_thread()->Send(msg); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 180 | #endif |
| 181 | |
| 182 | delete dib; |
| 183 | } |
| 184 | |
| 185 | // ----------------------------------------------------------------------------- |
| 186 | |
| 187 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 188 | skia::PlatformCanvas* RenderProcess::GetDrawingCanvas( |
| 189 | TransportDIB** memory, const gfx::Rect& rect) { |
| 190 | const size_t stride = skia::PlatformCanvas::StrideForWidth(rect.width()); |
| 191 | const size_t size = stride * rect.height(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 193 | if (!GetTransportDIBFromCache(memory, size)) { |
| 194 | *memory = CreateTransportDIB(size); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 195 | if (!*memory) |
| 196 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 199 | return CanvasFromTransportDIB(*memory, rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | } |
| 201 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 202 | void RenderProcess::ReleaseTransportDIB(TransportDIB* mem) { |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 203 | if (PutSharedMemInCache(mem)) { |
| 204 | shared_mem_cache_cleaner_.Reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | return; |
| 206 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 207 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 208 | FreeTransportDIB(mem); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 211 | bool RenderProcess::GetTransportDIBFromCache(TransportDIB** mem, |
| 212 | size_t size) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | // look for a cached object that is suitable for the requested size. |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 214 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 215 | if (shared_mem_cache_[i] && |
| 216 | size <= shared_mem_cache_[i]->size()) { |
| 217 | *mem = shared_mem_cache_[i]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | shared_mem_cache_[i] = NULL; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 219 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | } |
| 221 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | return false; |
| 224 | } |
| 225 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 226 | int RenderProcess::FindFreeCacheSlot(size_t size) { |
| 227 | // simple algorithm: |
| 228 | // - look for an empty slot to store mem, or |
| 229 | // - if full, then replace smallest entry which is smaller than |size| |
| 230 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
| 231 | if (shared_mem_cache_[i] == NULL) |
| 232 | return i; |
| 233 | } |
| 234 | |
| 235 | size_t smallest_size = size; |
| 236 | int smallest_index = -1; |
| 237 | |
| 238 | for (size_t i = 1; i < arraysize(shared_mem_cache_); ++i) { |
| 239 | const size_t entry_size = shared_mem_cache_[i]->size(); |
| 240 | if (entry_size < smallest_size) { |
| 241 | smallest_size = entry_size; |
| 242 | smallest_index = i; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (smallest_index != -1) { |
| 247 | FreeTransportDIB(shared_mem_cache_[smallest_index]); |
| 248 | shared_mem_cache_[smallest_index] = NULL; |
| 249 | } |
| 250 | |
| 251 | return smallest_index; |
| 252 | } |
| 253 | |
| 254 | bool RenderProcess::PutSharedMemInCache(TransportDIB* mem) { |
| 255 | const int slot = FindFreeCacheSlot(mem->size()); |
| 256 | if (slot == -1) |
| 257 | return false; |
| 258 | |
| 259 | shared_mem_cache_[slot] = mem; |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | void RenderProcess::ClearTransportDIBCache() { |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 264 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 265 | if (shared_mem_cache_[i]) { |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 266 | FreeTransportDIB(shared_mem_cache_[i]); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 267 | shared_mem_cache_[i] = NULL; |
| 268 | } |
| 269 | } |
| 270 | } |