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.
5 // This is the shared code for the new (Chrome 37) security interstitials. It is
6 // used for both SSL interstitials and Safe Browsing interstitials.
8 var expandedDetails = false;
11 // Should match SecurityInterstitialCommands in security_interstitial_page.h
12 var CMD_DONT_PROCEED = 0;
14 // Ways for user to get more information
15 var CMD_SHOW_MORE_SECTION = 2;
16 var CMD_OPEN_HELP_CENTER = 3;
17 var CMD_OPEN_DIAGNOSTIC = 4;
18 // Primary button actions
20 var CMD_OPEN_DATE_SETTINGS = 6;
21 var CMD_OPEN_LOGIN = 7;
22 // Safe Browsing Extended Reporting
23 var CMD_DO_REPORT = 8;
24 var CMD_DONT_REPORT = 9;
25 var CMD_OPEN_REPORTING_PRIVACY = 10;
28 * A convenience method for sending commands to the parent page.
29 * @param {string} cmd The command to send.
31 function sendCommand(cmd) {
32 window.domAutomationController.setAutomationId(1);
33 window.domAutomationController.send(cmd);
37 * This allows errors to be skippped by typing "danger" into the page.
38 * @param {string} e The key that was just pressed.
40 function handleKeypress(e) {
41 var BYPASS_SEQUENCE = 'danger';
42 if (BYPASS_SEQUENCE.charCodeAt(keyPressState) == e.keyCode) {
44 if (keyPressState == BYPASS_SEQUENCE.length) {
45 sendCommand(CMD_PROCEED);
54 * This appends a piece of debugging information to the end of the warning.
55 * When complete, the caller must also make the debugging div
56 * (error-debugging-info) visible.
57 * @param {string} title The name of this debugging field.
58 * @param {string} value The value of the debugging field.
60 function appendDebuggingField(title, value) {
61 // The values input here are not trusted. Never use innerHTML on these
63 var spanTitle = document.createElement('span');
64 spanTitle.classList.add('debugging-title');
65 spanTitle.innerText = title + ': ';
67 var spanValue = document.createElement('span');
68 spanValue.classList.add('debugging-value');
69 spanValue.innerText = value;
71 var pElem = document.createElement('p');
72 pElem.classList.add('debugging-content');
73 pElem.appendChild(spanTitle);
74 pElem.appendChild(spanValue);
75 $('error-debugging-info').appendChild(pElem);
78 function toggleDebuggingInfo() {
79 $('error-debugging-info').classList.toggle('hidden');
82 function setupEvents() {
83 var overridable = loadTimeData.getBoolean('overridable');
84 var interstitialType = loadTimeData.getString('type');
85 var ssl = interstitialType == 'SSL';
86 var captivePortal = interstitialType == 'CAPTIVE_PORTAL';
87 var badClock = ssl && loadTimeData.getBoolean('bad_clock');
88 var hidePrimaryButton = badClock && loadTimeData.getBoolean(
89 'hide_primary_button');
92 $('body').classList.add(badClock ? 'bad-clock' : 'ssl');
93 $('error-code').textContent = loadTimeData.getString('errorCode');
94 $('error-code').classList.remove('hidden');
95 } else if (captivePortal) {
96 $('body').classList.add('captive-portal');
98 $('body').classList.add('safe-browsing');
101 if (hidePrimaryButton) {
102 $('primary-button').classList.add('hidden');
104 $('primary-button').addEventListener('click', function() {
105 switch (interstitialType) {
106 case 'CAPTIVE_PORTAL':
107 sendCommand(CMD_OPEN_LOGIN);
112 sendCommand(CMD_OPEN_DATE_SETTINGS);
113 else if (overridable)
114 sendCommand(CMD_DONT_PROCEED);
116 sendCommand(CMD_RELOAD);
120 sendCommand(CMD_DONT_PROCEED);
124 throw 'Invalid interstitial type';
130 // Captive portal page isn't overridable.
131 $('proceed-link').addEventListener('click', function(event) {
132 sendCommand(CMD_PROCEED);
135 $('final-paragraph').classList.add('hidden');
138 if (ssl && overridable) {
139 $('proceed-link').classList.add('small-link');
140 } else if ($('help-link')) {
141 // Overridable SSL page doesn't have this link.
142 $('help-link').addEventListener('click', function(event) {
143 if (ssl || loadTimeData.getBoolean('phishing'))
144 sendCommand(CMD_OPEN_HELP_CENTER);
146 sendCommand(CMD_OPEN_DIAGNOSTIC);
151 // Captive portal page doesn't have details button.
152 $('details-button').classList.add('hidden');
154 $('details-button').addEventListener('click', function(event) {
155 var hiddenDetails = $('details').classList.toggle('hidden');
158 // Details appear over the main content on small screens.
159 $('main-content').classList.toggle('hidden', !hiddenDetails);
161 $('main-content').classList.remove('hidden');
164 $('details-button').innerText = hiddenDetails ?
165 loadTimeData.getString('openDetails') :
166 loadTimeData.getString('closeDetails');
167 if (!expandedDetails) {
168 // Record a histogram entry only the first time that details is opened.
169 sendCommand(CMD_SHOW_MORE_SECTION);
170 expandedDetails = true;
175 preventDefaultOnPoundLinkClicks();
176 setupExtendedReportingCheckbox();
177 setupSSLDebuggingInfo();
178 document.addEventListener('keypress', handleKeypress);
181 document.addEventListener('DOMContentLoaded', setupEvents);