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/path_service.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/values.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/chrome_version_info.h"
21 #include "chrome/common/crash_keys.h"
22 #include "chrome/common/extensions/extension_constants.h"
23 #include "chrome/common/localized_error.h"
24 #include "chrome/common/pepper_permission_util.h"
25 #include "chrome/common/render_messages.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/grit/generated_resources.h"
28 #include "chrome/grit/locale_settings.h"
29 #include "chrome/grit/renderer_resources.h"
30 #include "chrome/renderer/benchmarking_extension.h"
31 #include "chrome/renderer/chrome_render_frame_observer.h"
32 #include "chrome/renderer/chrome_render_process_observer.h"
33 #include "chrome/renderer/chrome_render_view_observer.h"
34 #include "chrome/renderer/content_settings_observer.h"
35 #include "chrome/renderer/external_extension.h"
36 #include "chrome/renderer/loadtimes_extension_bindings.h"
37 #include "chrome/renderer/media/cast_ipc_dispatcher.h"
38 #include "chrome/renderer/media/chrome_key_systems.h"
39 #include "chrome/renderer/net/net_error_helper.h"
40 #include "chrome/renderer/net/prescient_networking_dispatcher.h"
41 #include "chrome/renderer/net/renderer_net_predictor.h"
42 #include "chrome/renderer/net_benchmarking_extension.h"
43 #include "chrome/renderer/page_load_histograms.h"
44 #include "chrome/renderer/pepper/pepper_helper.h"
45 #include "chrome/renderer/playback_extension.h"
46 #include "chrome/renderer/plugins/chrome_plugin_placeholder.h"
47 #include "chrome/renderer/plugins/plugin_uma.h"
48 #include "chrome/renderer/plugins/shadow_dom_plugin_placeholder.h"
49 #include "chrome/renderer/prefetch_helper.h"
50 #include "chrome/renderer/prerender/prerender_dispatcher.h"
51 #include "chrome/renderer/prerender/prerender_helper.h"
52 #include "chrome/renderer/prerender/prerender_media_load_deferrer.h"
53 #include "chrome/renderer/prerender/prerenderer_client.h"
54 #include "chrome/renderer/principals_extension_bindings.h"
55 #include "chrome/renderer/printing/print_web_view_helper.h"
56 #include "chrome/renderer/safe_browsing/malware_dom_details.h"
57 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h"
58 #include "chrome/renderer/searchbox/search_bouncer.h"
59 #include "chrome/renderer/searchbox/searchbox.h"
60 #include "chrome/renderer/searchbox/searchbox_extension.h"
61 #include "chrome/renderer/tts_dispatcher.h"
62 #include "chrome/renderer/worker_permission_client_proxy.h"
63 #include "components/autofill/content/renderer/autofill_agent.h"
64 #include "components/autofill/content/renderer/password_autofill_agent.h"
65 #include "components/autofill/content/renderer/password_generation_agent.h"
66 #include "components/content_settings/core/common/content_settings_pattern.h"
67 #include "components/dom_distiller/core/url_constants.h"
68 #include "components/nacl/renderer/ppb_nacl_private_impl.h"
69 #include "components/password_manager/content/renderer/credential_manager_client.h"
70 #include "components/pdf/renderer/ppb_pdf_impl.h"
71 #include "components/plugins/renderer/mobile_youtube_plugin.h"
72 #include "components/signin/core/common/profile_management_switches.h"
73 #include "components/visitedlink/renderer/visitedlink_slave.h"
74 #include "components/web_cache/renderer/web_cache_render_process_observer.h"
75 #include "content/public/common/content_constants.h"
76 #include "content/public/renderer/render_frame.h"
77 #include "content/public/renderer/render_thread.h"
78 #include "content/public/renderer/render_view.h"
79 #include "content/public/renderer/render_view_visitor.h"
80 #include "extensions/common/constants.h"
81 #include "ipc/ipc_sync_channel.h"
82 #include "net/base/net_errors.h"
83 #include "ppapi/c/private/ppb_nacl_private.h"
84 #include "ppapi/c/private/ppb_pdf.h"
85 #include "ppapi/shared_impl/ppapi_switches.h"
86 #include "third_party/WebKit/public/platform/WebURL.h"
87 #include "third_party/WebKit/public/platform/WebURLError.h"
88 #include "third_party/WebKit/public/platform/WebURLRequest.h"
89 #include "third_party/WebKit/public/web/WebCache.h"
90 #include "third_party/WebKit/public/web/WebDataSource.h"
91 #include "third_party/WebKit/public/web/WebDocument.h"
92 #include "third_party/WebKit/public/web/WebElement.h"
93 #include "third_party/WebKit/public/web/WebLocalFrame.h"
94 #include "third_party/WebKit/public/web/WebPluginContainer.h"
95 #include "third_party/WebKit/public/web/WebPluginParams.h"
96 #include "third_party/WebKit/public/web/WebPluginPlaceholder.h"
97 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
98 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
99 #include "ui/base/l10n/l10n_util.h"
100 #include "ui/base/layout.h"
101 #include "ui/base/resource/resource_bundle.h"
102 #include "ui/base/webui/jstemplate_builder.h"
103 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
105 #if !defined(DISABLE_NACL)
106 #include "components/nacl/common/nacl_constants.h"
107 #include "components/nacl/renderer/nacl_helper.h"
110 #if defined(ENABLE_EXTENSIONS)
111 #include "chrome/common/extensions/chrome_extensions_client.h"
112 #include "chrome/common/extensions/extension_process_policy.h"
113 #include "chrome/renderer/extensions/chrome_extensions_dispatcher_delegate.h"
114 #include "chrome/renderer/extensions/chrome_extensions_renderer_client.h"
115 #include "chrome/renderer/extensions/extension_frame_helper.h"
116 #include "chrome/renderer/extensions/renderer_permissions_policy_delegate.h"
117 #include "chrome/renderer/extensions/resource_request_policy.h"
118 #include "extensions/common/extension.h"
119 #include "extensions/common/extension_set.h"
120 #include "extensions/common/extension_urls.h"
121 #include "extensions/common/switches.h"
122 #include "extensions/renderer/dispatcher.h"
123 #include "extensions/renderer/extension_helper.h"
124 #include "extensions/renderer/extensions_render_frame_observer.h"
125 #include "extensions/renderer/guest_view/guest_view_container.h"
126 #include "extensions/renderer/script_context.h"
129 #if defined(ENABLE_FULL_PRINTING)
130 #include "chrome/renderer/pepper/chrome_pdf_print_client.h"
133 #if defined(ENABLE_SPELLCHECK)
134 #include "chrome/renderer/spellchecker/spellcheck.h"
135 #include "chrome/renderer/spellchecker/spellcheck_provider.h"
138 #if defined(ENABLE_WEBRTC)
139 #include "chrome/renderer/media/webrtc_logging_message_filter.h"
143 #include "chrome_elf/blacklist/blacklist.h"
146 using autofill::AutofillAgent
;
147 using autofill::PasswordAutofillAgent
;
148 using autofill::PasswordGenerationAgent
;
149 using base::ASCIIToUTF16
;
150 using base::UserMetricsAction
;
151 using content::RenderThread
;
152 using content::WebPluginInfo
;
153 using extensions::Extension
;
154 using blink::WebCache
;
155 using blink::WebConsoleMessage
;
156 using blink::WebDataSource
;
157 using blink::WebDocument
;
158 using blink::WebFrame
;
159 using blink::WebLocalFrame
;
160 using blink::WebPlugin
;
161 using blink::WebPluginParams
;
162 using blink::WebSecurityOrigin
;
163 using blink::WebSecurityPolicy
;
164 using blink::WebString
;
166 using blink::WebURLError
;
167 using blink::WebURLRequest
;
168 using blink::WebURLResponse
;
169 using blink::WebVector
;
173 ChromeContentRendererClient
* g_current_client
;
175 #if defined(ENABLE_PLUGINS)
176 const char* const kPredefinedAllowedCompositorOrigins
[] = {
177 "6EAED1924DB611B6EEF2A664BD077BE7EAD33B8F", // see crbug.com/383937
178 "4EB74897CB187C7633357C2FE832E0AD6A44883A" // see crbug.com/383937
181 const char* const kPredefinedAllowedVideoDecodeOrigins
[] = {
182 "6EAED1924DB611B6EEF2A664BD077BE7EAD33B8F", // see crbug.com/383937
183 "4EB74897CB187C7633357C2FE832E0AD6A44883A" // see crbug.com/383937
187 static void AppendParams(const std::vector
<base::string16
>& additional_names
,
188 const std::vector
<base::string16
>& additional_values
,
189 WebVector
<WebString
>* existing_names
,
190 WebVector
<WebString
>* existing_values
) {
191 DCHECK(additional_names
.size() == additional_values
.size());
192 DCHECK(existing_names
->size() == existing_values
->size());
194 size_t existing_size
= existing_names
->size();
195 size_t total_size
= existing_size
+ additional_names
.size();
197 WebVector
<WebString
> names(total_size
);
198 WebVector
<WebString
> values(total_size
);
200 for (size_t i
= 0; i
< existing_size
; ++i
) {
201 names
[i
] = (*existing_names
)[i
];
202 values
[i
] = (*existing_values
)[i
];
205 for (size_t i
= 0; i
< additional_names
.size(); ++i
) {
206 names
[existing_size
+ i
] = additional_names
[i
];
207 values
[existing_size
+ i
] = additional_values
[i
];
210 existing_names
->swap(names
);
211 existing_values
->swap(values
);
214 #if defined(ENABLE_SPELLCHECK)
215 class SpellCheckReplacer
: public content::RenderViewVisitor
{
217 explicit SpellCheckReplacer(SpellCheck
* spellcheck
)
218 : spellcheck_(spellcheck
) {}
219 bool Visit(content::RenderView
* render_view
) override
;
222 SpellCheck
* spellcheck_
; // New shared spellcheck for all views. Weak Ptr.
223 DISALLOW_COPY_AND_ASSIGN(SpellCheckReplacer
);
226 bool SpellCheckReplacer::Visit(content::RenderView
* render_view
) {
227 SpellCheckProvider
* provider
= SpellCheckProvider::Get(render_view
);
229 provider
->set_spellcheck(spellcheck_
);
234 // For certain sandboxed Pepper plugins, use the JavaScript Content Settings.
235 bool ShouldUseJavaScriptSettingForPlugin(const WebPluginInfo
& plugin
) {
236 if (plugin
.type
!= WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS
&&
237 plugin
.type
!= WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS
) {
241 #if !defined(DISABLE_NACL)
242 // Treat Native Client invocations like JavaScript.
243 if (plugin
.name
== ASCIIToUTF16(nacl::kNaClPluginName
))
247 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
248 // Treat CDM invocations like JavaScript.
249 if (plugin
.name
== ASCIIToUTF16(kWidevineCdmDisplayName
)) {
250 DCHECK(plugin
.type
== WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS
);
253 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
258 #if defined(ENABLE_EXTENSIONS)
259 void IsGuestViewApiAvailableToScriptContext(
260 bool* api_is_available
,
261 extensions::ScriptContext
* context
) {
262 if (context
->GetAvailability("guestViewInternal").is_available()) {
263 *api_is_available
= true;
270 ChromeContentRendererClient::ChromeContentRendererClient() {
271 g_current_client
= this;
273 #if defined(ENABLE_EXTENSIONS)
274 extensions::ExtensionsClient::Set(
275 extensions::ChromeExtensionsClient::GetInstance());
276 extensions::ExtensionsRendererClient::Set(
277 ChromeExtensionsRendererClient::GetInstance());
279 #if defined(ENABLE_PLUGINS)
280 for (size_t i
= 0; i
< arraysize(kPredefinedAllowedCompositorOrigins
); ++i
)
281 allowed_compositor_origins_
.insert(kPredefinedAllowedCompositorOrigins
[i
]);
282 for (size_t i
= 0; i
< arraysize(kPredefinedAllowedVideoDecodeOrigins
); ++i
)
283 allowed_video_decode_origins_
.insert(
284 kPredefinedAllowedVideoDecodeOrigins
[i
]);
288 ChromeContentRendererClient::~ChromeContentRendererClient() {
289 g_current_client
= NULL
;
292 void ChromeContentRendererClient::RenderThreadStarted() {
293 RenderThread
* thread
= RenderThread::Get();
295 chrome_observer_
.reset(new ChromeRenderProcessObserver(this));
296 web_cache_observer_
.reset(new web_cache::WebCacheRenderProcessObserver());
298 #if defined(ENABLE_EXTENSIONS)
299 extension_dispatcher_delegate_
.reset(
300 new ChromeExtensionsDispatcherDelegate());
301 // ChromeRenderViewTest::SetUp() creates its own ExtensionDispatcher and
302 // injects it using SetExtensionDispatcher(). Don't overwrite it.
303 if (!extension_dispatcher_
) {
304 extension_dispatcher_
.reset(
305 new extensions::Dispatcher(extension_dispatcher_delegate_
.get()));
307 permissions_policy_delegate_
.reset(
308 new extensions::RendererPermissionsPolicyDelegate(
309 extension_dispatcher_
.get()));
312 prescient_networking_dispatcher_
.reset(new PrescientNetworkingDispatcher());
313 net_predictor_
.reset(new RendererNetPredictor());
314 #if defined(ENABLE_SPELLCHECK)
315 // ChromeRenderViewTest::SetUp() creates a Spellcheck and injects it using
316 // SetSpellcheck(). Don't overwrite it.
318 spellcheck_
.reset(new SpellCheck());
319 thread
->AddObserver(spellcheck_
.get());
322 visited_link_slave_
.reset(new visitedlink::VisitedLinkSlave());
323 #if defined(FULL_SAFE_BROWSING)
324 phishing_classifier_
.reset(safe_browsing::PhishingClassifierFilter::Create());
326 prerender_dispatcher_
.reset(new prerender::PrerenderDispatcher());
327 #if defined(ENABLE_WEBRTC)
328 webrtc_logging_message_filter_
= new WebRtcLoggingMessageFilter(
329 content::RenderThread::Get()->GetIOMessageLoopProxy());
331 search_bouncer_
.reset(new SearchBouncer());
333 thread
->AddObserver(chrome_observer_
.get());
334 thread
->AddObserver(web_cache_observer_
.get());
335 #if defined(ENABLE_EXTENSIONS)
336 thread
->AddObserver(extension_dispatcher_
.get());
338 #if defined(FULL_SAFE_BROWSING)
339 thread
->AddObserver(phishing_classifier_
.get());
341 thread
->AddObserver(visited_link_slave_
.get());
342 thread
->AddObserver(prerender_dispatcher_
.get());
343 thread
->AddObserver(search_bouncer_
.get());
345 #if defined(ENABLE_WEBRTC)
346 thread
->AddFilter(webrtc_logging_message_filter_
.get());
348 thread
->AddFilter(new CastIPCDispatcher(
349 content::RenderThread::Get()->GetIOMessageLoopProxy()));
351 thread
->RegisterExtension(extensions_v8::ExternalExtension::Get());
352 thread
->RegisterExtension(extensions_v8::LoadTimesExtension::Get());
354 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
355 if (command_line
->HasSwitch(switches::kEnableBenchmarking
))
356 thread
->RegisterExtension(extensions_v8::BenchmarkingExtension::Get());
357 if (command_line
->HasSwitch(switches::kEnableNetBenchmarking
))
358 thread
->RegisterExtension(extensions_v8::NetBenchmarkingExtension::Get());
359 if (command_line
->HasSwitch(switches::kInstantProcess
))
360 thread
->RegisterExtension(extensions_v8::SearchBoxExtension::Get());
362 if (command_line
->HasSwitch(switches::kPlaybackMode
) ||
363 command_line
->HasSwitch(switches::kRecordMode
)) {
364 thread
->RegisterExtension(extensions_v8::PlaybackExtension::Get());
367 // TODO(guohui): needs to forward the new-profile-management switch to
368 // renderer processes.
369 if (switches::IsEnableAccountConsistency())
370 thread
->RegisterExtension(extensions_v8::PrincipalsExtension::Get());
372 // chrome:, chrome-search:, chrome-devtools:, and chrome-distiller: pages
373 // should not be accessible by normal content, and should also be unable to
374 // script anything but themselves (to help limit the damage that a corrupt
375 // page could cause).
376 WebString
chrome_ui_scheme(ASCIIToUTF16(content::kChromeUIScheme
));
377 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(chrome_ui_scheme
);
379 WebString
chrome_search_scheme(ASCIIToUTF16(chrome::kChromeSearchScheme
));
380 // The Instant process can only display the content but not read it. Other
381 // processes can't display it or read it.
382 if (!command_line
->HasSwitch(switches::kInstantProcess
))
383 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(chrome_search_scheme
);
385 WebString
dev_tools_scheme(ASCIIToUTF16(content::kChromeDevToolsScheme
));
386 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(dev_tools_scheme
);
388 WebString
dom_distiller_scheme(
389 ASCIIToUTF16(dom_distiller::kDomDistillerScheme
));
390 // TODO(nyquist): Add test to ensure this happens when the flag is set.
391 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(dom_distiller_scheme
);
393 #if defined(OS_CHROMEOS)
394 WebString
external_file_scheme(ASCIIToUTF16(chrome::kExternalFileScheme
));
395 WebSecurityPolicy::registerURLSchemeAsLocal(external_file_scheme
);
398 // chrome: and chrome-search: pages should not be accessible by bookmarklets
399 // or javascript: URLs typed in the omnibox.
400 WebSecurityPolicy::registerURLSchemeAsNotAllowingJavascriptURLs(
402 WebSecurityPolicy::registerURLSchemeAsNotAllowingJavascriptURLs(
403 chrome_search_scheme
);
405 // chrome:, chrome-search:, chrome-extension:, and chrome-extension-resource:
406 // resources shouldn't trigger insecure content warnings.
407 WebSecurityPolicy::registerURLSchemeAsSecure(chrome_ui_scheme
);
408 WebSecurityPolicy::registerURLSchemeAsSecure(chrome_search_scheme
);
410 WebString
extension_scheme(ASCIIToUTF16(extensions::kExtensionScheme
));
411 WebSecurityPolicy::registerURLSchemeAsSecure(extension_scheme
);
413 WebString
extension_resource_scheme(
414 ASCIIToUTF16(extensions::kExtensionResourceScheme
));
415 WebSecurityPolicy::registerURLSchemeAsSecure(extension_resource_scheme
);
417 // chrome:, chrome-extension:, chrome-extension-resource: resources should be
418 // allowed to receive CORS requests.
419 WebSecurityPolicy::registerURLSchemeAsCORSEnabled(chrome_ui_scheme
);
420 WebSecurityPolicy::registerURLSchemeAsCORSEnabled(extension_scheme
);
421 WebSecurityPolicy::registerURLSchemeAsCORSEnabled(extension_resource_scheme
);
423 // chrome-extension: resources should bypass Content Security Policy checks
424 // when included in protected resources.
425 WebSecurityPolicy::registerURLSchemeAsBypassingContentSecurityPolicy(
427 WebSecurityPolicy::registerURLSchemeAsBypassingContentSecurityPolicy(
428 extension_resource_scheme
);
431 // Report if the renderer process has been patched by chrome_elf.
432 // TODO(csharp): Remove once the renderer is no longer getting
434 if (blacklist::IsBlacklistInitialized())
435 UMA_HISTOGRAM_BOOLEAN("Blacklist.PatchedInRenderer", true);
437 #if defined(ENABLE_FULL_PRINTING)
438 pdf_print_client_
.reset(new ChromePDFPrintClient());
439 pdf::PPB_PDF_Impl::SetPrintClient(pdf_print_client_
.get());
443 void ChromeContentRendererClient::RenderFrameCreated(
444 content::RenderFrame
* render_frame
) {
445 new ChromeRenderFrameObserver(render_frame
);
447 extensions::Dispatcher
* ext_dispatcher
= NULL
;
448 #if defined(ENABLE_EXTENSIONS)
449 ext_dispatcher
= extension_dispatcher_
.get();
451 ContentSettingsObserver
* content_settings
=
452 new ContentSettingsObserver(render_frame
, ext_dispatcher
);
453 if (chrome_observer_
.get()) {
454 content_settings
->SetContentSettingRules(
455 chrome_observer_
->content_setting_rules());
458 #if defined(ENABLE_EXTENSIONS)
459 new extensions::ExtensionsRenderFrameObserver(render_frame
);
460 new extensions::ExtensionFrameHelper(render_frame
, ext_dispatcher
);
463 #if defined(ENABLE_PLUGINS)
464 new PepperHelper(render_frame
);
467 #if !defined(DISABLE_NACL)
468 new nacl::NaClHelper(render_frame
);
471 // TODO(jam): when the frame tree moves into content and parent() works at
472 // RenderFrame construction, simplify this by just checking parent().
473 if (render_frame
->GetRenderView()->GetMainRenderFrame() != render_frame
) {
474 // Avoid any race conditions from having the browser tell subframes that
475 // they're prerendering.
476 if (prerender::PrerenderHelper::IsPrerendering(
477 render_frame
->GetRenderView()->GetMainRenderFrame())) {
478 new prerender::PrerenderHelper(render_frame
);
482 if (render_frame
->GetRenderView()->GetMainRenderFrame() == render_frame
) {
483 // Only attach NetErrorHelper to the main frame, since only the main frame
484 // should get error pages.
485 // PrefetchHelper is also needed only for main frames.
486 new NetErrorHelper(render_frame
);
487 new prefetch::PrefetchHelper(render_frame
);
491 void ChromeContentRendererClient::RenderViewCreated(
492 content::RenderView
* render_view
) {
493 #if defined(ENABLE_EXTENSIONS)
494 new extensions::ExtensionHelper(render_view
, extension_dispatcher_
.get());
495 extension_dispatcher_
->OnRenderViewCreated(render_view
);
497 new PageLoadHistograms(render_view
);
498 #if defined(ENABLE_PRINTING)
499 new printing::PrintWebViewHelper(render_view
);
501 #if defined(ENABLE_SPELLCHECK)
502 new SpellCheckProvider(render_view
, spellcheck_
.get());
504 new prerender::PrerendererClient(render_view
);
505 #if defined(FULL_SAFE_BROWSING)
506 safe_browsing::MalwareDOMDetails::Create(render_view
);
509 PasswordGenerationAgent
* password_generation_agent
=
510 new PasswordGenerationAgent(render_view
);
511 PasswordAutofillAgent
* password_autofill_agent
=
512 new PasswordAutofillAgent(render_view
);
513 new AutofillAgent(render_view
,
514 password_autofill_agent
,
515 password_generation_agent
);
517 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
518 if (command_line
->HasSwitch(switches::kInstantProcess
))
519 new SearchBox(render_view
);
521 new ChromeRenderViewObserver(render_view
, web_cache_observer_
.get());
523 new password_manager::CredentialManagerClient(render_view
);
526 void ChromeContentRendererClient::SetNumberOfViews(int number_of_views
) {
527 base::debug::SetCrashKeyValue(crash_keys::kNumberOfViews
,
528 base::IntToString(number_of_views
));
531 SkBitmap
* ChromeContentRendererClient::GetSadPluginBitmap() {
532 return const_cast<SkBitmap
*>(ResourceBundle::GetSharedInstance().
533 GetImageNamed(IDR_SAD_PLUGIN
).ToSkBitmap());
536 SkBitmap
* ChromeContentRendererClient::GetSadWebViewBitmap() {
537 return const_cast<SkBitmap
*>(ResourceBundle::GetSharedInstance().
538 GetImageNamed(IDR_SAD_WEBVIEW
).ToSkBitmap());
541 #if defined(ENABLE_EXTENSIONS)
542 const Extension
* ChromeContentRendererClient::GetExtensionByOrigin(
543 const WebSecurityOrigin
& origin
) const {
544 if (!EqualsASCII(origin
.protocol(), extensions::kExtensionScheme
))
547 const std::string extension_id
= origin
.host().utf8().data();
548 return extension_dispatcher_
->extensions()->GetByID(extension_id
);
552 scoped_ptr
<blink::WebPluginPlaceholder
>
553 ChromeContentRendererClient::CreatePluginPlaceholder(
554 content::RenderFrame
* render_frame
,
555 blink::WebLocalFrame
* frame
,
556 const blink::WebPluginParams
& orig_params
) {
557 return CreateShadowDOMPlaceholderForPluginInfo(
558 render_frame
, frame
, orig_params
);
561 bool ChromeContentRendererClient::OverrideCreatePlugin(
562 content::RenderFrame
* render_frame
,
563 WebLocalFrame
* frame
,
564 const WebPluginParams
& params
,
565 WebPlugin
** plugin
) {
566 std::string orig_mime_type
= params
.mimeType
.utf8();
567 #if defined(ENABLE_EXTENSIONS)
568 if (orig_mime_type
== content::kBrowserPluginMimeType
) {
569 bool guest_view_api_available
= false;
570 extension_dispatcher_
->script_context_set().ForEach(
571 render_frame
->GetRenderView(),
572 base::Bind(&IsGuestViewApiAvailableToScriptContext
,
573 &guest_view_api_available
));
574 if (guest_view_api_available
)
579 ChromeViewHostMsg_GetPluginInfo_Output output
;
580 #if defined(ENABLE_PLUGINS)
581 render_frame
->Send(new ChromeViewHostMsg_GetPluginInfo(
582 render_frame
->GetRoutingID(), GURL(params
.url
),
583 frame
->top()->document().url(), orig_mime_type
, &output
));
585 if (output
.plugin
.type
== content::WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN
)
588 output
.status
.value
= ChromeViewHostMsg_GetPluginInfo_Status::kNotFound
;
590 *plugin
= CreatePlugin(render_frame
, frame
, params
, output
);
594 WebPlugin
* ChromeContentRendererClient::CreatePluginReplacement(
595 content::RenderFrame
* render_frame
,
596 const base::FilePath
& plugin_path
) {
597 ChromePluginPlaceholder
* placeholder
=
598 ChromePluginPlaceholder::CreateErrorPlugin(render_frame
, plugin_path
);
599 return placeholder
->plugin();
602 void ChromeContentRendererClient::DeferMediaLoad(
603 content::RenderFrame
* render_frame
,
604 const base::Closure
& closure
) {
605 #if defined(OS_ANDROID)
606 // Chromium for Android doesn't support prerender yet.
610 if (!prerender::PrerenderHelper::IsPrerendering(render_frame
)) {
615 // Lifetime is tied to |render_frame| via content::RenderFrameObserver.
616 new prerender::PrerenderMediaLoadDeferrer(render_frame
, closure
);
620 WebPlugin
* ChromeContentRendererClient::CreatePlugin(
621 content::RenderFrame
* render_frame
,
622 WebLocalFrame
* frame
,
623 const WebPluginParams
& original_params
,
624 const ChromeViewHostMsg_GetPluginInfo_Output
& output
) {
625 const ChromeViewHostMsg_GetPluginInfo_Status
& status
= output
.status
;
626 const WebPluginInfo
& plugin
= output
.plugin
;
627 const std::string
& actual_mime_type
= output
.actual_mime_type
;
628 const base::string16
& group_name
= output
.group_name
;
629 const std::string
& identifier
= output
.group_identifier
;
630 ChromeViewHostMsg_GetPluginInfo_Status::Value status_value
= status
.value
;
631 GURL
url(original_params
.url
);
632 std::string orig_mime_type
= original_params
.mimeType
.utf8();
633 ChromePluginPlaceholder
* placeholder
= NULL
;
635 // If the browser plugin is to be enabled, this should be handled by the
636 // renderer, so the code won't reach here due to the early exit in
637 // OverrideCreatePlugin.
638 if (status_value
== ChromeViewHostMsg_GetPluginInfo_Status::kNotFound
||
639 orig_mime_type
== content::kBrowserPluginMimeType
) {
640 #if defined(OS_ANDROID)
641 if (plugins::MobileYouTubePlugin::IsYouTubeURL(url
, orig_mime_type
)) {
642 base::StringPiece
template_html(
643 ResourceBundle::GetSharedInstance().GetRawDataResource(
644 IDR_MOBILE_YOUTUBE_PLUGIN_HTML
));
645 return (new plugins::MobileYouTubePlugin(
650 GURL(ChromePluginPlaceholder::kPluginPlaceholderDataURL
)))
654 PluginUMAReporter::GetInstance()->ReportPluginMissing(orig_mime_type
, url
);
655 placeholder
= ChromePluginPlaceholder::CreateMissingPlugin(
656 render_frame
, frame
, original_params
);
658 // TODO(bauerb): This should be in content/.
659 WebPluginParams
params(original_params
);
660 for (size_t i
= 0; i
< plugin
.mime_types
.size(); ++i
) {
661 if (plugin
.mime_types
[i
].mime_type
== actual_mime_type
) {
662 AppendParams(plugin
.mime_types
[i
].additional_param_names
,
663 plugin
.mime_types
[i
].additional_param_values
,
664 ¶ms
.attributeNames
,
665 ¶ms
.attributeValues
);
669 if (params
.mimeType
.isNull() && (actual_mime_type
.size() > 0)) {
670 // Webkit might say that mime type is null while we already know the
671 // actual mime type via ChromeViewHostMsg_GetPluginInfo. In that case
672 // we should use what we know since WebpluginDelegateProxy does some
673 // specific initializations based on this information.
674 params
.mimeType
= WebString::fromUTF8(actual_mime_type
.c_str());
677 ContentSettingsObserver
* observer
=
678 ContentSettingsObserver::Get(render_frame
);
680 const ContentSettingsType content_type
=
681 ShouldUseJavaScriptSettingForPlugin(plugin
) ?
682 CONTENT_SETTINGS_TYPE_JAVASCRIPT
:
683 CONTENT_SETTINGS_TYPE_PLUGINS
;
686 ChromeViewHostMsg_GetPluginInfo_Status::kUnauthorized
||
687 status_value
== ChromeViewHostMsg_GetPluginInfo_Status::kClickToPlay
||
688 status_value
== ChromeViewHostMsg_GetPluginInfo_Status::kBlocked
) &&
689 observer
->IsPluginTemporarilyAllowed(identifier
)) {
690 status_value
= ChromeViewHostMsg_GetPluginInfo_Status::kAllowed
;
693 // Allow full-page plug-ins for click-to-play.
694 if (status_value
== ChromeViewHostMsg_GetPluginInfo_Status::kClickToPlay
&&
697 frame
->document().isPluginDocument()) {
698 status_value
= ChromeViewHostMsg_GetPluginInfo_Status::kAllowed
;
702 // In Windows we need to check if we can load NPAPI plugins.
703 // For example, if the render view is in the Ash desktop, we should not.
704 if (status_value
== ChromeViewHostMsg_GetPluginInfo_Status::kAllowed
&&
705 plugin
.type
== content::WebPluginInfo::PLUGIN_TYPE_NPAPI
) {
706 if (observer
->AreNPAPIPluginsBlocked())
708 ChromeViewHostMsg_GetPluginInfo_Status::kNPAPINotSupported
;
712 switch (status_value
) {
713 case ChromeViewHostMsg_GetPluginInfo_Status::kNotFound
: {
717 case ChromeViewHostMsg_GetPluginInfo_Status::kAllowed
: {
718 #if !defined(DISABLE_NACL) && defined(ENABLE_EXTENSIONS)
719 const bool is_nacl_plugin
=
720 plugin
.name
== ASCIIToUTF16(nacl::kNaClPluginName
);
721 const bool is_nacl_mime_type
=
722 actual_mime_type
== nacl::kNaClPluginMimeType
;
723 const bool is_pnacl_mime_type
=
724 actual_mime_type
== nacl::kPnaclPluginMimeType
;
725 if (is_nacl_plugin
|| is_nacl_mime_type
|| is_pnacl_mime_type
) {
726 bool is_nacl_unrestricted
= false;
727 if (is_nacl_mime_type
) {
728 is_nacl_unrestricted
=
729 CommandLine::ForCurrentProcess()->HasSwitch(
730 switches::kEnableNaCl
);
731 } else if (is_pnacl_mime_type
) {
732 is_nacl_unrestricted
= true;
736 if (is_nacl_mime_type
|| is_pnacl_mime_type
) {
737 // Normal NaCl/PNaCl embed. The app URL is the page URL.
739 app_url
= frame
->top()->document().url();
741 // NaCl is being invoked as a content handler. Look up the NaCl
742 // module using the MIME type. The app URL is the manifest URL.
743 manifest_url
= GetNaClContentHandlerURL(actual_mime_type
, plugin
);
744 app_url
= manifest_url
;
746 const Extension
* extension
=
747 g_current_client
->extension_dispatcher_
->extensions()->
748 GetExtensionOrAppByURL(manifest_url
);
749 if (!IsNaClAllowed(manifest_url
,
751 is_nacl_unrestricted
,
754 WebString error_message
;
755 if (is_nacl_mime_type
) {
757 "Only unpacked extensions and apps installed from the Chrome "
758 "Web Store can load NaCl modules without enabling Native "
759 "Client in about:flags.";
760 } else if (is_pnacl_mime_type
) {
762 "Portable Native Client must not be disabled in about:flags.";
764 frame
->addMessageToConsole(
765 WebConsoleMessage(WebConsoleMessage::LevelError
,
767 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
774 IDR_BLOCKED_PLUGIN_HTML
,
775 #if defined(OS_CHROMEOS)
776 l10n_util::GetStringUTF16(IDS_NACL_PLUGIN_BLOCKED
));
778 l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED
, group_name
));
783 #endif // !defined(DISABLE_NACL) && defined(ENABLE_EXTENSIONS)
785 // Delay loading plugins if prerendering.
786 // TODO(mmenke): In the case of prerendering, feed into
787 // ChromeContentRendererClient::CreatePlugin instead, to
788 // reduce the chance of future regressions.
789 if (prerender::PrerenderHelper::IsPrerendering(render_frame
)) {
790 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
797 IDR_CLICK_TO_PLAY_PLUGIN_HTML
,
798 l10n_util::GetStringFUTF16(IDS_PLUGIN_LOAD
, group_name
));
799 placeholder
->set_blocked_for_prerendering(true);
800 placeholder
->set_allow_loading(true);
804 return render_frame
->CreatePlugin(frame
, plugin
, params
);
806 case ChromeViewHostMsg_GetPluginInfo_Status::kNPAPINotSupported
: {
807 RenderThread::Get()->RecordAction(
808 UserMetricsAction("Plugin_NPAPINotSupported"));
809 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
816 IDR_BLOCKED_PLUGIN_HTML
,
817 l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_SUPPORTED_METRO
));
818 render_frame
->Send(new ChromeViewHostMsg_NPAPINotSupported(
819 render_frame
->GetRoutingID(), identifier
));
822 case ChromeViewHostMsg_GetPluginInfo_Status::kDisabled
: {
823 PluginUMAReporter::GetInstance()->ReportPluginDisabled(orig_mime_type
,
825 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
832 IDR_DISABLED_PLUGIN_HTML
,
833 l10n_util::GetStringFUTF16(IDS_PLUGIN_DISABLED
, group_name
));
836 case ChromeViewHostMsg_GetPluginInfo_Status::kOutdatedBlocked
: {
837 #if defined(ENABLE_PLUGIN_INSTALLATION)
838 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
845 IDR_BLOCKED_PLUGIN_HTML
,
846 l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED
, group_name
));
847 placeholder
->set_allow_loading(true);
848 render_frame
->Send(new ChromeViewHostMsg_BlockedOutdatedPlugin(
849 render_frame
->GetRoutingID(), placeholder
->CreateRoutingId(),
856 case ChromeViewHostMsg_GetPluginInfo_Status::kOutdatedDisallowed
: {
857 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
864 IDR_BLOCKED_PLUGIN_HTML
,
865 l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED
, group_name
));
868 case ChromeViewHostMsg_GetPluginInfo_Status::kUnauthorized
: {
869 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
876 IDR_BLOCKED_PLUGIN_HTML
,
877 l10n_util::GetStringFUTF16(IDS_PLUGIN_NOT_AUTHORIZED
, group_name
));
878 placeholder
->set_allow_loading(true);
879 // Check to see if old infobar should be displayed.
880 std::string trial_group
=
881 base::FieldTrialList::FindFullName("UnauthorizedPluginInfoBar");
882 if (plugin
.type
!= content::WebPluginInfo::PLUGIN_TYPE_NPAPI
||
883 trial_group
== "Enabled") {
884 render_frame
->Send(new ChromeViewHostMsg_BlockedUnauthorizedPlugin(
885 render_frame
->GetRoutingID(),
889 // Send IPC for showing blocked plugins page action.
890 observer
->DidBlockContentType(content_type
);
894 case ChromeViewHostMsg_GetPluginInfo_Status::kClickToPlay
: {
895 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
902 IDR_CLICK_TO_PLAY_PLUGIN_HTML
,
903 l10n_util::GetStringFUTF16(IDS_PLUGIN_LOAD
, group_name
));
904 placeholder
->set_allow_loading(true);
905 RenderThread::Get()->RecordAction(
906 UserMetricsAction("Plugin_ClickToPlay"));
907 observer
->DidBlockContentType(content_type
);
910 case ChromeViewHostMsg_GetPluginInfo_Status::kBlocked
: {
911 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
918 IDR_BLOCKED_PLUGIN_HTML
,
919 l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED
, group_name
));
920 placeholder
->set_allow_loading(true);
921 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Blocked"));
922 observer
->DidBlockContentType(content_type
);
925 case ChromeViewHostMsg_GetPluginInfo_Status::kBlockedByPolicy
: {
926 placeholder
= ChromePluginPlaceholder::CreateBlockedPlugin(
933 IDR_BLOCKED_PLUGIN_HTML
,
934 l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED
, group_name
));
935 placeholder
->set_allow_loading(false);
936 RenderThread::Get()->RecordAction(
937 UserMetricsAction("Plugin_BlockedByPolicy"));
938 observer
->DidBlockContentType(content_type
);
943 placeholder
->SetStatus(status
);
944 return placeholder
->plugin();
947 // For NaCl content handling plugins, the NaCl manifest is stored in an
948 // additonal 'nacl' param associated with the MIME type.
950 GURL
ChromeContentRendererClient::GetNaClContentHandlerURL(
951 const std::string
& actual_mime_type
,
952 const content::WebPluginInfo
& plugin
) {
953 // Look for the manifest URL among the MIME type's additonal parameters.
954 const char kNaClPluginManifestAttribute
[] = "nacl";
955 base::string16 nacl_attr
= ASCIIToUTF16(kNaClPluginManifestAttribute
);
956 for (size_t i
= 0; i
< plugin
.mime_types
.size(); ++i
) {
957 if (plugin
.mime_types
[i
].mime_type
== actual_mime_type
) {
958 const content::WebPluginMimeType
& content_type
= plugin
.mime_types
[i
];
959 for (size_t i
= 0; i
< content_type
.additional_param_names
.size(); ++i
) {
960 if (content_type
.additional_param_names
[i
] == nacl_attr
)
961 return GURL(content_type
.additional_param_values
[i
]);
969 #if !defined(DISABLE_NACL)
971 bool ChromeContentRendererClient::IsNaClAllowed(
972 const GURL
& manifest_url
,
974 bool is_nacl_unrestricted
,
975 const Extension
* extension
,
976 WebPluginParams
* params
) {
977 // Temporarily allow these whitelisted apps and WebUIs to use NaCl.
978 std::string app_url_host
= app_url
.host();
979 std::string manifest_url_path
= manifest_url
.path();
981 bool is_whitelisted_web_ui
=
982 app_url
.spec() == chrome::kChromeUIAppListStartPageURL
;
985 // Whitelisted apps must be served over https.
986 app_url
.SchemeIs("https") &&
987 manifest_url
.SchemeIs("https") &&
988 (EndsWith(app_url_host
, "plus.google.com", false) ||
989 EndsWith(app_url_host
, "plus.sandbox.google.com", false)) &&
990 manifest_url
.DomainIs("ssl.gstatic.com") &&
991 (manifest_url_path
.find("s2/oz/nacl/") == 1 ||
992 manifest_url_path
.find("photos/nacl/") == 1);
994 std::string manifest_fs_host
;
995 if (manifest_url
.SchemeIsFileSystem() && manifest_url
.inner_url()) {
996 manifest_fs_host
= manifest_url
.inner_url()->host();
998 bool is_hangouts_app
=
999 // Whitelisted apps must be served over secure scheme.
1000 app_url
.SchemeIs("https") &&
1001 manifest_url
.SchemeIsSecure() &&
1002 manifest_url
.SchemeIsFileSystem() &&
1003 (EndsWith(app_url_host
, "talkgadget.google.com", false) ||
1004 EndsWith(app_url_host
, "plus.google.com", false) ||
1005 EndsWith(app_url_host
, "plus.sandbox.google.com", false)) &&
1006 // The manifest must be loaded from the host's FileSystem.
1007 (manifest_fs_host
== app_url_host
);
1009 bool is_whitelisted_app
= is_photo_app
|| is_hangouts_app
;
1011 bool is_extension_from_webstore
= false;
1012 bool is_invoked_by_hosted_app
= false;
1013 bool is_extension_unrestricted
= false;
1014 #if defined(ENABLE_EXTENSIONS)
1015 is_extension_from_webstore
= extension
&& extension
->from_webstore();
1017 is_invoked_by_hosted_app
= extension
&&
1018 extension
->is_hosted_app() &&
1019 extension
->web_extent().MatchesURL(app_url
);
1021 // Allow built-in extensions and extensions under development.
1022 is_extension_unrestricted
= extension
&&
1023 (extension
->location() == extensions::Manifest::COMPONENT
||
1024 extensions::Manifest::IsUnpackedLocation(extension
->location()));
1025 #endif // defined(ENABLE_EXTENSIONS)
1027 bool is_invoked_by_extension
= app_url
.SchemeIs("chrome-extension");
1029 // The NaCl PDF viewer is always allowed and can use 'Dev' interfaces.
1030 bool is_nacl_pdf_viewer
=
1031 (is_extension_from_webstore
&&
1032 manifest_url
.SchemeIs("chrome-extension") &&
1033 manifest_url
.host() == "acadkphlmlegjaadjagenfimbpphcgnh");
1035 // Allow Chrome Web Store extensions, built-in extensions and extensions
1036 // under development if the invocation comes from a URL with an extension
1037 // scheme. Also allow invocations if they are from whitelisted URLs or
1038 // if --enable-nacl is set.
1039 bool is_nacl_allowed
= is_nacl_unrestricted
||
1040 is_whitelisted_web_ui
||
1041 is_whitelisted_app
||
1042 is_nacl_pdf_viewer
||
1043 is_invoked_by_hosted_app
||
1044 (is_invoked_by_extension
&&
1045 (is_extension_from_webstore
||
1046 is_extension_unrestricted
));
1047 if (is_nacl_allowed
) {
1048 bool app_can_use_dev_interfaces
= is_nacl_pdf_viewer
;
1049 // Make sure that PPAPI 'dev' interfaces aren't available for production
1050 // apps unless they're whitelisted.
1051 WebString dev_attribute
= WebString::fromUTF8("@dev");
1052 if ((!is_whitelisted_app
&& !is_extension_from_webstore
) ||
1053 app_can_use_dev_interfaces
) {
1054 // Add the special '@dev' attribute.
1055 std::vector
<base::string16
> param_names
;
1056 std::vector
<base::string16
> param_values
;
1057 param_names
.push_back(dev_attribute
);
1058 param_values
.push_back(WebString());
1062 ¶ms
->attributeNames
,
1063 ¶ms
->attributeValues
);
1065 // If the params somehow contain '@dev', remove it.
1066 size_t attribute_count
= params
->attributeNames
.size();
1067 for (size_t i
= 0; i
< attribute_count
; ++i
) {
1068 if (params
->attributeNames
[i
].equals(dev_attribute
))
1069 params
->attributeNames
[i
] = WebString();
1073 return is_nacl_allowed
;
1075 #endif // defined(DISABLE_NACL)
1077 bool ChromeContentRendererClient::HasErrorPage(int http_status_code
,
1078 std::string
* error_domain
) {
1079 // Use an internal error page, if we have one for the status code.
1080 if (!LocalizedError::HasStrings(LocalizedError::kHttpErrorDomain
,
1081 http_status_code
)) {
1085 *error_domain
= LocalizedError::kHttpErrorDomain
;
1089 bool ChromeContentRendererClient::ShouldSuppressErrorPage(
1090 content::RenderFrame
* render_frame
,
1092 // Unit tests for ChromeContentRendererClient pass a NULL RenderFrame here.
1093 // Unfortunately it's very difficult to construct a mock RenderView, so skip
1094 // this functionality in this case.
1096 content::RenderView
* render_view
= render_frame
->GetRenderView();
1097 content::RenderFrame
* main_render_frame
= render_view
->GetMainRenderFrame();
1098 blink::WebFrame
* web_frame
= render_frame
->GetWebFrame();
1099 NetErrorHelper
* net_error_helper
= NetErrorHelper::Get(main_render_frame
);
1100 if (net_error_helper
->ShouldSuppressErrorPage(web_frame
, url
))
1103 // Do not flash an error page if the Instant new tab page fails to load.
1104 return search_bouncer_
.get() && search_bouncer_
->IsNewTabPage(url
);
1107 void ChromeContentRendererClient::GetNavigationErrorStrings(
1108 content::RenderView
* render_view
,
1109 blink::WebFrame
* frame
,
1110 const blink::WebURLRequest
& failed_request
,
1111 const blink::WebURLError
& error
,
1112 std::string
* error_html
,
1113 base::string16
* error_description
) {
1114 const GURL failed_url
= error
.unreachableURL
;
1115 const Extension
* extension
= NULL
;
1117 #if defined(ENABLE_EXTENSIONS)
1118 if (failed_url
.is_valid() &&
1119 !failed_url
.SchemeIs(extensions::kExtensionScheme
)) {
1120 extension
= extension_dispatcher_
->extensions()->GetExtensionOrAppByURL(
1125 bool is_post
= EqualsASCII(failed_request
.httpMethod(), "POST");
1128 bool extension_but_not_bookmark_app
= false;
1129 #if defined(ENABLE_EXTENSIONS)
1130 extension_but_not_bookmark_app
= extension
&& !extension
->from_bookmark();
1132 // Use a local error page.
1133 if (extension_but_not_bookmark_app
) {
1134 #if defined(ENABLE_EXTENSIONS)
1135 // TODO(erikkay): Should we use a different template for different
1137 int resource_id
= IDR_ERROR_APP_HTML
;
1138 const base::StringPiece
template_html(
1139 ResourceBundle::GetSharedInstance().GetRawDataResource(
1141 if (template_html
.empty()) {
1142 NOTREACHED() << "unable to load template. ID: " << resource_id
;
1144 base::DictionaryValue error_strings
;
1145 LocalizedError::GetAppErrorStrings(failed_url
, extension
,
1147 // "t" is the id of the template's root node.
1148 *error_html
= webui::GetTemplatesHtml(template_html
, &error_strings
,
1153 // TODO(ellyjones): change GetNavigationErrorStrings to take a RenderFrame
1154 // instead of a RenderView, then pass that in.
1155 // This is safe for now because we only install the NetErrorHelper on the
1156 // main render frame anyway; see the TODO(ellyjones) in
1157 // RenderFrameCreated.
1158 content::RenderFrame
* main_render_frame
=
1159 render_view
->GetMainRenderFrame();
1160 NetErrorHelper
* helper
= NetErrorHelper::Get(main_render_frame
);
1161 helper
->GetErrorHTML(frame
, error
, is_post
, error_html
);
1165 if (error_description
) {
1167 *error_description
= LocalizedError::GetErrorDetails(error
, is_post
);
1171 bool ChromeContentRendererClient::RunIdleHandlerWhenWidgetsHidden() {
1172 #if defined(ENABLE_EXTENSIONS)
1173 return !extension_dispatcher_
->is_extension_process();
1179 bool ChromeContentRendererClient::AllowPopup() {
1180 #if defined(ENABLE_EXTENSIONS)
1181 extensions::ScriptContext
* current_context
=
1182 extension_dispatcher_
->script_context_set().GetCurrent();
1183 if (!current_context
|| !current_context
->extension())
1185 // See http://crbug.com/117446 for the subtlety of this check.
1186 switch (current_context
->context_type()) {
1187 case extensions::Feature::UNSPECIFIED_CONTEXT
:
1188 case extensions::Feature::WEB_PAGE_CONTEXT
:
1189 case extensions::Feature::UNBLESSED_EXTENSION_CONTEXT
:
1190 case extensions::Feature::WEBUI_CONTEXT
:
1192 case extensions::Feature::BLESSED_EXTENSION_CONTEXT
:
1193 case extensions::Feature::CONTENT_SCRIPT_CONTEXT
:
1195 case extensions::Feature::BLESSED_WEB_PAGE_CONTEXT
:
1196 return !current_context
->web_frame()->parent();
1203 bool ChromeContentRendererClient::ShouldFork(WebFrame
* frame
,
1205 const std::string
& http_method
,
1206 bool is_initial_navigation
,
1207 bool is_server_redirect
,
1208 bool* send_referrer
) {
1209 DCHECK(!frame
->parent());
1211 // If this is the Instant process, fork all navigations originating from the
1212 // renderer. The destination page will then be bucketed back to this Instant
1213 // process if it is an Instant url, or to another process if not. Conversely,
1214 // fork if this is a non-Instant process navigating to an Instant url, so that
1215 // such navigations can also be bucketed into an Instant renderer.
1216 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kInstantProcess
) ||
1217 (search_bouncer_
.get() && search_bouncer_
->ShouldFork(url
))) {
1218 *send_referrer
= true;
1222 // For now, we skip the rest for POST submissions. This is because
1223 // http://crbug.com/101395 is more likely to cause compatibility issues
1224 // with hosted apps and extensions than WebUI pages. We will remove this
1225 // check when cross-process POST submissions are supported.
1226 if (http_method
!= "GET")
1229 // If this is the Signin process, fork all navigations originating from the
1230 // renderer. The destination page will then be bucketed back to this Signin
1231 // process if it is a Signin url, or to another process if not.
1232 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSigninProcess
)) {
1233 // We never want to allow non-signin pages to fork-on-POST to a
1234 // signin-related action URL. We'll need to handle this carefully once
1235 // http://crbug.com/101395 is fixed. The CHECK ensures we don't forget.
1236 CHECK_NE(http_method
, "POST");
1240 // If |url| matches one of the prerendered URLs, stop this navigation and try
1241 // to swap in the prerendered page on the browser process. If the prerendered
1242 // page no longer exists by the time the OpenURL IPC is handled, a normal
1243 // navigation is attempted.
1244 if (prerender_dispatcher_
.get() &&
1245 prerender_dispatcher_
->IsPrerenderURL(url
)) {
1246 *send_referrer
= true;
1250 #if defined(ENABLE_EXTENSIONS)
1251 const extensions::ExtensionSet
* extensions
=
1252 extension_dispatcher_
->extensions();
1254 // Determine if the new URL is an extension (excluding bookmark apps).
1255 const Extension
* new_url_extension
= extensions::GetNonBookmarkAppExtension(
1257 bool is_extension_url
= !!new_url_extension
;
1259 // If the navigation would cross an app extent boundary, we also need
1260 // to defer to the browser to ensure process isolation. This is not necessary
1261 // for server redirects, which will be transferred to a new process by the
1262 // browser process when they are ready to commit. It is necessary for client
1263 // redirects, which won't be transferred in the same way.
1264 if (!is_server_redirect
&&
1265 CrossesExtensionExtents(frame
, url
, *extensions
, is_extension_url
,
1266 is_initial_navigation
)) {
1267 // Include the referrer in this case since we're going from a hosted web
1268 // page. (the packaged case is handled previously by the extension
1270 *send_referrer
= true;
1272 const Extension
* extension
=
1273 extension_dispatcher_
->extensions()->GetExtensionOrAppByURL(url
);
1274 if (extension
&& extension
->is_app()) {
1275 UMA_HISTOGRAM_ENUMERATION(
1276 extension
->is_platform_app() ?
1277 extension_misc::kPlatformAppLaunchHistogram
:
1278 extension_misc::kAppLaunchHistogram
,
1279 extension_misc::APP_LAUNCH_CONTENT_NAVIGATION
,
1280 extension_misc::APP_LAUNCH_BUCKET_BOUNDARY
);
1285 // If this is a reload, check whether it has the wrong process type. We
1286 // should send it to the browser if it's an extension URL (e.g., hosted app)
1287 // in a normal process, or if it's a process for an extension that has been
1289 if (frame
->top()->document().url() == url
) {
1290 if (is_extension_url
!= extension_dispatcher_
->is_extension_process())
1293 #endif // defined(ENABLE_EXTENSIONS)
1298 bool ChromeContentRendererClient::WillSendRequest(
1299 blink::WebFrame
* frame
,
1300 ui::PageTransition transition_type
,
1302 const GURL
& first_party_for_cookies
,
1304 // Check whether the request should be allowed. If not allowed, we reset the
1305 // URL to something invalid to prevent the request and cause an error.
1306 #if defined(ENABLE_EXTENSIONS)
1307 if (url
.SchemeIs(extensions::kExtensionScheme
) &&
1308 !extensions::ResourceRequestPolicy::CanRequestResource(
1312 extension_dispatcher_
->extensions())) {
1313 *new_url
= GURL(chrome::kExtensionInvalidRequestURL
);
1317 if (url
.SchemeIs(extensions::kExtensionResourceScheme
) &&
1318 !extensions::ResourceRequestPolicy::CanRequestExtensionResourceScheme(
1321 *new_url
= GURL(chrome::kExtensionResourceInvalidRequestURL
);
1326 const content::RenderView
* render_view
=
1327 content::RenderView::FromWebView(frame
->view());
1328 SearchBox
* search_box
= SearchBox::Get(render_view
);
1329 if (search_box
&& url
.SchemeIs(chrome::kChromeSearchScheme
)) {
1330 if (url
.host() == chrome::kChromeUIThumbnailHost
)
1331 return search_box
->GenerateThumbnailURLFromTransientURL(url
, new_url
);
1332 else if (url
.host() == chrome::kChromeUIFaviconHost
)
1333 return search_box
->GenerateFaviconURLFromTransientURL(url
, new_url
);
1339 void ChromeContentRendererClient::DidCreateScriptContext(
1340 WebFrame
* frame
, v8::Handle
<v8::Context
> context
, int extension_group
,
1342 #if defined(ENABLE_EXTENSIONS)
1343 extension_dispatcher_
->DidCreateScriptContext(
1344 frame
, context
, extension_group
, world_id
);
1348 unsigned long long ChromeContentRendererClient::VisitedLinkHash(
1349 const char* canonical_url
, size_t length
) {
1350 return visited_link_slave_
->ComputeURLFingerprint(canonical_url
, length
);
1353 bool ChromeContentRendererClient::IsLinkVisited(unsigned long long link_hash
) {
1354 return visited_link_slave_
->IsVisited(link_hash
);
1357 blink::WebPrescientNetworking
*
1358 ChromeContentRendererClient::GetPrescientNetworking() {
1359 return prescient_networking_dispatcher_
.get();
1362 bool ChromeContentRendererClient::ShouldOverridePageVisibilityState(
1363 const content::RenderFrame
* render_frame
,
1364 blink::WebPageVisibilityState
* override_state
) {
1365 if (!prerender::PrerenderHelper::IsPrerendering(render_frame
))
1368 *override_state
= blink::WebPageVisibilityStatePrerender
;
1372 #if defined(ENABLE_EXTENSIONS)
1373 void ChromeContentRendererClient::SetExtensionDispatcherForTest(
1374 extensions::Dispatcher
* extension_dispatcher
) {
1375 extension_dispatcher_
.reset(extension_dispatcher
);
1376 permissions_policy_delegate_
.reset(
1377 new extensions::RendererPermissionsPolicyDelegate(
1378 extension_dispatcher_
.get()));
1381 extensions::Dispatcher
*
1382 ChromeContentRendererClient::GetExtensionDispatcherForTest() {
1383 return extension_dispatcher_
.get();
1386 bool ChromeContentRendererClient::CrossesExtensionExtents(
1388 const GURL
& new_url
,
1389 const extensions::ExtensionSet
& extensions
,
1390 bool is_extension_url
,
1391 bool is_initial_navigation
) {
1392 GURL
old_url(frame
->top()->document().url());
1394 // If old_url is still empty and this is an initial navigation, then this is
1395 // a window.open operation. We should look at the opener URL.
1396 if (is_initial_navigation
&& old_url
.is_empty() && frame
->opener()) {
1397 // If we're about to open a normal web page from a same-origin opener stuck
1398 // in an extension process, we want to keep it in process to allow the
1399 // opener to script it.
1400 WebDocument opener_document
= frame
->opener()->document();
1401 WebSecurityOrigin opener
= frame
->opener()->document().securityOrigin();
1402 bool opener_is_extension_url
=
1403 !opener
.isUnique() && extensions
.GetExtensionOrAppByURL(
1404 opener_document
.url()) != NULL
;
1405 if (!is_extension_url
&&
1406 !opener_is_extension_url
&&
1407 extension_dispatcher_
->is_extension_process() &&
1408 opener
.canRequest(WebURL(new_url
)))
1411 // In all other cases, we want to compare against the top frame's URL (as
1412 // opposed to the opener frame's), since that's what determines the type of
1413 // process. This allows iframes outside an app to open a popup in the app.
1414 old_url
= frame
->top()->opener()->top()->document().url();
1417 // Only consider keeping non-app URLs in an app process if this window
1418 // has an opener (in which case it might be an OAuth popup that tries to
1419 // script an iframe within the app).
1420 bool should_consider_workaround
= !!frame
->opener();
1422 return extensions::CrossesExtensionProcessBoundary(
1423 extensions
, old_url
, new_url
, should_consider_workaround
);
1425 #endif // defined(ENABLE_EXTENSIONS)
1427 #if defined(ENABLE_SPELLCHECK)
1428 void ChromeContentRendererClient::SetSpellcheck(SpellCheck
* spellcheck
) {
1429 RenderThread
* thread
= RenderThread::Get();
1430 if (spellcheck_
.get() && thread
)
1431 thread
->RemoveObserver(spellcheck_
.get());
1432 spellcheck_
.reset(spellcheck
);
1433 SpellCheckReplacer
replacer(spellcheck_
.get());
1434 content::RenderView::ForEach(&replacer
);
1436 thread
->AddObserver(spellcheck_
.get());
1441 bool ChromeContentRendererClient::WasWebRequestUsedBySomeExtensions() {
1442 #if defined(ENABLE_EXTENSIONS)
1443 return g_current_client
->extension_dispatcher_
1444 ->WasWebRequestUsedBySomeExtensions();
1450 const void* ChromeContentRendererClient::CreatePPAPIInterface(
1451 const std::string
& interface_name
) {
1452 #if defined(ENABLE_PLUGINS)
1453 #if !defined(DISABLE_NACL)
1454 if (interface_name
== PPB_NACL_PRIVATE_INTERFACE
)
1455 return nacl::GetNaClPrivateInterface();
1456 #endif // DISABLE_NACL
1457 if (interface_name
== PPB_PDF_INTERFACE
)
1458 return pdf::PPB_PDF_Impl::GetInterface();
1463 bool ChromeContentRendererClient::IsExternalPepperPlugin(
1464 const std::string
& module_name
) {
1465 // TODO(bbudge) remove this when the trusted NaCl plugin has been removed.
1466 // We must defer certain plugin events for NaCl instances since we switch
1467 // from the in-process to the out-of-process proxy after instantiating them.
1468 return module_name
== "Native Client";
1471 #if defined(ENABLE_PLUGINS) && defined(ENABLE_EXTENSIONS)
1472 bool ChromeContentRendererClient::IsExtensionOrSharedModuleWhitelisted(
1473 const GURL
& url
, const std::set
<std::string
>& whitelist
) {
1474 const extensions::ExtensionSet
* extension_set
=
1475 g_current_client
->extension_dispatcher_
->extensions();
1476 return chrome::IsExtensionOrSharedModuleWhitelisted(url
, extension_set
,
1481 blink::WebSpeechSynthesizer
*
1482 ChromeContentRendererClient::OverrideSpeechSynthesizer(
1483 blink::WebSpeechSynthesizerClient
* client
) {
1484 return new TtsDispatcher(client
);
1487 bool ChromeContentRendererClient::AllowPepperMediaStreamAPI(
1489 #if !defined(OS_ANDROID)
1490 // Allow only the Hangouts app to use the MediaStream APIs. It's OK to check
1491 // the whitelist in the renderer, since we're only preventing access until
1492 // these APIs are public and stable.
1493 std::string url_host
= url
.host();
1494 if (url
.SchemeIs("https") &&
1495 (EndsWith(url_host
, "talkgadget.google.com", false) ||
1496 EndsWith(url_host
, "plus.google.com", false) ||
1497 EndsWith(url_host
, "plus.sandbox.google.com", false)) &&
1498 StartsWithASCII(url
.path(), "/hangouts/", false)) {
1501 // Allow access for tests.
1502 if (CommandLine::ForCurrentProcess()->HasSwitch(
1503 switches::kEnablePepperTesting
)) {
1506 #endif // !defined(OS_ANDROID)
1510 void ChromeContentRendererClient::AddKeySystems(
1511 std::vector
<media::KeySystemInfo
>* key_systems
) {
1512 AddChromeKeySystems(key_systems
);
1515 bool ChromeContentRendererClient::ShouldReportDetailedMessageForSource(
1516 const base::string16
& source
) const {
1517 #if defined(ENABLE_EXTENSIONS)
1518 return extensions::IsSourceFromAnExtension(source
);
1524 bool ChromeContentRendererClient::ShouldEnableSiteIsolationPolicy() const {
1525 // SiteIsolationPolicy is off by default. We would like to activate cross-site
1526 // document blocking (for UMA data collection) for normal renderer processes
1527 // running a normal web page from the Internet. We only turn on
1528 // SiteIsolationPolicy for a renderer process that does not have the extension
1530 #if defined(ENABLE_EXTENSIONS)
1531 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
1532 return !command_line
->HasSwitch(extensions::switches::kExtensionProcess
);
1538 blink::WebWorkerPermissionClientProxy
*
1539 ChromeContentRendererClient::CreateWorkerPermissionClientProxy(
1540 content::RenderFrame
* render_frame
,
1541 blink::WebFrame
* frame
) {
1542 return new WorkerPermissionClientProxy(render_frame
, frame
);
1545 bool ChromeContentRendererClient::IsPluginAllowedToUseDevChannelAPIs() {
1546 #if defined(ENABLE_PLUGINS)
1547 // Allow access for tests.
1548 if (CommandLine::ForCurrentProcess()->HasSwitch(
1549 switches::kEnablePepperTesting
)) {
1553 chrome::VersionInfo::Channel channel
= chrome::VersionInfo::GetChannel();
1554 // Allow dev channel APIs to be used on "Canary", "Dev", and "Unknown"
1555 // releases of Chrome. Permitting "Unknown" allows these APIs to be used on
1556 // Chromium builds as well.
1557 return channel
<= chrome::VersionInfo::CHANNEL_DEV
;
1563 bool ChromeContentRendererClient::IsPluginAllowedToUseCompositorAPI(
1565 #if defined(ENABLE_PLUGINS) && defined(ENABLE_EXTENSIONS)
1566 if (CommandLine::ForCurrentProcess()->HasSwitch(
1567 switches::kEnablePepperTesting
))
1569 if (IsExtensionOrSharedModuleWhitelisted(url
, allowed_compositor_origins_
))
1572 chrome::VersionInfo::Channel channel
= chrome::VersionInfo::GetChannel();
1573 return channel
<= chrome::VersionInfo::CHANNEL_DEV
;
1579 bool ChromeContentRendererClient::IsPluginAllowedToUseVideoDecodeAPI(
1581 #if defined(ENABLE_PLUGINS) && defined(ENABLE_EXTENSIONS)
1582 if (CommandLine::ForCurrentProcess()->HasSwitch(
1583 switches::kEnablePepperTesting
))
1586 if (IsExtensionOrSharedModuleWhitelisted(url
, allowed_video_decode_origins_
))
1589 chrome::VersionInfo::Channel channel
= chrome::VersionInfo::GetChannel();
1590 return channel
<= chrome::VersionInfo::CHANNEL_DEV
;
1596 content::BrowserPluginDelegate
*
1597 ChromeContentRendererClient::CreateBrowserPluginDelegate(
1598 content::RenderFrame
* render_frame
,
1599 const std::string
& mime_type
) {
1600 #if defined(ENABLE_EXTENSIONS)
1601 return new extensions::GuestViewContainer(render_frame
, mime_type
);