| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/net/net_error_helper.h" | 5 #include "chrome/renderer/net/net_error_helper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/common/localized_error.h" | 13 #include "chrome/common/localized_error.h" |
| 14 #include "chrome/common/net/net_error_info.h" | 14 #include "chrome/common/net/net_error_info.h" |
| 15 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
| 16 #include "content/public/common/content_client.h" | 16 #include "content/public/common/content_client.h" |
| 17 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
| 18 #include "content/public/renderer/content_renderer_client.h" | 18 #include "content/public/renderer/content_renderer_client.h" |
| 19 #include "content/public/renderer/render_frame.h" |
| 19 #include "content/public/renderer/render_thread.h" | 20 #include "content/public/renderer/render_thread.h" |
| 20 #include "content/public/renderer/render_view.h" | 21 #include "content/public/renderer/render_view.h" |
| 21 #include "content/public/renderer/resource_fetcher.h" | 22 #include "content/public/renderer/resource_fetcher.h" |
| 22 #include "grit/renderer_resources.h" | 23 #include "grit/renderer_resources.h" |
| 23 #include "ipc/ipc_message.h" | 24 #include "ipc/ipc_message.h" |
| 24 #include "ipc/ipc_message_macros.h" | 25 #include "ipc/ipc_message_macros.h" |
| 25 #include "third_party/WebKit/public/platform/WebURL.h" | 26 #include "third_party/WebKit/public/platform/WebURL.h" |
| 26 #include "third_party/WebKit/public/platform/WebURLError.h" | 27 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 27 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 28 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 28 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 29 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 29 #include "third_party/WebKit/public/web/WebDataSource.h" | 30 #include "third_party/WebKit/public/web/WebDataSource.h" |
| 30 #include "third_party/WebKit/public/web/WebFrame.h" | 31 #include "third_party/WebKit/public/web/WebFrame.h" |
| 31 #include "third_party/WebKit/public/web/WebView.h" | 32 #include "third_party/WebKit/public/web/WebView.h" |
| 32 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" |
| 33 #include "ui/base/webui/jstemplate_builder.h" | 34 #include "ui/base/webui/jstemplate_builder.h" |
| 34 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 35 | 36 |
| 36 using base::JSONWriter; | 37 using base::JSONWriter; |
| 37 using chrome_common_net::DnsProbeStatus; | 38 using chrome_common_net::DnsProbeStatus; |
| 38 using chrome_common_net::DnsProbeStatusToString; | 39 using chrome_common_net::DnsProbeStatusToString; |
| 40 using content::RenderFrame; |
| 41 using content::RenderFrameObserver; |
| 39 using content::RenderThread; | 42 using content::RenderThread; |
| 40 using content::RenderView; | |
| 41 using content::RenderViewObserver; | |
| 42 using content::kUnreachableWebDataURL; | 43 using content::kUnreachableWebDataURL; |
| 43 | 44 |
| 44 namespace { | 45 namespace { |
| 45 | 46 |
| 46 // Number of seconds to wait for the alternate error page server. If it takes | 47 // Number of seconds to wait for the alternate error page server. If it takes |
| 47 // too long, just use the local error page. | 48 // too long, just use the local error page. |
| 48 static const int kAlterErrorPageFetchTimeoutSec = 3000; | 49 static const int kAlterErrorPageFetchTimeoutSec = 3000; |
| 49 | 50 |
| 50 NetErrorHelperCore::PageType GetLoadingPageType(const blink::WebFrame* frame) { | 51 NetErrorHelperCore::PageType GetLoadingPageType(const blink::WebFrame* frame) { |
| 51 GURL url = frame->provisionalDataSource()->request().url(); | 52 GURL url = frame->provisionalDataSource()->request().url(); |
| 52 if (!url.is_valid() || url.spec() != kUnreachableWebDataURL) | 53 if (!url.is_valid() || url.spec() != kUnreachableWebDataURL) |
| 53 return NetErrorHelperCore::NON_ERROR_PAGE; | 54 return NetErrorHelperCore::NON_ERROR_PAGE; |
| 54 return NetErrorHelperCore::ERROR_PAGE; | 55 return NetErrorHelperCore::ERROR_PAGE; |
| 55 } | 56 } |
| 56 | 57 |
| 57 NetErrorHelperCore::FrameType GetFrameType(const blink::WebFrame* frame) { | 58 NetErrorHelperCore::FrameType GetFrameType(const blink::WebFrame* frame) { |
| 58 if (!frame->parent()) | 59 if (!frame->parent()) |
| 59 return NetErrorHelperCore::MAIN_FRAME; | 60 return NetErrorHelperCore::MAIN_FRAME; |
| 60 return NetErrorHelperCore::SUB_FRAME; | 61 return NetErrorHelperCore::SUB_FRAME; |
| 61 } | 62 } |
| 62 | 63 |
| 63 } // namespace | 64 } // namespace |
| 64 | 65 |
| 65 NetErrorHelper::NetErrorHelper(RenderView* render_view) | 66 NetErrorHelper::NetErrorHelper(RenderFrame* render_view) |
| 66 : RenderViewObserver(render_view), | 67 : RenderFrameObserver(render_view), |
| 67 content::RenderViewObserverTracker<NetErrorHelper>(render_view), | 68 content::RenderFrameObserverTracker<NetErrorHelper>(render_view), |
| 68 core_(this) { | 69 core_(this) { |
| 69 } | 70 } |
| 70 | 71 |
| 71 NetErrorHelper::~NetErrorHelper() { | 72 NetErrorHelper::~NetErrorHelper() { |
| 72 } | 73 } |
| 73 | 74 |
| 74 void NetErrorHelper::DidStartProvisionalLoad(blink::WebFrame* frame) { | 75 void NetErrorHelper::DidStartProvisionalLoad(blink::WebFrame* frame) { |
| 75 core_.OnStartLoad(GetFrameType(frame), GetLoadingPageType(frame)); | 76 core_.OnStartLoad(GetFrameType(frame), GetLoadingPageType(frame)); |
| 76 } | 77 } |
| 77 | 78 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 int resource_id = IDR_NET_ERROR_HTML; | 117 int resource_id = IDR_NET_ERROR_HTML; |
| 117 const base::StringPiece template_html( | 118 const base::StringPiece template_html( |
| 118 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id)); | 119 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id)); |
| 119 if (template_html.empty()) { | 120 if (template_html.empty()) { |
| 120 NOTREACHED() << "unable to load template."; | 121 NOTREACHED() << "unable to load template."; |
| 121 } else { | 122 } else { |
| 122 base::DictionaryValue error_strings; | 123 base::DictionaryValue error_strings; |
| 123 LocalizedError::GetStrings(error.reason, error.domain.utf8(), | 124 LocalizedError::GetStrings(error.reason, error.domain.utf8(), |
| 124 error.unreachableURL, is_failed_post, | 125 error.unreachableURL, is_failed_post, |
| 125 RenderThread::Get()->GetLocale(), | 126 RenderThread::Get()->GetLocale(), |
| 126 render_view()->GetAcceptLanguages(), | 127 render_frame()->GetRenderView()-> |
| 128 GetAcceptLanguages(), |
| 127 &error_strings); | 129 &error_strings); |
| 128 // "t" is the id of the template's root node. | 130 // "t" is the id of the template's root node. |
| 129 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); | 131 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); |
| 130 } | 132 } |
| 131 } | 133 } |
| 132 | 134 |
| 133 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, | 135 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, |
| 134 const GURL& failed_url) { | 136 const GURL& failed_url) { |
| 135 blink::WebView* web_view = render_view()->GetWebView(); | 137 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
| 136 if (!web_view) | 138 if (!web_view) |
| 137 return; | 139 return; |
| 138 blink::WebFrame* frame = web_view->mainFrame(); | 140 blink::WebFrame* frame = web_view->mainFrame(); |
| 139 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); | 141 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); |
| 140 } | 142 } |
| 141 | 143 |
| 142 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, | 144 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, |
| 143 bool is_failed_post) { | 145 bool is_failed_post) { |
| 144 base::DictionaryValue error_strings; | 146 base::DictionaryValue error_strings; |
| 145 LocalizedError::GetStrings(error.reason, | 147 LocalizedError::GetStrings(error.reason, |
| 146 error.domain.utf8(), | 148 error.domain.utf8(), |
| 147 error.unreachableURL, | 149 error.unreachableURL, |
| 148 is_failed_post, | 150 is_failed_post, |
| 149 RenderThread::Get()->GetLocale(), | 151 RenderThread::Get()->GetLocale(), |
| 150 render_view()->GetAcceptLanguages(), | 152 render_frame()->GetRenderView()-> |
| 153 GetAcceptLanguages(), |
| 151 &error_strings); | 154 &error_strings); |
| 152 | 155 |
| 153 std::string json; | 156 std::string json; |
| 154 JSONWriter::Write(&error_strings, &json); | 157 JSONWriter::Write(&error_strings, &json); |
| 155 | 158 |
| 156 std::string js = "if (window.updateForDnsProbe) " | 159 std::string js = "if (window.updateForDnsProbe) " |
| 157 "updateForDnsProbe(" + json + ");"; | 160 "updateForDnsProbe(" + json + ");"; |
| 158 base::string16 js16; | 161 base::string16 js16; |
| 159 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { | 162 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { |
| 160 NOTREACHED(); | 163 NOTREACHED(); |
| 161 return; | 164 return; |
| 162 } | 165 } |
| 163 | 166 |
| 164 base::string16 frame_xpath; | 167 base::string16 frame_xpath; |
| 165 render_view()->EvaluateScript(frame_xpath, js16, 0, false); | 168 render_frame()->GetRenderView()->EvaluateScript(frame_xpath, js16, 0, false); |
| 166 } | 169 } |
| 167 | 170 |
| 168 void NetErrorHelper::FetchErrorPage(const GURL& url) { | 171 void NetErrorHelper::FetchErrorPage(const GURL& url) { |
| 169 DCHECK(!alt_error_page_fetcher_.get()); | 172 DCHECK(!alt_error_page_fetcher_.get()); |
| 170 | 173 |
| 171 blink::WebView* web_view = render_view()->GetWebView(); | 174 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
| 172 if (!web_view) | 175 if (!web_view) |
| 173 return; | 176 return; |
| 174 blink::WebFrame* frame = web_view->mainFrame(); | 177 blink::WebFrame* frame = web_view->mainFrame(); |
| 175 | 178 |
| 176 alt_error_page_fetcher_.reset( | 179 alt_error_page_fetcher_.reset( |
| 177 content::ResourceFetcher::Create( | 180 content::ResourceFetcher::Create( |
| 178 url, frame, blink::WebURLRequest::TargetIsMainFrame, | 181 url, frame, blink::WebURLRequest::TargetIsMainFrame, |
| 179 base::Bind(&NetErrorHelper::OnAlternateErrorPageRetrieved, | 182 base::Bind(&NetErrorHelper::OnAlternateErrorPageRetrieved, |
| 180 base::Unretained(this)))); | 183 base::Unretained(this)))); |
| 181 | 184 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 205 // The fetcher may only be deleted after |data| is passed to |core_|. Move | 208 // The fetcher may only be deleted after |data| is passed to |core_|. Move |
| 206 // it to a temporary to prevent any potential re-entrancy issues. | 209 // it to a temporary to prevent any potential re-entrancy issues. |
| 207 scoped_ptr<content::ResourceFetcher> fetcher( | 210 scoped_ptr<content::ResourceFetcher> fetcher( |
| 208 alt_error_page_fetcher_.release()); | 211 alt_error_page_fetcher_.release()); |
| 209 if (!response.isNull() && response.httpStatusCode() == 200) { | 212 if (!response.isNull() && response.httpStatusCode() == 200) { |
| 210 core_.OnAlternateErrorPageFetched(data); | 213 core_.OnAlternateErrorPageFetched(data); |
| 211 } else { | 214 } else { |
| 212 core_.OnAlternateErrorPageFetched(""); | 215 core_.OnAlternateErrorPageFetched(""); |
| 213 } | 216 } |
| 214 } | 217 } |
| OLD | NEW |