OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "chrome/browser/ui/webui/interstitials/interstitial_ui.h" |
| 6 |
| 7 #include "base/strings/string_util.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
| 11 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 12 #include "chrome/browser/ssl/ssl_blocking_page.h" |
| 13 #include "chrome/common/url_constants.h" |
| 14 #include "content/public/browser/interstitial_page_delegate.h" |
| 15 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/browser/web_ui.h" |
| 17 #include "content/public/browser/web_ui_controller.h" |
| 18 #include "content/public/browser/web_ui_data_source.h" |
| 19 #include "grit/browser_resources.h" |
| 20 #include "net/base/net_errors.h" |
| 21 #include "net/base/url_util.h" |
| 22 #include "net/cert/x509_certificate.h" |
| 23 #include "net/ssl/ssl_info.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 class InterstitialHTMLSource : public content::URLDataSource { |
| 28 public: |
| 29 InterstitialHTMLSource(Profile* profile, |
| 30 content::WebContents* web_contents); |
| 31 virtual ~InterstitialHTMLSource(); |
| 32 |
| 33 // content::URLDataSource: |
| 34 virtual std::string GetMimeType(const std::string& mime_type) const OVERRIDE; |
| 35 virtual std::string GetSource() const OVERRIDE; |
| 36 virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE; |
| 37 virtual void StartDataRequest( |
| 38 const std::string& path, |
| 39 int render_process_id, |
| 40 int render_frame_id, |
| 41 const content::URLDataSource::GotDataCallback& callback) OVERRIDE; |
| 42 |
| 43 private: |
| 44 Profile* profile_; |
| 45 content::WebContents* web_contents_; |
| 46 DISALLOW_COPY_AND_ASSIGN(InterstitialHTMLSource); |
| 47 }; |
| 48 |
| 49 SSLBlockingPage* CreateSSLBlockingPage(content::WebContents* web_contents) { |
| 50 // Random parameters for SSL blocking page. |
| 51 int cert_error = net::ERR_CERT_CONTAINS_ERRORS; |
| 52 GURL request_url("https://example.com"); |
| 53 bool overridable = false; |
| 54 bool strict_enforcement = false; |
| 55 std::string url_param; |
| 56 if (net::GetValueForKeyInQuery(web_contents->GetURL(), |
| 57 "url", |
| 58 &url_param)) { |
| 59 if (GURL(url_param).is_valid()) |
| 60 request_url = GURL(url_param); |
| 61 } |
| 62 std::string overridable_param; |
| 63 if (net::GetValueForKeyInQuery(web_contents->GetURL(), |
| 64 "overridable", |
| 65 &overridable_param)) { |
| 66 overridable = overridable_param == "1"; |
| 67 } |
| 68 std::string strict_enforcement_param; |
| 69 if (net::GetValueForKeyInQuery(web_contents->GetURL(), |
| 70 "strict_enforcement", |
| 71 &strict_enforcement_param)) { |
| 72 strict_enforcement = strict_enforcement_param == "1"; |
| 73 } |
| 74 net::SSLInfo ssl_info; |
| 75 ssl_info.cert = new net::X509Certificate( |
| 76 request_url.host(), "CA", base::Time::Max(), base::Time::Max()); |
| 77 // This delegate doesn't create an interstitial. |
| 78 return SSLBlockingPage::CreateForWebUI(web_contents, cert_error, ssl_info, |
| 79 request_url, overridable, |
| 80 strict_enforcement); |
| 81 } |
| 82 |
| 83 SafeBrowsingBlockingPage* CreateSafeBrowsingBlockingPage( |
| 84 content::WebContents* web_contents) { |
| 85 SBThreatType threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 86 GURL request_url("http://example.com"); |
| 87 std::string url_param; |
| 88 if (net::GetValueForKeyInQuery(web_contents->GetURL(), |
| 89 "url", |
| 90 &url_param)) { |
| 91 if (GURL(url_param).is_valid()) |
| 92 request_url = GURL(url_param); |
| 93 } |
| 94 std::string type_param; |
| 95 if (net::GetValueForKeyInQuery(web_contents->GetURL(), |
| 96 "type", |
| 97 &type_param)) { |
| 98 if (type_param == "malware") { |
| 99 threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 100 } else if (type_param == "phishing") { |
| 101 threat_type = SB_THREAT_TYPE_URL_PHISHING; |
| 102 } else if (type_param == "clientside_malware") { |
| 103 threat_type = SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL; |
| 104 } else if (type_param == "clientside_phishing") { |
| 105 threat_type = SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL; |
| 106 // Interstitials for client side phishing urls load after the page loads |
| 107 // (see SafeBrowsingBlockingPage::IsMainPageLoadBlocked), so there should |
| 108 // either be a new navigation entry, or there shouldn't be any pending |
| 109 // entries. Clear any pending navigation entries. |
| 110 content::NavigationController* controller = |
| 111 &web_contents->GetController(); |
| 112 controller->DiscardNonCommittedEntries(); |
| 113 } |
| 114 } |
| 115 SafeBrowsingBlockingPage::UnsafeResource resource; |
| 116 resource.url = request_url; |
| 117 resource.threat_type = threat_type; |
| 118 return SafeBrowsingBlockingPage::CreateBlockingPage( |
| 119 g_browser_process->safe_browsing_service()->ui_manager(), |
| 120 web_contents, |
| 121 resource); |
| 122 } |
| 123 |
| 124 } // namespace |
| 125 |
| 126 InterstitialUI::InterstitialUI(content::WebUI* web_ui) |
| 127 : WebUIController(web_ui) { |
| 128 Profile* profile = Profile::FromWebUI(web_ui); |
| 129 scoped_ptr<InterstitialHTMLSource> html_source( |
| 130 new InterstitialHTMLSource(profile->GetOriginalProfile(), |
| 131 web_ui->GetWebContents())); |
| 132 content::URLDataSource::Add(profile, html_source.release()); |
| 133 } |
| 134 |
| 135 InterstitialUI::~InterstitialUI() { |
| 136 } |
| 137 |
| 138 // InterstitialHTMLSource |
| 139 |
| 140 InterstitialHTMLSource::InterstitialHTMLSource( |
| 141 Profile* profile, |
| 142 content::WebContents* web_contents) |
| 143 : profile_(profile), |
| 144 web_contents_(web_contents) { |
| 145 } |
| 146 |
| 147 InterstitialHTMLSource::~InterstitialHTMLSource() { |
| 148 } |
| 149 |
| 150 std::string InterstitialHTMLSource::GetMimeType( |
| 151 const std::string& mime_type) const { |
| 152 return "text/html"; |
| 153 } |
| 154 |
| 155 std::string InterstitialHTMLSource::GetSource() const { |
| 156 return chrome::kChromeUIInterstitialHost; |
| 157 } |
| 158 |
| 159 bool InterstitialHTMLSource::ShouldAddContentSecurityPolicy() |
| 160 const { |
| 161 return false; |
| 162 } |
| 163 |
| 164 void InterstitialHTMLSource::StartDataRequest( |
| 165 const std::string& path, |
| 166 int render_process_id, |
| 167 int render_frame_id, |
| 168 const content::URLDataSource::GotDataCallback& callback) { |
| 169 scoped_ptr<content::InterstitialPageDelegate> interstitial_delegate; |
| 170 if (StartsWithASCII(path, "ssl", true)) { |
| 171 interstitial_delegate.reset(CreateSSLBlockingPage(web_contents_)); |
| 172 } else if (StartsWithASCII(path, "safebrowsing", true)) { |
| 173 interstitial_delegate.reset(CreateSafeBrowsingBlockingPage(web_contents_)); |
| 174 } |
| 175 |
| 176 std::string html; |
| 177 if (interstitial_delegate.get()) { |
| 178 html = interstitial_delegate.get()->GetHTMLContents(); |
| 179 } else { |
| 180 html = "<html><head><title>Interstitials</title></head>" |
| 181 "<body><h2>Choose an interstitial<h2>" |
| 182 "<h3>SSL</h3>" |
| 183 "<a href='ssl'>example.com</a><br>" |
| 184 "<a href='ssl?url=https://google.com'>SSL (google.com)</a><br>" |
| 185 "<a href='ssl?overridable=1&strict_enforcement=0'>" |
| 186 " example.com (Overridable)</a>" |
| 187 "<br><br>" |
| 188 "<h3>SafeBrowsing</h3>" |
| 189 "<a href='safebrowsing?type=malware'>Malware</a><br>" |
| 190 "<a href='safebrowsing?type=phishing'>Phishing</a><br>" |
| 191 "<a href='safebrowsing?type=clientside_malware'>" |
| 192 " Client Side Malware</a><br>" |
| 193 "<a href='safebrowsing?type=clientside_phishing'>" |
| 194 " Client Side Phishing</a><br>" |
| 195 "</body></html>"; |
| 196 } |
| 197 scoped_refptr<base::RefCountedString> html_bytes = new base::RefCountedString; |
| 198 html_bytes->data().assign(html.begin(), html.end()); |
| 199 callback.Run(html_bytes.get()); |
| 200 } |
OLD | NEW |