- add sources.
[platform/framework/web/crosswalk.git] / src / android_webview / renderer / aw_content_renderer_client.cc
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.
4
5 #include "android_webview/renderer/aw_content_renderer_client.h"
6
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"
22 #include "url/gurl.h"
23
24 namespace android_webview {
25
26 AwContentRendererClient::AwContentRendererClient() {
27 }
28
29 AwContentRendererClient::~AwContentRendererClient() {
30 }
31
32 void AwContentRendererClient::RenderThreadStarted() {
33   WebKit::WebString content_scheme(
34       ASCIIToUTF16(android_webview::kContentScheme));
35   WebKit::WebSecurityPolicy::registerURLSchemeAsLocal(content_scheme);
36
37   WebKit::WebString aw_scheme(
38       ASCIIToUTF16(android_webview::kAndroidWebViewVideoPosterScheme));
39   WebKit::WebSecurityPolicy::registerURLSchemeAsSecure(aw_scheme);
40
41   content::RenderThread* thread = content::RenderThread::Get();
42
43   aw_render_process_observer_.reset(new AwRenderProcessObserver);
44   thread->AddObserver(aw_render_process_observer_.get());
45
46   visited_link_slave_.reset(new visitedlink::VisitedLinkSlave);
47   thread->AddObserver(visited_link_slave_.get());
48 }
49
50 void AwContentRendererClient::RenderViewCreated(
51     content::RenderView* render_view) {
52   AwRenderViewExt::RenderViewCreated(render_view);
53
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);
59 }
60
61 std::string AwContentRendererClient::GetDefaultEncoding() {
62   return AwResource::GetDefaultTextEncoding();
63 }
64
65 bool AwContentRendererClient::HasErrorPage(int http_status_code,
66                           std::string* error_domain) {
67   return http_status_code >= 400;
68 }
69
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) {
77   if (error_html) {
78     GURL error_url(failed_request.url());
79     std::string err = UTF16ToUTF8(error.localizedDescription);
80     std::string contents;
81     if (err.empty()) {
82       contents = AwResource::GetNoDomainPageContent();
83     } else {
84       contents = AwResource::GetLoadErrorPageContent();
85       ReplaceSubstringsAfterOffset(&contents, 0, "%e", err);
86     }
87
88     ReplaceSubstringsAfterOffset(&contents, 0, "%s",
89                                  error_url.possibly_invalid_spec());
90     *error_html = contents;
91   }
92   if (error_description) {
93     if (error.localizedDescription.isEmpty())
94       *error_description = ASCIIToUTF16(net::ErrorToString(error.reason));
95     else
96       *error_description = error.localizedDescription;
97   }
98 }
99
100 unsigned long long AwContentRendererClient::VisitedLinkHash(
101     const char* canonical_url,
102     size_t length) {
103   return visited_link_slave_->ComputeURLFingerprint(canonical_url, length);
104 }
105
106 bool AwContentRendererClient::IsLinkVisited(unsigned long long link_hash) {
107   return visited_link_slave_->IsVisited(link_hash);
108 }
109
110 }  // namespace android_webview