| 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" |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | #include "chrome/browser/net/dns_global.h" // TODO(jar): DNS calls should be renderer specific, not including browser. |
| 23 | #include "chrome/common/chrome_switches.h" |
| [email protected] | 8ee8189e | 2008-10-08 19:35:21 | [diff] [blame] | 24 | #include "chrome/common/chrome_paths.h" |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | #include "chrome/common/ipc_channel.h" |
| 26 | #include "chrome/common/ipc_message_utils.h" |
| 27 | #include "chrome/common/render_messages.h" |
| 28 | #include "chrome/renderer/render_view.h" |
| 29 | #include "webkit/glue/webkit_glue.h" |
| 30 | |
| 31 | //----------------------------------------------------------------------------- |
| 32 | |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | bool RenderProcess::load_plugins_in_process_ = false; |
| 34 | |
| 35 | //----------------------------------------------------------------------------- |
| 36 | |
| 37 | RenderProcess::RenderProcess(const std::wstring& channel_name) |
| 38 | : render_thread_(channel_name), |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 39 | ALLOW_THIS_IN_INITIALIZER_LIST(clearer_factory_(this)) { |
| 40 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | shared_mem_cache_[i] = NULL; |
| 42 | } |
| 43 | |
| 44 | RenderProcess::~RenderProcess() { |
| 45 | // We need to stop the RenderThread as the clearer_factory_ |
| 46 | // member could be in use while the object itself is destroyed, |
| 47 | // as a result of the containing RenderProcess object being destroyed. |
| 48 | // This race condition causes a crash when the renderer process is shutting |
| 49 | // down. |
| 50 | render_thread_.Stop(); |
| 51 | ClearSharedMemCache(); |
| 52 | } |
| 53 | |
| 54 | // static |
| 55 | bool RenderProcess::GlobalInit(const std::wstring &channel_name) { |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 56 | #if defined(OS_WIN) |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | // HACK: See http://b/issue?id=1024307 for rationale. |
| 58 | if (GetModuleHandle(L"LPK.DLL") == NULL) { |
| 59 | // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works |
| 60 | // when buffering into a EMF buffer for printing. |
| 61 | typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs); |
| 62 | GdiInitializeLanguagePack gdi_init_lpk = |
| 63 | reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress( |
| 64 | GetModuleHandle(L"GDI32.DLL"), |
| 65 | "GdiInitializeLanguagePack")); |
| 66 | DCHECK(gdi_init_lpk); |
| 67 | if (gdi_init_lpk) { |
| 68 | gdi_init_lpk(0); |
| 69 | } |
| 70 | } |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 71 | #endif |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 73 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | if (command_line.HasSwitch(switches::kJavaScriptFlags)) { |
| 75 | webkit_glue::SetJavaScriptFlags( |
| 76 | command_line.GetSwitchValue(switches::kJavaScriptFlags)); |
| 77 | } |
| 78 | if (command_line.HasSwitch(switches::kPlaybackMode) || |
| 79 | command_line.HasSwitch(switches::kRecordMode)) { |
| 80 | webkit_glue::SetRecordPlaybackMode(true); |
| 81 | } |
| 82 | |
| 83 | if (command_line.HasSwitch(switches::kInProcessPlugins) || |
| 84 | command_line.HasSwitch(switches::kSingleProcess)) |
| 85 | load_plugins_in_process_ = true; |
| 86 | |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | if (command_line.HasSwitch(switches::kEnableWatchdog)) { |
| 88 | // TODO(JAR): Need to implement renderer IO msgloop watchdog. |
| 89 | } |
| 90 | |
| 91 | if (command_line.HasSwitch(switches::kDumpHistogramsOnExit)) { |
| 92 | StatisticsRecorder::set_dump_on_exit(true); |
| 93 | } |
| 94 | |
| [email protected] | 8ee8189e | 2008-10-08 19:35:21 | [diff] [blame] | 95 | if (command_line.HasSwitch(switches::kGearsInRenderer)) { |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 96 | #if defined(OS_WIN) |
| [email protected] | 8ee8189e | 2008-10-08 19:35:21 | [diff] [blame] | 97 | // Load gears.dll on startup so we can access it before the sandbox |
| 98 | // blocks us. |
| 99 | std::wstring path; |
| 100 | if (PathService::Get(chrome::FILE_GEARS_PLUGIN, &path)) |
| 101 | LoadLibrary(path.c_str()); |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 102 | #else |
| 103 | // TODO(port) Need to handle loading gears on non-Windows platforms |
| 104 | NOTIMPLEMENTED(); |
| 105 | #endif |
| [email protected] | 8ee8189e | 2008-10-08 19:35:21 | [diff] [blame] | 106 | } |
| 107 | |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | ChildProcessFactory<RenderProcess> factory; |
| 109 | return ChildProcess::GlobalInit(channel_name, &factory); |
| 110 | } |
| 111 | |
| 112 | // static |
| 113 | void RenderProcess::GlobalCleanup() { |
| 114 | ChildProcess::GlobalCleanup(); |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // static |
| 118 | bool RenderProcess::ShouldLoadPluginsInProcess() { |
| 119 | return load_plugins_in_process_; |
| 120 | } |
| 121 | |
| 122 | // static |
| [email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 123 | base::SharedMemory* RenderProcess::AllocSharedMemory(size_t size) { |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | self()->clearer_factory_.RevokeAll(); |
| 125 | |
| [email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 126 | base::SharedMemory* mem = self()->GetSharedMemFromCache(size); |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | if (mem) |
| 128 | return mem; |
| 129 | |
| 130 | // Round-up size to allocation granularity |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 131 | size_t allocation_granularity = base::SysInfo::VMAllocationGranularity(); |
| 132 | size = size / allocation_granularity + 1; |
| 133 | size = size * allocation_granularity; |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | |
| [email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 135 | mem = new base::SharedMemory(); |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | if (!mem) |
| 137 | return NULL; |
| 138 | if (!mem->Create(L"", false, true, size)) { |
| 139 | delete mem; |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | return mem; |
| 144 | } |
| 145 | |
| 146 | // static |
| [email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 147 | void RenderProcess::FreeSharedMemory(base::SharedMemory* mem) { |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | if (self()->PutSharedMemInCache(mem)) { |
| 149 | self()->ScheduleCacheClearer(); |
| 150 | return; |
| 151 | } |
| 152 | DeleteSharedMem(mem); |
| 153 | } |
| 154 | |
| 155 | // static |
| [email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 156 | void RenderProcess::DeleteSharedMem(base::SharedMemory* mem) { |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | delete mem; |
| 158 | } |
| 159 | |
| [email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 160 | base::SharedMemory* RenderProcess::GetSharedMemFromCache(size_t size) { |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | // look for a cached object that is suitable for the requested size. |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 162 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
| [email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 163 | base::SharedMemory* mem = shared_mem_cache_[i]; |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | if (mem && mem->max_size() >= size) { |
| 165 | shared_mem_cache_[i] = NULL; |
| 166 | return mem; |
| 167 | } |
| 168 | } |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| [email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 172 | bool RenderProcess::PutSharedMemInCache(base::SharedMemory* mem) { |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 173 | // simple algorithm: |
| 174 | // - look for an empty slot to store mem, or |
| 175 | // - if full, then replace any existing cache entry that is smaller than the |
| 176 | // given shared memory object. |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 177 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | if (!shared_mem_cache_[i]) { |
| 179 | shared_mem_cache_[i] = mem; |
| 180 | return true; |
| 181 | } |
| 182 | } |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 183 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
| [email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 184 | base::SharedMemory* cached_mem = shared_mem_cache_[i]; |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | if (cached_mem->max_size() < mem->max_size()) { |
| 186 | shared_mem_cache_[i] = mem; |
| 187 | DeleteSharedMem(cached_mem); |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | void RenderProcess::ClearSharedMemCache() { |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame^] | 195 | for (size_t i = 0; i < arraysize(shared_mem_cache_); ++i) { |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | if (shared_mem_cache_[i]) { |
| 197 | DeleteSharedMem(shared_mem_cache_[i]); |
| 198 | shared_mem_cache_[i] = NULL; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void RenderProcess::ScheduleCacheClearer() { |
| 204 | // If we already have a deferred clearer, then revoke it so we effectively |
| 205 | // delay cache clearing until idle for our desired interval. |
| 206 | clearer_factory_.RevokeAll(); |
| 207 | |
| 208 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 209 | clearer_factory_.NewRunnableMethod(&RenderProcess::ClearSharedMemCache), |
| 210 | 5000 /* 5 seconds */); |
| 211 | } |
| 212 | |
| 213 | void RenderProcess::Cleanup() { |
| 214 | #ifndef NDEBUG |
| 215 | // log important leaked objects |
| 216 | webkit_glue::CheckForLeaks(); |
| 217 | #endif |
| 218 | } |