Bug 1340468 - Notify formautofill add-on of which item is being selected. r=MattN

MozReview-Commit-ID: 3YfW35Zon1Q

--HG--
extra : rebase_source : b73ad7b533cd499588c6b5be31a8e809ba8c36db
This commit is contained in:
Ray Lin 2017-02-24 10:15:55 +08:00
Родитель af52995ddf
Коммит 29a2a2194f
4 изменённых файлов: 120 добавлений и 7 удалений

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

@ -163,6 +163,7 @@ let ProfileAutocomplete = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
_lastAutoCompleteResult: null,
_lastAutoCompleteFocusedInput: null,
_registered: false,
_factory: null,
@ -196,6 +197,7 @@ let ProfileAutocomplete = {
setProfileAutoCompleteResult(result) {
this._lastAutoCompleteResult = result;
this._lastAutoCompleteFocusedInput = formFillController.focusedInput;
},
observe(subject, topic, data) {
@ -218,6 +220,16 @@ let ProfileAutocomplete = {
.getInterface(Ci.nsIContentFrameMessageManager);
},
_getSelectedIndex(contentWindow) {
let mm = this._frameMMFromWindow(contentWindow);
let selectedIndexResult = mm.sendSyncMessage("FormAutoComplete:GetSelectedIndex", {});
if (selectedIndexResult.length != 1 || !Number.isInteger(selectedIndexResult[0])) {
throw new Error("Invalid autocomplete selectedIndex");
}
return selectedIndexResult[0];
},
_fillFromAutocompleteRow(focusedInput) {
this.log.debug("_fillFromAutocompleteRow:", focusedInput);
let formDetails = FormAutofillContent.getFormDetails(focusedInput);
@ -226,13 +238,7 @@ let ProfileAutocomplete = {
return;
}
let mm = this._frameMMFromWindow(focusedInput.ownerGlobal);
let selectedIndexResult = mm.sendSyncMessage("FormAutoComplete:GetSelectedIndex", {});
if (selectedIndexResult.length != 1 || !Number.isInteger(selectedIndexResult[0])) {
throw new Error("Invalid autocomplete selectedIndex");
}
let selectedIndex = selectedIndexResult[0];
let selectedIndex = this._getSelectedIndex(focusedInput.ownerGlobal);
if (selectedIndex == -1 ||
!this._lastAutoCompleteResult ||
this._lastAutoCompleteResult.getStyleAt(selectedIndex) != "autofill-profile") {
@ -244,6 +250,35 @@ let ProfileAutocomplete = {
formHandler.autofillFormFields(profile, focusedInput);
},
_clearProfilePreview() {
let focusedInput = formFillController.focusedInput || this._lastAutoCompleteFocusedInput;
if (!focusedInput || !FormAutofillContent.getFormDetails(focusedInput)) {
return;
}
let formHandler = FormAutofillContent.getFormHandler(focusedInput);
formHandler.clearPreviewedFormFields();
},
_previewSelectedProfile(selectedIndex) {
let focusedInput = formFillController.focusedInput;
if (!focusedInput || !FormAutofillContent.getFormDetails(focusedInput)) {
// The observer notification is for a different process/frame.
return;
}
if (!this._lastAutoCompleteResult ||
this._lastAutoCompleteResult.getStyleAt(selectedIndex) != "autofill-profile") {
return;
}
let profile = JSON.parse(this._lastAutoCompleteResult.getCommentAt(selectedIndex));
let formHandler = FormAutofillContent.getFormHandler(focusedInput);
formHandler.previewFormFields(profile);
},
};
/**
@ -383,6 +418,16 @@ var FormAutofillContent = {
_markAsAutofillField(field) {
formFillController.markAsAutofillField(field);
},
_previewProfile(doc) {
let selectedIndex = ProfileAutocomplete._getSelectedIndex(doc.ownerGlobal);
if (selectedIndex === -1) {
ProfileAutocomplete._clearProfilePreview();
} else {
ProfileAutocomplete._previewSelectedProfile(selectedIndex);
}
},
};

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

@ -124,4 +124,37 @@ FormAutofillHandler.prototype = {
}
}
},
/**
* Populates result to the preview layers with given profile.
*
* @param {Object} profile
* A profile to be previewed with
*/
previewFormFields(profile) {
log.debug("preview profile in autofillFormFields:", profile);
for (let fieldDetail of this.fieldDetails) {
let value = profile[fieldDetail.fieldName] || "";
// Skip the fields that already has text entered
if (fieldDetail.element.value) {
continue;
}
// TODO: Set highlight style and preview text.
}
},
clearPreviewedFormFields() {
log.debug("clear previewed fields in:", this.form);
for (let fieldDetail of this.fieldDetails) {
// TODO: Clear preview text
// We keep the highlight of all fields if this form has
// already been auto-filled with a profile.
if (this.filledProfileGUID == null) {
// TODO: Remove highlight style
}
}
},
};

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

@ -22,6 +22,8 @@ Cu.import("resource://formautofill/FormAutofillContent.jsm");
var FormAutofillFrameScript = {
init() {
addEventListener("DOMContentLoaded", this);
addMessageListener("FormAutofill:PreviewProfile", this);
addMessageListener("FormAutoComplete:PopupClosed", this);
},
handleEvent(evt) {
@ -44,6 +46,19 @@ var FormAutofillFrameScript = {
}
}
},
receiveMessage(aMessage) {
if (!Services.prefs.getBoolPref("browser.formautofill.enabled")) {
return;
}
switch (aMessage.name) {
case "FormAutofill:PreviewProfile":
case "FormAutoComplete:PopupClosed":
FormAutofillContent._previewProfile(content.document);
break;
}
},
};
FormAutofillFrameScript.init();

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

@ -43,6 +43,22 @@
]]>
</constructor>
<property name="selected" onget="return this.getAttribute('selected') == 'true';">
<setter><![CDATA[
if (val) {
this.setAttribute("selected", "true");
} else {
this.removeAttribute("selected");
}
let {AutoCompletePopup} = Cu.import("resource://gre/modules/AutoCompletePopup.jsm", {});
AutoCompletePopup.sendMessageToBrowser("FormAutofill:PreviewProfile");
return val;
]]></setter>
</property>
<method name="_cleanup">
<body>
<![CDATA[
@ -51,6 +67,10 @@
</body>
</method>
<method name="_onChanged">
<body></body>
</method>
<method name="_onOverflow">
<body></body>
</method>