content_renderer_client_impl.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "weblayer/renderer/content_renderer_client_impl.h"
  5. #include "base/feature_list.h"
  6. #include "build/build_config.h"
  7. #include "components/autofill/content/renderer/autofill_agent.h"
  8. #include "components/autofill/content/renderer/password_autofill_agent.h"
  9. #include "components/content_capture/common/content_capture_features.h"
  10. #include "components/content_capture/renderer/content_capture_sender.h"
  11. #include "components/content_settings/renderer/content_settings_agent_impl.h"
  12. #include "components/error_page/common/error.h"
  13. #include "components/grit/components_scaled_resources.h"
  14. #include "components/js_injection/renderer/js_communication.h"
  15. #include "components/no_state_prefetch/common/prerender_url_loader_throttle.h"
  16. #include "components/no_state_prefetch/renderer/no_state_prefetch_client.h"
  17. #include "components/no_state_prefetch/renderer/no_state_prefetch_helper.h"
  18. #include "components/no_state_prefetch/renderer/no_state_prefetch_utils.h"
  19. #include "components/no_state_prefetch/renderer/prerender_render_frame_observer.h"
  20. #include "components/page_load_metrics/renderer/metrics_render_frame_observer.h"
  21. #include "components/subresource_filter/content/renderer/ad_resource_tracker.h"
  22. #include "components/subresource_filter/content/renderer/subresource_filter_agent.h"
  23. #include "components/subresource_filter/content/renderer/unverified_ruleset_dealer.h"
  24. #include "components/subresource_filter/core/common/common_features.h"
  25. #include "components/webapps/renderer/web_page_metadata_agent.h"
  26. #include "content/public/renderer/render_frame.h"
  27. #include "content/public/renderer/render_thread.h"
  28. #include "third_party/blink/public/platform/platform.h"
  29. #include "third_party/blink/public/platform/web_runtime_features.h"
  30. #include "ui/base/resource/resource_bundle.h"
  31. #include "weblayer/common/features.h"
  32. #include "weblayer/renderer/error_page_helper.h"
  33. #include "weblayer/renderer/url_loader_throttle_provider.h"
  34. #include "weblayer/renderer/weblayer_render_frame_observer.h"
  35. #include "weblayer/renderer/weblayer_render_thread_observer.h"
  36. #if BUILDFLAG(IS_ANDROID)
  37. #include "components/android_system_error_page/error_page_populator.h"
  38. #include "components/cdm/renderer/android_key_systems.h"
  39. #include "components/spellcheck/renderer/spellcheck.h" // nogncheck
  40. #include "components/spellcheck/renderer/spellcheck_provider.h" // nogncheck
  41. #include "content/public/common/url_constants.h"
  42. #include "content/public/renderer/render_thread.h"
  43. #include "services/service_manager/public/cpp/local_interface_provider.h"
  44. #include "third_party/blink/public/platform/web_runtime_features.h"
  45. #include "third_party/blink/public/platform/web_string.h"
  46. #include "third_party/blink/public/web/web_security_policy.h"
  47. #endif
  48. namespace weblayer {
  49. namespace {
  50. #if BUILDFLAG(IS_ANDROID)
  51. class SpellcheckInterfaceProvider
  52. : public service_manager::LocalInterfaceProvider {
  53. public:
  54. SpellcheckInterfaceProvider() = default;
  55. SpellcheckInterfaceProvider(const SpellcheckInterfaceProvider&) = delete;
  56. SpellcheckInterfaceProvider& operator=(const SpellcheckInterfaceProvider&) =
  57. delete;
  58. ~SpellcheckInterfaceProvider() override = default;
  59. // service_manager::LocalInterfaceProvider:
  60. void GetInterface(const std::string& interface_name,
  61. mojo::ScopedMessagePipeHandle interface_pipe) override {
  62. // A dirty hack to make SpellCheckHost requests work on WebLayer.
  63. // TODO(crbug.com/806394): Use a WebView-specific service for SpellCheckHost
  64. // and SafeBrowsing, instead of |content_browser|.
  65. content::RenderThread::Get()->BindHostReceiver(mojo::GenericPendingReceiver(
  66. interface_name, std::move(interface_pipe)));
  67. }
  68. };
  69. #endif // BUILDFLAG(IS_ANDROID)
  70. } // namespace
  71. ContentRendererClientImpl::ContentRendererClientImpl() = default;
  72. ContentRendererClientImpl::~ContentRendererClientImpl() = default;
  73. void ContentRendererClientImpl::RenderThreadStarted() {
  74. #if BUILDFLAG(IS_ANDROID)
  75. if (!spellcheck_) {
  76. local_interface_provider_ = std::make_unique<SpellcheckInterfaceProvider>();
  77. spellcheck_ = std::make_unique<SpellCheck>(local_interface_provider_.get());
  78. }
  79. blink::WebSecurityPolicy::RegisterURLSchemeAsAllowedForReferrer(
  80. blink::WebString::FromUTF8(content::kAndroidAppScheme));
  81. #endif
  82. content::RenderThread* thread = content::RenderThread::Get();
  83. weblayer_observer_ = std::make_unique<WebLayerRenderThreadObserver>();
  84. thread->AddObserver(weblayer_observer_.get());
  85. browser_interface_broker_ =
  86. blink::Platform::Current()->GetBrowserInterfaceBroker();
  87. subresource_filter_ruleset_dealer_ =
  88. std::make_unique<subresource_filter::UnverifiedRulesetDealer>();
  89. thread->AddObserver(subresource_filter_ruleset_dealer_.get());
  90. }
  91. void ContentRendererClientImpl::RenderFrameCreated(
  92. content::RenderFrame* render_frame) {
  93. auto* render_frame_observer = new WebLayerRenderFrameObserver(render_frame);
  94. new prerender::PrerenderRenderFrameObserver(render_frame);
  95. ErrorPageHelper::Create(render_frame);
  96. autofill::PasswordAutofillAgent* password_autofill_agent =
  97. new autofill::PasswordAutofillAgent(
  98. render_frame, render_frame_observer->associated_interfaces());
  99. new autofill::AutofillAgent(render_frame, password_autofill_agent, nullptr,
  100. nullptr,
  101. render_frame_observer->associated_interfaces());
  102. auto* agent = new content_settings::ContentSettingsAgentImpl(
  103. render_frame, false /* should_whitelist */,
  104. std::make_unique<content_settings::ContentSettingsAgentImpl::Delegate>());
  105. if (weblayer_observer_) {
  106. if (weblayer_observer_->content_settings_manager()) {
  107. mojo::Remote<content_settings::mojom::ContentSettingsManager> manager;
  108. weblayer_observer_->content_settings_manager()->Clone(
  109. manager.BindNewPipeAndPassReceiver());
  110. agent->SetContentSettingsManager(std::move(manager));
  111. }
  112. }
  113. auto* metrics_render_frame_observer =
  114. new page_load_metrics::MetricsRenderFrameObserver(render_frame);
  115. auto ad_resource_tracker =
  116. std::make_unique<subresource_filter::AdResourceTracker>();
  117. metrics_render_frame_observer->SetAdResourceTracker(
  118. ad_resource_tracker.get());
  119. auto* subresource_filter_agent =
  120. new subresource_filter::SubresourceFilterAgent(
  121. render_frame, subresource_filter_ruleset_dealer_.get(),
  122. std::move(ad_resource_tracker));
  123. subresource_filter_agent->Initialize();
  124. #if BUILDFLAG(IS_ANDROID)
  125. // |SpellCheckProvider| manages its own lifetime (and destroys itself when the
  126. // RenderFrame is destroyed).
  127. new SpellCheckProvider(render_frame, spellcheck_.get(),
  128. local_interface_provider_.get());
  129. #endif
  130. new js_injection::JsCommunication(render_frame);
  131. if (render_frame->IsMainFrame())
  132. new webapps::WebPageMetadataAgent(render_frame);
  133. if (content_capture::features::IsContentCaptureEnabledInWebLayer()) {
  134. new content_capture::ContentCaptureSender(
  135. render_frame, render_frame_observer->associated_interfaces());
  136. }
  137. if (!render_frame->IsMainFrame()) {
  138. auto* main_frame_no_state_prefetch_helper =
  139. prerender::NoStatePrefetchHelper::Get(
  140. render_frame->GetMainRenderFrame());
  141. if (main_frame_no_state_prefetch_helper) {
  142. // Avoid any race conditions from having the browser tell subframes that
  143. // they're no-state prefetching.
  144. new prerender::NoStatePrefetchHelper(
  145. render_frame,
  146. main_frame_no_state_prefetch_helper->histogram_prefix());
  147. }
  148. }
  149. }
  150. void ContentRendererClientImpl::WebViewCreated(blink::WebView* web_view,
  151. bool was_created_by_renderer) {
  152. new prerender::NoStatePrefetchClient(web_view);
  153. }
  154. SkBitmap* ContentRendererClientImpl::GetSadPluginBitmap() {
  155. return const_cast<SkBitmap*>(ui::ResourceBundle::GetSharedInstance()
  156. .GetImageNamed(IDR_SAD_PLUGIN)
  157. .ToSkBitmap());
  158. }
  159. SkBitmap* ContentRendererClientImpl::GetSadWebViewBitmap() {
  160. return const_cast<SkBitmap*>(ui::ResourceBundle::GetSharedInstance()
  161. .GetImageNamed(IDR_SAD_WEBVIEW)
  162. .ToSkBitmap());
  163. }
  164. void ContentRendererClientImpl::PrepareErrorPage(
  165. content::RenderFrame* render_frame,
  166. const blink::WebURLError& error,
  167. const std::string& http_method,
  168. content::mojom::AlternativeErrorPageOverrideInfoPtr
  169. alternative_error_page_info,
  170. std::string* error_html) {
  171. auto* error_page_helper = ErrorPageHelper::GetForFrame(render_frame);
  172. if (error_page_helper)
  173. error_page_helper->PrepareErrorPage();
  174. #if BUILDFLAG(IS_ANDROID)
  175. // This does nothing if |error_html| is non-null (which happens if the
  176. // embedder injects an error page).
  177. android_system_error_page::PopulateErrorPageHtml(error, error_html);
  178. #endif
  179. }
  180. std::unique_ptr<blink::URLLoaderThrottleProvider>
  181. ContentRendererClientImpl::CreateURLLoaderThrottleProvider(
  182. blink::URLLoaderThrottleProviderType provider_type) {
  183. return std::make_unique<URLLoaderThrottleProvider>(
  184. browser_interface_broker_.get(), provider_type);
  185. }
  186. void ContentRendererClientImpl::GetSupportedKeySystems(
  187. media::GetSupportedKeySystemsCB cb) {
  188. media::KeySystemPropertiesVector key_systems;
  189. #if BUILDFLAG(IS_ANDROID)
  190. #if BUILDFLAG(ENABLE_WIDEVINE)
  191. cdm::AddAndroidWidevine(&key_systems);
  192. #endif // BUILDFLAG(ENABLE_WIDEVINE)
  193. cdm::AddAndroidPlatformKeySystems(&key_systems);
  194. #endif // BUILDFLAG(IS_ANDROID)
  195. std::move(cb).Run(std::move(key_systems));
  196. }
  197. void ContentRendererClientImpl::
  198. SetRuntimeFeaturesDefaultsBeforeBlinkInitialization() {
  199. blink::WebRuntimeFeatures::EnablePerformanceManagerInstrumentation(true);
  200. #if BUILDFLAG(IS_ANDROID)
  201. // Web Share is experimental by default, and explicitly enabled on Android
  202. // (for both Chrome and WebLayer).
  203. blink::WebRuntimeFeatures::EnableWebShare(true);
  204. #endif
  205. if (base::FeatureList::IsEnabled(subresource_filter::kAdTagging)) {
  206. blink::WebRuntimeFeatures::EnableAdTagging(true);
  207. }
  208. }
  209. bool ContentRendererClientImpl::IsPrefetchOnly(
  210. content::RenderFrame* render_frame) {
  211. return prerender::NoStatePrefetchHelper::IsPrefetching(render_frame);
  212. }
  213. bool ContentRendererClientImpl::DeferMediaLoad(
  214. content::RenderFrame* render_frame,
  215. bool has_played_media_before,
  216. base::OnceClosure closure) {
  217. return prerender::DeferMediaLoad(render_frame, has_played_media_before,
  218. std::move(closure));
  219. }
  220. } // namespace weblayer