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