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.
5 #include "android_webview/renderer/aw_content_renderer_client.h"
7 #include "android_webview/common/aw_resource.h"
8 #include "android_webview/common/render_view_messages.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_message_port_client.h"
13 #include "android_webview/renderer/aw_print_web_view_helper_delegate.h"
14 #include "android_webview/renderer/aw_render_frame_ext.h"
15 #include "android_webview/renderer/aw_render_view_ext.h"
16 #include "android_webview/renderer/print_render_frame_observer.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "components/autofill/content/renderer/autofill_agent.h"
20 #include "components/autofill/content/renderer/password_autofill_agent.h"
21 #include "components/printing/renderer/print_web_view_helper.h"
22 #include "components/visitedlink/renderer/visitedlink_slave.h"
23 #include "content/public/common/url_constants.h"
24 #include "content/public/renderer/document_state.h"
25 #include "content/public/renderer/navigation_state.h"
26 #include "content/public/renderer/render_frame.h"
27 #include "content/public/renderer/render_thread.h"
28 #include "content/public/renderer/render_view.h"
29 #include "net/base/escape.h"
30 #include "net/base/net_errors.h"
31 #include "third_party/WebKit/public/platform/WebString.h"
32 #include "third_party/WebKit/public/platform/WebURL.h"
33 #include "third_party/WebKit/public/platform/WebURLError.h"
34 #include "third_party/WebKit/public/platform/WebURLRequest.h"
35 #include "third_party/WebKit/public/web/WebFrame.h"
36 #include "third_party/WebKit/public/web/WebNavigationType.h"
37 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
40 using content::RenderThread
;
42 namespace android_webview
{
44 AwContentRendererClient::AwContentRendererClient() {
47 AwContentRendererClient::~AwContentRendererClient() {
50 void AwContentRendererClient::RenderThreadStarted() {
51 blink::WebString
content_scheme(
52 base::ASCIIToUTF16(android_webview::kContentScheme
));
53 blink::WebSecurityPolicy::registerURLSchemeAsLocal(content_scheme
);
55 blink::WebString
aw_scheme(
56 base::ASCIIToUTF16(android_webview::kAndroidWebViewVideoPosterScheme
));
57 blink::WebSecurityPolicy::registerURLSchemeAsSecure(aw_scheme
);
59 RenderThread
* thread
= RenderThread::Get();
61 aw_render_process_observer_
.reset(new AwRenderProcessObserver
);
62 thread
->AddObserver(aw_render_process_observer_
.get());
64 visited_link_slave_
.reset(new visitedlink::VisitedLinkSlave
);
65 thread
->AddObserver(visited_link_slave_
.get());
68 bool AwContentRendererClient::HandleNavigation(
69 content::RenderFrame
* render_frame
,
70 content::DocumentState
* document_state
,
72 blink::WebFrame
* frame
,
73 const blink::WebURLRequest
& request
,
74 blink::WebNavigationType type
,
75 blink::WebNavigationPolicy default_policy
,
78 // Only GETs can be overridden.
79 if (!request
.httpMethod().equals("GET"))
82 // Any navigation from loadUrl, and goBack/Forward are considered application-
83 // initiated and hence will not yield a shouldOverrideUrlLoading() callback.
84 // Webview classic does not consider reload application-initiated so we
85 // continue the same behavior.
86 // TODO(sgurun) is_content_initiated is normally false for cross-origin
87 // navigations but since android_webview does not swap out renderers, this
88 // works fine. This will stop working if android_webview starts swapping out
89 // renderers on navigation.
90 bool application_initiated
=
91 !document_state
->navigation_state()->IsContentInitiated() ||
92 type
== blink::WebNavigationTypeBackForward
;
94 // Don't offer application-initiated navigations unless it's a redirect.
95 if (application_initiated
&& !is_redirect
)
98 const GURL
& gurl
= request
.url();
99 // For HTTP schemes, only top-level navigations can be overridden. Similarly,
100 // WebView Classic lets app override only top level about:blank navigations.
101 // So we filter out non-top about:blank navigations here.
102 if (frame
->parent() &&
103 (gurl
.SchemeIs(url::kHttpScheme
) || gurl
.SchemeIs(url::kHttpsScheme
) ||
104 gurl
.SchemeIs(url::kAboutScheme
)))
107 // use NavigationInterception throttle to handle the call as that can
108 // be deferred until after the java side has been constructed.
109 if (opener_id
!= MSG_ROUTING_NONE
) {
113 bool ignore_navigation
= false;
114 base::string16 url
= request
.url().string();
116 int render_frame_id
= render_frame
->GetRoutingID();
117 RenderThread::Get()->Send(new AwViewHostMsg_ShouldOverrideUrlLoading(
118 render_frame_id
, url
, &ignore_navigation
));
119 return ignore_navigation
;
122 void AwContentRendererClient::RenderFrameCreated(
123 content::RenderFrame
* render_frame
) {
124 new AwContentSettingsClient(render_frame
);
125 new PrintRenderFrameObserver(render_frame
);
126 new AwRenderFrameExt(render_frame
);
127 new AwMessagePortClient(render_frame
);
129 // TODO(jam): when the frame tree moves into content and parent() works at
130 // RenderFrame construction, simplify this by just checking parent().
131 content::RenderFrame
* parent_frame
=
132 render_frame
->GetRenderView()->GetMainRenderFrame();
133 if (parent_frame
&& parent_frame
!= render_frame
) {
134 // Avoid any race conditions from having the browser's UI thread tell the IO
135 // thread that a subframe was created.
136 RenderThread::Get()->Send(new AwViewHostMsg_SubFrameCreated(
137 parent_frame
->GetRoutingID(), render_frame
->GetRoutingID()));
140 // TODO(sgurun) do not create a password autofill agent (change
141 // autofill agent to store a weakptr).
142 autofill::PasswordAutofillAgent
* password_autofill_agent
=
143 new autofill::PasswordAutofillAgent(render_frame
);
144 new autofill::AutofillAgent(render_frame
, password_autofill_agent
, NULL
);
147 void AwContentRendererClient::RenderViewCreated(
148 content::RenderView
* render_view
) {
149 AwRenderViewExt::RenderViewCreated(render_view
);
151 new printing::PrintWebViewHelper(
153 scoped_ptr
<printing::PrintWebViewHelper::Delegate
>(
154 new AwPrintWebViewHelperDelegate()));
157 bool AwContentRendererClient::HasErrorPage(int http_status_code
,
158 std::string
* error_domain
) {
159 return http_status_code
>= 400;
162 void AwContentRendererClient::GetNavigationErrorStrings(
163 content::RenderView
* /* render_view */,
164 blink::WebFrame
* /* frame */,
165 const blink::WebURLRequest
& failed_request
,
166 const blink::WebURLError
& error
,
167 std::string
* error_html
,
168 base::string16
* error_description
) {
170 GURL
error_url(failed_request
.url());
171 std::string err
= base::UTF16ToUTF8(error
.localizedDescription
);
172 std::string contents
;
174 contents
= AwResource::GetNoDomainPageContent();
176 contents
= AwResource::GetLoadErrorPageContent();
177 ReplaceSubstringsAfterOffset(&contents
, 0, "%e", err
);
180 ReplaceSubstringsAfterOffset(&contents
, 0, "%s",
181 net::EscapeForHTML(error_url
.possibly_invalid_spec()));
182 *error_html
= contents
;
184 if (error_description
) {
185 if (error
.localizedDescription
.isEmpty())
186 *error_description
= base::ASCIIToUTF16(net::ErrorToString(error
.reason
));
188 *error_description
= error
.localizedDescription
;
192 unsigned long long AwContentRendererClient::VisitedLinkHash(
193 const char* canonical_url
,
195 return visited_link_slave_
->ComputeURLFingerprint(canonical_url
, length
);
198 bool AwContentRendererClient::IsLinkVisited(unsigned long long link_hash
) {
199 return visited_link_slave_
->IsVisited(link_hash
);
202 void AwContentRendererClient::AddKeySystems(
203 std::vector
<media::KeySystemInfo
>* key_systems
) {
204 AwAddKeySystems(key_systems
);
207 bool AwContentRendererClient::ShouldOverridePageVisibilityState(
208 const content::RenderFrame
* render_frame
,
209 blink::WebPageVisibilityState
* override_state
) {
210 // webview is always visible due to rendering requirements.
211 *override_state
= blink::WebPageVisibilityStateVisible
;
215 } // namespace android_webview