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/url_constants.h"
9 #include "android_webview/renderer/aw_render_view_ext.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/autofill/content/renderer/autofill_agent.h"
13 #include "components/autofill/content/renderer/password_autofill_agent.h"
14 #include "components/visitedlink/renderer/visitedlink_slave.h"
15 #include "content/public/renderer/render_thread.h"
16 #include "net/base/net_errors.h"
17 #include "third_party/WebKit/public/platform/WebString.h"
18 #include "third_party/WebKit/public/platform/WebURL.h"
19 #include "third_party/WebKit/public/platform/WebURLError.h"
20 #include "third_party/WebKit/public/platform/WebURLRequest.h"
21 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
24 namespace android_webview {
26 AwContentRendererClient::AwContentRendererClient() {
29 AwContentRendererClient::~AwContentRendererClient() {
32 void AwContentRendererClient::RenderThreadStarted() {
33 WebKit::WebString content_scheme(
34 ASCIIToUTF16(android_webview::kContentScheme));
35 WebKit::WebSecurityPolicy::registerURLSchemeAsLocal(content_scheme);
37 WebKit::WebString aw_scheme(
38 ASCIIToUTF16(android_webview::kAndroidWebViewVideoPosterScheme));
39 WebKit::WebSecurityPolicy::registerURLSchemeAsSecure(aw_scheme);
41 content::RenderThread* thread = content::RenderThread::Get();
43 aw_render_process_observer_.reset(new AwRenderProcessObserver);
44 thread->AddObserver(aw_render_process_observer_.get());
46 visited_link_slave_.reset(new visitedlink::VisitedLinkSlave);
47 thread->AddObserver(visited_link_slave_.get());
50 void AwContentRendererClient::RenderViewCreated(
51 content::RenderView* render_view) {
52 AwRenderViewExt::RenderViewCreated(render_view);
54 // TODO(sgurun) do not create a password autofill agent (change
55 // autofill agent to store a weakptr).
56 autofill::PasswordAutofillAgent* password_autofill_agent =
57 new autofill::PasswordAutofillAgent(render_view);
58 new autofill::AutofillAgent(render_view, password_autofill_agent);
61 std::string AwContentRendererClient::GetDefaultEncoding() {
62 return AwResource::GetDefaultTextEncoding();
65 bool AwContentRendererClient::HasErrorPage(int http_status_code,
66 std::string* error_domain) {
67 return http_status_code >= 400;
70 void AwContentRendererClient::GetNavigationErrorStrings(
71 WebKit::WebFrame* /* frame */,
72 const WebKit::WebURLRequest& failed_request,
73 const WebKit::WebURLError& error,
74 const std::string& accept_languages,
75 std::string* error_html,
76 string16* error_description) {
78 GURL error_url(failed_request.url());
79 std::string err = UTF16ToUTF8(error.localizedDescription);
82 contents = AwResource::GetNoDomainPageContent();
84 contents = AwResource::GetLoadErrorPageContent();
85 ReplaceSubstringsAfterOffset(&contents, 0, "%e", err);
88 ReplaceSubstringsAfterOffset(&contents, 0, "%s",
89 error_url.possibly_invalid_spec());
90 *error_html = contents;
92 if (error_description) {
93 if (error.localizedDescription.isEmpty())
94 *error_description = ASCIIToUTF16(net::ErrorToString(error.reason));
96 *error_description = error.localizedDescription;
100 unsigned long long AwContentRendererClient::VisitedLinkHash(
101 const char* canonical_url,
103 return visited_link_slave_->ComputeURLFingerprint(canonical_url, length);
106 bool AwContentRendererClient::IsLinkVisited(unsigned long long link_hash) {
107 return visited_link_slave_->IsVisited(link_hash);
110 } // namespace android_webview