Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / ssl / bad_clock_blocking_page.cc
blob6d7075398810e6ad4e007815400da910a97c5aeb
1 // Copyright 2015 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 "chrome/browser/ssl/bad_clock_blocking_page.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/build_time.h"
10 #include "base/callback_helpers.h"
11 #include "base/command_line.h"
12 #include "base/i18n/rtl.h"
13 #include "base/i18n/time_formatting.h"
14 #include "base/process/launch.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_piece.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/time/time.h"
21 #include "base/values.h"
22 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/renderer_preferences_util.h"
25 #include "chrome/browser/ssl/ssl_error_classification.h"
26 #include "chrome/browser/ssl/ssl_error_info.h"
27 #include "chrome/common/pref_names.h"
28 #include "chrome/grit/chromium_strings.h"
29 #include "chrome/grit/generated_resources.h"
30 #include "components/google/core/browser/google_util.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/cert_store.h"
33 #include "content/public/browser/interstitial_page.h"
34 #include "content/public/browser/interstitial_page_delegate.h"
35 #include "content/public/browser/navigation_controller.h"
36 #include "content/public/browser/navigation_entry.h"
37 #include "content/public/browser/render_process_host.h"
38 #include "content/public/browser/render_view_host.h"
39 #include "content/public/browser/web_contents.h"
40 #include "content/public/common/renderer_preferences.h"
41 #include "content/public/common/ssl_status.h"
42 #include "grit/browser_resources.h"
43 #include "net/base/net_errors.h"
44 #include "net/base/net_util.h"
45 #include "ui/base/l10n/l10n_util.h"
47 #if defined(OS_ANDROID)
48 #include "chrome/browser/android/intent_helper.h"
49 #endif
51 #if defined(OS_CHROMEOS)
52 #include "chrome/browser/profiles/profile_manager.h"
53 #include "chrome/browser/ui/chrome_pages.h"
54 #include "chrome/common/url_constants.h"
55 #endif
57 #if defined(OS_WIN)
58 #include "base/base_paths_win.h"
59 #include "base/path_service.h"
60 #include "base/strings/string16.h"
61 #include "base/win/windows_version.h"
62 #endif
64 using base::ASCIIToUTF16;
65 using base::TimeTicks;
66 using content::InterstitialPage;
67 using content::InterstitialPageDelegate;
68 using content::NavigationController;
69 using content::NavigationEntry;
71 namespace {
73 void LaunchDateAndTimeSettings() {
74 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
75 // The code for each OS is completely separate, in order to avoid bugs like
76 // https://crbug.com/430877 .
77 #if defined(OS_ANDROID)
78 chrome::android::OpenDateAndTimeSettings();
80 #elif defined(OS_CHROMEOS)
81 std::string sub_page =
82 std::string(chrome::kSearchSubPage) + "#" +
83 l10n_util::GetStringUTF8(IDS_OPTIONS_SETTINGS_SECTION_TITLE_DATETIME);
84 chrome::ShowSettingsSubPageForProfile(ProfileManager::GetActiveUserProfile(),
85 sub_page);
87 #elif defined(OS_IOS)
88 // iOS does not have a way to launch the date and time settings.
89 NOTREACHED();
91 #elif defined(OS_LINUX)
92 struct ClockCommand {
93 const char* pathname;
94 const char* argument;
96 static const ClockCommand kClockCommands[] = {
97 // Unity
98 {"/usr/bin/unity-control-center", "datetime"},
99 // GNOME
101 // NOTE: On old Ubuntu, naming control panels doesn't work, so it
102 // opens the overview. This will have to be good enough.
103 {"/usr/bin/gnome-control-center", "datetime"},
104 {"/usr/local/bin/gnome-control-center", "datetime"},
105 {"/opt/bin/gnome-control-center", "datetime"},
106 // KDE
107 {"/usr/bin/kcmshell4", "clock"},
108 {"/usr/local/bin/kcmshell4", "clock"},
109 {"/opt/bin/kcmshell4", "clock"},
112 base::CommandLine command(base::FilePath(""));
113 for (const ClockCommand& cmd : kClockCommands) {
114 base::FilePath pathname(cmd.pathname);
115 if (base::PathExists(pathname)) {
116 command.SetProgram(pathname);
117 command.AppendArg(cmd.argument);
118 break;
121 if (command.GetProgram().empty()) {
122 // Alas, there is nothing we can do.
123 return;
126 base::LaunchOptions options;
127 options.wait = false;
128 options.allow_new_privs = true;
129 base::LaunchProcess(command, options);
131 #elif defined(OS_MACOSX)
132 base::CommandLine command(base::FilePath("/usr/bin/open"));
133 command.AppendArg("/System/Library/PreferencePanes/DateAndTime.prefPane");
135 base::LaunchOptions options;
136 options.wait = false;
137 base::LaunchProcess(command, options);
139 #elif defined(OS_WIN)
140 base::FilePath path;
141 PathService::Get(base::DIR_SYSTEM, &path);
142 static const base::char16 kControlPanelExe[] = L"control.exe";
143 path = path.Append(base::string16(kControlPanelExe));
144 base::CommandLine command(path);
145 command.AppendArg(std::string("/name"));
146 command.AppendArg(std::string("Microsoft.DateAndTime"));
148 base::LaunchOptions options;
149 options.wait = false;
150 base::LaunchProcess(command, options);
152 #else
153 NOTREACHED();
155 #endif
156 // Don't add code here! (See the comment at the beginning of the function.)
159 } // namespace
161 // static
162 InterstitialPageDelegate::TypeID BadClockBlockingPage::kTypeForTesting =
163 &BadClockBlockingPage::kTypeForTesting;
165 // Note that we always create a navigation entry with SSL errors.
166 // No error happening loading a sub-resource triggers an interstitial so far.
167 // Creating an interstitial without showing (e.g. from chrome://interstitials)
168 // it leaks memory, so don't create it here.
169 BadClockBlockingPage::BadClockBlockingPage(
170 content::WebContents* web_contents,
171 int cert_error,
172 const net::SSLInfo& ssl_info,
173 const GURL& request_url,
174 const base::Time& time_triggered,
175 const base::Callback<void(bool)>& callback)
176 : SecurityInterstitialPage(web_contents, request_url),
177 callback_(callback),
178 cert_error_(cert_error),
179 ssl_info_(ssl_info),
180 time_triggered_(time_triggered) {}
182 bool BadClockBlockingPage::ShouldCreateNewNavigation() const {
183 return true;
186 InterstitialPageDelegate::TypeID BadClockBlockingPage::GetTypeForTesting()
187 const {
188 return BadClockBlockingPage::kTypeForTesting;
191 BadClockBlockingPage::~BadClockBlockingPage() {
192 if (!callback_.is_null()) {
193 // Deny when the page is closed.
194 NotifyDenyCertificate();
198 void BadClockBlockingPage::PopulateInterstitialStrings(
199 base::DictionaryValue* load_time_data) {
200 CHECK(load_time_data);
201 base::string16 url(GetFormattedHostName());
203 // Values that are currently still shared with the SSL interstitial.
204 load_time_data->SetString("type", "SSL");
205 load_time_data->SetString("errorCode", net::ErrorToString(cert_error_));
206 load_time_data->SetString(
207 "openDetails", l10n_util::GetStringUTF16(IDS_SSL_V2_OPEN_DETAILS_BUTTON));
208 load_time_data->SetString(
209 "closeDetails",
210 l10n_util::GetStringUTF16(IDS_SSL_V2_CLOSE_DETAILS_BUTTON));
212 // Strings for the bad clock warning specifically.
213 load_time_data->SetBoolean("bad_clock", true);
214 load_time_data->SetBoolean("overridable", false);
215 #if defined(OS_IOS)
216 load_time_data->SetBoolean("hide_primary_button", true);
217 #else
218 load_time_data->SetBoolean("hide_primary_button", false);
219 #endif
221 int heading_string =
222 SSLErrorClassification::IsUserClockInTheFuture(time_triggered_)
223 ? IDS_SSL_V2_CLOCK_AHEAD_HEADING
224 : IDS_SSL_V2_CLOCK_BEHIND_HEADING;
226 load_time_data->SetString("tabTitle",
227 l10n_util::GetStringUTF16(IDS_SSL_V2_CLOCK_TITLE));
228 load_time_data->SetString("heading",
229 l10n_util::GetStringUTF16(heading_string));
230 load_time_data->SetString(
231 "primaryParagraph",
232 l10n_util::GetStringFUTF16(
233 IDS_SSL_V2_CLOCK_PRIMARY_PARAGRAPH, url,
234 base::TimeFormatFriendlyDateAndTime(time_triggered_)));
236 load_time_data->SetString(
237 "primaryButtonText",
238 l10n_util::GetStringUTF16(IDS_SSL_V2_CLOCK_UPDATE_DATE_AND_TIME));
239 load_time_data->SetString(
240 "explanationParagraph",
241 l10n_util::GetStringUTF16(IDS_SSL_V2_CLOCK_EXPLANATION));
243 // The interstitial template expects this string, but we're not using it.
244 load_time_data->SetString("finalParagraph", std::string());
246 // Set debugging information at the bottom of the warning.
247 load_time_data->SetString("subject",
248 ssl_info_.cert->subject().GetDisplayName());
249 load_time_data->SetString("issuer",
250 ssl_info_.cert->issuer().GetDisplayName());
251 load_time_data->SetString(
252 "expirationDate",
253 base::TimeFormatShortDate(ssl_info_.cert->valid_expiry()));
254 load_time_data->SetString("currentDate",
255 base::TimeFormatShortDate(time_triggered_));
256 std::vector<std::string> encoded_chain;
257 ssl_info_.cert->GetPEMEncodedChain(&encoded_chain);
258 load_time_data->SetString(
259 "pem", base::JoinString(encoded_chain, base::StringPiece()));
262 void BadClockBlockingPage::OverrideEntry(NavigationEntry* entry) {
263 int cert_id = content::CertStore::GetInstance()->StoreCert(
264 ssl_info_.cert.get(), web_contents()->GetRenderProcessHost()->GetID());
265 DCHECK(cert_id);
267 entry->GetSSL().security_style =
268 content::SECURITY_STYLE_AUTHENTICATION_BROKEN;
269 entry->GetSSL().cert_id = cert_id;
270 entry->GetSSL().cert_status = ssl_info_.cert_status;
271 entry->GetSSL().security_bits = ssl_info_.security_bits;
274 // This handles the commands sent from the interstitial JavaScript.
275 // DO NOT reorder or change this logic without also changing the JavaScript!
276 void BadClockBlockingPage::CommandReceived(const std::string& command) {
277 if (command == "\"pageLoadComplete\"") {
278 // content::WaitForRenderFrameReady sends this message when the page
279 // load completes. Ignore it.
280 return;
283 int cmd = 0;
284 bool retval = base::StringToInt(command, &cmd);
285 DCHECK(retval);
286 switch (cmd) {
287 case CMD_DONT_PROCEED:
288 interstitial_page()->DontProceed();
289 break;
290 case CMD_DO_REPORT:
291 SetReportingPreference(true);
292 break;
293 case CMD_DONT_REPORT:
294 SetReportingPreference(false);
295 break;
296 case CMD_SHOW_MORE_SECTION:
297 break;
298 case CMD_OPEN_DATE_SETTINGS:
299 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
300 base::Bind(&LaunchDateAndTimeSettings));
301 break;
302 case CMD_OPEN_REPORTING_PRIVACY:
303 OpenExtendedReportingPrivacyPolicy();
304 break;
305 case CMD_PROCEED:
306 case CMD_OPEN_HELP_CENTER:
307 case CMD_RELOAD:
308 case CMD_OPEN_DIAGNOSTIC:
309 // Not supported for the bad clock interstitial.
310 NOTREACHED() << "Unexpected command: " << command;
314 void BadClockBlockingPage::OverrideRendererPrefs(
315 content::RendererPreferences* prefs) {
316 Profile* profile =
317 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
318 renderer_preferences_util::UpdateFromSystemSettings(prefs, profile,
319 web_contents());
322 void BadClockBlockingPage::OnDontProceed() {
323 NotifyDenyCertificate();
326 void BadClockBlockingPage::NotifyDenyCertificate() {
327 // It's possible that callback_ may not exist if the user clicks "Proceed"
328 // followed by pressing the back button before the interstitial is hidden.
329 // In that case the certificate will still be treated as allowed.
330 if (callback_.is_null())
331 return;
333 base::ResetAndReturn(&callback_).Run(false);