Add default implementations for AppWindowRegistry::Observer notifications.
[chromium-blink-merge.git] / android_webview / renderer / aw_content_renderer_client.cc
blob400d009c93c99dff679b161b3ada9ab5df2c8705
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_key_systems.h"
11 #include "android_webview/renderer/aw_permission_client.h"
12 #include "android_webview/renderer/aw_render_frame_observer.h"
13 #include "android_webview/renderer/aw_render_view_ext.h"
14 #include "android_webview/renderer/print_web_view_helper.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "components/autofill/content/renderer/autofill_agent.h"
18 #include "components/autofill/content/renderer/password_autofill_agent.h"
19 #include "components/visitedlink/renderer/visitedlink_slave.h"
20 #include "content/public/common/url_constants.h"
21 #include "content/public/renderer/document_state.h"
22 #include "content/public/renderer/navigation_state.h"
23 #include "content/public/renderer/render_frame.h"
24 #include "content/public/renderer/render_thread.h"
25 #include "content/public/renderer/render_view.h"
26 #include "net/base/escape.h"
27 #include "net/base/net_errors.h"
28 #include "third_party/WebKit/public/platform/WebString.h"
29 #include "third_party/WebKit/public/platform/WebURL.h"
30 #include "third_party/WebKit/public/platform/WebURLError.h"
31 #include "third_party/WebKit/public/platform/WebURLRequest.h"
32 #include "third_party/WebKit/public/web/WebFrame.h"
33 #include "third_party/WebKit/public/web/WebNavigationType.h"
34 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
35 #include "url/gurl.h"
37 using content::RenderThread;
39 namespace android_webview {
41 AwContentRendererClient::AwContentRendererClient() {
44 AwContentRendererClient::~AwContentRendererClient() {
47 void AwContentRendererClient::RenderThreadStarted() {
48 blink::WebString content_scheme(
49 base::ASCIIToUTF16(android_webview::kContentScheme));
50 blink::WebSecurityPolicy::registerURLSchemeAsLocal(content_scheme);
52 blink::WebString aw_scheme(
53 base::ASCIIToUTF16(android_webview::kAndroidWebViewVideoPosterScheme));
54 blink::WebSecurityPolicy::registerURLSchemeAsSecure(aw_scheme);
56 RenderThread* thread = RenderThread::Get();
58 aw_render_process_observer_.reset(new AwRenderProcessObserver);
59 thread->AddObserver(aw_render_process_observer_.get());
61 visited_link_slave_.reset(new visitedlink::VisitedLinkSlave);
62 thread->AddObserver(visited_link_slave_.get());
65 bool AwContentRendererClient::HandleNavigation(
66 content::RenderFrame* render_frame,
67 content::DocumentState* document_state,
68 int opener_id,
69 blink::WebFrame* frame,
70 const blink::WebURLRequest& request,
71 blink::WebNavigationType type,
72 blink::WebNavigationPolicy default_policy,
73 bool is_redirect) {
75 // Only GETs can be overridden.
76 if (!request.httpMethod().equals("GET"))
77 return false;
79 // Any navigation from loadUrl, and goBack/Forward are considered application-
80 // initiated and hence will not yield a shouldOverrideUrlLoading() callback.
81 // Webview classic does not consider reload application-initiated so we
82 // continue the same behavior.
83 // TODO(sgurun) is_content_initiated is normally false for cross-origin
84 // navigations but since android_webview does not swap out renderers, this
85 // works fine. This will stop working if android_webview starts swapping out
86 // renderers on navigation.
87 bool application_initiated =
88 !document_state->navigation_state()->is_content_initiated()
89 || type == blink::WebNavigationTypeBackForward;
91 // Don't offer application-initiated navigations unless it's a redirect.
92 if (application_initiated && !is_redirect)
93 return false;
95 const GURL& gurl = request.url();
96 // For HTTP schemes, only top-level navigations can be overridden. Similarly,
97 // WebView Classic lets app override only top level about:blank navigations.
98 // So we filter out non-top about:blank navigations here.
99 if (frame->parent() && (gurl.SchemeIs(url::kHttpScheme) ||
100 gurl.SchemeIs(url::kHttpsScheme) ||
101 gurl.SchemeIs(content::kAboutScheme)))
102 return false;
104 // use NavigationInterception throttle to handle the call as that can
105 // be deferred until after the java side has been constructed.
106 if (opener_id != MSG_ROUTING_NONE) {
107 return false;
110 bool ignore_navigation = false;
111 base::string16 url = request.url().string();
113 int render_frame_id = render_frame->GetRoutingID();
114 RenderThread::Get()->Send(new AwViewHostMsg_ShouldOverrideUrlLoading(
115 render_frame_id, url, &ignore_navigation));
116 return ignore_navigation;
119 void AwContentRendererClient::RenderFrameCreated(
120 content::RenderFrame* render_frame) {
121 new AwPermissionClient(render_frame);
122 new AwRenderFrameObserver(render_frame);
124 // TODO(jam): when the frame tree moves into content and parent() works at
125 // RenderFrame construction, simplify this by just checking parent().
126 content::RenderFrame* parent_frame =
127 render_frame->GetRenderView()->GetMainRenderFrame();
128 if (parent_frame && parent_frame != render_frame) {
129 // Avoid any race conditions from having the browser's UI thread tell the IO
130 // thread that a subframe was created.
131 RenderThread::Get()->Send(new AwViewHostMsg_SubFrameCreated(
132 parent_frame->GetRoutingID(), render_frame->GetRoutingID()));
136 void AwContentRendererClient::RenderViewCreated(
137 content::RenderView* render_view) {
138 AwRenderViewExt::RenderViewCreated(render_view);
140 new printing::PrintWebViewHelper(render_view);
141 // TODO(sgurun) do not create a password autofill agent (change
142 // autofill agent to store a weakptr).
143 autofill::PasswordAutofillAgent* password_autofill_agent =
144 new autofill::PasswordAutofillAgent(render_view);
145 new autofill::AutofillAgent(render_view, password_autofill_agent, NULL);
148 std::string AwContentRendererClient::GetDefaultEncoding() {
149 return AwResource::GetDefaultTextEncoding();
152 bool AwContentRendererClient::HasErrorPage(int http_status_code,
153 std::string* error_domain) {
154 return http_status_code >= 400;
157 void AwContentRendererClient::GetNavigationErrorStrings(
158 content::RenderView* /* render_view */,
159 blink::WebFrame* /* frame */,
160 const blink::WebURLRequest& failed_request,
161 const blink::WebURLError& error,
162 std::string* error_html,
163 base::string16* error_description) {
164 if (error_html) {
165 GURL error_url(failed_request.url());
166 std::string err = base::UTF16ToUTF8(error.localizedDescription);
167 std::string contents;
168 if (err.empty()) {
169 contents = AwResource::GetNoDomainPageContent();
170 } else {
171 contents = AwResource::GetLoadErrorPageContent();
172 ReplaceSubstringsAfterOffset(&contents, 0, "%e", err);
175 ReplaceSubstringsAfterOffset(&contents, 0, "%s",
176 net::EscapeForHTML(error_url.possibly_invalid_spec()));
177 *error_html = contents;
179 if (error_description) {
180 if (error.localizedDescription.isEmpty())
181 *error_description = base::ASCIIToUTF16(net::ErrorToString(error.reason));
182 else
183 *error_description = error.localizedDescription;
187 unsigned long long AwContentRendererClient::VisitedLinkHash(
188 const char* canonical_url,
189 size_t length) {
190 return visited_link_slave_->ComputeURLFingerprint(canonical_url, length);
193 bool AwContentRendererClient::IsLinkVisited(unsigned long long link_hash) {
194 return visited_link_slave_->IsVisited(link_hash);
197 void AwContentRendererClient::AddKeySystems(
198 std::vector<content::KeySystemInfo>* key_systems) {
199 AwAddKeySystems(key_systems);
202 } // namespace android_webview