OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 /** | |
6 * This view displays options for exporting the captured data. | |
7 */ | |
8 var ExportView = (function() { | |
9 'use strict'; | |
10 | |
11 // We inherit from DivView. | |
12 var superClass = DivView; | |
13 | |
14 /** | |
15 * @constructor | |
16 */ | |
17 function ExportView() { | |
18 assertFirstConstructorCall(ExportView); | |
19 | |
20 // Call superclass's constructor. | |
21 superClass.call(this, ExportView.MAIN_BOX_ID); | |
22 | |
23 this.deprecatedNoticeUI_ = $(ExportView.DEPRECATED_NOTICE_UI_ID); | |
24 this.showDeprecatedUIButton_ = $(ExportView.SHOW_DEPRECATED_UI_BUTTON_ID); | |
25 this.showDeprecatedUIButton_.onclick = this.onShowDeprecatedUI_.bind(this); | |
26 this.deprecatedExportUI_ = $(ExportView.DEPRECATED_EXPORT_UI_ID); | |
27 | |
28 var privacyStrippingCheckbox = $(ExportView.PRIVACY_STRIPPING_CHECKBOX_ID); | |
29 privacyStrippingCheckbox.onclick = | |
30 this.onSetPrivacyStripping_.bind(this, privacyStrippingCheckbox); | |
31 | |
32 this.saveFileButton_ = $(ExportView.SAVE_FILE_BUTTON_ID); | |
33 this.saveFileButton_.onclick = this.onSaveFile_.bind(this); | |
34 this.saveStatusText_ = $(ExportView.SAVE_STATUS_TEXT_ID); | |
35 this.isSaving_ = false; | |
36 | |
37 this.userCommentsTextArea_ = $(ExportView.USER_COMMENTS_TEXT_AREA_ID); | |
38 this.updateSaveFileButton_(); | |
39 this.userCommentsTextArea_.onkeyup = this.onUserCommentsUpdated_.bind(this); | |
40 | |
41 // Track blob for previous log dump so it can be revoked when a new dump is | |
42 // saved. | |
43 this.lastBlobURL_ = null; | |
44 | |
45 // Cached copy of the last loaded log dump, for use when exporting. | |
46 this.loadedLogDump_ = null; | |
47 } | |
48 | |
49 ExportView.TAB_ID = 'tab-handle-export'; | |
50 ExportView.TAB_NAME = 'Export'; | |
51 ExportView.TAB_HASH = '#export'; | |
52 | |
53 // IDs for special HTML elements in export_view.html | |
54 ExportView.DEPRECATED_NOTICE_UI_ID = 'export-view-deprecated-notice'; | |
55 ExportView.SHOW_DEPRECATED_UI_BUTTON_ID = 'export-view-show-deprecated-ui'; | |
56 ExportView.DEPRECATED_EXPORT_UI_ID = 'export-view-ui-deprecated'; | |
57 | |
58 ExportView.MAIN_BOX_ID = 'export-view-tab-content'; | |
59 ExportView.DOWNLOAD_ANCHOR_ID = 'export-view-download-anchor'; | |
60 ExportView.SAVE_FILE_BUTTON_ID = 'export-view-save-log-file'; | |
61 ExportView.SAVE_STATUS_TEXT_ID = 'export-view-save-status-text'; | |
62 ExportView.PRIVACY_STRIPPING_CHECKBOX_ID = | |
63 'export-view-privacy-stripping-checkbox'; | |
64 ExportView.USER_COMMENTS_TEXT_AREA_ID = 'export-view-user-comments'; | |
65 ExportView.PRIVACY_WARNING_ID = 'export-view-privacy-warning'; | |
66 | |
67 cr.addSingletonGetter(ExportView); | |
68 | |
69 ExportView.prototype = { | |
70 // Inherit the superclass's methods. | |
71 __proto__: superClass.prototype, | |
72 | |
73 /** | |
74 * Hides the export-view deprecation warning message and shows the | |
75 * deprecated export-view UI. | |
76 */ | |
77 onShowDeprecatedUI_: function() { | |
78 setNodeDisplay(this.deprecatedNoticeUI_, false); | |
79 setNodeDisplay(this.deprecatedExportUI_, true); | |
80 }, | |
81 | |
82 /** | |
83 * Depending on the value of the checkbox, enables or disables stripping | |
84 * cookies and passwords from log dumps and displayed events. | |
85 */ | |
86 onSetPrivacyStripping_: function(privacyStrippingCheckbox) { | |
87 SourceTracker.getInstance().setPrivacyStripping( | |
88 privacyStrippingCheckbox.checked); | |
89 }, | |
90 | |
91 /** | |
92 * When loading a log dump, cache it for future export and continue showing | |
93 * the ExportView. | |
94 */ | |
95 onLoadLogFinish: function(polledData, tabData, logDump) { | |
96 this.loadedLogDump_ = logDump; | |
97 this.setUserComments_(logDump.userComments); | |
98 return true; | |
99 }, | |
100 | |
101 /** | |
102 * Sets the save to file status text, displayed below the save to file | |
103 * button, to |text|. Also enables or disables the save button based on the | |
104 * value of |isSaving|, which must be true if the save process is still | |
105 * ongoing, and false when the operation has stopped, regardless of success | |
106 * of failure. | |
107 */ | |
108 setSaveFileStatus: function(text, isSaving) { | |
109 this.isSaving_ = isSaving; | |
110 this.updateSaveFileButton_(); | |
111 this.saveStatusText_.textContent = text; | |
112 }, | |
113 | |
114 updateSaveFileButton_: function() { | |
115 this.saveFileButton_.disabled = | |
116 this.userCommentsTextArea_.value == '' || this.isSaving_; | |
117 }, | |
118 | |
119 showPrivacyWarning: function() { | |
120 setNodeDisplay($(ExportView.PRIVACY_WARNING_ID), true); | |
121 $(ExportView.PRIVACY_STRIPPING_CHECKBOX_ID).checked = false; | |
122 $(ExportView.PRIVACY_STRIPPING_CHECKBOX_ID).disabled = true; | |
123 | |
124 // Updating the checkbox doesn't actually disable privacy stripping, since | |
125 // the onclick function will not be called. | |
126 this.onSetPrivacyStripping_($(ExportView.PRIVACY_STRIPPING_CHECKBOX_ID)); | |
127 }, | |
128 | |
129 /** | |
130 * If not already busy saving a log dump, triggers asynchronous | |
131 * generation of log dump and starts waiting for it to complete. | |
132 */ | |
133 onSaveFile_: function() { | |
134 if (this.saveFileButton_.disabled) | |
135 return; | |
136 | |
137 // Clean up previous blob, if any, to reduce resource usage. | |
138 if (this.lastBlobURL_) { | |
139 window.URL.revokeObjectURL(this.lastBlobURL_); | |
140 this.lastBlobURL_ = null; | |
141 } | |
142 this.createLogDump_(this.onLogDumpCreated_.bind(this)); | |
143 }, | |
144 | |
145 /** | |
146 * Creates a log dump, and either synchronously or asynchronously calls | |
147 * |callback| if it succeeds. Separate from onSaveFile_ for unit tests. | |
148 */ | |
149 createLogDump_: function(callback) { | |
150 // Get an explanation for the dump file (this is mandatory!) | |
151 var userComments = this.userCommentsTextArea_.value; | |
152 | |
153 this.setSaveFileStatus('Preparing data...', true); | |
154 | |
155 var privacyStripping = SourceTracker.getInstance().getPrivacyStripping(); | |
156 | |
157 // If we have a cached log dump, update it synchronously. | |
158 if (this.loadedLogDump_) { | |
159 var dumpText = log_util.createUpdatedLogDump( | |
160 userComments, this.loadedLogDump_, privacyStripping); | |
161 callback(dumpText); | |
162 return; | |
163 } | |
164 | |
165 // Otherwise, poll information from the browser before creating one. | |
166 log_util.createLogDumpAsync(userComments, callback, privacyStripping); | |
167 }, | |
168 | |
169 /** | |
170 * Sets the user comments. | |
171 */ | |
172 setUserComments_: function(userComments) { | |
173 this.userCommentsTextArea_.value = userComments; | |
174 this.onUserCommentsUpdated_(); | |
175 }, | |
176 | |
177 /** | |
178 * User comments are updated. | |
179 */ | |
180 onUserCommentsUpdated_: function() { | |
181 this.updateSaveFileButton_(); | |
182 }, | |
183 | |
184 /** | |
185 * Creates a blob url and starts downloading it. | |
186 */ | |
187 onLogDumpCreated_: function(dumpText) { | |
188 var textBlob = new Blob([dumpText], {type: 'octet/stream'}); | |
189 this.lastBlobURL_ = window.URL.createObjectURL(textBlob); | |
190 | |
191 // Update the anchor tag and simulate a click on it to start the | |
192 // download. | |
193 var downloadAnchor = $(ExportView.DOWNLOAD_ANCHOR_ID); | |
194 downloadAnchor.href = this.lastBlobURL_; | |
195 downloadAnchor.click(); | |
196 | |
197 this.setSaveFileStatus('Dump successful', false); | |
198 } | |
199 }; | |
200 | |
201 return ExportView; | |
202 })(); | |
OLD | NEW |