Bug 1288558 - Part 1: Simply move the functions/variables from passwordManagerCommon to passwordManager. r=MattN

MozReview-Commit-ID: IbBu31XQjCB

--HG--
extra : rebase_source : 0997f3410c15315dc2227f49294f7cb5b2e4175e
This commit is contained in:
Steve Chung 2016-09-05 15:47:20 +08:00
Родитель 1691a32cc5
Коммит c151888fe8
4 изменённых файлов: 223 добавлений и 236 удалений

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

@ -3,9 +3,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*** =================== SAVED SIGNONS CODE =================== ***/
var { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
Cu.import("resource://gre/modules/AppConstants.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DeferredTask",
"resource://gre/modules/DeferredTask.jsm");
@ -13,13 +15,78 @@ XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
"resource://gre/modules/PlacesUtils.jsm");
var kSignonBundle;
var kObserverService;
// interface variables
var passwordmanager = null;
var showingPasswords = false;
// password-manager lists
var signons = [];
var deletedSignons = [];
var signonsTree;
var signonReloadDisplay = {
observe: function(subject, topic, data) {
if (topic == "passwordmgr-storage-changed") {
switch (data) {
case "addLogin":
case "modifyLogin":
case "removeLogin":
case "removeAllLogins":
if (!signonsTree) {
return;
}
signons.length = 0;
LoadSignons();
// apply the filter if needed
if (document.getElementById("filter") && document.getElementById("filter").value != "") {
_filterPasswords();
}
break;
}
kObserverService.notifyObservers(null, "passwordmgr-dialog-updated", null);
}
}
}
// Formatter for localization.
var dateFormatter = new Intl.DateTimeFormat(undefined,
{ day: "numeric", month: "short", year: "numeric" });
var dateAndTimeFormatter = new Intl.DateTimeFormat(undefined,
{ day: "numeric", month: "short", year: "numeric",
hour: "numeric", minute: "numeric" });
function Startup() {
// xpconnect to password manager interfaces
passwordmanager = Components.classes["@mozilla.org/login-manager;1"]
.getService(Components.interfaces.nsILoginManager);
// be prepared to reload the display if anything changes
kObserverService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
kObserverService.addObserver(signonReloadDisplay, "passwordmgr-storage-changed", false);
signonsTree = document.getElementById("signonsTree");
}
// TODO: Integragte to SignonsStartup?
function HandleTreeColumnClick(sortFunction, event) {
if (event.target.nodeName != "treecol" || event.button != 0) {
return;
}
let sortField = event.target.getAttribute("data-field-name");
if (!sortField) {
return;
}
sortFunction(sortField);
Services.telemetry.getKeyedHistogramById("PWMGR_MANAGE_SORTED").add(sortField);
}
function SignonsStartup() {
kSignonBundle = document.getElementById("signonBundle");
document.getElementById("togglePasswords").label = kSignonBundle.getString("showPasswords");
@ -44,6 +111,10 @@ function SignonsStartup() {
FocusFilterBox();
}
function Shutdown() {
kObserverService.removeObserver(signonReloadDisplay, "passwordmgr-storage-changed");
}
function setFilter(aFilterString) {
document.getElementById("filter").value = aFilterString;
_filterPasswords();
@ -160,6 +231,73 @@ var signonsTreeView = {
},
};
function SortTree(tree, view, table, column, lastSortColumn, lastSortAscending, updateSelection) {
// remember which item was selected so we can restore it after the sort
var selections = GetTreeSelections(tree);
var selectedNumber = selections.length ? table[selections[0]].number : -1;
// determine if sort is to be ascending or descending
var ascending = (column == lastSortColumn) ? !lastSortAscending : true;
function compareFunc(a, b) {
var valA, valB;
switch (column) {
case "hostname":
var realmA = a.httpRealm;
var realmB = b.httpRealm;
realmA = realmA == null ? "" : realmA.toLowerCase();
realmB = realmB == null ? "" : realmB.toLowerCase();
valA = a[column].toLowerCase() + realmA;
valB = b[column].toLowerCase() + realmB;
break;
case "username":
case "password":
valA = a[column].toLowerCase();
valB = b[column].toLowerCase();
break;
default:
valA = a[column];
valB = b[column];
}
if (valA < valB)
return -1;
if (valA > valB)
return 1;
return 0;
}
// do the sort
table.sort(compareFunc);
if (!ascending)
table.reverse();
// restore the selection
var selectedRow = -1;
if (selectedNumber>=0 && updateSelection) {
for (var s=0; s<table.length; s++) {
if (table[s].number == selectedNumber) {
// update selection
// note: we need to deselect before reselecting in order to trigger ...Selected()
tree.view.selection.select(-1);
tree.view.selection.select(s);
selectedRow = s;
break;
}
}
}
// display the results
tree.treeBoxObject.invalidate();
if (selectedRow >= 0) {
tree.treeBoxObject.ensureRowIsVisible(selectedRow)
}
return ascending;
}
function LoadSignons() {
// loads signons into table
@ -193,6 +331,25 @@ function LoadSignons() {
return true;
}
function GetTreeSelections(tree) {
var selections = [];
var select = tree.view.selection;
if (select) {
var count = select.getRangeCount();
var min = new Object();
var max = new Object();
for (var i=0; i<count; i++) {
select.getRangeAt(i, min, max);
for (var k=min.value; k<=max.value; k++) {
if (k != -1) {
selections[selections.length] = k;
}
}
}
}
return selections;
}
function SignonSelected() {
var selections = GetTreeSelections(signonsTree);
if (selections.length) {
@ -202,6 +359,47 @@ function SignonSelected() {
}
}
function DeleteSelectedItemFromTree
(tree, view, table, deletedTable, removeButton, removeAllButton) {
// Turn off tree selection notifications during the deletion
tree.view.selection.selectEventsSuppressed = true;
// remove selected items from list (by setting them to null) and place in deleted list
var selections = GetTreeSelections(tree);
for (var s=selections.length-1; s>= 0; s--) {
var i = selections[s];
deletedTable[deletedTable.length] = table[i];
table[i] = null;
}
// collapse list by removing all the null entries
for (var j=0; j<table.length; j++) {
if (table[j] == null) {
var k = j;
while ((k < table.length) && (table[k] == null)) {
k++;
}
table.splice(j, k-j);
view.rowCount -= k - j;
tree.treeBoxObject.rowCountChanged(j, j - k);
}
}
// update selection and/or buttons
if (table.length) {
// update selection
var nextSelection = (selections[0] < table.length) ? selections[0] : table.length-1;
tree.view.selection.select(nextSelection);
tree.treeBoxObject.ensureRowIsVisible(nextSelection);
} else {
// disable buttons
document.getElementById(removeButton).setAttribute("disabled", "true")
document.getElementById(removeAllButton).setAttribute("disabled", "true");
}
tree.view.selection.selectEventsSuppressed = false;
}
function DeleteSignon() {
var syncNeeded = (signonsTreeView._filterSet.length != 0);
DeleteSelectedItemFromTree(signonsTree, signonsTreeView,
@ -210,6 +408,30 @@ function DeleteSignon() {
FinalizeSignonDeletions(syncNeeded);
}
function DeleteAllFromTree(tree, view, table, deletedTable, removeButton, removeAllButton) {
// remove all items from table and place in deleted table
for (var i=0; i<table.length; i++) {
deletedTable[deletedTable.length] = table[i];
}
table.length = 0;
// clear out selections
view.selection.select(-1);
// update the tree view and notify the tree
view.rowCount = 0;
var box = tree.treeBoxObject;
box.rowCountChanged(0, -deletedTable.length);
box.invalidate();
// disable buttons
document.getElementById(removeButton).setAttribute("disabled", "true")
document.getElementById(removeAllButton).setAttribute("disabled", "true");
}
function DeleteAllSignons() {
var prompter = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
@ -242,9 +464,7 @@ function TogglePasswordVisible() {
// Notify observers that the password visibility toggling is
// completed. (Mostly useful for tests)
Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService)
.notifyObservers(null, "passwordmgr-password-toggle-complete", null);
kObserverService.notifyObservers(null, "passwordmgr-password-toggle-complete", null);
Services.telemetry.getHistogramById("PWMGR_MANAGE_VISIBILITY_TOGGLED").add(showingPasswords);
}

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

@ -16,7 +16,6 @@
title="&savedLogins.title;"
persist="width height screenX screenY">
<script type="application/javascript" src="chrome://passwordmgr/content/passwordManagerCommon.js"/>
<script type="application/javascript" src="chrome://passwordmgr/content/passwordManager.js"/>
<stringbundle id="signonBundle"

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

@ -1,231 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*** =================== INITIALISATION CODE =================== ***/
var { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
Components.utils.import("resource://gre/modules/Services.jsm");
var kObserverService;
// interface variables
var passwordmanager = null;
// password-manager lists
var signons = [];
var deletedSignons = [];
var signonsTree;
var showingPasswords = false;
function Startup() {
// xpconnect to password manager interfaces
passwordmanager = Components.classes["@mozilla.org/login-manager;1"]
.getService(Components.interfaces.nsILoginManager);
// be prepared to reload the display if anything changes
kObserverService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
kObserverService.addObserver(signonReloadDisplay, "passwordmgr-storage-changed", false);
signonsTree = document.getElementById("signonsTree");
}
function Shutdown() {
kObserverService.removeObserver(signonReloadDisplay, "passwordmgr-storage-changed");
}
var signonReloadDisplay = {
observe: function(subject, topic, data) {
if (topic == "passwordmgr-storage-changed") {
switch (data) {
case "addLogin":
case "modifyLogin":
case "removeLogin":
case "removeAllLogins":
if (!signonsTree) {
return;
}
signons.length = 0;
LoadSignons();
// apply the filter if needed
if (document.getElementById("filter") && document.getElementById("filter").value != "") {
_filterPasswords();
}
break;
}
kObserverService.notifyObservers(null, "passwordmgr-dialog-updated", null);
}
}
}
/*** =================== GENERAL CODE =================== ***/
function DeleteAllFromTree(tree, view, table, deletedTable, removeButton, removeAllButton) {
// remove all items from table and place in deleted table
for (var i=0; i<table.length; i++) {
deletedTable[deletedTable.length] = table[i];
}
table.length = 0;
// clear out selections
view.selection.select(-1);
// update the tree view and notify the tree
view.rowCount = 0;
var box = tree.treeBoxObject;
box.rowCountChanged(0, -deletedTable.length);
box.invalidate();
// disable buttons
document.getElementById(removeButton).setAttribute("disabled", "true")
document.getElementById(removeAllButton).setAttribute("disabled", "true");
}
function DeleteSelectedItemFromTree
(tree, view, table, deletedTable, removeButton, removeAllButton) {
// Turn off tree selection notifications during the deletion
tree.view.selection.selectEventsSuppressed = true;
// remove selected items from list (by setting them to null) and place in deleted list
var selections = GetTreeSelections(tree);
for (var s=selections.length-1; s>= 0; s--) {
var i = selections[s];
deletedTable[deletedTable.length] = table[i];
table[i] = null;
}
// collapse list by removing all the null entries
for (var j=0; j<table.length; j++) {
if (table[j] == null) {
var k = j;
while ((k < table.length) && (table[k] == null)) {
k++;
}
table.splice(j, k-j);
view.rowCount -= k - j;
tree.treeBoxObject.rowCountChanged(j, j - k);
}
}
// update selection and/or buttons
if (table.length) {
// update selection
var nextSelection = (selections[0] < table.length) ? selections[0] : table.length-1;
tree.view.selection.select(nextSelection);
tree.treeBoxObject.ensureRowIsVisible(nextSelection);
} else {
// disable buttons
document.getElementById(removeButton).setAttribute("disabled", "true")
document.getElementById(removeAllButton).setAttribute("disabled", "true");
}
tree.view.selection.selectEventsSuppressed = false;
}
function GetTreeSelections(tree) {
var selections = [];
var select = tree.view.selection;
if (select) {
var count = select.getRangeCount();
var min = new Object();
var max = new Object();
for (var i=0; i<count; i++) {
select.getRangeAt(i, min, max);
for (var k=min.value; k<=max.value; k++) {
if (k != -1) {
selections[selections.length] = k;
}
}
}
}
return selections;
}
function HandleTreeColumnClick(sortFunction, event) {
if (event.target.nodeName != "treecol" || event.button != 0) {
return;
}
let sortField = event.target.getAttribute("data-field-name");
if (!sortField) {
return;
}
sortFunction(sortField);
Services.telemetry.getKeyedHistogramById("PWMGR_MANAGE_SORTED").add(sortField);
}
function SortTree(tree, view, table, column, lastSortColumn, lastSortAscending, updateSelection) {
// remember which item was selected so we can restore it after the sort
var selections = GetTreeSelections(tree);
var selectedNumber = selections.length ? table[selections[0]].number : -1;
// determine if sort is to be ascending or descending
var ascending = (column == lastSortColumn) ? !lastSortAscending : true;
function compareFunc(a, b) {
var valA, valB;
switch (column) {
case "hostname":
var realmA = a.httpRealm;
var realmB = b.httpRealm;
realmA = realmA == null ? "" : realmA.toLowerCase();
realmB = realmB == null ? "" : realmB.toLowerCase();
valA = a[column].toLowerCase() + realmA;
valB = b[column].toLowerCase() + realmB;
break;
case "username":
case "password":
valA = a[column].toLowerCase();
valB = b[column].toLowerCase();
break;
default:
valA = a[column];
valB = b[column];
}
if (valA < valB)
return -1;
if (valA > valB)
return 1;
return 0;
}
// do the sort
table.sort(compareFunc);
if (!ascending)
table.reverse();
// restore the selection
var selectedRow = -1;
if (selectedNumber>=0 && updateSelection) {
for (var s=0; s<table.length; s++) {
if (table[s].number == selectedNumber) {
// update selection
// note: we need to deselect before reselecting in order to trigger ...Selected()
tree.view.selection.select(-1);
tree.view.selection.select(s);
selectedRow = s;
break;
}
}
}
// display the results
tree.treeBoxObject.invalidate();
if (selectedRow >= 0) {
tree.treeBoxObject.ensureRowIsVisible(selectedRow)
}
return ascending;
}

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

@ -7,5 +7,4 @@ toolkit.jar:
content/passwordmgr/login.xml (content/login.xml)
* content/passwordmgr/passwordManager.xul (content/passwordManager.xul)
content/passwordmgr/passwordManager.js (content/passwordManager.js)
content/passwordmgr/passwordManagerCommon.js (content/passwordManagerCommon.js)
content/passwordmgr/recipes.json (content/recipes.json)