Carlos IL | 55ef4b4 | 2018-02-21 21:00:57 | [diff] [blame] | 1 | // Copyright 2018 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/ssl/connection_help_tab_helper.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/feature_list.h" |
| 9 | #include "base/metrics/histogram_macros.h" |
| 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "chrome/common/chrome_features.h" |
| 12 | #include "chrome/common/chrome_switches.h" |
| 13 | #include "content/public/browser/navigation_handle.h" |
| 14 | #include "content/public/browser/web_contents.h" |
| 15 | #include "content/public/common/referrer.h" |
| 16 | #include "net/base/net_errors.h" |
| 17 | #include "url/gurl.h" |
| 18 | |
| 19 | namespace { |
| 20 | const char kHelpCenterConnectionHelpUrl[] = |
| 21 | "https://support.google.com/chrome/answer/6098869/"; |
| 22 | const char kBundledConnectionHelpUrl[] = "chrome://connection-help"; |
| 23 | |
| 24 | void MaybeRedirectToBundledHelp(content::WebContents* web_contents) { |
Carlos IL | 178915c1 | 2018-04-04 23:35:02 | [diff] [blame] | 25 | if (!base::FeatureList::IsEnabled(features::kBundledConnectionHelpFeature)) |
| 26 | return; |
| 27 | GURL::Replacements replacements; |
| 28 | std::string error_code = web_contents->GetURL().ref(); |
| 29 | replacements.SetRefStr(error_code); |
| 30 | web_contents->GetController().LoadURL( |
| 31 | GURL(kBundledConnectionHelpUrl).ReplaceComponents(replacements), |
| 32 | content::Referrer(), ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 33 | std::string()); |
Carlos IL | 55ef4b4 | 2018-02-21 21:00:57 | [diff] [blame] | 34 | } |
| 35 | } // namespace |
| 36 | |
| 37 | DEFINE_WEB_CONTENTS_USER_DATA_KEY(ConnectionHelpTabHelper); |
| 38 | |
| 39 | ConnectionHelpTabHelper::~ConnectionHelpTabHelper() {} |
| 40 | |
| 41 | void ConnectionHelpTabHelper::DidAttachInterstitialPage() { |
Carlos IL | 178915c1 | 2018-04-04 23:35:02 | [diff] [blame] | 42 | GURL::Replacements replacements; |
| 43 | replacements.ClearRef(); |
| 44 | if (web_contents()->GetURL().ReplaceComponents(replacements) == |
| 45 | GetHelpCenterURL()) { |
Carlos IL | 55ef4b4 | 2018-02-21 21:00:57 | [diff] [blame] | 46 | UMA_HISTOGRAM_ENUMERATION( |
| 47 | "SSL.CertificateErrorHelpCenterVisited", |
| 48 | ConnectionHelpTabHelper::LearnMoreClickResult::kFailedWithInterstitial, |
| 49 | ConnectionHelpTabHelper::LearnMoreClickResult::kLearnMoreResultCount); |
| 50 | MaybeRedirectToBundledHelp(web_contents()); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void ConnectionHelpTabHelper::DidFinishNavigation( |
| 55 | content::NavigationHandle* navigation_handle) { |
Carlos IL | 178915c1 | 2018-04-04 23:35:02 | [diff] [blame] | 56 | GURL::Replacements replacements; |
| 57 | replacements.ClearRef(); |
| 58 | if (web_contents()->GetURL().ReplaceComponents(replacements) == |
| 59 | GetHelpCenterURL()) { |
Carlos IL | 55ef4b4 | 2018-02-21 21:00:57 | [diff] [blame] | 60 | LearnMoreClickResult histogram_value; |
| 61 | if (navigation_handle->IsErrorPage()) { |
| 62 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 63 | switches::kCommittedInterstitials) && |
| 64 | net::IsCertificateError(navigation_handle->GetNetErrorCode())) { |
| 65 | // When committed interstitials are enabled, DidAttachInterstitialPage |
| 66 | // does not get called, so check if this navigation resulted in an SSL |
| 67 | // error. |
| 68 | histogram_value = ConnectionHelpTabHelper::LearnMoreClickResult:: |
| 69 | kFailedWithInterstitial; |
| 70 | MaybeRedirectToBundledHelp(web_contents()); |
| 71 | } else { |
| 72 | histogram_value = |
| 73 | ConnectionHelpTabHelper::LearnMoreClickResult::kFailedOther; |
| 74 | } |
| 75 | } else { |
| 76 | histogram_value = |
| 77 | ConnectionHelpTabHelper::LearnMoreClickResult::kSucceeded; |
| 78 | } |
| 79 | UMA_HISTOGRAM_ENUMERATION( |
| 80 | "SSL.CertificateErrorHelpCenterVisited", histogram_value, |
| 81 | ConnectionHelpTabHelper::LearnMoreClickResult::kLearnMoreResultCount); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void ConnectionHelpTabHelper::SetHelpCenterUrlForTesting(const GURL& url) { |
| 86 | testing_url_ = url; |
| 87 | } |
| 88 | |
| 89 | ConnectionHelpTabHelper::ConnectionHelpTabHelper( |
| 90 | content::WebContents* web_contents) |
| 91 | : content::WebContentsObserver(web_contents) {} |
| 92 | |
| 93 | GURL ConnectionHelpTabHelper::GetHelpCenterURL() { |
| 94 | if (testing_url_.is_valid()) |
| 95 | return testing_url_; |
| 96 | return GURL(kHelpCenterConnectionHelpUrl); |
| 97 | } |