Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / security_warnings / interstitial_v2.js
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 // This is the shared code for the new (Chrome 37) security interstitials. It is
6 // used for both SSL interstitials and Safe Browsing interstitials.
7
8 var expandedDetails = false;
9 var keyPressState = 0;
10
11 /**
12  * A convenience method for sending commands to the parent page.
13  * @param {string} cmd  The command to send.
14  */
15 function sendCommand(cmd) {
16   window.domAutomationController.setAutomationId(1);
17   window.domAutomationController.send(cmd);
18 }
19
20 /**
21  * This allows errors to be skippped by typing "danger" into the page.
22  * @param {string} e The key that was just pressed.
23  */
24 function handleKeypress(e) {
25   var BYPASS_SEQUENCE = 'danger';
26   if (BYPASS_SEQUENCE.charCodeAt(keyPressState) == e.keyCode) {
27     keyPressState++;
28     if (keyPressState == BYPASS_SEQUENCE.length) {
29       sendCommand(SSL_CMD_PROCEED);
30       keyPressState = 0;
31     }
32   } else {
33     keyPressState = 0;
34   }
35 }
36
37 /**
38  * This appends a piece of debugging information to the end of the warning.
39  * When complete, the caller must also make the debugging div
40  * (error-debugging-info) visible.
41  * @param {string} title  The name of this debugging field.
42  * @param {string} value  The value of the debugging field.
43  */
44 function appendDebuggingField(title, value) {
45   // The values input here are not trusted. Never use innerHTML on these
46   // values!
47   var spanTitle = document.createElement('span');
48   spanTitle.classList.add('debugging-title');
49   spanTitle.innerText = title + ': ';
50
51   var spanValue = document.createElement('span');
52   spanValue.classList.add('debugging-value');
53   spanValue.innerText = value;
54
55   var pElem = document.createElement('p');
56   pElem.classList.add('debugging-content');
57   pElem.appendChild(spanTitle);
58   pElem.appendChild(spanValue);
59   $('error-debugging-info').appendChild(pElem);
60 }
61
62 function toggleDebuggingInfo() {
63   $('error-debugging-info').classList.toggle('hidden');
64 }
65
66 function setupEvents() {
67   var overridable = loadTimeData.getBoolean('overridable');
68   var ssl = loadTimeData.getString('type') === 'SSL';
69   var badClock = ssl && loadTimeData.getBoolean('bad_clock');
70   var hidePrimaryButton = badClock && loadTimeData.getBoolean(
71       'hide_primary_button');
72
73   if (ssl) {
74     $('body').classList.add(badClock ? 'bad-clock' : 'ssl');
75     $('error-code').textContent = loadTimeData.getString('errorCode');
76     $('error-code').classList.remove('hidden');
77   } else {
78     $('body').classList.add('safe-browsing');
79   }
80
81   if (hidePrimaryButton) {
82     $('primary-button').classList.add('hidden');
83   } else {
84     $('primary-button').addEventListener('click', function() {
85       if (!ssl)
86         sendCommand(SB_CMD_TAKE_ME_BACK);
87       else if (badClock)
88         sendCommand(SSL_CMD_CLOCK);
89       else if (overridable)
90         sendCommand(SSL_CMD_DONT_PROCEED);
91       else
92         sendCommand(SSL_CMD_RELOAD);
93     });
94   }
95
96   if (overridable) {
97     $('proceed-link').addEventListener('click', function(event) {
98       sendCommand(ssl ? SSL_CMD_PROCEED : SB_CMD_PROCEED);
99     });
100   } else if (!ssl) {
101     $('final-paragraph').classList.add('hidden');
102   }
103
104   if (ssl && overridable) {
105     $('proceed-link').classList.add('small-link');
106   } else if ($('help-link')) {
107     // Overridable SSL page doesn't have this link.
108     $('help-link').addEventListener('click', function(event) {
109       if (ssl)
110         sendCommand(SSL_CMD_HELP);
111       else if (loadTimeData.getBoolean('phishing'))
112         sendCommand(SB_CMD_LEARN_MORE_2);
113       else
114         sendCommand(SB_CMD_SHOW_DIAGNOSTIC);
115     });
116   }
117
118   $('details-button').addEventListener('click', function(event) {
119     var hiddenDetails = $('details').classList.toggle('hidden');
120     $('details-button').innerText = hiddenDetails ?
121         loadTimeData.getString('openDetails') :
122         loadTimeData.getString('closeDetails');
123     if (!expandedDetails) {
124       // Record a histogram entry only the first time that details is opened.
125       sendCommand(ssl ? SSL_CMD_MORE : SB_CMD_EXPANDED_SEE_MORE);
126       expandedDetails = true;
127     }
128   });
129
130   preventDefaultOnPoundLinkClicks();
131   setupCheckbox();
132   setupSSLDebuggingInfo();
133   document.addEventListener('keypress', handleKeypress);
134 }
135
136 document.addEventListener('DOMContentLoaded', setupEvents);