Bug 1330932 - [Mortar] Support password-locked PDF file. f=lchang, r=evelyn

MozReview-Commit-ID: 7V3vh4Shvw

--HG--
extra : rebase_source : 36fc8ddc04bd390ac3bd52334783e336393e327d
This commit is contained in:
Rex Lee 2017-01-17 19:48:09 +08:00
Родитель eb37fd7ca3
Коммит b64cd8f280
4 изменённых файлов: 104 добавлений и 2 удалений

Просмотреть файл

@ -0,0 +1,89 @@
'use strict';
class PasswordPrompt {
/**
* @constructs PasswordPrompt
*/
constructor(viewport) {
this.overlayName = 'passwordOverlay';
this.container = document.getElementById('passwordOverlay');
this.overlaysParent = document.getElementById('overlayContainer');
this.label = document.getElementById('passwordText');
this.input = document.getElementById('password');
this.submitButton = document.getElementById('passwordSubmit');
this.cancelButton = document.getElementById('passwordCancel');
this.updateCallback = null;
this.reason = null;
this.active = false;
this._viewport = viewport;
// PDFium doesn't return the result of password check. We count the retries
// to determine whether to show "invalid password" prompt instead.
// PDFium allows at most 3 times of tries.
this._passwordTries = 0;
this._viewport.onPasswordRequest = this.open.bind(this);
// Attach the event listeners.
this.submitButton.addEventListener('click', this);
this.cancelButton.addEventListener('click', this);
this.input.addEventListener('keydown', this);
}
handleEvent(e) {
switch(e.type) {
case 'keydown':
if (e.target == this.input && e.keyCode === KeyEvent.DOM_VK_RETURN) {
this.verify();
e.stopPropagation();
} else if (e.currentTarget == window &&
e.keyCode === KeyEvent.DOM_VK_ESCAPE) {
this.close();
e.preventDefault();
e.stopImmediatePropagation();
}
break;
case 'click':
if (e.target == this.submitButton) {
this.verify();
} else if (e.target == this.cancelButton) {
this.close();
}
break;
}
}
open() {
this.container.classList.remove('hidden');
this.overlaysParent.classList.remove('hidden');
window.addEventListener('keydown', this);
this.active = true;
let promptKey = this._passwordTries ? 'password_invalid' : 'password_label';
this._passwordTries++;
this.input.type = 'password';
this.input.focus();
document.l10n.formatValue(promptKey).then(promptString => {
this.label.textContent = promptString;
});
}
close() {
this.container.classList.add('hidden');
this.overlaysParent.classList.add('hidden');
window.removeEventListener('keydown', this);
this.active = false;
this.input.value = '';
this.input.type = '';
}
verify() {
let password = this.input.value;
if (password && password.length > 0) {
this.close();
this._viewport.verifyPassword(password);
}
}
}

Просмотреть файл

@ -7,6 +7,7 @@
window.addEventListener('DOMContentLoaded', function() {
let viewport = new Viewport();
let toolbar = new Toolbar(viewport);
let passwordPrompt = new PasswordPrompt(viewport);
// Expose the custom viewport object to runtime
window.createCustomViewport = function(actionHandler) {

Просмотреть файл

@ -36,6 +36,7 @@ class Viewport {
this.onZoomChanged = null;
this.onDimensionChanged = null;
this.onPageChanged = null;
this.onPasswordRequest = null;
this._viewportController.addEventListener('scroll', this);
window.addEventListener('resize', this);
@ -123,14 +124,14 @@ class Viewport {
}
_getScrollbarWidth() {
var div = document.createElement('div');
let div = document.createElement('div');
div.style.visibility = 'hidden';
div.style.overflow = 'scroll';
div.style.width = '50px';
div.style.height = '50px';
div.style.position = 'absolute';
document.body.appendChild(div);
var result = div.offsetWidth - div.clientWidth;
let result = div.offsetWidth - div.clientWidth;
div.remove();
return result;
}
@ -468,6 +469,13 @@ class Viewport {
}
}
verifyPassword(password) {
this._doAction({
type: 'getPasswordComplete',
password: password
});
}
handleEvent(evt) {
switch(evt.type) {
case 'resize':
@ -591,6 +599,9 @@ class Viewport {
case 'fullscreenChange':
this._handleFullscreenChange(message.fullscreen);
break;
case 'getPassword':
this.onPasswordRequest && this.onPasswordRequest();
break;
}
}
}

Просмотреть файл

@ -18,6 +18,7 @@
<script src="js/polyfill.js"></script>
<script src="js/toolbar.js"></script>
<script src="js/viewport.js"></script>
<script src="js/password-prompt.js"></script>
<script src="js/viewer.js"></script>
</head>