| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "chrome/renderer/chrome_render_process_observer.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/metrics/histogram.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/process_util.h" |
| 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/render_messages.h" |
| 15 #include "content/common/view_messages.h" |
| 16 #include "content/renderer/render_thread.h" |
| 17 #include "content/renderer/render_view.h" |
| 18 #include "content/renderer/render_view_visitor.h" |
| 19 #include "crypto/nss_util.h" |
| 20 #include "third_party/sqlite/sqlite3.h" |
| 21 #include "third_party/tcmalloc/chromium/src/google/malloc_extension.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCache.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCrossOriginPreflig
htResultCache.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFontCache.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 27 #include "v8/include/v8.h" |
| 28 |
| 29 #if defined(OS_WIN) |
| 30 #include "app/win/iat_patch_function.h" |
| 31 #endif |
| 32 |
| 33 using WebKit::WebCache; |
| 34 using WebKit::WebCrossOriginPreflightResultCache; |
| 35 using WebKit::WebFontCache; |
| 36 |
| 37 namespace { |
| 38 |
| 39 #if defined(OS_WIN) |
| 40 |
| 41 static app::win::IATPatchFunction g_iat_patch_createdca; |
| 42 HDC WINAPI CreateDCAPatch(LPCSTR driver_name, |
| 43 LPCSTR device_name, |
| 44 LPCSTR output, |
| 45 const void* init_data) { |
| 46 DCHECK(std::string("DISPLAY") == std::string(driver_name)); |
| 47 DCHECK(!device_name); |
| 48 DCHECK(!output); |
| 49 DCHECK(!init_data); |
| 50 |
| 51 // CreateDC fails behind the sandbox, but not CreateCompatibleDC. |
| 52 return CreateCompatibleDC(NULL); |
| 53 } |
| 54 |
| 55 static app::win::IATPatchFunction g_iat_patch_get_font_data; |
| 56 DWORD WINAPI GetFontDataPatch(HDC hdc, |
| 57 DWORD table, |
| 58 DWORD offset, |
| 59 LPVOID buffer, |
| 60 DWORD length) { |
| 61 int rv = GetFontData(hdc, table, offset, buffer, length); |
| 62 if (rv == GDI_ERROR && hdc) { |
| 63 HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT)); |
| 64 |
| 65 LOGFONT logfont; |
| 66 if (GetObject(font, sizeof(LOGFONT), &logfont)) { |
| 67 std::vector<char> font_data; |
| 68 if (RenderThread::current()->Send(new ViewHostMsg_PreCacheFont(logfont))) |
| 69 rv = GetFontData(hdc, table, offset, buffer, length); |
| 70 } |
| 71 } |
| 72 return rv; |
| 73 } |
| 74 |
| 75 #endif |
| 76 |
| 77 class RenderViewContentSettingsSetter : public RenderViewVisitor { |
| 78 public: |
| 79 RenderViewContentSettingsSetter(const GURL& url, |
| 80 const ContentSettings& content_settings) |
| 81 : url_(url), |
| 82 content_settings_(content_settings) { |
| 83 } |
| 84 |
| 85 virtual bool Visit(RenderView* render_view) { |
| 86 if (GURL(render_view->webview()->mainFrame()->url()) == url_) |
| 87 render_view->SetContentSettings(content_settings_); |
| 88 return true; |
| 89 } |
| 90 |
| 91 private: |
| 92 GURL url_; |
| 93 ContentSettings content_settings_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(RenderViewContentSettingsSetter); |
| 96 }; |
| 97 |
| 98 } // namespace |
| 99 |
| 100 bool ChromeRenderProcessObserver::is_incognito_process_ = false; |
| 101 |
| 102 ChromeRenderProcessObserver::ChromeRenderProcessObserver() { |
| 103 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 104 |
| 105 if (command_line.HasSwitch(switches::kEnableWatchdog)) { |
| 106 // TODO(JAR): Need to implement renderer IO msgloop watchdog. |
| 107 } |
| 108 |
| 109 if (command_line.HasSwitch(switches::kDumpHistogramsOnExit)) { |
| 110 base::StatisticsRecorder::set_dump_on_exit(true); |
| 111 } |
| 112 |
| 113 #if defined(OS_WIN) |
| 114 // Need to patch a few functions for font loading to work correctly. |
| 115 FilePath pdf; |
| 116 if (PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf) && |
| 117 file_util::PathExists(pdf)) { |
| 118 g_iat_patch_createdca.Patch( |
| 119 pdf.value().c_str(), "gdi32.dll", "CreateDCA", CreateDCAPatch); |
| 120 g_iat_patch_get_font_data.Patch( |
| 121 pdf.value().c_str(), "gdi32.dll", "GetFontData", GetFontDataPatch); |
| 122 } |
| 123 #endif |
| 124 |
| 125 #if defined(OS_LINUX) |
| 126 // Remoting requires NSS to function properly. |
| 127 if (!command_line.HasSwitch(switches::kSingleProcess) && |
| 128 command_line.HasSwitch(switches::kEnableRemoting)) { |
| 129 #if defined(USE_NSS) |
| 130 // We are going to fork to engage the sandbox and we have not loaded |
| 131 // any security modules so it is safe to disable the fork check in NSS. |
| 132 crypto::DisableNSSForkCheck(); |
| 133 crypto::ForceNSSNoDBInit(); |
| 134 crypto::EnsureNSSInit(); |
| 135 #else |
| 136 // TODO(bulach): implement openssl support. |
| 137 NOTREACHED() << "Remoting is not supported for openssl"; |
| 138 #endif |
| 139 } |
| 140 #endif |
| 141 } |
| 142 |
| 143 ChromeRenderProcessObserver::~ChromeRenderProcessObserver() { |
| 144 } |
| 145 |
| 146 bool ChromeRenderProcessObserver::OnControlMessageReceived( |
| 147 const IPC::Message& message) { |
| 148 bool handled = true; |
| 149 IPC_BEGIN_MESSAGE_MAP(ChromeRenderProcessObserver, message) |
| 150 IPC_MESSAGE_HANDLER(ViewMsg_SetIsIncognitoProcess, OnSetIsIncognitoProcess) |
| 151 IPC_MESSAGE_HANDLER(ViewMsg_SetContentSettingsForCurrentURL, |
| 152 OnSetContentSettingsForCurrentURL) |
| 153 IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities) |
| 154 IPC_MESSAGE_HANDLER(ViewMsg_ClearCache, OnClearCache) |
| 155 #if defined(USE_TCMALLOC) |
| 156 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererTcmalloc, OnGetRendererTcmalloc) |
| 157 #endif |
| 158 IPC_MESSAGE_HANDLER(ViewMsg_GetV8HeapStats, OnGetV8HeapStats) |
| 159 IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats, OnGetCacheResourceStats) |
| 160 IPC_MESSAGE_HANDLER(ViewMsg_PurgeMemory, OnPurgeMemory) |
| 161 IPC_MESSAGE_UNHANDLED(handled = false) |
| 162 IPC_END_MESSAGE_MAP() |
| 163 return handled; |
| 164 } |
| 165 |
| 166 void ChromeRenderProcessObserver::OnSetIsIncognitoProcess( |
| 167 bool is_incognito_process) { |
| 168 is_incognito_process_ = is_incognito_process; |
| 169 } |
| 170 |
| 171 void ChromeRenderProcessObserver::OnSetContentSettingsForCurrentURL( |
| 172 const GURL& url, |
| 173 const ContentSettings& content_settings) { |
| 174 RenderViewContentSettingsSetter setter(url, content_settings); |
| 175 RenderView::ForEach(&setter); |
| 176 } |
| 177 |
| 178 void ChromeRenderProcessObserver::OnSetCacheCapacities(size_t min_dead_capacity, |
| 179 size_t max_dead_capacity, |
| 180 size_t capacity) { |
| 181 WebCache::setCapacities( |
| 182 min_dead_capacity, max_dead_capacity, capacity); |
| 183 } |
| 184 |
| 185 void ChromeRenderProcessObserver::OnClearCache() { |
| 186 WebCache::clear(); |
| 187 } |
| 188 |
| 189 void ChromeRenderProcessObserver::OnGetCacheResourceStats() { |
| 190 WebCache::ResourceTypeStats stats; |
| 191 WebCache::getResourceTypeStats(&stats); |
| 192 Send(new ViewHostMsg_ResourceTypeStats(stats)); |
| 193 } |
| 194 |
| 195 #if defined(USE_TCMALLOC) |
| 196 void ChromeRenderProcessObserver::OnGetRendererTcmalloc() { |
| 197 std::string result; |
| 198 char buffer[1024 * 32]; |
| 199 base::ProcessId pid = base::GetCurrentProcId(); |
| 200 MallocExtension::instance()->GetStats(buffer, sizeof(buffer)); |
| 201 result.append(buffer); |
| 202 Send(new ViewHostMsg_RendererTcmalloc(pid, result)); |
| 203 } |
| 204 #endif |
| 205 |
| 206 void ChromeRenderProcessObserver::OnGetV8HeapStats() { |
| 207 v8::HeapStatistics heap_stats; |
| 208 v8::V8::GetHeapStatistics(&heap_stats); |
| 209 Send(new ViewHostMsg_V8HeapStats(heap_stats.total_heap_size(), |
| 210 heap_stats.used_heap_size())); |
| 211 } |
| 212 |
| 213 void ChromeRenderProcessObserver::OnPurgeMemory() { |
| 214 // Clear the object cache (as much as possible; some live objects cannot be |
| 215 // freed). |
| 216 WebCache::clear(); |
| 217 |
| 218 // Clear the font/glyph cache. |
| 219 WebFontCache::clear(); |
| 220 |
| 221 // Clear the Cross-Origin Preflight cache. |
| 222 WebCrossOriginPreflightResultCache::clear(); |
| 223 |
| 224 // Release all freeable memory from the SQLite process-global page cache (a |
| 225 // low-level object which backs the Connection-specific page caches). |
| 226 while (sqlite3_release_memory(std::numeric_limits<int>::max()) > 0) { |
| 227 } |
| 228 |
| 229 // Repeatedly call the V8 idle notification until it returns true ("nothing |
| 230 // more to free"). Note that it makes more sense to do this than to implement |
| 231 // a new "delete everything" pass because object references make it difficult |
| 232 // to free everything possible in just one pass. |
| 233 while (!v8::V8::IdleNotification()) { |
| 234 } |
| 235 |
| 236 #if (defined(OS_WIN) || defined(OS_LINUX)) && defined(USE_TCMALLOC) |
| 237 // Tell tcmalloc to release any free pages it's still holding. |
| 238 MallocExtension::instance()->ReleaseFreeMemory(); |
| 239 #endif |
| 240 } |
| OLD | NEW |