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_execution_termination_filter.h"
11 #include "android_webview/renderer/aw_key_systems.h"
12 #include "android_webview/renderer/aw_permission_client.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/print_render_frame_observer.h"
16 #include "android_webview/renderer/print_web_view_helper.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/visitedlink/renderer/visitedlink_slave.h"
22 #include "content/public/common/url_constants.h"
23 #include "content/public/renderer/document_state.h"
24 #include "content/public/renderer/navigation_state.h"
25 #include "content/public/renderer/render_frame.h"
26 #include "content/public/renderer/render_thread.h"
27 #include "content/public/renderer/render_view.h"
28 #include "net/base/escape.h"
29 #include "net/base/net_errors.h"
30 #include "third_party/WebKit/public/platform/WebString.h"
31 #include "third_party/WebKit/public/platform/WebURL.h"
32 #include "third_party/WebKit/public/platform/WebURLError.h"
33 #include "third_party/WebKit/public/platform/WebURLRequest.h"
34 #include "third_party/WebKit/public/web/WebFrame.h"
35 #include "third_party/WebKit/public/web/WebKit.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());
67 execution_termination_filter_
= new AwExecutionTerminationFilter(
68 thread
->GetIOMessageLoopProxy(),
69 thread
->GetMessageLoop()->message_loop_proxy());
70 thread
->AddFilter(execution_termination_filter_
.get());
71 thread
->AddObserver(this);
74 void AwContentRendererClient::WebKitInitialized() {
75 execution_termination_filter_
->SetRenderThreadIsolate(
76 blink::mainThreadIsolate());
79 bool AwContentRendererClient::HandleNavigation(
80 content::RenderFrame
* render_frame
,
81 content::DocumentState
* document_state
,
83 blink::WebFrame
* frame
,
84 const blink::WebURLRequest
& request
,
85 blink::WebNavigationType type
,
86 blink::WebNavigationPolicy default_policy
,
89 // Only GETs can be overridden.
90 if (!request
.httpMethod().equals("GET"))
93 // Any navigation from loadUrl, and goBack/Forward are considered application-
94 // initiated and hence will not yield a shouldOverrideUrlLoading() callback.
95 // Webview classic does not consider reload application-initiated so we
96 // continue the same behavior.
97 // TODO(sgurun) is_content_initiated is normally false for cross-origin
98 // navigations but since android_webview does not swap out renderers, this
99 // works fine. This will stop working if android_webview starts swapping out
100 // renderers on navigation.
101 bool application_initiated
=
102 !document_state
->navigation_state()->is_content_initiated()
103 || type
== blink::WebNavigationTypeBackForward
;
105 // Don't offer application-initiated navigations unless it's a redirect.
106 if (application_initiated
&& !is_redirect
)
109 const GURL
& gurl
= request
.url();
110 // For HTTP schemes, only top-level navigations can be overridden. Similarly,
111 // WebView Classic lets app override only top level about:blank navigations.
112 // So we filter out non-top about:blank navigations here.
113 if (frame
->parent() &&
114 (gurl
.SchemeIs(url::kHttpScheme
) || gurl
.SchemeIs(url::kHttpsScheme
) ||
115 gurl
.SchemeIs(url::kAboutScheme
)))
118 // use NavigationInterception throttle to handle the call as that can
119 // be deferred until after the java side has been constructed.
120 if (opener_id
!= MSG_ROUTING_NONE
) {
124 bool ignore_navigation
= false;
125 base::string16 url
= request
.url().string();
127 int render_frame_id
= render_frame
->GetRoutingID();
128 RenderThread::Get()->Send(new AwViewHostMsg_ShouldOverrideUrlLoading(
129 render_frame_id
, url
, &ignore_navigation
));
130 return ignore_navigation
;
133 void AwContentRendererClient::RenderFrameCreated(
134 content::RenderFrame
* render_frame
) {
135 new AwPermissionClient(render_frame
);
136 new PrintRenderFrameObserver(render_frame
);
137 new AwRenderFrameExt(render_frame
);
139 // TODO(jam): when the frame tree moves into content and parent() works at
140 // RenderFrame construction, simplify this by just checking parent().
141 content::RenderFrame
* parent_frame
=
142 render_frame
->GetRenderView()->GetMainRenderFrame();
143 if (parent_frame
&& parent_frame
!= render_frame
) {
144 // Avoid any race conditions from having the browser's UI thread tell the IO
145 // thread that a subframe was created.
146 RenderThread::Get()->Send(new AwViewHostMsg_SubFrameCreated(
147 parent_frame
->GetRoutingID(), render_frame
->GetRoutingID()));
151 void AwContentRendererClient::RenderViewCreated(
152 content::RenderView
* render_view
) {
153 AwRenderViewExt::RenderViewCreated(render_view
);
155 new printing::PrintWebViewHelper(render_view
);
156 // TODO(sgurun) do not create a password autofill agent (change
157 // autofill agent to store a weakptr).
158 autofill::PasswordAutofillAgent
* password_autofill_agent
=
159 new autofill::PasswordAutofillAgent(render_view
);
160 new autofill::AutofillAgent(render_view
, password_autofill_agent
, NULL
);
163 std::string
AwContentRendererClient::GetDefaultEncoding() {
164 return AwResource::GetDefaultTextEncoding();
167 bool AwContentRendererClient::HasErrorPage(int http_status_code
,
168 std::string
* error_domain
) {
169 return http_status_code
>= 400;
172 void AwContentRendererClient::GetNavigationErrorStrings(
173 content::RenderView
* /* render_view */,
174 blink::WebFrame
* /* frame */,
175 const blink::WebURLRequest
& failed_request
,
176 const blink::WebURLError
& error
,
177 std::string
* error_html
,
178 base::string16
* error_description
) {
180 GURL
error_url(failed_request
.url());
181 std::string err
= base::UTF16ToUTF8(error
.localizedDescription
);
182 std::string contents
;
184 contents
= AwResource::GetNoDomainPageContent();
186 contents
= AwResource::GetLoadErrorPageContent();
187 ReplaceSubstringsAfterOffset(&contents
, 0, "%e", err
);
190 ReplaceSubstringsAfterOffset(&contents
, 0, "%s",
191 net::EscapeForHTML(error_url
.possibly_invalid_spec()));
192 *error_html
= contents
;
194 if (error_description
) {
195 if (error
.localizedDescription
.isEmpty())
196 *error_description
= base::ASCIIToUTF16(net::ErrorToString(error
.reason
));
198 *error_description
= error
.localizedDescription
;
202 unsigned long long AwContentRendererClient::VisitedLinkHash(
203 const char* canonical_url
,
205 return visited_link_slave_
->ComputeURLFingerprint(canonical_url
, length
);
208 bool AwContentRendererClient::IsLinkVisited(unsigned long long link_hash
) {
209 return visited_link_slave_
->IsVisited(link_hash
);
212 void AwContentRendererClient::AddKeySystems(
213 std::vector
<content::KeySystemInfo
>* key_systems
) {
214 AwAddKeySystems(key_systems
);
217 } // namespace android_webview