aw_content_renderer_client.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 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.
  4. #include "android_webview/renderer/aw_content_renderer_client.h"
  5. #include <memory>
  6. #include <vector>
  7. #include "android_webview/common/aw_switches.h"
  8. #include "android_webview/common/mojom/frame.mojom.h"
  9. #include "android_webview/common/url_constants.h"
  10. #include "android_webview/renderer/aw_content_settings_client.h"
  11. #include "android_webview/renderer/aw_key_systems.h"
  12. #include "android_webview/renderer/aw_print_render_frame_helper_delegate.h"
  13. #include "android_webview/renderer/aw_render_frame_ext.h"
  14. #include "android_webview/renderer/aw_render_view_ext.h"
  15. #include "android_webview/renderer/aw_safe_browsing_error_page_controller_delegate_impl.h"
  16. #include "android_webview/renderer/aw_url_loader_throttle_provider.h"
  17. #include "android_webview/renderer/aw_websocket_handshake_throttle_provider.h"
  18. #include "android_webview/renderer/browser_exposed_renderer_interfaces.h"
  19. #include "base/command_line.h"
  20. #include "base/i18n/rtl.h"
  21. #include "base/metrics/histogram_macros.h"
  22. #include "base/strings/string_util.h"
  23. #include "components/android_system_error_page/error_page_populator.h"
  24. #include "components/js_injection/renderer/js_communication.h"
  25. #include "components/page_load_metrics/renderer/metrics_render_frame_observer.h"
  26. #include "components/printing/renderer/print_render_frame_helper.h"
  27. #include "components/visitedlink/renderer/visitedlink_reader.h"
  28. #include "content/public/child/child_thread.h"
  29. #include "content/public/common/url_constants.h"
  30. #include "content/public/renderer/render_frame.h"
  31. #include "content/public/renderer/render_thread.h"
  32. #include "ipc/ipc_sync_channel.h"
  33. #include "mojo/public/cpp/bindings/binder_map.h"
  34. #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
  35. #include "third_party/blink/public/platform/platform.h"
  36. #include "third_party/blink/public/platform/web_string.h"
  37. #include "third_party/blink/public/platform/web_url.h"
  38. #include "third_party/blink/public/platform/web_url_request.h"
  39. #include "third_party/blink/public/web/web_frame.h"
  40. #include "third_party/blink/public/web/web_navigation_type.h"
  41. #include "third_party/blink/public/web/web_security_policy.h"
  42. #include "url/gurl.h"
  43. #include "url/url_constants.h"
  44. #if BUILDFLAG(ENABLE_SPELLCHECK)
  45. #include "components/spellcheck/renderer/spellcheck.h"
  46. #include "components/spellcheck/renderer/spellcheck_provider.h"
  47. #endif
  48. using content::RenderThread;
  49. namespace android_webview {
  50. AwContentRendererClient::AwContentRendererClient() = default;
  51. AwContentRendererClient::~AwContentRendererClient() = default;
  52. void AwContentRendererClient::RenderThreadStarted() {
  53. RenderThread* thread = RenderThread::Get();
  54. aw_render_thread_observer_ = std::make_unique<AwRenderThreadObserver>();
  55. thread->AddObserver(aw_render_thread_observer_.get());
  56. visited_link_reader_ = std::make_unique<visitedlink::VisitedLinkReader>();
  57. browser_interface_broker_ =
  58. blink::Platform::Current()->GetBrowserInterfaceBroker();
  59. #if BUILDFLAG(ENABLE_SPELLCHECK)
  60. if (!spellcheck_)
  61. spellcheck_ = std::make_unique<SpellCheck>(this);
  62. #endif
  63. }
  64. void AwContentRendererClient::ExposeInterfacesToBrowser(
  65. mojo::BinderMap* binders) {
  66. // NOTE: Do not add binders directly within this method. Instead, modify the
  67. // definition of |ExposeRendererInterfacesToBrowser()| to ensure security
  68. // review coverage.
  69. ExposeRendererInterfacesToBrowser(this, binders);
  70. }
  71. bool AwContentRendererClient::HandleNavigation(
  72. content::RenderFrame* render_frame,
  73. blink::WebFrame* frame,
  74. const blink::WebURLRequest& request,
  75. blink::WebNavigationType type,
  76. blink::WebNavigationPolicy default_policy,
  77. bool is_redirect) {
  78. // Only GETs can be overridden.
  79. if (!request.HttpMethod().Equals("GET"))
  80. return false;
  81. // Any navigation from loadUrl, and goBack/Forward are considered application-
  82. // initiated and hence will not yield a shouldOverrideUrlLoading() callback.
  83. // Webview classic does not consider reload application-initiated so we
  84. // continue the same behavior.
  85. bool application_initiated = type == blink::kWebNavigationTypeBackForward;
  86. // Don't offer application-initiated navigations unless it's a redirect.
  87. if (application_initiated && !is_redirect)
  88. return false;
  89. bool is_outermost_main_frame = frame->IsOutermostMainFrame();
  90. const GURL& gurl = request.Url();
  91. // For HTTP schemes, only top-level navigations can be overridden. Similarly,
  92. // WebView Classic lets app override only top level about:blank navigations.
  93. // So we filter out non-top about:blank navigations here.
  94. if (!is_outermost_main_frame &&
  95. (gurl.SchemeIs(url::kHttpScheme) || gurl.SchemeIs(url::kHttpsScheme) ||
  96. gurl.SchemeIs(url::kAboutScheme)))
  97. return false;
  98. AwRenderViewExt* view =
  99. AwRenderViewExt::FromWebView(render_frame->GetWebView());
  100. // use NavigationInterception throttle to handle the call as that can
  101. // be deferred until after the java side has been constructed.
  102. //
  103. // TODO(nick): `view->created_by_renderer()` was plumbed in to
  104. // preserve the existing code behavior, but it doesn't appear to be correct.
  105. // In particular, this value will be true for the initial navigation of a
  106. // RenderView created via window.open(), but it will also be true for all
  107. // subsequent navigations in that RenderView, no matter how they are
  108. // initiated.
  109. if (view->created_by_renderer()) {
  110. return false;
  111. }
  112. bool ignore_navigation = false;
  113. std::u16string url = request.Url().GetString().Utf16();
  114. bool has_user_gesture = request.HasUserGesture();
  115. mojo::AssociatedRemote<mojom::FrameHost> frame_host_remote;
  116. render_frame->GetRemoteAssociatedInterfaces()->GetInterface(
  117. &frame_host_remote);
  118. frame_host_remote->ShouldOverrideUrlLoading(
  119. url, has_user_gesture, is_redirect, is_outermost_main_frame,
  120. &ignore_navigation);
  121. return ignore_navigation;
  122. }
  123. void AwContentRendererClient::RenderFrameCreated(
  124. content::RenderFrame* render_frame) {
  125. new AwContentSettingsClient(render_frame);
  126. new printing::PrintRenderFrameHelper(
  127. render_frame, std::make_unique<AwPrintRenderFrameHelperDelegate>());
  128. new AwRenderFrameExt(render_frame);
  129. new js_injection::JsCommunication(render_frame);
  130. new AwSafeBrowsingErrorPageControllerDelegateImpl(render_frame);
  131. content::RenderFrame* main_frame = render_frame->GetMainRenderFrame();
  132. if (main_frame && main_frame != render_frame) {
  133. // Avoid any race conditions from having the browser's UI thread tell the IO
  134. // thread that a subframe was created.
  135. GetRenderMessageFilter()->SubFrameCreated(main_frame->GetRoutingID(),
  136. render_frame->GetRoutingID());
  137. }
  138. #if BUILDFLAG(ENABLE_SPELLCHECK)
  139. new SpellCheckProvider(render_frame, spellcheck_.get(), this);
  140. #endif
  141. // Owned by |render_frame|.
  142. new page_load_metrics::MetricsRenderFrameObserver(render_frame);
  143. }
  144. void AwContentRendererClient::WebViewCreated(blink::WebView* web_view,
  145. bool was_created_by_renderer) {
  146. AwRenderViewExt::WebViewCreated(web_view, was_created_by_renderer);
  147. }
  148. void AwContentRendererClient::PrepareErrorPage(
  149. content::RenderFrame* render_frame,
  150. const blink::WebURLError& error,
  151. const std::string& http_method,
  152. content::mojom::AlternativeErrorPageOverrideInfoPtr
  153. alternative_error_page_info,
  154. std::string* error_html) {
  155. AwSafeBrowsingErrorPageControllerDelegateImpl::Get(render_frame)
  156. ->PrepareForErrorPage();
  157. android_system_error_page::PopulateErrorPageHtml(error, error_html);
  158. }
  159. uint64_t AwContentRendererClient::VisitedLinkHash(const char* canonical_url,
  160. size_t length) {
  161. return visited_link_reader_->ComputeURLFingerprint(canonical_url, length);
  162. }
  163. bool AwContentRendererClient::IsLinkVisited(uint64_t link_hash) {
  164. return visited_link_reader_->IsVisited(link_hash);
  165. }
  166. void AwContentRendererClient::RunScriptsAtDocumentStart(
  167. content::RenderFrame* render_frame) {
  168. js_injection::JsCommunication* communication =
  169. js_injection::JsCommunication::Get(render_frame);
  170. communication->RunScriptsAtDocumentStart();
  171. }
  172. void AwContentRendererClient::GetSupportedKeySystems(
  173. media::GetSupportedKeySystemsCB cb) {
  174. media::KeySystemPropertiesVector key_systems;
  175. AwAddKeySystems(&key_systems);
  176. std::move(cb).Run(std::move(key_systems));
  177. }
  178. std::unique_ptr<blink::WebSocketHandshakeThrottleProvider>
  179. AwContentRendererClient::CreateWebSocketHandshakeThrottleProvider() {
  180. return std::make_unique<AwWebSocketHandshakeThrottleProvider>(
  181. browser_interface_broker_.get());
  182. }
  183. std::unique_ptr<blink::URLLoaderThrottleProvider>
  184. AwContentRendererClient::CreateURLLoaderThrottleProvider(
  185. blink::URLLoaderThrottleProviderType provider_type) {
  186. return std::make_unique<AwURLLoaderThrottleProvider>(
  187. browser_interface_broker_.get(), provider_type);
  188. }
  189. void AwContentRendererClient::GetInterface(
  190. const std::string& interface_name,
  191. mojo::ScopedMessagePipeHandle interface_pipe) {
  192. // A dirty hack to make SpellCheckHost requests work on WebView.
  193. // TODO(crbug.com/806394): Use a WebView-specific service for SpellCheckHost
  194. // and SafeBrowsing, instead of |content_browser|.
  195. RenderThread::Get()->BindHostReceiver(
  196. mojo::GenericPendingReceiver(interface_name, std::move(interface_pipe)));
  197. }
  198. mojom::RenderMessageFilter* AwContentRendererClient::GetRenderMessageFilter() {
  199. if (!render_message_filter_) {
  200. RenderThread::Get()->GetChannel()->GetRemoteAssociatedInterface(
  201. &render_message_filter_);
  202. }
  203. return render_message_filter_.get();
  204. }
  205. } // namespace android_webview