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/render_frame.h"
25 #include "content/public/renderer/render_thread.h"
26 #include "content/public/renderer/render_view.h"
27 #include "net/base/escape.h"
28 #include "net/base/net_errors.h"
29 #include "third_party/WebKit/public/platform/WebString.h"
30 #include "third_party/WebKit/public/platform/WebURL.h"
31 #include "third_party/WebKit/public/platform/WebURLError.h"
32 #include "third_party/WebKit/public/platform/WebURLRequest.h"
33 #include "third_party/WebKit/public/web/WebFrame.h"
34 #include "third_party/WebKit/public/web/WebSecurityPolicy.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 void AwContentRendererClient::RenderFrameCreated(
66 content::RenderFrame
* render_frame
) {
67 new AwContentSettingsClient(render_frame
);
68 new PrintRenderFrameObserver(render_frame
);
69 new AwRenderFrameExt(render_frame
);
70 new AwMessagePortClient(render_frame
);
72 // TODO(jam): when the frame tree moves into content and parent() works at
73 // RenderFrame construction, simplify this by just checking parent().
74 content::RenderFrame
* parent_frame
=
75 render_frame
->GetRenderView()->GetMainRenderFrame();
76 if (parent_frame
&& parent_frame
!= render_frame
) {
77 // Avoid any race conditions from having the browser's UI thread tell the IO
78 // thread that a subframe was created.
79 RenderThread::Get()->Send(new AwViewHostMsg_SubFrameCreated(
80 parent_frame
->GetRoutingID(), render_frame
->GetRoutingID()));
83 // TODO(sgurun) do not create a password autofill agent (change
84 // autofill agent to store a weakptr).
85 autofill::PasswordAutofillAgent
* password_autofill_agent
=
86 new autofill::PasswordAutofillAgent(render_frame
);
87 new autofill::AutofillAgent(render_frame
, password_autofill_agent
, NULL
);
90 void AwContentRendererClient::RenderViewCreated(
91 content::RenderView
* render_view
) {
92 AwRenderViewExt::RenderViewCreated(render_view
);
94 new printing::PrintWebViewHelper(
96 scoped_ptr
<printing::PrintWebViewHelper::Delegate
>(
97 new AwPrintWebViewHelperDelegate()));
100 bool AwContentRendererClient::HasErrorPage(int http_status_code
,
101 std::string
* error_domain
) {
102 return http_status_code
>= 400;
105 void AwContentRendererClient::GetNavigationErrorStrings(
106 content::RenderView
* /* render_view */,
107 blink::WebFrame
* /* frame */,
108 const blink::WebURLRequest
& failed_request
,
109 const blink::WebURLError
& error
,
110 std::string
* error_html
,
111 base::string16
* error_description
) {
113 GURL
error_url(failed_request
.url());
114 std::string err
= base::UTF16ToUTF8(error
.localizedDescription
);
115 std::string contents
;
117 contents
= AwResource::GetNoDomainPageContent();
119 contents
= AwResource::GetLoadErrorPageContent();
120 base::ReplaceSubstringsAfterOffset(&contents
, 0, "%e", err
);
123 base::ReplaceSubstringsAfterOffset(&contents
, 0, "%s",
124 net::EscapeForHTML(error_url
.possibly_invalid_spec()));
125 *error_html
= contents
;
127 if (error_description
) {
128 if (error
.localizedDescription
.isEmpty())
129 *error_description
= base::ASCIIToUTF16(net::ErrorToString(error
.reason
));
131 *error_description
= error
.localizedDescription
;
135 unsigned long long AwContentRendererClient::VisitedLinkHash(
136 const char* canonical_url
,
138 return visited_link_slave_
->ComputeURLFingerprint(canonical_url
, length
);
141 bool AwContentRendererClient::IsLinkVisited(unsigned long long link_hash
) {
142 return visited_link_slave_
->IsVisited(link_hash
);
145 void AwContentRendererClient::AddKeySystems(
146 std::vector
<media::KeySystemInfo
>* key_systems
) {
147 AwAddKeySystems(key_systems
);
150 bool AwContentRendererClient::ShouldOverridePageVisibilityState(
151 const content::RenderFrame
* render_frame
,
152 blink::WebPageVisibilityState
* override_state
) {
153 // webview is always visible due to rendering requirements.
154 *override_state
= blink::WebPageVisibilityStateVisible
;
158 } // namespace android_webview