Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / chrome / renderer / chrome_content_renderer_client.cc
blobad8fee16a7ce064a1fdf89be4f3ec1648b929ef2
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_content_renderer_client.h"
7 #include "base/command_line.h"
8 #include "base/debug/crash_logging.h"
9 #include "base/logging.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/metrics/histogram.h"
12 #include "base/metrics/user_metrics_action.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/chrome_version_info.h"
20 #include "chrome/common/crash_keys.h"
21 #include "chrome/common/extensions/extension_metrics.h"
22 #include "chrome/common/localized_error.h"
23 #include "chrome/common/pepper_permission_util.h"
24 #include "chrome/common/render_messages.h"
25 #include "chrome/common/url_constants.h"
26 #include "chrome/grit/generated_resources.h"
27 #include "chrome/grit/locale_settings.h"
28 #include "chrome/grit/renderer_resources.h"
29 #include "chrome/renderer/benchmarking_extension.h"
30 #include "chrome/renderer/chrome_render_frame_observer.h"
31 #include "chrome/renderer/chrome_render_process_observer.h"
32 #include "chrome/renderer/chrome_render_view_observer.h"
33 #include "chrome/renderer/content_settings_observer.h"
34 #include "chrome/renderer/external_extension.h"
35 #include "chrome/renderer/loadtimes_extension_bindings.h"
36 #include "chrome/renderer/media/chrome_key_systems.h"
37 #include "chrome/renderer/net/net_error_helper.h"
38 #include "chrome/renderer/net_benchmarking_extension.h"
39 #include "chrome/renderer/page_load_histograms.h"
40 #include "chrome/renderer/pepper/pepper_helper.h"
41 #include "chrome/renderer/plugins/chrome_plugin_placeholder.h"
42 #include "chrome/renderer/plugins/plugin_preroller.h"
43 #include "chrome/renderer/plugins/plugin_uma.h"
44 #include "chrome/renderer/plugins/shadow_dom_plugin_placeholder.h"
45 #include "chrome/renderer/prerender/prerender_dispatcher.h"
46 #include "chrome/renderer/prerender/prerender_helper.h"
47 #include "chrome/renderer/prerender/prerender_media_load_deferrer.h"
48 #include "chrome/renderer/prerender/prerenderer_client.h"
49 #include "chrome/renderer/safe_browsing/malware_dom_details.h"
50 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h"
51 #include "chrome/renderer/searchbox/search_bouncer.h"
52 #include "chrome/renderer/searchbox/searchbox.h"
53 #include "chrome/renderer/searchbox/searchbox_extension.h"
54 #include "chrome/renderer/tts_dispatcher.h"
55 #include "chrome/renderer/worker_content_settings_client_proxy.h"
56 #include "components/autofill/content/renderer/autofill_agent.h"
57 #include "components/autofill/content/renderer/password_autofill_agent.h"
58 #include "components/autofill/content/renderer/password_generation_agent.h"
59 #include "components/content_settings/core/common/content_settings_pattern.h"
60 #include "components/dom_distiller/core/url_constants.h"
61 #include "components/nacl/renderer/ppb_nacl_private.h"
62 #include "components/nacl/renderer/ppb_nacl_private_impl.h"
63 #include "components/network_hints/renderer/prescient_networking_dispatcher.h"
64 #include "components/password_manager/content/renderer/credential_manager_client.h"
65 #include "components/pdf/renderer/ppb_pdf_impl.h"
66 #include "components/plugins/renderer/mobile_youtube_plugin.h"
67 #include "components/signin/core/common/profile_management_switches.h"
68 #include "components/visitedlink/renderer/visitedlink_slave.h"
69 #include "components/web_cache/renderer/web_cache_render_process_observer.h"
70 #include "content/public/common/content_constants.h"
71 #include "content/public/renderer/plugin_instance_throttler.h"
72 #include "content/public/renderer/render_frame.h"
73 #include "content/public/renderer/render_thread.h"
74 #include "content/public/renderer/render_view.h"
75 #include "content/public/renderer/render_view_visitor.h"
76 #include "extensions/common/constants.h"
77 #include "ipc/ipc_sync_channel.h"
78 #include "net/base/net_errors.h"
79 #include "ppapi/c/private/ppb_pdf.h"
80 #include "ppapi/shared_impl/ppapi_switches.h"
81 #include "third_party/WebKit/public/platform/WebURL.h"
82 #include "third_party/WebKit/public/platform/WebURLError.h"
83 #include "third_party/WebKit/public/platform/WebURLRequest.h"
84 #include "third_party/WebKit/public/web/WebCache.h"
85 #include "third_party/WebKit/public/web/WebDataSource.h"
86 #include "third_party/WebKit/public/web/WebDocument.h"
87 #include "third_party/WebKit/public/web/WebElement.h"
88 #include "third_party/WebKit/public/web/WebLocalFrame.h"
89 #include "third_party/WebKit/public/web/WebPluginContainer.h"
90 #include "third_party/WebKit/public/web/WebPluginParams.h"
91 #include "third_party/WebKit/public/web/WebPluginPlaceholder.h"
92 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
93 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
94 #include "ui/base/l10n/l10n_util.h"
95 #include "ui/base/layout.h"
96 #include "ui/base/resource/resource_bundle.h"
97 #include "ui/base/webui/jstemplate_builder.h"
98 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
100 #if !defined(DISABLE_NACL)
101 #include "components/nacl/common/nacl_constants.h"
102 #include "components/nacl/renderer/nacl_helper.h"
103 #endif
105 #if defined(ENABLE_EXTENSIONS)
106 #include "chrome/common/extensions/chrome_extensions_client.h"
107 #include "chrome/common/extensions/extension_process_policy.h"
108 #include "chrome/renderer/extensions/chrome_extensions_dispatcher_delegate.h"
109 #include "chrome/renderer/extensions/chrome_extensions_renderer_client.h"
110 #include "chrome/renderer/extensions/renderer_permissions_policy_delegate.h"
111 #include "chrome/renderer/extensions/resource_request_policy.h"
112 #include "chrome/renderer/media/cast_ipc_dispatcher.h"
113 #include "extensions/common/extension.h"
114 #include "extensions/common/extension_set.h"
115 #include "extensions/common/extension_urls.h"
116 #include "extensions/common/switches.h"
117 #include "extensions/renderer/dispatcher.h"
118 #include "extensions/renderer/extension_frame_helper.h"
119 #include "extensions/renderer/extension_helper.h"
120 #include "extensions/renderer/extensions_render_frame_observer.h"
121 #include "extensions/renderer/guest_view/extensions_guest_view_container.h"
122 #include "extensions/renderer/guest_view/guest_view_container.h"
123 #include "extensions/renderer/guest_view/mime_handler_view/mime_handler_view_container.h"
124 #include "extensions/renderer/script_context.h"
125 #endif
127 #if defined(ENABLE_IPC_FUZZER)
128 #include "chrome/common/external_ipc_dumper.h"
129 #endif
131 #if defined(ENABLE_PRINTING)
132 #include "chrome/renderer/printing/chrome_print_web_view_helper_delegate.h"
133 #include "components/printing/renderer/print_web_view_helper.h"
134 #endif
136 #if defined(ENABLE_PRINT_PREVIEW)
137 #include "chrome/renderer/pepper/chrome_pdf_print_client.h"
138 #endif
140 #if defined(ENABLE_SPELLCHECK)
141 #include "chrome/renderer/spellchecker/spellcheck.h"
142 #include "chrome/renderer/spellchecker/spellcheck_provider.h"
143 #endif
145 #if defined(ENABLE_WEBRTC)
146 #include "chrome/renderer/media/webrtc_logging_message_filter.h"
147 #endif
149 #if defined(OS_WIN)
150 #include "chrome_elf/blacklist/blacklist.h"
151 #endif
153 using autofill::AutofillAgent;
154 using autofill::PasswordAutofillAgent;
155 using autofill::PasswordGenerationAgent;
156 using base::ASCIIToUTF16;
157 using base::UserMetricsAction;
158 using content::PluginInstanceThrottler;
159 using content::RenderFrame;
160 using content::RenderThread;
161 using content::WebPluginInfo;
162 using extensions::Extension;
163 using blink::WebCache;
164 using blink::WebConsoleMessage;
165 using blink::WebDataSource;
166 using blink::WebDocument;
167 using blink::WebPlugin;
168 using blink::WebPluginParams;
169 using blink::WebSecurityOrigin;
170 using blink::WebSecurityPolicy;
171 using blink::WebString;
172 using blink::WebURL;
173 using blink::WebURLError;
174 using blink::WebURLRequest;
175 using blink::WebURLResponse;
176 using blink::WebVector;
178 namespace {
180 ChromeContentRendererClient* g_current_client;
182 // Whitelist PPAPI for Android Runtime for Chromium. (See crbug.com/383937)
183 #if defined(ENABLE_PLUGINS)
184 const char* const kPredefinedAllowedCameraDeviceOrigins[] = {
185 "6EAED1924DB611B6EEF2A664BD077BE7EAD33B8F",
186 "4EB74897CB187C7633357C2FE832E0AD6A44883A"
189 const char* const kPredefinedAllowedCompositorOrigins[] = {
190 "6EAED1924DB611B6EEF2A664BD077BE7EAD33B8F",
191 "4EB74897CB187C7633357C2FE832E0AD6A44883A"
193 #endif
195 void AppendParams(const std::vector<base::string16>& additional_names,
196 const std::vector<base::string16>& additional_values,
197 WebVector<WebString>* existing_names,
198 WebVector<WebString>* existing_values) {
199 DCHECK(additional_names.size() == additional_values.size());
200 DCHECK(existing_names->size() == existing_values->size());
202 size_t existing_size = existing_names->size();
203 size_t total_size = existing_size + additional_names.size();
205 WebVector<WebString> names(total_size);
206 WebVector<WebString> values(total_size);
208 for (size_t i = 0; i < existing_size; ++i) {
209 names[i] = (*existing_names)[i];
210 values[i] = (*existing_values)[i];
213 for (size_t i = 0; i < additional_names.size(); ++i) {
214 names[existing_size + i] = additional_names[i];
215 values[existing_size + i] = additional_values[i];
218 existing_names->swap(names);
219 existing_values->swap(values);
222 #if defined(ENABLE_SPELLCHECK)
223 class SpellCheckReplacer : public content::RenderViewVisitor {
224 public:
225 explicit SpellCheckReplacer(SpellCheck* spellcheck)
226 : spellcheck_(spellcheck) {}
227 bool Visit(content::RenderView* render_view) override;
229 private:
230 SpellCheck* spellcheck_; // New shared spellcheck for all views. Weak Ptr.
231 DISALLOW_COPY_AND_ASSIGN(SpellCheckReplacer);
234 bool SpellCheckReplacer::Visit(content::RenderView* render_view) {
235 SpellCheckProvider* provider = SpellCheckProvider::Get(render_view);
236 DCHECK(provider);
237 provider->set_spellcheck(spellcheck_);
238 return true;
240 #endif
242 // For certain sandboxed Pepper plugins, use the JavaScript Content Settings.
243 bool ShouldUseJavaScriptSettingForPlugin(const WebPluginInfo& plugin) {
244 if (plugin.type != WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS &&
245 plugin.type != WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS) {
246 return false;
249 #if !defined(DISABLE_NACL)
250 // Treat Native Client invocations like JavaScript.
251 if (plugin.name == ASCIIToUTF16(nacl::kNaClPluginName))
252 return true;
253 #endif
255 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
256 // Treat CDM invocations like JavaScript.
257 if (plugin.name == ASCIIToUTF16(kWidevineCdmDisplayName)) {
258 DCHECK(plugin.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS);
259 return true;
261 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
263 return false;
266 #if defined(ENABLE_EXTENSIONS)
267 void IsGuestViewApiAvailableToScriptContext(
268 bool* api_is_available,
269 extensions::ScriptContext* context) {
270 if (context->GetAvailability("guestViewInternal").is_available()) {
271 *api_is_available = true;
274 #endif
276 #if defined(ENABLE_PLUGINS)
277 // Presence of the poster param within plugin object tags.
278 // These numeric values are used in UMA logs; do not change them.
279 enum PosterParamPresence {
280 POSTER_PRESENCE_NO_PARAM_PPS_DISABLED = 0,
281 POSTER_PRESENCE_NO_PARAM_PPS_ENABLED = 1,
282 POSTER_PRESENCE_PARAM_EXISTS_PPS_DISABLED = 2,
283 POSTER_PRESENCE_PARAM_EXISTS_PPS_ENABLED = 3,
284 POSTER_PRESENCE_NUM_ITEMS
287 const char kPluginPowerSaverPosterParamPresenceHistogram[] =
288 "Plugin.PowerSaver.PosterParamPresence";
290 void RecordPosterParamPresence(PosterParamPresence presence) {
291 UMA_HISTOGRAM_ENUMERATION(kPluginPowerSaverPosterParamPresenceHistogram,
292 presence, POSTER_PRESENCE_NUM_ITEMS);
295 void TrackPosterParamPresence(const blink::WebPluginParams& params,
296 bool power_saver_enabled) {
297 DCHECK_EQ(params.attributeNames.size(), params.attributeValues.size());
299 for (size_t i = 0; i < params.attributeNames.size(); ++i) {
300 if (params.attributeNames[i].utf8() == "poster") {
301 if (power_saver_enabled)
302 RecordPosterParamPresence(POSTER_PRESENCE_PARAM_EXISTS_PPS_ENABLED);
303 else
304 RecordPosterParamPresence(POSTER_PRESENCE_PARAM_EXISTS_PPS_DISABLED);
306 return;
310 if (power_saver_enabled)
311 RecordPosterParamPresence(POSTER_PRESENCE_NO_PARAM_PPS_ENABLED);
312 else
313 RecordPosterParamPresence(POSTER_PRESENCE_NO_PARAM_PPS_DISABLED);
316 std::string GetPluginInstancePosterAttribute(
317 const blink::WebPluginParams& params) {
318 DCHECK_EQ(params.attributeNames.size(), params.attributeValues.size());
320 for (size_t i = 0; i < params.attributeNames.size(); ++i) {
321 if (params.attributeNames[i].utf8() == "poster" &&
322 !params.attributeValues[i].isEmpty()) {
323 return params.attributeValues[i].utf8();
326 return std::string();
328 #endif
330 #if defined(ENABLE_EXTENSIONS)
331 bool IsStandaloneExtensionProcess() {
332 return base::CommandLine::ForCurrentProcess()->HasSwitch(
333 extensions::switches::kExtensionProcess);
335 #endif
336 } // namespace
338 ChromeContentRendererClient::ChromeContentRendererClient() {
339 g_current_client = this;
341 #if defined(ENABLE_EXTENSIONS)
342 extensions::ExtensionsClient::Set(
343 extensions::ChromeExtensionsClient::GetInstance());
344 extensions::ExtensionsRendererClient::Set(
345 ChromeExtensionsRendererClient::GetInstance());
346 #endif
347 #if defined(ENABLE_PLUGINS)
348 for (size_t i = 0; i < arraysize(kPredefinedAllowedCameraDeviceOrigins); ++i)
349 allowed_camera_device_origins_.insert(
350 kPredefinedAllowedCameraDeviceOrigins[i]);
351 for (size_t i = 0; i < arraysize(kPredefinedAllowedCompositorOrigins); ++i)
352 allowed_compositor_origins_.insert(kPredefinedAllowedCompositorOrigins[i]);
353 #endif
356 ChromeContentRendererClient::~ChromeContentRendererClient() {
357 g_current_client = NULL;
360 void ChromeContentRendererClient::RenderThreadStarted() {
361 RenderThread* thread = RenderThread::Get();
363 chrome_observer_.reset(new ChromeRenderProcessObserver());
364 web_cache_observer_.reset(new web_cache::WebCacheRenderProcessObserver());
366 #if defined(ENABLE_EXTENSIONS)
367 extension_dispatcher_delegate_.reset(
368 new ChromeExtensionsDispatcherDelegate());
369 // ChromeRenderViewTest::SetUp() creates its own ExtensionDispatcher and
370 // injects it using SetExtensionDispatcher(). Don't overwrite it.
371 if (!extension_dispatcher_) {
372 extension_dispatcher_.reset(
373 new extensions::Dispatcher(extension_dispatcher_delegate_.get()));
375 permissions_policy_delegate_.reset(
376 new extensions::RendererPermissionsPolicyDelegate(
377 extension_dispatcher_.get()));
378 #endif
380 prescient_networking_dispatcher_.reset(
381 new network_hints::PrescientNetworkingDispatcher());
382 #if defined(ENABLE_SPELLCHECK)
383 // ChromeRenderViewTest::SetUp() creates a Spellcheck and injects it using
384 // SetSpellcheck(). Don't overwrite it.
385 if (!spellcheck_) {
386 spellcheck_.reset(new SpellCheck());
387 thread->AddObserver(spellcheck_.get());
389 #endif
390 visited_link_slave_.reset(new visitedlink::VisitedLinkSlave());
391 #if defined(FULL_SAFE_BROWSING)
392 phishing_classifier_.reset(safe_browsing::PhishingClassifierFilter::Create());
393 #endif
394 prerender_dispatcher_.reset(new prerender::PrerenderDispatcher());
395 #if defined(ENABLE_WEBRTC)
396 webrtc_logging_message_filter_ = new WebRtcLoggingMessageFilter(
397 RenderThread::Get()->GetIOMessageLoopProxy());
398 #endif
399 search_bouncer_.reset(new SearchBouncer());
401 thread->AddObserver(chrome_observer_.get());
402 thread->AddObserver(web_cache_observer_.get());
403 #if defined(ENABLE_EXTENSIONS)
404 thread->AddObserver(extension_dispatcher_.get());
405 #endif
406 #if defined(FULL_SAFE_BROWSING)
407 thread->AddObserver(phishing_classifier_.get());
408 #endif
409 thread->AddObserver(visited_link_slave_.get());
410 thread->AddObserver(prerender_dispatcher_.get());
411 thread->AddObserver(search_bouncer_.get());
413 #if defined(ENABLE_WEBRTC)
414 thread->AddFilter(webrtc_logging_message_filter_.get());
415 #endif
416 #if defined(ENABLE_EXTENSIONS)
417 thread->AddFilter(new CastIPCDispatcher(
418 RenderThread::Get()->GetIOMessageLoopProxy()));
419 #endif
421 thread->RegisterExtension(extensions_v8::ExternalExtension::Get());
422 thread->RegisterExtension(extensions_v8::LoadTimesExtension::Get());
424 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
425 if (command_line->HasSwitch(switches::kEnableBenchmarking))
426 thread->RegisterExtension(extensions_v8::BenchmarkingExtension::Get());
427 if (command_line->HasSwitch(switches::kEnableNetBenchmarking))
428 thread->RegisterExtension(extensions_v8::NetBenchmarkingExtension::Get());
429 if (command_line->HasSwitch(switches::kInstantProcess))
430 thread->RegisterExtension(extensions_v8::SearchBoxExtension::Get());
432 // chrome:, chrome-search:, chrome-devtools:, and chrome-distiller: pages
433 // should not be accessible by normal content, and should also be unable to
434 // script anything but themselves (to help limit the damage that a corrupt
435 // page could cause).
436 WebString chrome_ui_scheme(ASCIIToUTF16(content::kChromeUIScheme));
437 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(chrome_ui_scheme);
439 WebString chrome_search_scheme(ASCIIToUTF16(chrome::kChromeSearchScheme));
440 // The Instant process can only display the content but not read it. Other
441 // processes can't display it or read it.
442 if (!command_line->HasSwitch(switches::kInstantProcess))
443 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(chrome_search_scheme);
445 WebString dev_tools_scheme(ASCIIToUTF16(content::kChromeDevToolsScheme));
446 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(dev_tools_scheme);
448 WebString dom_distiller_scheme(
449 ASCIIToUTF16(dom_distiller::kDomDistillerScheme));
450 // TODO(nyquist): Add test to ensure this happens when the flag is set.
451 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(dom_distiller_scheme);
453 #if defined(OS_CHROMEOS)
454 WebString external_file_scheme(ASCIIToUTF16(content::kExternalFileScheme));
455 WebSecurityPolicy::registerURLSchemeAsLocal(external_file_scheme);
456 #endif
458 #if defined(ENABLE_IPC_FUZZER)
459 if (command_line->HasSwitch(switches::kIpcDumpDirectory)) {
460 base::FilePath dump_directory =
461 command_line->GetSwitchValuePath(switches::kIpcDumpDirectory);
462 IPC::ChannelProxy::OutgoingMessageFilter* filter =
463 LoadExternalIPCDumper(dump_directory);
464 thread->GetChannel()->set_outgoing_message_filter(filter);
466 #endif
468 // chrome: and chrome-search: pages should not be accessible by bookmarklets
469 // or javascript: URLs typed in the omnibox.
470 WebSecurityPolicy::registerURLSchemeAsNotAllowingJavascriptURLs(
471 chrome_ui_scheme);
472 WebSecurityPolicy::registerURLSchemeAsNotAllowingJavascriptURLs(
473 chrome_search_scheme);
475 // chrome:, chrome-search:, chrome-extension:, and chrome-extension-resource:
476 // resources shouldn't trigger insecure content warnings.
477 WebSecurityPolicy::registerURLSchemeAsSecure(chrome_ui_scheme);
478 WebSecurityPolicy::registerURLSchemeAsSecure(chrome_search_scheme);
480 WebString extension_scheme(ASCIIToUTF16(extensions::kExtensionScheme));
481 WebSecurityPolicy::registerURLSchemeAsSecure(extension_scheme);
483 WebString extension_resource_scheme(
484 ASCIIToUTF16(extensions::kExtensionResourceScheme));
485 WebSecurityPolicy::registerURLSchemeAsSecure(extension_resource_scheme);
487 // chrome:, chrome-extension:, chrome-extension-resource: resources should be
488 // allowed to receive CORS requests.
489 WebSecurityPolicy::registerURLSchemeAsCORSEnabled(chrome_ui_scheme);
490 WebSecurityPolicy::registerURLSchemeAsCORSEnabled(extension_scheme);
491 WebSecurityPolicy::registerURLSchemeAsCORSEnabled(extension_resource_scheme);
493 // chrome-extension: resources should bypass Content Security Policy checks
494 // when included in protected resources.
495 WebSecurityPolicy::registerURLSchemeAsBypassingContentSecurityPolicy(
496 extension_scheme);
497 WebSecurityPolicy::registerURLSchemeAsBypassingContentSecurityPolicy(
498 extension_resource_scheme);
500 #if defined(OS_WIN)
501 // Report if the renderer process has been patched by chrome_elf.
502 // TODO(csharp): Remove once the renderer is no longer getting
503 // patched this way.
504 if (blacklist::IsBlacklistInitialized())
505 UMA_HISTOGRAM_BOOLEAN("Blacklist.PatchedInRenderer", true);
506 #endif
507 #if defined(ENABLE_PRINT_PREVIEW)
508 pdf_print_client_.reset(new ChromePDFPrintClient());
509 pdf::PPB_PDF_Impl::SetPrintClient(pdf_print_client_.get());
510 #endif
513 void ChromeContentRendererClient::RenderFrameCreated(
514 content::RenderFrame* render_frame) {
515 new ChromeRenderFrameObserver(render_frame);
517 bool should_whitelist_for_content_settings =
518 base::CommandLine::ForCurrentProcess()->HasSwitch(
519 switches::kInstantProcess);
520 extensions::Dispatcher* ext_dispatcher = NULL;
521 #if defined(ENABLE_EXTENSIONS)
522 ext_dispatcher = extension_dispatcher_.get();
523 #endif
524 ContentSettingsObserver* content_settings = new ContentSettingsObserver(
525 render_frame, ext_dispatcher, should_whitelist_for_content_settings);
526 if (chrome_observer_.get()) {
527 content_settings->SetContentSettingRules(
528 chrome_observer_->content_setting_rules());
531 #if defined(ENABLE_EXTENSIONS)
532 new extensions::ExtensionsRenderFrameObserver(render_frame);
533 new extensions::ExtensionFrameHelper(render_frame, ext_dispatcher);
534 #endif
536 #if defined(ENABLE_PLUGINS)
537 new PepperHelper(render_frame);
538 #endif
540 #if !defined(DISABLE_NACL)
541 new nacl::NaClHelper(render_frame);
542 #endif
544 // TODO(jam): when the frame tree moves into content and parent() works at
545 // RenderFrame construction, simplify this by just checking parent().
546 if (render_frame->GetRenderView()->GetMainRenderFrame() != render_frame) {
547 // Avoid any race conditions from having the browser tell subframes that
548 // they're prerendering.
549 if (prerender::PrerenderHelper::IsPrerendering(
550 render_frame->GetRenderView()->GetMainRenderFrame())) {
551 new prerender::PrerenderHelper(render_frame);
555 if (render_frame->GetRenderView()->GetMainRenderFrame() == render_frame) {
556 // Only attach NetErrorHelper to the main frame, since only the main frame
557 // should get error pages.
558 new NetErrorHelper(render_frame);
561 PasswordGenerationAgent* password_generation_agent =
562 new PasswordGenerationAgent(render_frame);
563 PasswordAutofillAgent* password_autofill_agent =
564 new PasswordAutofillAgent(render_frame);
565 new AutofillAgent(render_frame, password_autofill_agent,
566 password_generation_agent);
569 void ChromeContentRendererClient::RenderViewCreated(
570 content::RenderView* render_view) {
572 #if defined(ENABLE_EXTENSIONS)
573 new extensions::ExtensionHelper(render_view, extension_dispatcher_.get());
574 extension_dispatcher_->OnRenderViewCreated(render_view);
575 #endif
576 new PageLoadHistograms(render_view);
577 #if defined(ENABLE_PRINTING)
578 new printing::PrintWebViewHelper(
579 render_view,
580 scoped_ptr<printing::PrintWebViewHelper::Delegate>(
581 new ChromePrintWebViewHelperDelegate()));
582 #endif
583 #if defined(ENABLE_SPELLCHECK)
584 new SpellCheckProvider(render_view, spellcheck_.get());
585 #endif
586 new prerender::PrerendererClient(render_view);
587 #if defined(FULL_SAFE_BROWSING)
588 safe_browsing::MalwareDOMDetails::Create(render_view);
589 #endif
591 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
592 if (command_line->HasSwitch(switches::kInstantProcess))
593 new SearchBox(render_view);
595 new ChromeRenderViewObserver(render_view, web_cache_observer_.get());
597 new password_manager::CredentialManagerClient(render_view);
601 SkBitmap* ChromeContentRendererClient::GetSadPluginBitmap() {
602 return const_cast<SkBitmap*>(ResourceBundle::GetSharedInstance().
603 GetImageNamed(IDR_SAD_PLUGIN).ToSkBitmap());
606 SkBitmap* ChromeContentRendererClient::GetSadWebViewBitmap() {
607 return const_cast<SkBitmap*>(ResourceBundle::GetSharedInstance().
608 GetImageNamed(IDR_SAD_WEBVIEW).ToSkBitmap());
611 #if defined(ENABLE_EXTENSIONS)
612 const Extension* ChromeContentRendererClient::GetExtensionByOrigin(
613 const WebSecurityOrigin& origin) const {
614 if (!EqualsASCII(origin.protocol(), extensions::kExtensionScheme))
615 return NULL;
617 const std::string extension_id = origin.host().utf8().data();
618 return extension_dispatcher_->extensions()->GetByID(extension_id);
620 #endif
622 scoped_ptr<blink::WebPluginPlaceholder>
623 ChromeContentRendererClient::CreatePluginPlaceholder(
624 content::RenderFrame* render_frame,
625 blink::WebLocalFrame* frame,
626 const blink::WebPluginParams& orig_params) {
627 return CreateShadowDOMPlaceholderForPluginInfo(
628 render_frame, frame, orig_params);
631 bool ChromeContentRendererClient::OverrideCreatePlugin(
632 content::RenderFrame* render_frame,
633 blink::WebLocalFrame* frame,
634 const WebPluginParams& params,
635 WebPlugin** plugin) {
636 std::string orig_mime_type = params.mimeType.utf8();
637 #if defined(ENABLE_EXTENSIONS)
638 if (orig_mime_type == content::kBrowserPluginMimeType) {
639 bool guest_view_api_available = false;
640 extension_dispatcher_->script_context_set().ForEach(
641 render_frame->GetRenderView(),
642 base::Bind(&IsGuestViewApiAvailableToScriptContext,
643 &guest_view_api_available));
644 if (guest_view_api_available)
645 return false;
647 #endif
649 ChromeViewHostMsg_GetPluginInfo_Output output;
650 #if defined(ENABLE_PLUGINS)
651 render_frame->Send(new ChromeViewHostMsg_GetPluginInfo(
652 render_frame->GetRoutingID(), GURL(params.url),
653 frame->top()->document().url(), orig_mime_type, &output));
655 #else
656 output.status = ChromeViewHostMsg_GetPluginInfo_Status::kNotFound;
657 #endif
658 *plugin = CreatePlugin(render_frame, frame, params, output);
659 return true;
662 WebPlugin* ChromeContentRendererClient::CreatePluginReplacement(
663 content::RenderFrame* render_frame,
664 const base::FilePath& plugin_path) {
665 ChromePluginPlaceholder* placeholder =
666 ChromePluginPlaceholder::CreateErrorPlugin(render_frame, plugin_path);
667 return placeholder->plugin();
670 void ChromeContentRendererClient::DeferMediaLoad(
671 content::RenderFrame* render_frame,
672 const base::Closure& closure) {
673 #if defined(OS_ANDROID)
674 // Chromium for Android doesn't support prerender yet.
675 closure.Run();
676 return;
677 #else
678 if (!prerender::PrerenderHelper::IsPrerendering(render_frame)) {
679 closure.Run();
680 return;
683 // Lifetime is tied to |render_frame| via content::RenderFrameObserver.
684 new prerender::PrerenderMediaLoadDeferrer(render_frame, closure);
685 #endif
688 WebPlugin* ChromeContentRendererClient::CreatePlugin(
689 content::RenderFrame* render_frame,
690 blink::WebLocalFrame* frame,
691 const WebPluginParams& original_params,
692 const ChromeViewHostMsg_GetPluginInfo_Output& output) {
693 const WebPluginInfo& info = output.plugin;
694 const std::string& actual_mime_type = output.actual_mime_type;
695 const base::string16& group_name = output.group_name;
696 const std::string& identifier = output.group_identifier;
697 ChromeViewHostMsg_GetPluginInfo_Status status = output.status;
698 GURL url(original_params.url);
699 std::string orig_mime_type = original_params.mimeType.utf8();
700 ChromePluginPlaceholder* placeholder = NULL;
702 // If the browser plugin is to be enabled, this should be handled by the
703 // renderer, so the code won't reach here due to the early exit in
704 // OverrideCreatePlugin.
705 if (status == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound ||
706 orig_mime_type == content::kBrowserPluginMimeType) {
707 #if defined(OS_ANDROID)
708 if (plugins::MobileYouTubePlugin::IsYouTubeURL(url, orig_mime_type)) {
709 base::StringPiece template_html(
710 ResourceBundle::GetSharedInstance().GetRawDataResource(
711 IDR_MOBILE_YOUTUBE_PLUGIN_HTML));
712 return (new plugins::MobileYouTubePlugin(
713 render_frame,
714 frame,
715 original_params,
716 template_html,
717 GURL(ChromePluginPlaceholder::kPluginPlaceholderDataURL)))
718 ->plugin();
720 #endif
721 PluginUMAReporter::GetInstance()->ReportPluginMissing(orig_mime_type, url);
722 placeholder = ChromePluginPlaceholder::CreateMissingPlugin(
723 render_frame, frame, original_params);
724 } else {
725 // TODO(bauerb): This should be in content/.
726 WebPluginParams params(original_params);
727 for (size_t i = 0; i < info.mime_types.size(); ++i) {
728 if (info.mime_types[i].mime_type == actual_mime_type) {
729 AppendParams(info.mime_types[i].additional_param_names,
730 info.mime_types[i].additional_param_values,
731 &params.attributeNames, &params.attributeValues);
732 break;
735 if (params.mimeType.isNull() && (actual_mime_type.size() > 0)) {
736 // Webkit might say that mime type is null while we already know the
737 // actual mime type via ChromeViewHostMsg_GetPluginInfo. In that case
738 // we should use what we know since WebpluginDelegateProxy does some
739 // specific initializations based on this information.
740 params.mimeType = WebString::fromUTF8(actual_mime_type.c_str());
743 ContentSettingsObserver* observer =
744 ContentSettingsObserver::Get(render_frame);
746 const ContentSettingsType content_type =
747 ShouldUseJavaScriptSettingForPlugin(info)
748 ? CONTENT_SETTINGS_TYPE_JAVASCRIPT
749 : CONTENT_SETTINGS_TYPE_PLUGINS;
751 if ((status == ChromeViewHostMsg_GetPluginInfo_Status::kUnauthorized ||
752 status == ChromeViewHostMsg_GetPluginInfo_Status::kBlocked) &&
753 observer->IsPluginTemporarilyAllowed(identifier)) {
754 status = ChromeViewHostMsg_GetPluginInfo_Status::kAllowed;
757 #if defined(OS_WIN)
758 // In Windows we need to check if we can load NPAPI plugins.
759 // For example, if the render view is in the Ash desktop, we should not.
760 // If user is on ALLOW or DETECT setting, loading needs to be blocked here.
761 if ((status == ChromeViewHostMsg_GetPluginInfo_Status::kAllowed ||
762 status ==
763 ChromeViewHostMsg_GetPluginInfo_Status::kPlayImportantContent) &&
764 info.type == content::WebPluginInfo::PLUGIN_TYPE_NPAPI) {
765 if (observer->AreNPAPIPluginsBlocked())
766 status = ChromeViewHostMsg_GetPluginInfo_Status::kNPAPINotSupported;
768 #endif
770 auto create_blocked_plugin =
771 [&render_frame, &frame, &params, &info, &identifier, &group_name](
772 int template_id, const base::string16& message) {
773 return ChromePluginPlaceholder::CreateBlockedPlugin(
774 render_frame, frame, params, info, identifier, group_name,
775 template_id, message, PlaceholderPosterInfo());
777 switch (status) {
778 case ChromeViewHostMsg_GetPluginInfo_Status::kNotFound: {
779 NOTREACHED();
780 break;
782 case ChromeViewHostMsg_GetPluginInfo_Status::kAllowed:
783 case ChromeViewHostMsg_GetPluginInfo_Status::kPlayImportantContent: {
784 #if !defined(DISABLE_NACL) && defined(ENABLE_EXTENSIONS)
785 const bool is_nacl_plugin =
786 info.name == ASCIIToUTF16(nacl::kNaClPluginName);
787 const bool is_nacl_mime_type =
788 actual_mime_type == nacl::kNaClPluginMimeType;
789 const bool is_pnacl_mime_type =
790 actual_mime_type == nacl::kPnaclPluginMimeType;
791 if (is_nacl_plugin || is_nacl_mime_type || is_pnacl_mime_type) {
792 bool is_nacl_unrestricted = false;
793 if (is_nacl_mime_type) {
794 is_nacl_unrestricted =
795 base::CommandLine::ForCurrentProcess()->HasSwitch(
796 switches::kEnableNaCl);
797 } else if (is_pnacl_mime_type) {
798 is_nacl_unrestricted = true;
800 GURL manifest_url;
801 GURL app_url;
802 if (is_nacl_mime_type || is_pnacl_mime_type) {
803 // Normal NaCl/PNaCl embed. The app URL is the page URL.
804 manifest_url = url;
805 app_url = frame->top()->document().url();
806 } else {
807 // NaCl is being invoked as a content handler. Look up the NaCl
808 // module using the MIME type. The app URL is the manifest URL.
809 manifest_url = GetNaClContentHandlerURL(actual_mime_type, info);
810 app_url = manifest_url;
812 const Extension* extension =
813 g_current_client->extension_dispatcher_->extensions()->
814 GetExtensionOrAppByURL(manifest_url);
815 if (!IsNaClAllowed(manifest_url,
816 app_url,
817 is_nacl_unrestricted,
818 extension,
819 &params)) {
820 WebString error_message;
821 if (is_nacl_mime_type) {
822 error_message =
823 "Only unpacked extensions and apps installed from the Chrome "
824 "Web Store can load NaCl modules without enabling Native "
825 "Client in about:flags.";
826 } else if (is_pnacl_mime_type) {
827 error_message =
828 "Portable Native Client must not be disabled in about:flags.";
830 frame->addMessageToConsole(
831 WebConsoleMessage(WebConsoleMessage::LevelError,
832 error_message));
833 placeholder = create_blocked_plugin(
834 IDR_BLOCKED_PLUGIN_HTML,
835 #if defined(OS_CHROMEOS)
836 l10n_util::GetStringUTF16(IDS_NACL_PLUGIN_BLOCKED));
837 #else
838 l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED, group_name));
839 #endif
840 break;
843 #endif // !defined(DISABLE_NACL) && defined(ENABLE_EXTENSIONS)
845 #if defined(ENABLE_PLUGINS)
846 // Delay loading plugins if prerendering.
847 // TODO(mmenke): In the case of prerendering, feed into
848 // ChromeContentRendererClient::CreatePlugin instead, to
849 // reduce the chance of future regressions.
850 bool is_prerendering =
851 prerender::PrerenderHelper::IsPrerendering(render_frame);
853 // TODO(tommycli): Plugin Power Saver is disabled on prerendered pages.
854 // This is because the placeholder does not feed back into
855 // ChromeContentRendererClient::CreatePlugin. Because of this, it does
856 // not handle the preroll to UI overlay placeholder flow correctly.
858 // Background tab plugin deferral is disabled for the same reason.
860 // https://crbug.com/471427
861 bool power_saver_enabled =
862 !is_prerendering &&
863 status ==
864 ChromeViewHostMsg_GetPluginInfo_Status::kPlayImportantContent;
866 if (info.name == ASCIIToUTF16(content::kFlashPluginName))
867 TrackPosterParamPresence(params, power_saver_enabled);
869 PlaceholderPosterInfo poster_info;
870 if (power_saver_enabled) {
871 poster_info.poster_attribute =
872 GetPluginInstancePosterAttribute(params);
873 poster_info.base_url = frame->document().url();
876 if (is_prerendering || !poster_info.poster_attribute.empty()) {
877 placeholder = ChromePluginPlaceholder::CreateBlockedPlugin(
878 render_frame, frame, params, info, identifier, group_name,
879 poster_info.poster_attribute.empty() ? IDR_BLOCKED_PLUGIN_HTML
880 : IDR_PLUGIN_POSTER_HTML,
881 l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED, group_name),
882 poster_info);
883 placeholder->set_blocked_for_prerendering(is_prerendering);
884 placeholder->set_power_saver_enabled(power_saver_enabled);
885 placeholder->set_allow_loading(true);
886 break;
889 scoped_ptr<content::PluginInstanceThrottler> throttler;
890 if (power_saver_enabled) {
891 throttler = PluginInstanceThrottler::Create();
892 // PluginPreroller manages its own lifetime.
893 new PluginPreroller(
894 render_frame, frame, params, info, identifier, group_name,
895 l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED, group_name),
896 throttler.get());
899 return render_frame->CreatePlugin(frame, info, params,
900 throttler.Pass());
901 #else // !defined(ENABLE_PLUGINS)
902 return render_frame->CreatePlugin(frame, info, params, nullptr);
903 #endif // defined(ENABLE_PLUGINS)
905 case ChromeViewHostMsg_GetPluginInfo_Status::kNPAPINotSupported: {
906 RenderThread::Get()->RecordAction(
907 UserMetricsAction("Plugin_NPAPINotSupported"));
908 placeholder = create_blocked_plugin(
909 IDR_BLOCKED_PLUGIN_HTML,
910 l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_SUPPORTED_METRO));
911 render_frame->Send(new ChromeViewHostMsg_NPAPINotSupported(
912 render_frame->GetRoutingID(), identifier));
913 break;
915 case ChromeViewHostMsg_GetPluginInfo_Status::kDisabled: {
916 PluginUMAReporter::GetInstance()->ReportPluginDisabled(orig_mime_type,
917 url);
918 placeholder = create_blocked_plugin(
919 IDR_DISABLED_PLUGIN_HTML,
920 l10n_util::GetStringFUTF16(IDS_PLUGIN_DISABLED, group_name));
921 break;
923 case ChromeViewHostMsg_GetPluginInfo_Status::kOutdatedBlocked: {
924 #if defined(ENABLE_PLUGIN_INSTALLATION)
925 placeholder = create_blocked_plugin(
926 IDR_BLOCKED_PLUGIN_HTML,
927 l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED, group_name));
928 placeholder->set_allow_loading(true);
929 render_frame->Send(new ChromeViewHostMsg_BlockedOutdatedPlugin(
930 render_frame->GetRoutingID(), placeholder->CreateRoutingId(),
931 identifier));
932 #else
933 NOTREACHED();
934 #endif
935 break;
937 case ChromeViewHostMsg_GetPluginInfo_Status::kOutdatedDisallowed: {
938 placeholder = create_blocked_plugin(
939 IDR_BLOCKED_PLUGIN_HTML,
940 l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED, group_name));
941 break;
943 case ChromeViewHostMsg_GetPluginInfo_Status::kUnauthorized: {
944 placeholder = create_blocked_plugin(
945 IDR_BLOCKED_PLUGIN_HTML,
946 l10n_util::GetStringFUTF16(IDS_PLUGIN_NOT_AUTHORIZED, group_name));
947 placeholder->set_allow_loading(true);
948 if (info.type != content::WebPluginInfo::PLUGIN_TYPE_NPAPI) {
949 render_frame->Send(new ChromeViewHostMsg_BlockedUnauthorizedPlugin(
950 render_frame->GetRoutingID(),
951 group_name,
952 identifier));
954 observer->DidBlockContentType(content_type, group_name);
955 break;
957 case ChromeViewHostMsg_GetPluginInfo_Status::kBlocked: {
958 placeholder = create_blocked_plugin(
959 IDR_BLOCKED_PLUGIN_HTML,
960 l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED, group_name));
961 placeholder->set_allow_loading(true);
962 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Blocked"));
963 observer->DidBlockContentType(content_type, group_name);
964 break;
966 case ChromeViewHostMsg_GetPluginInfo_Status::kBlockedByPolicy: {
967 placeholder = create_blocked_plugin(
968 IDR_BLOCKED_PLUGIN_HTML,
969 l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED, group_name));
970 placeholder->set_allow_loading(false);
971 RenderThread::Get()->RecordAction(
972 UserMetricsAction("Plugin_BlockedByPolicy"));
973 observer->DidBlockContentType(content_type, group_name);
974 break;
978 placeholder->SetStatus(status);
979 return placeholder->plugin();
982 // For NaCl content handling plugins, the NaCl manifest is stored in an
983 // additonal 'nacl' param associated with the MIME type.
984 // static
985 GURL ChromeContentRendererClient::GetNaClContentHandlerURL(
986 const std::string& actual_mime_type,
987 const content::WebPluginInfo& plugin) {
988 // Look for the manifest URL among the MIME type's additonal parameters.
989 const char kNaClPluginManifestAttribute[] = "nacl";
990 base::string16 nacl_attr = ASCIIToUTF16(kNaClPluginManifestAttribute);
991 for (size_t i = 0; i < plugin.mime_types.size(); ++i) {
992 if (plugin.mime_types[i].mime_type == actual_mime_type) {
993 const content::WebPluginMimeType& content_type = plugin.mime_types[i];
994 for (size_t i = 0; i < content_type.additional_param_names.size(); ++i) {
995 if (content_type.additional_param_names[i] == nacl_attr)
996 return GURL(content_type.additional_param_values[i]);
998 break;
1001 return GURL();
1004 #if !defined(DISABLE_NACL)
1005 // static
1006 bool ChromeContentRendererClient::IsNaClAllowed(
1007 const GURL& manifest_url,
1008 const GURL& app_url,
1009 bool is_nacl_unrestricted,
1010 const Extension* extension,
1011 WebPluginParams* params) {
1012 // Temporarily allow these whitelisted apps and WebUIs to use NaCl.
1013 std::string app_url_host = app_url.host();
1014 std::string manifest_url_path = manifest_url.path();
1016 bool is_whitelisted_web_ui =
1017 app_url.spec() == chrome::kChromeUIAppListStartPageURL;
1019 bool is_photo_app =
1020 // Whitelisted apps must be served over https.
1021 app_url.SchemeIs("https") &&
1022 manifest_url.SchemeIs("https") &&
1023 (EndsWith(app_url_host, "plus.google.com", false) ||
1024 EndsWith(app_url_host, "plus.sandbox.google.com", false)) &&
1025 manifest_url.DomainIs("ssl.gstatic.com") &&
1026 (manifest_url_path.find("s2/oz/nacl/") == 1 ||
1027 manifest_url_path.find("photos/nacl/") == 1);
1029 std::string manifest_fs_host;
1030 if (manifest_url.SchemeIsFileSystem() && manifest_url.inner_url()) {
1031 manifest_fs_host = manifest_url.inner_url()->host();
1033 bool is_hangouts_app =
1034 // Whitelisted apps must be served over secure scheme.
1035 app_url.SchemeIs("https") &&
1036 manifest_url.SchemeIsSecure() &&
1037 manifest_url.SchemeIsFileSystem() &&
1038 (EndsWith(app_url_host, "talkgadget.google.com", false) ||
1039 EndsWith(app_url_host, "plus.google.com", false) ||
1040 EndsWith(app_url_host, "plus.sandbox.google.com", false)) &&
1041 // The manifest must be loaded from the host's FileSystem.
1042 (manifest_fs_host == app_url_host);
1044 bool is_whitelisted_app = is_photo_app || is_hangouts_app;
1046 bool is_extension_from_webstore = false;
1047 bool is_invoked_by_hosted_app = false;
1048 bool is_extension_unrestricted = false;
1049 #if defined(ENABLE_EXTENSIONS)
1050 is_extension_from_webstore = extension && extension->from_webstore();
1052 is_invoked_by_hosted_app = extension &&
1053 extension->is_hosted_app() &&
1054 extension->web_extent().MatchesURL(app_url);
1056 // Allow built-in extensions and extensions under development.
1057 is_extension_unrestricted = extension &&
1058 (extension->location() == extensions::Manifest::COMPONENT ||
1059 extensions::Manifest::IsUnpackedLocation(extension->location()));
1060 #endif // defined(ENABLE_EXTENSIONS)
1062 bool is_invoked_by_extension = app_url.SchemeIs("chrome-extension");
1064 // The NaCl PDF viewer is always allowed and can use 'Dev' interfaces.
1065 bool is_nacl_pdf_viewer =
1066 (is_extension_from_webstore &&
1067 manifest_url.SchemeIs("chrome-extension") &&
1068 manifest_url.host() == "acadkphlmlegjaadjagenfimbpphcgnh");
1070 // Allow Chrome Web Store extensions, built-in extensions and extensions
1071 // under development if the invocation comes from a URL with an extension
1072 // scheme. Also allow invocations if they are from whitelisted URLs or
1073 // if --enable-nacl is set.
1074 bool is_nacl_allowed = is_nacl_unrestricted ||
1075 is_whitelisted_web_ui ||
1076 is_whitelisted_app ||
1077 is_nacl_pdf_viewer ||
1078 is_invoked_by_hosted_app ||
1079 (is_invoked_by_extension &&
1080 (is_extension_from_webstore ||
1081 is_extension_unrestricted));
1082 if (is_nacl_allowed) {
1083 bool app_can_use_dev_interfaces = is_nacl_pdf_viewer;
1084 // Make sure that PPAPI 'dev' interfaces aren't available for production
1085 // apps unless they're whitelisted.
1086 WebString dev_attribute = WebString::fromUTF8("@dev");
1087 if ((!is_whitelisted_app && !is_extension_from_webstore) ||
1088 app_can_use_dev_interfaces) {
1089 // Add the special '@dev' attribute.
1090 std::vector<base::string16> param_names;
1091 std::vector<base::string16> param_values;
1092 param_names.push_back(dev_attribute);
1093 param_values.push_back(WebString());
1094 AppendParams(
1095 param_names,
1096 param_values,
1097 &params->attributeNames,
1098 &params->attributeValues);
1099 } else {
1100 // If the params somehow contain '@dev', remove it.
1101 size_t attribute_count = params->attributeNames.size();
1102 for (size_t i = 0; i < attribute_count; ++i) {
1103 if (params->attributeNames[i].equals(dev_attribute))
1104 params->attributeNames[i] = WebString();
1108 return is_nacl_allowed;
1110 #endif // defined(DISABLE_NACL)
1112 bool ChromeContentRendererClient::HasErrorPage(int http_status_code,
1113 std::string* error_domain) {
1114 // Use an internal error page, if we have one for the status code.
1115 if (!LocalizedError::HasStrings(LocalizedError::kHttpErrorDomain,
1116 http_status_code)) {
1117 return false;
1120 *error_domain = LocalizedError::kHttpErrorDomain;
1121 return true;
1124 bool ChromeContentRendererClient::ShouldSuppressErrorPage(
1125 content::RenderFrame* render_frame,
1126 const GURL& url) {
1127 // Unit tests for ChromeContentRendererClient pass a NULL RenderFrame here.
1128 // Unfortunately it's very difficult to construct a mock RenderView, so skip
1129 // this functionality in this case.
1130 if (render_frame) {
1131 content::RenderView* render_view = render_frame->GetRenderView();
1132 content::RenderFrame* main_render_frame = render_view->GetMainRenderFrame();
1133 blink::WebFrame* web_frame = render_frame->GetWebFrame();
1134 NetErrorHelper* net_error_helper = NetErrorHelper::Get(main_render_frame);
1135 if (net_error_helper->ShouldSuppressErrorPage(web_frame, url))
1136 return true;
1138 // Do not flash an error page if the Instant new tab page fails to load.
1139 return search_bouncer_.get() && search_bouncer_->IsNewTabPage(url);
1142 void ChromeContentRendererClient::GetNavigationErrorStrings(
1143 content::RenderView* render_view,
1144 blink::WebFrame* frame,
1145 const blink::WebURLRequest& failed_request,
1146 const blink::WebURLError& error,
1147 std::string* error_html,
1148 base::string16* error_description) {
1149 const GURL failed_url = error.unreachableURL;
1150 const Extension* extension = NULL;
1152 #if defined(ENABLE_EXTENSIONS)
1153 if (failed_url.is_valid() &&
1154 !failed_url.SchemeIs(extensions::kExtensionScheme)) {
1155 extension = extension_dispatcher_->extensions()->GetExtensionOrAppByURL(
1156 failed_url);
1158 #endif
1160 bool is_post = EqualsASCII(failed_request.httpMethod(), "POST");
1162 if (error_html) {
1163 bool extension_but_not_bookmark_app = false;
1164 #if defined(ENABLE_EXTENSIONS)
1165 extension_but_not_bookmark_app = extension && !extension->from_bookmark();
1166 #endif
1167 // Use a local error page.
1168 if (extension_but_not_bookmark_app) {
1169 #if defined(ENABLE_EXTENSIONS)
1170 // TODO(erikkay): Should we use a different template for different
1171 // error messages?
1172 int resource_id = IDR_ERROR_APP_HTML;
1173 const base::StringPiece template_html(
1174 ResourceBundle::GetSharedInstance().GetRawDataResource(
1175 resource_id));
1176 if (template_html.empty()) {
1177 NOTREACHED() << "unable to load template. ID: " << resource_id;
1178 } else {
1179 base::DictionaryValue error_strings;
1180 const std::string locale = RenderThread::Get()->GetLocale();
1181 LocalizedError::GetAppErrorStrings(failed_url, extension, locale,
1182 &error_strings);
1183 // "t" is the id of the template's root node.
1184 *error_html = webui::GetTemplatesHtml(template_html, &error_strings,
1185 "t");
1187 #endif
1188 } else {
1189 // TODO(ellyjones): change GetNavigationErrorStrings to take a RenderFrame
1190 // instead of a RenderView, then pass that in.
1191 // This is safe for now because we only install the NetErrorHelper on the
1192 // main render frame anyway; see the TODO(ellyjones) in
1193 // RenderFrameCreated.
1194 content::RenderFrame* main_render_frame =
1195 render_view->GetMainRenderFrame();
1196 NetErrorHelper* helper = NetErrorHelper::Get(main_render_frame);
1197 helper->GetErrorHTML(frame, error, is_post, error_html);
1201 if (error_description) {
1202 if (!extension)
1203 *error_description = LocalizedError::GetErrorDetails(error, is_post);
1207 bool ChromeContentRendererClient::RunIdleHandlerWhenWidgetsHidden() {
1208 #if defined(ENABLE_EXTENSIONS)
1209 return !IsStandaloneExtensionProcess();
1210 #else
1211 return true;
1212 #endif
1215 bool ChromeContentRendererClient::AllowPopup() {
1216 #if defined(ENABLE_EXTENSIONS)
1217 extensions::ScriptContext* current_context =
1218 extension_dispatcher_->script_context_set().GetCurrent();
1219 if (!current_context || !current_context->extension())
1220 return false;
1221 // See http://crbug.com/117446 for the subtlety of this check.
1222 switch (current_context->context_type()) {
1223 case extensions::Feature::UNSPECIFIED_CONTEXT:
1224 case extensions::Feature::WEB_PAGE_CONTEXT:
1225 case extensions::Feature::UNBLESSED_EXTENSION_CONTEXT:
1226 case extensions::Feature::WEBUI_CONTEXT:
1227 return false;
1228 case extensions::Feature::BLESSED_EXTENSION_CONTEXT:
1229 case extensions::Feature::CONTENT_SCRIPT_CONTEXT:
1230 return true;
1231 case extensions::Feature::BLESSED_WEB_PAGE_CONTEXT:
1232 return !current_context->web_frame()->parent();
1234 NOTREACHED();
1235 #endif
1236 return false;
1239 bool ChromeContentRendererClient::ShouldFork(blink::WebFrame* frame,
1240 const GURL& url,
1241 const std::string& http_method,
1242 bool is_initial_navigation,
1243 bool is_server_redirect,
1244 bool* send_referrer) {
1245 DCHECK(!frame->parent());
1247 // If this is the Instant process, fork all navigations originating from the
1248 // renderer. The destination page will then be bucketed back to this Instant
1249 // process if it is an Instant url, or to another process if not. Conversely,
1250 // fork if this is a non-Instant process navigating to an Instant url, so that
1251 // such navigations can also be bucketed into an Instant renderer.
1252 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1253 switches::kInstantProcess) ||
1254 (search_bouncer_.get() && search_bouncer_->ShouldFork(url))) {
1255 *send_referrer = true;
1256 return true;
1259 // For now, we skip the rest for POST submissions. This is because
1260 // http://crbug.com/101395 is more likely to cause compatibility issues
1261 // with hosted apps and extensions than WebUI pages. We will remove this
1262 // check when cross-process POST submissions are supported.
1263 if (http_method != "GET")
1264 return false;
1266 // If |url| matches one of the prerendered URLs, stop this navigation and try
1267 // to swap in the prerendered page on the browser process. If the prerendered
1268 // page no longer exists by the time the OpenURL IPC is handled, a normal
1269 // navigation is attempted.
1270 if (prerender_dispatcher_.get() &&
1271 prerender_dispatcher_->IsPrerenderURL(url)) {
1272 *send_referrer = true;
1273 return true;
1276 #if defined(ENABLE_EXTENSIONS)
1277 const extensions::ExtensionSet* extensions =
1278 extension_dispatcher_->extensions();
1280 // Determine if the new URL is an extension (excluding bookmark apps).
1281 const Extension* new_url_extension = extensions::GetNonBookmarkAppExtension(
1282 *extensions, url);
1283 bool is_extension_url = !!new_url_extension;
1285 // If the navigation would cross an app extent boundary, we also need
1286 // to defer to the browser to ensure process isolation. This is not necessary
1287 // for server redirects, which will be transferred to a new process by the
1288 // browser process when they are ready to commit. It is necessary for client
1289 // redirects, which won't be transferred in the same way.
1290 if (!is_server_redirect &&
1291 CrossesExtensionExtents(frame, url, *extensions, is_extension_url,
1292 is_initial_navigation)) {
1293 // Include the referrer in this case since we're going from a hosted web
1294 // page. (the packaged case is handled previously by the extension
1295 // navigation test)
1296 *send_referrer = true;
1298 const Extension* extension =
1299 extension_dispatcher_->extensions()->GetExtensionOrAppByURL(url);
1300 if (extension && extension->is_app()) {
1301 extensions::RecordAppLaunchType(
1302 extension_misc::APP_LAUNCH_CONTENT_NAVIGATION, extension->GetType());
1304 return true;
1307 // If this is a reload, check whether it has the wrong process type. We
1308 // should send it to the browser if it's an extension URL (e.g., hosted app)
1309 // in a normal process, or if it's a process for an extension that has been
1310 // uninstalled.
1311 if (frame->top()->document().url() == url) {
1312 if (is_extension_url != IsStandaloneExtensionProcess())
1313 return true;
1315 #endif // defined(ENABLE_EXTENSIONS)
1317 return false;
1320 #if defined(ENABLE_EXTENSIONS)
1321 bool ChromeContentRendererClient::ShouldForwardToGuestContainer(
1322 const IPC::Message& msg) {
1323 return extensions::GuestViewContainer::HandlesMessage(msg);
1325 #endif
1327 bool ChromeContentRendererClient::WillSendRequest(
1328 blink::WebFrame* frame,
1329 ui::PageTransition transition_type,
1330 const GURL& url,
1331 const GURL& first_party_for_cookies,
1332 GURL* new_url) {
1333 // Check whether the request should be allowed. If not allowed, we reset the
1334 // URL to something invalid to prevent the request and cause an error.
1335 #if defined(ENABLE_EXTENSIONS)
1336 if (url.SchemeIs(extensions::kExtensionScheme) &&
1337 !extensions::ResourceRequestPolicy::CanRequestResource(
1338 url,
1339 frame,
1340 transition_type,
1341 extension_dispatcher_->extensions())) {
1342 *new_url = GURL(chrome::kExtensionInvalidRequestURL);
1343 return true;
1346 if (url.SchemeIs(extensions::kExtensionResourceScheme) &&
1347 !extensions::ResourceRequestPolicy::CanRequestExtensionResourceScheme(
1348 url,
1349 frame)) {
1350 *new_url = GURL(chrome::kExtensionResourceInvalidRequestURL);
1351 return true;
1353 #endif
1355 const content::RenderView* render_view =
1356 content::RenderView::FromWebView(frame->view());
1357 SearchBox* search_box = SearchBox::Get(render_view);
1358 if (search_box && url.SchemeIs(chrome::kChromeSearchScheme)) {
1359 SearchBox::ImageSourceType type = SearchBox::NONE;
1360 if (url.host() == chrome::kChromeUIFaviconHost)
1361 type = SearchBox::FAVICON;
1362 else if (url.host() == chrome::kChromeUILargeIconHost)
1363 type = SearchBox::LARGE_ICON;
1364 else if (url.host() == chrome::kChromeUIFallbackIconHost)
1365 type = SearchBox::FALLBACK_ICON;
1366 else if (url.host() == chrome::kChromeUIThumbnailHost)
1367 type = SearchBox::THUMB;
1368 if (type != SearchBox::NONE)
1369 return search_box->GenerateImageURLFromTransientURL(url, type, new_url);
1372 return false;
1375 unsigned long long ChromeContentRendererClient::VisitedLinkHash(
1376 const char* canonical_url, size_t length) {
1377 return visited_link_slave_->ComputeURLFingerprint(canonical_url, length);
1380 bool ChromeContentRendererClient::IsLinkVisited(unsigned long long link_hash) {
1381 return visited_link_slave_->IsVisited(link_hash);
1384 blink::WebPrescientNetworking*
1385 ChromeContentRendererClient::GetPrescientNetworking() {
1386 return prescient_networking_dispatcher_.get();
1389 bool ChromeContentRendererClient::ShouldOverridePageVisibilityState(
1390 const content::RenderFrame* render_frame,
1391 blink::WebPageVisibilityState* override_state) {
1392 if (!prerender::PrerenderHelper::IsPrerendering(render_frame))
1393 return false;
1395 *override_state = blink::WebPageVisibilityStatePrerender;
1396 return true;
1399 #if defined(ENABLE_EXTENSIONS)
1400 void ChromeContentRendererClient::SetExtensionDispatcherForTest(
1401 extensions::Dispatcher* extension_dispatcher) {
1402 extension_dispatcher_.reset(extension_dispatcher);
1403 permissions_policy_delegate_.reset(
1404 new extensions::RendererPermissionsPolicyDelegate(
1405 extension_dispatcher_.get()));
1408 extensions::Dispatcher*
1409 ChromeContentRendererClient::GetExtensionDispatcherForTest() {
1410 return extension_dispatcher_.get();
1413 bool ChromeContentRendererClient::CrossesExtensionExtents(
1414 blink::WebFrame* frame,
1415 const GURL& new_url,
1416 const extensions::ExtensionSet& extensions,
1417 bool is_extension_url,
1418 bool is_initial_navigation) {
1419 GURL old_url(frame->top()->document().url());
1421 // If old_url is still empty and this is an initial navigation, then this is
1422 // a window.open operation. We should look at the opener URL.
1423 if (is_initial_navigation && old_url.is_empty() && frame->opener()) {
1424 // If we're about to open a normal web page from a same-origin opener stuck
1425 // in an extension process, we want to keep it in process to allow the
1426 // opener to script it.
1427 WebDocument opener_document = frame->opener()->document();
1428 WebSecurityOrigin opener = frame->opener()->document().securityOrigin();
1429 bool opener_is_extension_url =
1430 !opener.isUnique() && extensions.GetExtensionOrAppByURL(
1431 opener_document.url()) != NULL;
1432 if (!is_extension_url &&
1433 !opener_is_extension_url &&
1434 IsStandaloneExtensionProcess() &&
1435 opener.canRequest(WebURL(new_url)))
1436 return false;
1438 // In all other cases, we want to compare against the top frame's URL (as
1439 // opposed to the opener frame's), since that's what determines the type of
1440 // process. This allows iframes outside an app to open a popup in the app.
1441 old_url = frame->top()->opener()->top()->document().url();
1444 // Only consider keeping non-app URLs in an app process if this window
1445 // has an opener (in which case it might be an OAuth popup that tries to
1446 // script an iframe within the app).
1447 bool should_consider_workaround = !!frame->opener();
1449 return extensions::CrossesExtensionProcessBoundary(
1450 extensions, old_url, new_url, should_consider_workaround);
1452 #endif // defined(ENABLE_EXTENSIONS)
1454 #if defined(ENABLE_SPELLCHECK)
1455 void ChromeContentRendererClient::SetSpellcheck(SpellCheck* spellcheck) {
1456 RenderThread* thread = RenderThread::Get();
1457 if (spellcheck_.get() && thread)
1458 thread->RemoveObserver(spellcheck_.get());
1459 spellcheck_.reset(spellcheck);
1460 SpellCheckReplacer replacer(spellcheck_.get());
1461 content::RenderView::ForEach(&replacer);
1462 if (thread)
1463 thread->AddObserver(spellcheck_.get());
1465 #endif
1467 // static
1468 bool ChromeContentRendererClient::WasWebRequestUsedBySomeExtensions() {
1469 #if defined(ENABLE_EXTENSIONS)
1470 return g_current_client->extension_dispatcher_
1471 ->WasWebRequestUsedBySomeExtensions();
1472 #else
1473 return false;
1474 #endif
1477 const void* ChromeContentRendererClient::CreatePPAPIInterface(
1478 const std::string& interface_name) {
1479 #if defined(ENABLE_PLUGINS)
1480 #if !defined(DISABLE_NACL)
1481 if (interface_name == PPB_NACL_PRIVATE_INTERFACE)
1482 return nacl::GetNaClPrivateInterface();
1483 #endif // DISABLE_NACL
1484 if (interface_name == PPB_PDF_INTERFACE)
1485 return pdf::PPB_PDF_Impl::GetInterface();
1486 #endif
1487 return NULL;
1490 bool ChromeContentRendererClient::IsExternalPepperPlugin(
1491 const std::string& module_name) {
1492 // TODO(bbudge) remove this when the trusted NaCl plugin has been removed.
1493 // We must defer certain plugin events for NaCl instances since we switch
1494 // from the in-process to the out-of-process proxy after instantiating them.
1495 return module_name == "Native Client";
1498 #if defined(ENABLE_PLUGINS) && defined(ENABLE_EXTENSIONS)
1499 bool ChromeContentRendererClient::IsExtensionOrSharedModuleWhitelisted(
1500 const GURL& url, const std::set<std::string>& whitelist) {
1501 const extensions::ExtensionSet* extension_set =
1502 g_current_client->extension_dispatcher_->extensions();
1503 return chrome::IsExtensionOrSharedModuleWhitelisted(url, extension_set,
1504 whitelist);
1506 #endif
1508 blink::WebSpeechSynthesizer*
1509 ChromeContentRendererClient::OverrideSpeechSynthesizer(
1510 blink::WebSpeechSynthesizerClient* client) {
1511 return new TtsDispatcher(client);
1514 bool ChromeContentRendererClient::AllowPepperMediaStreamAPI(
1515 const GURL& url) {
1516 #if !defined(OS_ANDROID)
1517 // Allow only the Hangouts app to use the MediaStream APIs. It's OK to check
1518 // the whitelist in the renderer, since we're only preventing access until
1519 // these APIs are public and stable.
1520 std::string url_host = url.host();
1521 if (url.SchemeIs("https") &&
1522 (EndsWith(url_host, "talkgadget.google.com", false) ||
1523 EndsWith(url_host, "plus.google.com", false) ||
1524 EndsWith(url_host, "plus.sandbox.google.com", false)) &&
1525 StartsWithASCII(url.path(), "/hangouts/", false)) {
1526 return true;
1528 // Allow access for tests.
1529 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1530 switches::kEnablePepperTesting)) {
1531 return true;
1533 #endif // !defined(OS_ANDROID)
1534 return false;
1537 void ChromeContentRendererClient::AddKeySystems(
1538 std::vector<media::KeySystemInfo>* key_systems) {
1539 AddChromeKeySystems(key_systems);
1542 bool ChromeContentRendererClient::ShouldReportDetailedMessageForSource(
1543 const base::string16& source) const {
1544 #if defined(ENABLE_EXTENSIONS)
1545 return extensions::IsSourceFromAnExtension(source);
1546 #else
1547 return false;
1548 #endif
1551 bool ChromeContentRendererClient::ShouldEnableSiteIsolationPolicy() const {
1552 // SiteIsolationPolicy is off by default. We would like to activate cross-site
1553 // document blocking (for UMA data collection) for normal renderer processes
1554 // running a normal web page from the Internet. We only turn on
1555 // SiteIsolationPolicy for a renderer process that does not have the extension
1556 // flag on.
1557 #if defined(ENABLE_EXTENSIONS)
1558 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
1559 return !command_line->HasSwitch(extensions::switches::kExtensionProcess);
1560 #else
1561 return true;
1562 #endif
1565 blink::WebWorkerContentSettingsClientProxy*
1566 ChromeContentRendererClient::CreateWorkerContentSettingsClientProxy(
1567 content::RenderFrame* render_frame,
1568 blink::WebFrame* frame) {
1569 return new WorkerContentSettingsClientProxy(render_frame, frame);
1572 bool ChromeContentRendererClient::IsPluginAllowedToUseDevChannelAPIs() {
1573 #if defined(ENABLE_PLUGINS)
1574 // Allow access for tests.
1575 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1576 switches::kEnablePepperTesting)) {
1577 return true;
1580 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
1581 // Allow dev channel APIs to be used on "Canary", "Dev", and "Unknown"
1582 // releases of Chrome. Permitting "Unknown" allows these APIs to be used on
1583 // Chromium builds as well.
1584 return channel <= chrome::VersionInfo::CHANNEL_DEV;
1585 #else
1586 return false;
1587 #endif
1590 bool ChromeContentRendererClient::IsPluginAllowedToUseCameraDeviceAPI(
1591 const GURL& url) {
1592 #if defined(ENABLE_PLUGINS) && defined(ENABLE_EXTENSIONS)
1593 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1594 switches::kEnablePepperTesting))
1595 return true;
1597 if (IsExtensionOrSharedModuleWhitelisted(url, allowed_camera_device_origins_))
1598 return true;
1599 #endif
1601 return false;
1604 bool ChromeContentRendererClient::IsPluginAllowedToUseCompositorAPI(
1605 const GURL& url) {
1606 #if defined(ENABLE_PLUGINS) && defined(ENABLE_EXTENSIONS)
1607 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1608 switches::kEnablePepperTesting))
1609 return true;
1610 if (IsExtensionOrSharedModuleWhitelisted(url, allowed_compositor_origins_))
1611 return true;
1613 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
1614 return channel <= chrome::VersionInfo::CHANNEL_DEV;
1615 #else
1616 return false;
1617 #endif
1620 content::BrowserPluginDelegate*
1621 ChromeContentRendererClient::CreateBrowserPluginDelegate(
1622 content::RenderFrame* render_frame,
1623 const std::string& mime_type,
1624 const GURL& original_url) {
1625 #if defined(ENABLE_EXTENSIONS)
1626 if (mime_type == content::kBrowserPluginMimeType) {
1627 return new extensions::ExtensionsGuestViewContainer(render_frame);
1628 } else {
1629 return new extensions::MimeHandlerViewContainer(
1630 render_frame, mime_type, original_url);
1632 #else
1633 return NULL;
1634 #endif
1637 void ChromeContentRendererClient::RecordRappor(const std::string& metric,
1638 const std::string& sample) {
1639 RenderThread::Get()->Send(new ChromeViewHostMsg_RecordRappor(metric, sample));
1642 void ChromeContentRendererClient::RecordRapporURL(const std::string& metric,
1643 const GURL& url) {
1644 RenderThread::Get()->Send(new ChromeViewHostMsg_RecordRapporURL(metric, url));