[go: nahoru, domu]

blob: e9df9c81073928f0b341589697c50cf1d39520ee [file] [log] [blame]
kaanb@chromium.org73ae0f3e2013-04-08 20:38:151// Copyright 2012 The Chromium Authors. All rights reserved.
boliu@chromium.org710b2602012-10-11 21:41:092// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "android_webview/renderer/aw_content_renderer_client.h"
6
Gyuyoung Kimdae5f102017-12-04 15:55:387#include <memory>
mnaganovf082f6c2015-08-05 20:09:538#include <vector>
9
boliu7b59e502015-08-14 15:57:4810#include "android_webview/common/aw_switches.h"
Gyuyoung Kim3f41f882021-01-15 00:12:4911#include "android_webview/common/mojom/frame.mojom.h"
boliu@chromium.org710b2602012-10-11 21:41:0912#include "android_webview/common/url_constants.h"
mlamouri007f9d72015-02-27 16:27:2513#include "android_webview/renderer/aw_content_settings_client.h"
ycheo@chromium.org6853d16a2013-11-27 14:35:0514#include "android_webview/renderer/aw_key_systems.h"
Wei Li13246442017-07-29 03:13:4015#include "android_webview/renderer/aw_print_render_frame_helper_delegate.h"
hjd@chromium.orgb1656552014-06-09 19:59:3816#include "android_webview/renderer/aw_render_frame_ext.h"
boliu@chromium.org710b2602012-10-11 21:41:0917#include "android_webview/renderer/aw_render_view_ext.h"
Carlos IL08747ea82019-11-06 17:44:5918#include "android_webview/renderer/aw_safe_browsing_error_page_controller_delegate_impl.h"
Yuzhu Shenfe5f3a52018-01-11 21:24:1319#include "android_webview/renderer/aw_url_loader_throttle_provider.h"
Hiroki Nakagawaea68b642018-04-13 06:24:4220#include "android_webview/renderer/aw_websocket_handshake_throttle_provider.h"
Ken Rockot13051c932019-11-14 20:11:5121#include "android_webview/renderer/browser_exposed_renderer_interfaces.h"
boliu7b59e502015-08-14 15:57:4822#include "base/command_line.h"
mnaganovf082f6c2015-08-05 20:09:5323#include "base/i18n/rtl.h"
Dale Curtis2fe03e92017-07-07 20:40:1924#include "base/metrics/histogram_macros.h"
mnaganovf082f6c2015-08-05 20:09:5325#include "base/strings/string_util.h"
Colin Blundell49f165d2020-01-14 14:08:5326#include "components/android_system_error_page/error_page_populator.h"
Scott Violet9343e642020-06-12 18:47:2127#include "components/js_injection/renderer/js_communication.h"
Tao Bai42e52f32019-09-28 00:07:5128#include "components/page_load_metrics/renderer/metrics_render_frame_observer.h"
Wei Li13246442017-07-29 03:13:4029#include "components/printing/renderer/print_render_frame_helper.h"
Robert Sesekffeae8e2019-11-26 17:10:1530#include "components/visitedlink/renderer/visitedlink_reader.h"
benbd9dc802017-04-19 01:37:4331#include "content/public/child/child_thread.h"
sgurun@chromium.org0b85508d2013-12-10 04:33:5332#include "content/public/common/url_constants.h"
gsennton68afca72016-01-12 11:07:3233#include "content/public/renderer/document_state.h"
jam@chromium.org1a4e9752013-12-31 20:10:5834#include "content/public/renderer/render_frame.h"
boliu@chromium.org710b2602012-10-11 21:41:0935#include "content/public/renderer/render_thread.h"
jam@chromium.org04741722014-01-13 17:03:5236#include "content/public/renderer/render_view.h"
Gyuyoung Kim3f41f882021-01-15 00:12:4937#include "ipc/ipc_sync_channel.h"
Ken Rockot13051c932019-11-14 20:11:5138#include "mojo/public/cpp/bindings/binder_map.h"
Gyuyoung Kimbd70b6ab2020-12-18 08:19:1239#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
Ken Rockot15253a482019-08-23 17:44:5940#include "third_party/blink/public/platform/platform.h"
Blink Reformata30d4232018-04-07 15:31:0641#include "third_party/blink/public/platform/web_string.h"
42#include "third_party/blink/public/platform/web_url.h"
Blink Reformata30d4232018-04-07 15:31:0643#include "third_party/blink/public/platform/web_url_request.h"
44#include "third_party/blink/public/web/web_frame.h"
45#include "third_party/blink/public/web/web_navigation_type.h"
46#include "third_party/blink/public/web/web_security_policy.h"
tfarina@chromium.org79fe2272013-07-13 20:01:4047#include "url/gurl.h"
mnaganov2cee4bd2015-08-05 16:07:4148#include "url/url_constants.h"
boliu@chromium.org710b2602012-10-11 21:41:0949
brettw41446f62016-11-14 23:23:2350#if BUILDFLAG(ENABLE_SPELLCHECK)
timvolodine469d6f72016-08-25 05:46:1051#include "components/spellcheck/renderer/spellcheck.h"
52#include "components/spellcheck/renderer/spellcheck_provider.h"
53#endif
54
sgurun@chromium.org0b85508d2013-12-10 04:33:5355using content::RenderThread;
56
boliu@chromium.org710b2602012-10-11 21:41:0957namespace android_webview {
58
Ken Rockot15253a482019-08-23 17:44:5959AwContentRendererClient::AwContentRendererClient() = default;
boliu@chromium.org710b2602012-10-11 21:41:0960
Ken Rockot15253a482019-08-23 17:44:5961AwContentRendererClient::~AwContentRendererClient() = default;
boliu@chromium.org710b2602012-10-11 21:41:0962
63void AwContentRendererClient::RenderThreadStarted() {
bashia820a502015-10-02 03:51:1164 RenderThread* thread = RenderThread::Get();
tyoshino832a58a2016-04-18 08:14:0865 aw_render_thread_observer_.reset(new AwRenderThreadObserver);
66 thread->AddObserver(aw_render_thread_observer_.get());
bashia820a502015-10-02 03:51:1167
Robert Sesekffeae8e2019-11-26 17:10:1568 visited_link_reader_.reset(new visitedlink::VisitedLinkReader);
benbd9dc802017-04-19 01:37:4369
Ken Rockot15253a482019-08-23 17:44:5970 browser_interface_broker_ =
Oksana Zhuravlovad4f1f5c2019-11-14 05:57:1171 blink::Platform::Current()->GetBrowserInterfaceBroker();
Kenichi Ishibashi92734c62019-08-20 14:12:3472
brettw41446f62016-11-14 23:23:2373#if BUILDFLAG(ENABLE_SPELLCHECK)
Ben Goodger16331302018-01-23 00:43:2174 if (!spellcheck_)
Ken Rockot13051c932019-11-14 20:11:5175 spellcheck_ = std::make_unique<SpellCheck>(this);
timvolodine469d6f72016-08-25 05:46:1076#endif
boliu@chromium.org710b2602012-10-11 21:41:0977}
78
Ken Rockot13051c932019-11-14 20:11:5179void AwContentRendererClient::ExposeInterfacesToBrowser(
80 mojo::BinderMap* binders) {
81 // NOTE: Do not add binders directly within this method. Instead, modify the
82 // definition of |ExposeRendererInterfacesToBrowser()| to ensure security
83 // review coverage.
84 ExposeRendererInterfacesToBrowser(this, binders);
85}
86
gsennton68afca72016-01-12 11:07:3287bool AwContentRendererClient::HandleNavigation(
88 content::RenderFrame* render_frame,
89 bool is_content_initiated,
nickf7b38222016-11-22 21:59:3590 bool render_view_was_created_by_renderer,
gsennton68afca72016-01-12 11:07:3291 blink::WebFrame* frame,
92 const blink::WebURLRequest& request,
93 blink::WebNavigationType type,
94 blink::WebNavigationPolicy default_policy,
95 bool is_redirect) {
96 // Only GETs can be overridden.
Blink Reformat1c4d759e2017-04-09 16:34:5497 if (!request.HttpMethod().Equals("GET"))
gsennton68afca72016-01-12 11:07:3298 return false;
99
100 // Any navigation from loadUrl, and goBack/Forward are considered application-
101 // initiated and hence will not yield a shouldOverrideUrlLoading() callback.
102 // Webview classic does not consider reload application-initiated so we
103 // continue the same behavior.
104 // TODO(sgurun) is_content_initiated is normally false for cross-origin
105 // navigations but since android_webview does not swap out renderers, this
106 // works fine. This will stop working if android_webview starts swapping out
107 // renderers on navigation.
108 bool application_initiated =
Blink Reformat1c4d759e2017-04-09 16:34:54109 !is_content_initiated || type == blink::kWebNavigationTypeBackForward;
gsennton68afca72016-01-12 11:07:32110
111 // Don't offer application-initiated navigations unless it's a redirect.
112 if (application_initiated && !is_redirect)
113 return false;
114
Blink Reformat1c4d759e2017-04-09 16:34:54115 bool is_main_frame = !frame->Parent();
116 const GURL& gurl = request.Url();
gsennton68afca72016-01-12 11:07:32117 // For HTTP schemes, only top-level navigations can be overridden. Similarly,
118 // WebView Classic lets app override only top level about:blank navigations.
119 // So we filter out non-top about:blank navigations here.
120 if (!is_main_frame &&
121 (gurl.SchemeIs(url::kHttpScheme) || gurl.SchemeIs(url::kHttpsScheme) ||
122 gurl.SchemeIs(url::kAboutScheme)))
123 return false;
124
125 // use NavigationInterception throttle to handle the call as that can
126 // be deferred until after the java side has been constructed.
nickf7b38222016-11-22 21:59:35127 //
128 // TODO(nick): |render_view_was_created_by_renderer| was plumbed in to
129 // preserve the existing code behavior, but it doesn't appear to be correct.
130 // In particular, this value will be true for the initial navigation of a
131 // RenderView created via window.open(), but it will also be true for all
132 // subsequent navigations in that RenderView, no matter how they are
133 // initiated.
134 if (render_view_was_created_by_renderer) {
gsennton68afca72016-01-12 11:07:32135 return false;
136 }
137
138 bool ignore_navigation = false;
Jan Wilken Dörrie85285b02021-03-11 23:38:47139 std::u16string url = request.Url().GetString().Utf16();
Blink Reformat1c4d759e2017-04-09 16:34:54140 bool has_user_gesture = request.HasUserGesture();
gsennton68afca72016-01-12 11:07:32141
Gyuyoung Kimbd70b6ab2020-12-18 08:19:12142 mojo::AssociatedRemote<mojom::FrameHost> frame_host_remote;
143 render_frame->GetRemoteAssociatedInterfaces()->GetInterface(
144 &frame_host_remote);
145 frame_host_remote->ShouldOverrideUrlLoading(
146 url, has_user_gesture, is_redirect, is_main_frame, &ignore_navigation);
147
gsennton68afca72016-01-12 11:07:32148 return ignore_navigation;
149}
150
jam@chromium.orga86c0e962013-12-17 17:10:39151void AwContentRendererClient::RenderFrameCreated(
152 content::RenderFrame* render_frame) {
mlamouri007f9d72015-02-27 16:27:25153 new AwContentSettingsClient(render_frame);
Wei Liffbf9e12017-07-29 20:55:21154 new printing::PrintRenderFrameHelper(
Gyuyoung Kimdae5f102017-12-04 15:55:38155 render_frame, std::make_unique<AwPrintRenderFrameHelperDelegate>());
hjd@chromium.orgb1656552014-06-09 19:59:38156 new AwRenderFrameExt(render_frame);
Scott Violet9343e642020-06-12 18:47:21157 new js_injection::JsCommunication(render_frame);
Carlos IL08747ea82019-11-06 17:44:59158 new AwSafeBrowsingErrorPageControllerDelegateImpl(render_frame);
jam@chromium.org04741722014-01-13 17:03:52159
jam@chromium.orge6345642014-01-25 00:25:28160 // TODO(jam): when the frame tree moves into content and parent() works at
161 // RenderFrame construction, simplify this by just checking parent().
162 content::RenderFrame* parent_frame =
163 render_frame->GetRenderView()->GetMainRenderFrame();
164 if (parent_frame && parent_frame != render_frame) {
jam@chromium.org04741722014-01-13 17:03:52165 // Avoid any race conditions from having the browser's UI thread tell the IO
166 // thread that a subframe was created.
Gyuyoung Kim3f41f882021-01-15 00:12:49167 GetRenderMessageFilter()->SubFrameCreated(parent_frame->GetRoutingID(),
168 render_frame->GetRoutingID());
jam@chromium.org04741722014-01-13 17:03:52169 }
estadeb1bc9bd2014-12-02 22:44:11170
xiaochengh0c92b1c2017-04-12 03:06:49171#if BUILDFLAG(ENABLE_SPELLCHECK)
Ben Goodger03c39c22017-10-26 23:45:37172 new SpellCheckProvider(render_frame, spellcheck_.get(), this);
xiaochengh0c92b1c2017-04-12 03:06:49173#endif
Tao Bai42e52f32019-09-28 00:07:51174
175 // Owned by |render_frame|.
176 new page_load_metrics::MetricsRenderFrameObserver(render_frame);
jam@chromium.orga86c0e962013-12-17 17:10:39177}
178
boliu@chromium.org710b2602012-10-11 21:41:09179void AwContentRendererClient::RenderViewCreated(
180 content::RenderView* render_view) {
Dave Tapuskadd4996e2021-02-08 20:36:25181 AwRenderViewExt::WebViewCreated(render_view->GetWebView());
boliu@chromium.org710b2602012-10-11 21:41:09182}
183
Carlos IL94293f92018-01-11 18:57:22184void AwContentRendererClient::PrepareErrorPage(
aberenta7f273b2016-08-18 10:46:11185 content::RenderFrame* render_frame,
abarth@chromium.orgc32e27082013-11-07 06:58:20186 const blink::WebURLError& error,
Dmitry Gozman96c429a2018-12-20 01:29:14187 const std::string& http_method,
Dmitry Gozmane63db7e2018-11-14 23:42:23188 std::string* error_html) {
Carlos IL08747ea82019-11-06 17:44:59189 AwSafeBrowsingErrorPageControllerDelegateImpl::Get(render_frame)
190 ->PrepareForErrorPage();
191
Colin Blundell49f165d2020-01-14 14:08:53192 android_system_error_page::PopulateErrorPageHtml(error, error_html);
boliu@chromium.org710b2602012-10-11 21:41:09193}
194
Miyoung Shind2dd8bc2019-03-11 02:45:40195uint64_t AwContentRendererClient::VisitedLinkHash(const char* canonical_url,
196 size_t length) {
Robert Sesekffeae8e2019-11-26 17:10:15197 return visited_link_reader_->ComputeURLFingerprint(canonical_url, length);
boliu@chromium.org710b2602012-10-11 21:41:09198}
199
Miyoung Shind2dd8bc2019-03-11 02:45:40200bool AwContentRendererClient::IsLinkVisited(uint64_t link_hash) {
Robert Sesekffeae8e2019-11-26 17:10:15201 return visited_link_reader_->IsVisited(link_hash);
boliu@chromium.org710b2602012-10-11 21:41:09202}
203
Shimi Zhang4defe642020-04-27 21:31:40204void AwContentRendererClient::RunScriptsAtDocumentStart(
205 content::RenderFrame* render_frame) {
Scott Violet9343e642020-06-12 18:47:21206 js_injection::JsCommunication* communication =
207 js_injection::JsCommunication::Get(render_frame);
danakja79ab9f2021-01-15 20:08:34208 communication->RunScriptsAtDocumentStart();
Shimi Zhang4defe642020-04-27 21:31:40209}
210
halliwell8f42dd52016-04-29 16:06:33211void AwContentRendererClient::AddSupportedKeySystems(
212 std::vector<std::unique_ptr<::media::KeySystemProperties>>* key_systems) {
ycheo@chromium.org6853d16a2013-11-27 14:35:05213 AwAddKeySystems(key_systems);
214}
215
Minggang Wangb747d8ef2021-03-03 07:48:09216std::unique_ptr<blink::WebSocketHandshakeThrottleProvider>
Hiroki Nakagawaea68b642018-04-13 06:24:42217AwContentRendererClient::CreateWebSocketHandshakeThrottleProvider() {
Ken Rockot15253a482019-08-23 17:44:59218 return std::make_unique<AwWebSocketHandshakeThrottleProvider>(
219 browser_interface_broker_.get());
ricea4ce57e62017-06-28 05:53:27220}
221
Minggang Wang1df377df2021-03-08 04:45:40222std::unique_ptr<blink::URLLoaderThrottleProvider>
Yuzhu Shenfe5f3a52018-01-11 21:24:13223AwContentRendererClient::CreateURLLoaderThrottleProvider(
Minggang Wang1df377df2021-03-08 04:45:40224 blink::URLLoaderThrottleProviderType provider_type) {
Ken Rockot15253a482019-08-23 17:44:59225 return std::make_unique<AwURLLoaderThrottleProvider>(
226 browser_interface_broker_.get(), provider_type);
Yuzhu Shenfe5f3a52018-01-11 21:24:13227}
228
Ben Goodger03c39c22017-10-26 23:45:37229void AwContentRendererClient::GetInterface(
230 const std::string& interface_name,
Xiaocheng Hud9085ee2018-02-16 22:32:30231 mojo::ScopedMessagePipeHandle interface_pipe) {
232 // A dirty hack to make SpellCheckHost requests work on WebView.
233 // TODO(crbug.com/806394): Use a WebView-specific service for SpellCheckHost
234 // and SafeBrowsing, instead of |content_browser|.
Ken Rockot15253a482019-08-23 17:44:59235 RenderThread::Get()->BindHostReceiver(
236 mojo::GenericPendingReceiver(interface_name, std::move(interface_pipe)));
Xiaocheng Hud9085ee2018-02-16 22:32:30237}
Ben Goodger03c39c22017-10-26 23:45:37238
Gyuyoung Kim3f41f882021-01-15 00:12:49239mojom::RenderMessageFilter* AwContentRendererClient::GetRenderMessageFilter() {
240 if (!render_message_filter_) {
241 RenderThread::Get()->GetChannel()->GetRemoteAssociatedInterface(
242 &render_message_filter_);
243 }
244 return render_message_filter_.get();
245}
246
boliu@chromium.org710b2602012-10-11 21:41:09247} // namespace android_webview