1 // Copyright (c) 2012 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.
5 #include "chrome/renderer/chrome_render_process_observer.h"
10 #include "base/allocator/allocator_extension.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/files/file_util.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/metrics/field_trial.h"
17 #include "base/metrics/histogram.h"
18 #include "base/metrics/statistics_recorder.h"
19 #include "base/native_library.h"
20 #include "base/path_service.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/threading/platform_thread.h"
23 #include "chrome/common/child_process_logging.h"
24 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/net/net_resource_provider.h"
27 #include "chrome/common/render_messages.h"
28 #include "chrome/common/url_constants.h"
29 #include "chrome/common/variations/variations_util.h"
30 #include "chrome/renderer/content_settings_observer.h"
31 #include "chrome/renderer/security_filter_peer.h"
32 #include "content/public/child/resource_dispatcher_delegate.h"
33 #include "content/public/renderer/render_thread.h"
34 #include "content/public/renderer/render_view.h"
35 #include "content/public/renderer/render_view_visitor.h"
36 #include "crypto/nss_util.h"
37 #include "net/base/net_errors.h"
38 #include "net/base/net_module.h"
39 #include "third_party/WebKit/public/web/WebCache.h"
40 #include "third_party/WebKit/public/web/WebDocument.h"
41 #include "third_party/WebKit/public/web/WebFrame.h"
42 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
43 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
44 #include "third_party/WebKit/public/web/WebView.h"
47 #include "base/win/iat_patch_function.h"
50 #if defined(ENABLE_EXTENSIONS)
51 #include "chrome/renderer/extensions/extension_localization_peer.h"
54 using blink::WebCache;
55 using blink::WebRuntimeFeatures;
56 using blink::WebSecurityPolicy;
57 using blink::WebString;
58 using content::RenderThread;
62 const int kCacheStatsDelayMS = 2000;
64 class RendererResourceDelegate : public content::ResourceDispatcherDelegate {
66 RendererResourceDelegate()
67 : weak_factory_(this) {
70 virtual content::RequestPeer* OnRequestComplete(
71 content::RequestPeer* current_peer,
72 content::ResourceType resource_type,
73 int error_code) OVERRIDE {
74 // Update the browser about our cache.
75 // Rate limit informing the host of our cache stats.
76 if (!weak_factory_.HasWeakPtrs()) {
77 base::MessageLoop::current()->PostDelayedTask(
79 base::Bind(&RendererResourceDelegate::InformHostOfCacheStats,
80 weak_factory_.GetWeakPtr()),
81 base::TimeDelta::FromMilliseconds(kCacheStatsDelayMS));
84 if (error_code == net::ERR_ABORTED) {
88 // Resource canceled with a specific error are filtered.
89 return SecurityFilterPeer::CreateSecurityFilterPeerForDeniedRequest(
90 resource_type, current_peer, error_code);
93 virtual content::RequestPeer* OnReceivedResponse(
94 content::RequestPeer* current_peer,
95 const std::string& mime_type,
96 const GURL& url) OVERRIDE {
97 #if defined(ENABLE_EXTENSIONS)
98 return ExtensionLocalizationPeer::CreateExtensionLocalizationPeer(
99 current_peer, RenderThread::Get(), mime_type, url);
106 void InformHostOfCacheStats() {
107 WebCache::UsageStats stats;
108 WebCache::getUsageStats(&stats);
109 RenderThread::Get()->Send(new ChromeViewHostMsg_UpdatedCacheStats(stats));
112 base::WeakPtrFactory<RendererResourceDelegate> weak_factory_;
114 DISALLOW_COPY_AND_ASSIGN(RendererResourceDelegate);
118 static base::win::IATPatchFunction g_iat_patch_createdca;
119 HDC WINAPI CreateDCAPatch(LPCSTR driver_name,
122 const void* init_data) {
123 DCHECK(std::string("DISPLAY") == std::string(driver_name));
124 DCHECK(!device_name);
128 // CreateDC fails behind the sandbox, but not CreateCompatibleDC.
129 return CreateCompatibleDC(NULL);
132 static base::win::IATPatchFunction g_iat_patch_get_font_data;
133 DWORD WINAPI GetFontDataPatch(HDC hdc,
138 int rv = GetFontData(hdc, table, offset, buffer, length);
139 if (rv == GDI_ERROR && hdc) {
140 HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT));
143 if (GetObject(font, sizeof(LOGFONT), &logfont)) {
144 std::vector<char> font_data;
145 RenderThread::Get()->PreCacheFont(logfont);
146 rv = GetFontData(hdc, table, offset, buffer, length);
147 RenderThread::Get()->ReleaseCachedFonts();
154 static const int kWaitForWorkersStatsTimeoutMS = 20;
156 class HeapStatisticsCollector {
158 HeapStatisticsCollector() : round_id_(0) {}
160 void InitiateCollection();
161 static HeapStatisticsCollector* Instance();
164 void CollectOnWorkerThread(scoped_refptr<base::TaskRunner> master,
166 void ReceiveStats(int round_id, size_t total_size, size_t used_size);
167 void SendStatsToBrowser(int round_id);
175 HeapStatisticsCollector* HeapStatisticsCollector::Instance() {
176 CR_DEFINE_STATIC_LOCAL(HeapStatisticsCollector, instance, ());
180 void HeapStatisticsCollector::InitiateCollection() {
181 v8::HeapStatistics heap_stats;
182 v8::Isolate::GetCurrent()->GetHeapStatistics(&heap_stats);
183 total_bytes_ = heap_stats.total_heap_size();
184 used_bytes_ = heap_stats.used_heap_size();
185 base::Closure collect = base::Bind(
186 &HeapStatisticsCollector::CollectOnWorkerThread,
187 base::Unretained(this),
188 base::MessageLoopProxy::current(),
190 workers_to_go_ = RenderThread::Get()->PostTaskToAllWebWorkers(collect);
191 if (workers_to_go_) {
192 // The guard task to send out partial stats
193 // in case some workers are not responsive.
194 base::MessageLoopProxy::current()->PostDelayedTask(
196 base::Bind(&HeapStatisticsCollector::SendStatsToBrowser,
197 base::Unretained(this),
199 base::TimeDelta::FromMilliseconds(kWaitForWorkersStatsTimeoutMS));
201 // No worker threads so just send out the main thread data right away.
202 SendStatsToBrowser(round_id_);
206 void HeapStatisticsCollector::CollectOnWorkerThread(
207 scoped_refptr<base::TaskRunner> master,
210 size_t total_bytes = 0;
211 size_t used_bytes = 0;
212 v8::Isolate* isolate = v8::Isolate::GetCurrent();
214 v8::HeapStatistics heap_stats;
215 isolate->GetHeapStatistics(&heap_stats);
216 total_bytes = heap_stats.total_heap_size();
217 used_bytes = heap_stats.used_heap_size();
221 base::Bind(&HeapStatisticsCollector::ReceiveStats,
222 base::Unretained(this),
228 void HeapStatisticsCollector::ReceiveStats(int round_id,
231 if (round_id != round_id_)
233 total_bytes_ += total_bytes;
234 used_bytes_ += used_bytes;
235 if (!--workers_to_go_)
236 SendStatsToBrowser(round_id);
239 void HeapStatisticsCollector::SendStatsToBrowser(int round_id) {
240 if (round_id != round_id_)
242 // TODO(alph): Do caching heap stats and use the cache if we haven't got
243 // reply from a worker.
244 // Currently a busy worker stats are not counted.
245 RenderThread::Get()->Send(new ChromeViewHostMsg_V8HeapStats(
246 total_bytes_, used_bytes_));
252 bool ChromeRenderProcessObserver::is_incognito_process_ = false;
254 ChromeRenderProcessObserver::ChromeRenderProcessObserver(
255 ChromeContentRendererClient* client)
257 webkit_initialized_(false) {
258 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
260 #if defined(ENABLE_AUTOFILL_DIALOG)
261 WebRuntimeFeatures::enableRequestAutocomplete(true);
264 if (command_line.HasSwitch(switches::kEnableShowModalDialog))
265 WebRuntimeFeatures::enableShowModalDialog(true);
267 if (command_line.HasSwitch(switches::kJavaScriptHarmony)) {
268 std::string flag("--harmony");
269 v8::V8::SetFlagsFromString(flag.c_str(), static_cast<int>(flag.size()));
272 RenderThread* thread = RenderThread::Get();
273 resource_delegate_.reset(new RendererResourceDelegate());
274 thread->SetResourceDispatcherDelegate(resource_delegate_.get());
276 // Configure modules that need access to resources.
277 net::NetModule::SetResourceProvider(chrome_common_net::NetResourceProvider);
280 // Need to patch a few functions for font loading to work correctly.
282 if (PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf) &&
283 base::PathExists(pdf)) {
284 g_iat_patch_createdca.Patch(
285 pdf.value().c_str(), "gdi32.dll", "CreateDCA", CreateDCAPatch);
286 g_iat_patch_get_font_data.Patch(
287 pdf.value().c_str(), "gdi32.dll", "GetFontData", GetFontDataPatch);
291 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(USE_NSS)
292 // On platforms where we use system NSS shared libraries,
293 // initialize NSS now because it won't be able to load the .so's
294 // after we engage the sandbox.
295 if (!command_line.HasSwitch(switches::kSingleProcess))
296 crypto::InitNSSSafely();
297 #elif defined(OS_WIN)
298 // crypt32.dll is used to decode X509 certificates for Chromoting.
299 // Only load this library when the feature is enabled.
300 base::LoadNativeLibrary(base::FilePath(L"crypt32.dll"), NULL);
302 // Setup initial set of crash dump data for Field Trials in this renderer.
303 chrome_variations::SetChildProcessLoggingVariationList();
306 ChromeRenderProcessObserver::~ChromeRenderProcessObserver() {
309 bool ChromeRenderProcessObserver::OnControlMessageReceived(
310 const IPC::Message& message) {
312 IPC_BEGIN_MESSAGE_MAP(ChromeRenderProcessObserver, message)
313 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetIsIncognitoProcess,
314 OnSetIsIncognitoProcess)
315 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetFieldTrialGroup, OnSetFieldTrialGroup)
316 IPC_MESSAGE_HANDLER(ChromeViewMsg_GetV8HeapStats, OnGetV8HeapStats)
317 IPC_MESSAGE_HANDLER(ChromeViewMsg_GetCacheResourceStats,
318 OnGetCacheResourceStats)
319 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetContentSettingRules,
320 OnSetContentSettingRules)
321 IPC_MESSAGE_UNHANDLED(handled = false)
322 IPC_END_MESSAGE_MAP()
326 void ChromeRenderProcessObserver::WebKitInitialized() {
327 webkit_initialized_ = true;
328 // chrome-native: is a scheme used for placeholder navigations that allow
329 // UIs to be drawn with platform native widgets instead of HTML. These pages
330 // should not be accessible, and should also be treated as empty documents
331 // that can commit synchronously. No code should be runnable in these pages,
332 // so it should not need to access anything nor should it allow javascript
333 // URLs since it should never be visible to the user.
334 WebString native_scheme(base::ASCIIToUTF16(chrome::kChromeNativeScheme));
335 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(native_scheme);
336 WebSecurityPolicy::registerURLSchemeAsEmptyDocument(native_scheme);
337 WebSecurityPolicy::registerURLSchemeAsNoAccess(native_scheme);
338 WebSecurityPolicy::registerURLSchemeAsNotAllowingJavascriptURLs(
342 void ChromeRenderProcessObserver::OnRenderProcessShutdown() {
343 webkit_initialized_ = false;
346 void ChromeRenderProcessObserver::OnSetIsIncognitoProcess(
347 bool is_incognito_process) {
348 is_incognito_process_ = is_incognito_process;
351 void ChromeRenderProcessObserver::OnSetContentSettingRules(
352 const RendererContentSettingRules& rules) {
353 content_setting_rules_ = rules;
356 void ChromeRenderProcessObserver::OnGetCacheResourceStats() {
357 WebCache::ResourceTypeStats stats;
358 if (webkit_initialized_)
359 WebCache::getResourceTypeStats(&stats);
360 RenderThread::Get()->Send(new ChromeViewHostMsg_ResourceTypeStats(stats));
363 void ChromeRenderProcessObserver::OnSetFieldTrialGroup(
364 const std::string& field_trial_name,
365 const std::string& group_name) {
366 base::FieldTrial* trial =
367 base::FieldTrialList::CreateFieldTrial(field_trial_name, group_name);
368 // TODO(mef): Remove this check after the investigation of 359406 is complete.
369 CHECK(trial) << field_trial_name << ":" << group_name;
370 // Ensure the trial is marked as "used" by calling group() on it. This is
371 // needed to ensure the trial is properly reported in renderer crash reports.
373 chrome_variations::SetChildProcessLoggingVariationList();
376 void ChromeRenderProcessObserver::OnGetV8HeapStats() {
377 HeapStatisticsCollector::Instance()->InitiateCollection();
380 const RendererContentSettingRules*
381 ChromeRenderProcessObserver::content_setting_rules() const {
382 return &content_setting_rules_;