Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / android_webview / renderer / aw_content_renderer_client.cc
blob3fcc253d62f442f697f9c88945888ee36b9b6bd9
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 <vector>
9 #include "android_webview/common/aw_resource.h"
10 #include "android_webview/common/aw_switches.h"
11 #include "android_webview/common/render_view_messages.h"
12 #include "android_webview/common/url_constants.h"
13 #include "android_webview/grit/aw_resources.h"
14 #include "android_webview/grit/aw_strings.h"
15 #include "android_webview/renderer/aw_content_settings_client.h"
16 #include "android_webview/renderer/aw_key_systems.h"
17 #include "android_webview/renderer/aw_message_port_client.h"
18 #include "android_webview/renderer/aw_print_web_view_helper_delegate.h"
19 #include "android_webview/renderer/aw_render_frame_ext.h"
20 #include "android_webview/renderer/aw_render_view_ext.h"
21 #include "android_webview/renderer/print_render_frame_observer.h"
22 #include "base/command_line.h"
23 #include "base/i18n/rtl.h"
24 #include "base/message_loop/message_loop.h"
25 #include "base/strings/string_util.h"
26 #include "base/strings/utf_string_conversions.h"
27 #include "components/autofill/content/renderer/autofill_agent.h"
28 #include "components/autofill/content/renderer/password_autofill_agent.h"
29 #include "components/printing/renderer/print_web_view_helper.h"
30 #include "components/visitedlink/renderer/visitedlink_slave.h"
31 #include "content/public/common/url_constants.h"
32 #include "content/public/renderer/render_frame.h"
33 #include "content/public/renderer/render_thread.h"
34 #include "content/public/renderer/render_view.h"
35 #include "net/base/escape.h"
36 #include "net/base/net_errors.h"
37 #include "third_party/WebKit/public/platform/WebString.h"
38 #include "third_party/WebKit/public/platform/WebURL.h"
39 #include "third_party/WebKit/public/platform/WebURLError.h"
40 #include "third_party/WebKit/public/platform/WebURLRequest.h"
41 #include "third_party/WebKit/public/web/WebFrame.h"
42 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
43 #include "ui/base/l10n/l10n_util.h"
44 #include "ui/base/resource/resource_bundle.h"
45 #include "url/gurl.h"
46 #include "url/url_constants.h"
48 using content::RenderThread;
50 namespace android_webview {
52 AwContentRendererClient::AwContentRendererClient()
53 : enable_page_visibility_(base::CommandLine::ForCurrentProcess()->HasSwitch(
54 switches::kEnablePageVisibility)) {}
56 AwContentRendererClient::~AwContentRendererClient() {
59 void AwContentRendererClient::RenderThreadStarted() {
60 blink::WebString content_scheme(base::ASCIIToUTF16(url::kContentScheme));
61 blink::WebSecurityPolicy::registerURLSchemeAsLocal(content_scheme);
63 blink::WebString aw_scheme(
64 base::ASCIIToUTF16(android_webview::kAndroidWebViewVideoPosterScheme));
65 blink::WebSecurityPolicy::registerURLSchemeAsSecure(aw_scheme);
67 RenderThread* thread = RenderThread::Get();
69 aw_render_process_observer_.reset(new AwRenderProcessObserver);
70 thread->AddObserver(aw_render_process_observer_.get());
72 visited_link_slave_.reset(new visitedlink::VisitedLinkSlave);
73 thread->AddObserver(visited_link_slave_.get());
76 void AwContentRendererClient::RenderFrameCreated(
77 content::RenderFrame* render_frame) {
78 new AwContentSettingsClient(render_frame);
79 new PrintRenderFrameObserver(render_frame);
80 new AwRenderFrameExt(render_frame);
81 new AwMessagePortClient(render_frame);
83 // TODO(jam): when the frame tree moves into content and parent() works at
84 // RenderFrame construction, simplify this by just checking parent().
85 content::RenderFrame* parent_frame =
86 render_frame->GetRenderView()->GetMainRenderFrame();
87 if (parent_frame && parent_frame != render_frame) {
88 // Avoid any race conditions from having the browser's UI thread tell the IO
89 // thread that a subframe was created.
90 RenderThread::Get()->Send(new AwViewHostMsg_SubFrameCreated(
91 parent_frame->GetRoutingID(), render_frame->GetRoutingID()));
94 // TODO(sgurun) do not create a password autofill agent (change
95 // autofill agent to store a weakptr).
96 autofill::PasswordAutofillAgent* password_autofill_agent =
97 new autofill::PasswordAutofillAgent(render_frame);
98 new autofill::AutofillAgent(render_frame, password_autofill_agent, NULL);
101 void AwContentRendererClient::RenderViewCreated(
102 content::RenderView* render_view) {
103 AwRenderViewExt::RenderViewCreated(render_view);
105 new printing::PrintWebViewHelper(
106 render_view,
107 scoped_ptr<printing::PrintWebViewHelper::Delegate>(
108 new AwPrintWebViewHelperDelegate()));
111 bool AwContentRendererClient::HasErrorPage(int http_status_code,
112 std::string* error_domain) {
113 return http_status_code >= 400;
116 void AwContentRendererClient::GetNavigationErrorStrings(
117 content::RenderView* /* render_view */,
118 blink::WebFrame* /* frame */,
119 const blink::WebURLRequest& failed_request,
120 const blink::WebURLError& error,
121 std::string* error_html,
122 base::string16* error_description) {
123 if (error_html) {
124 std::string url =
125 net::EscapeForHTML(GURL(failed_request.url()).possibly_invalid_spec());
126 std::string err =
127 base::UTF16ToUTF8(base::StringPiece16(error.localizedDescription));
129 std::vector<std::string> replacements;
130 replacements.push_back(
131 l10n_util::GetStringUTF8(IDS_AW_WEBPAGE_NOT_AVAILABLE));
132 if (err.empty()) {
133 replacements.push_back(l10n_util::GetStringFUTF8(
134 IDS_AW_WEBPAGE_TEMPORARILY_DOWN, base::UTF8ToUTF16(url)));
135 replacements.push_back(l10n_util::GetStringUTF8(
136 IDS_AW_WEBPAGE_TEMPORARILY_DOWN_SUGGESTIONS));
137 } else {
138 replacements.push_back(l10n_util::GetStringFUTF8(
139 IDS_AW_WEBPAGE_CAN_NOT_BE_LOADED, base::UTF8ToUTF16(url)));
140 replacements.push_back(err);
142 if (base::i18n::IsRTL())
143 replacements.push_back("direction: rtl;");
144 else
145 replacements.push_back("");
146 *error_html = base::ReplaceStringPlaceholders(
147 ResourceBundle::GetSharedInstance().GetRawDataResource(
148 IDR_AW_LOAD_ERROR_HTML),
149 replacements, nullptr);
151 if (error_description) {
152 if (error.localizedDescription.isEmpty())
153 *error_description = base::ASCIIToUTF16(net::ErrorToString(error.reason));
154 else
155 *error_description = error.localizedDescription;
159 unsigned long long AwContentRendererClient::VisitedLinkHash(
160 const char* canonical_url,
161 size_t length) {
162 return visited_link_slave_->ComputeURLFingerprint(canonical_url, length);
165 bool AwContentRendererClient::IsLinkVisited(unsigned long long link_hash) {
166 return visited_link_slave_->IsVisited(link_hash);
169 void AwContentRendererClient::AddKeySystems(
170 std::vector<media::KeySystemInfo>* key_systems) {
171 AwAddKeySystems(key_systems);
174 bool AwContentRendererClient::ShouldOverridePageVisibilityState(
175 const content::RenderFrame* render_frame,
176 blink::WebPageVisibilityState* override_state) {
177 if (enable_page_visibility_)
178 return false;
180 // webview is always visible due to rendering requirements.
181 *override_state = blink::WebPageVisibilityStateVisible;
182 return true;
185 } // namespace android_webview