2014-06-25 09:12:07 +04:00
|
|
|
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
|
2015-01-16 17:11:00 +03:00
|
|
|
// vim: set ts=2 sw=2 sts=2 et tw=80: */
|
2013-09-14 03:27:19 +04:00
|
|
|
// 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/.
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["RemoteFinder", "RemoteFinderListener"];
|
|
|
|
|
2016-03-23 04:18:30 +03:00
|
|
|
const { interfaces: Ci, classes: Cc, utils: Cu } = Components;
|
2013-09-14 03:27:19 +04:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2016-03-23 04:18:30 +03:00
|
|
|
Cu.import("resource://gre/modules/Geometry.jsm");
|
2013-09-14 03:27:19 +04:00
|
|
|
|
2015-01-16 17:11:00 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "GetClipboardSearchString",
|
|
|
|
() => Cu.import("resource://gre/modules/Finder.jsm", {}).GetClipboardSearchString
|
|
|
|
);
|
2016-03-23 04:18:30 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "Rect",
|
|
|
|
() => Cu.import("resource://gre/modules/Geometry.jsm", {}).Rect
|
|
|
|
);
|
2015-01-16 17:11:00 +03:00
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
function RemoteFinder(browser) {
|
2015-01-16 17:11:00 +03:00
|
|
|
this._listeners = new Set();
|
2013-09-14 03:27:19 +04:00
|
|
|
this._searchString = null;
|
|
|
|
|
2015-02-19 03:47:43 +03:00
|
|
|
this.swapBrowser(browser);
|
2013-09-14 03:27:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteFinder.prototype = {
|
2016-06-09 14:30:49 +03:00
|
|
|
destroy() {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:Destroy");
|
|
|
|
if (this._messageManager) {
|
|
|
|
this._messageManager.removeMessageListener("Finder:Result", this);
|
|
|
|
this._messageManager.removeMessageListener("Finder:MatchesResult", this);
|
2016-08-16 22:44:15 +03:00
|
|
|
this._messageManager.removeMessageListener("Finder:CurrentSelectionResult", this);
|
|
|
|
this._messageManager.removeMessageListener("Finder:HighlightFinished", this);
|
2016-06-09 14:30:49 +03:00
|
|
|
}
|
|
|
|
this._listeners.clear();
|
|
|
|
this._browser = this._messageManager = null;
|
|
|
|
},
|
|
|
|
|
2015-02-19 03:47:43 +03:00
|
|
|
swapBrowser: function(aBrowser) {
|
|
|
|
if (this._messageManager) {
|
|
|
|
this._messageManager.removeMessageListener("Finder:Result", this);
|
|
|
|
this._messageManager.removeMessageListener("Finder:MatchesResult", this);
|
2016-08-16 22:44:15 +03:00
|
|
|
this._messageManager.removeMessageListener("Finder:CurrentSelectionResult", this);
|
|
|
|
this._messageManager.removeMessageListener("Finder:HighlightFinished", this);
|
2015-02-19 03:47:43 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
aBrowser.messageManager.sendAsyncMessage("Finder:Initialize");
|
|
|
|
}
|
|
|
|
|
|
|
|
this._browser = aBrowser;
|
|
|
|
this._messageManager = this._browser.messageManager;
|
|
|
|
this._messageManager.addMessageListener("Finder:Result", this);
|
|
|
|
this._messageManager.addMessageListener("Finder:MatchesResult", this);
|
|
|
|
this._messageManager.addMessageListener("Finder:CurrentSelectionResult", this);
|
2016-03-25 17:26:14 +03:00
|
|
|
this._messageManager.addMessageListener("Finder:HighlightFinished", this);
|
2015-02-19 03:47:43 +03:00
|
|
|
|
|
|
|
// Ideally listeners would have removed themselves but that doesn't happen
|
|
|
|
// right now
|
|
|
|
this._listeners.clear();
|
|
|
|
},
|
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
addResultListener: function (aListener) {
|
2015-01-16 17:11:00 +03:00
|
|
|
this._listeners.add(aListener);
|
2013-09-14 03:27:19 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
removeResultListener: function (aListener) {
|
2015-01-16 17:11:00 +03:00
|
|
|
this._listeners.delete(aListener);
|
2013-09-14 03:27:19 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function (aMessage) {
|
2014-07-10 03:27:37 +04:00
|
|
|
// Only Finder:Result messages have the searchString field.
|
2015-01-16 17:11:00 +03:00
|
|
|
let callback;
|
|
|
|
let params;
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Finder:Result":
|
|
|
|
this._searchString = aMessage.data.searchString;
|
2016-03-23 04:18:30 +03:00
|
|
|
// The rect stops being a Geometry.jsm:Rect over IPC.
|
|
|
|
if (aMessage.data.rect) {
|
|
|
|
aMessage.data.rect = Rect.fromRect(aMessage.data.rect);
|
|
|
|
}
|
2015-01-16 17:11:00 +03:00
|
|
|
callback = "onFindResult";
|
|
|
|
params = [ aMessage.data ];
|
|
|
|
break;
|
|
|
|
case "Finder:MatchesResult":
|
|
|
|
callback = "onMatchesCountResult";
|
|
|
|
params = [ aMessage.data ];
|
|
|
|
break;
|
|
|
|
case "Finder:CurrentSelectionResult":
|
|
|
|
callback = "onCurrentSelection";
|
|
|
|
params = [ aMessage.data.selection, aMessage.data.initial ];
|
|
|
|
break;
|
2016-03-25 17:26:14 +03:00
|
|
|
case "Finder:HighlightFinished":
|
|
|
|
callback = "onHighlightFinished";
|
|
|
|
params = [ aMessage.data ];
|
|
|
|
break;
|
2014-07-10 03:27:37 +04:00
|
|
|
}
|
2013-09-14 03:27:19 +04:00
|
|
|
|
|
|
|
for (let l of this._listeners) {
|
2015-02-19 03:47:43 +03:00
|
|
|
// Don't let one callback throwing stop us calling the rest
|
|
|
|
try {
|
|
|
|
l[callback].apply(l, params);
|
2016-04-01 03:41:00 +03:00
|
|
|
} catch (e) {
|
|
|
|
if (!l[callback]) {
|
|
|
|
Cu.reportError(`Missing ${callback} callback on RemoteFinderListener`);
|
|
|
|
}
|
2015-02-19 03:47:43 +03:00
|
|
|
Cu.reportError(e);
|
|
|
|
}
|
2013-09-14 03:27:19 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get searchString() {
|
|
|
|
return this._searchString;
|
|
|
|
},
|
|
|
|
|
2015-01-16 17:11:00 +03:00
|
|
|
get clipboardSearchString() {
|
|
|
|
return GetClipboardSearchString(this._browser.loadContext);
|
|
|
|
},
|
|
|
|
|
|
|
|
setSearchStringToSelection() {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:SetSearchStringToSelection", {});
|
|
|
|
},
|
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
set caseSensitive(aSensitive) {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:CaseSensitive",
|
|
|
|
{ caseSensitive: aSensitive });
|
|
|
|
},
|
|
|
|
|
2016-06-28 16:13:53 +03:00
|
|
|
set entireWord(aEntireWord) {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:EntireWord",
|
|
|
|
{ entireWord: aEntireWord });
|
|
|
|
},
|
|
|
|
|
2015-01-16 17:11:00 +03:00
|
|
|
getInitialSelection: function() {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:GetInitialSelection", {});
|
|
|
|
},
|
|
|
|
|
2015-03-25 00:58:32 +03:00
|
|
|
fastFind: function (aSearchString, aLinksOnly, aDrawOutline) {
|
2013-09-14 03:27:19 +04:00
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:FastFind",
|
|
|
|
{ searchString: aSearchString,
|
2015-03-25 00:58:32 +03:00
|
|
|
linksOnly: aLinksOnly,
|
|
|
|
drawOutline: aDrawOutline });
|
2013-09-14 03:27:19 +04:00
|
|
|
},
|
|
|
|
|
2015-03-25 00:58:32 +03:00
|
|
|
findAgain: function (aFindBackwards, aLinksOnly, aDrawOutline) {
|
2013-09-14 03:27:19 +04:00
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:FindAgain",
|
|
|
|
{ findBackwards: aFindBackwards,
|
2015-03-25 00:58:32 +03:00
|
|
|
linksOnly: aLinksOnly,
|
|
|
|
drawOutline: aDrawOutline });
|
2013-09-14 03:27:19 +04:00
|
|
|
},
|
|
|
|
|
2016-07-28 13:34:13 +03:00
|
|
|
highlight: function (aHighlight, aWord, aLinksOnly) {
|
2013-09-14 03:27:19 +04:00
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:Highlight",
|
|
|
|
{ highlight: aHighlight,
|
2016-07-28 13:34:13 +03:00
|
|
|
linksOnly: aLinksOnly,
|
2013-09-14 03:27:19 +04:00
|
|
|
word: aWord });
|
|
|
|
},
|
|
|
|
|
2013-10-16 04:02:23 +04:00
|
|
|
enableSelection: function () {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:EnableSelection");
|
|
|
|
},
|
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
removeSelection: function () {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:RemoveSelection");
|
|
|
|
},
|
|
|
|
|
|
|
|
focusContent: function () {
|
2014-09-04 23:17:47 +04:00
|
|
|
// Allow Finder listeners to cancel focusing the content.
|
|
|
|
for (let l of this._listeners) {
|
|
|
|
try {
|
|
|
|
if ("shouldFocusContent" in l &&
|
|
|
|
!l.shouldFocusContent())
|
|
|
|
return;
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:58:32 +03:00
|
|
|
this._browser.focus();
|
2013-09-14 03:27:19 +04:00
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:FocusContent");
|
|
|
|
},
|
|
|
|
|
2016-06-09 14:30:49 +03:00
|
|
|
onFindbarClose: function () {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:FindbarClose");
|
|
|
|
},
|
|
|
|
|
2016-08-10 12:23:47 +03:00
|
|
|
onFindbarOpen: function () {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:FindbarOpen");
|
|
|
|
},
|
|
|
|
|
2016-06-09 14:30:49 +03:00
|
|
|
onModalHighlightChange: function(aUseModalHighlight) {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:ModalHighlightChange", {
|
|
|
|
useModalHighlight: aUseModalHighlight
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-07-27 17:37:26 +03:00
|
|
|
onHighlightAllChange: function(aHighlightAll) {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:HighlightAllChange", {
|
|
|
|
highlightAll: aHighlightAll
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
keyPress: function (aEvent) {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:KeyPress",
|
|
|
|
{ keyCode: aEvent.keyCode,
|
2015-06-12 20:07:00 +03:00
|
|
|
ctrlKey: aEvent.ctrlKey,
|
|
|
|
metaKey: aEvent.metaKey,
|
|
|
|
altKey: aEvent.altKey,
|
2013-09-14 03:27:19 +04:00
|
|
|
shiftKey: aEvent.shiftKey });
|
2014-07-10 03:27:37 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
requestMatchesCount: function (aSearchString, aMatchLimit, aLinksOnly) {
|
|
|
|
this._browser.messageManager.sendAsyncMessage("Finder:MatchesCount",
|
|
|
|
{ searchString: aSearchString,
|
|
|
|
matchLimit: aMatchLimit,
|
|
|
|
linksOnly: aLinksOnly });
|
2013-09-14 03:27:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function RemoteFinderListener(global) {
|
|
|
|
let {Finder} = Cu.import("resource://gre/modules/Finder.jsm", {});
|
|
|
|
this._finder = new Finder(global.docShell);
|
|
|
|
this._finder.addResultListener(this);
|
|
|
|
this._global = global;
|
|
|
|
|
|
|
|
for (let msg of this.MESSAGES) {
|
|
|
|
global.addMessageListener(msg, this);
|
|
|
|
}
|
2016-08-19 18:18:41 +03:00
|
|
|
global.addEventListener("unload", this.destroy.bind(this));
|
2013-09-14 03:27:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteFinderListener.prototype = {
|
|
|
|
MESSAGES: [
|
|
|
|
"Finder:CaseSensitive",
|
2016-06-09 14:30:49 +03:00
|
|
|
"Finder:Destroy",
|
2016-06-28 16:13:53 +03:00
|
|
|
"Finder:EntireWord",
|
2013-09-14 03:27:19 +04:00
|
|
|
"Finder:FastFind",
|
|
|
|
"Finder:FindAgain",
|
2015-01-16 17:11:00 +03:00
|
|
|
"Finder:SetSearchStringToSelection",
|
|
|
|
"Finder:GetInitialSelection",
|
2013-09-14 03:27:19 +04:00
|
|
|
"Finder:Highlight",
|
2016-07-27 17:37:26 +03:00
|
|
|
"Finder:HighlightAllChange",
|
2013-10-16 04:02:23 +04:00
|
|
|
"Finder:EnableSelection",
|
2013-09-14 03:27:19 +04:00
|
|
|
"Finder:RemoveSelection",
|
|
|
|
"Finder:FocusContent",
|
2016-06-09 14:30:49 +03:00
|
|
|
"Finder:FindbarClose",
|
2016-08-10 12:23:47 +03:00
|
|
|
"Finder:FindbarOpen",
|
2014-07-10 03:27:37 +04:00
|
|
|
"Finder:KeyPress",
|
2016-06-09 14:30:49 +03:00
|
|
|
"Finder:MatchesCount",
|
|
|
|
"Finder:ModalHighlightChange"
|
2013-09-14 03:27:19 +04:00
|
|
|
],
|
|
|
|
|
2016-08-19 18:18:41 +03:00
|
|
|
destroy() {
|
|
|
|
this._finder.destroy();
|
|
|
|
for (let msg of this.MESSAGES)
|
|
|
|
this._global.removeMessageListener(msg, this);
|
|
|
|
this._finder = this._global = null;
|
|
|
|
},
|
|
|
|
|
2014-01-14 05:58:33 +04:00
|
|
|
onFindResult: function (aData) {
|
|
|
|
this._global.sendAsyncMessage("Finder:Result", aData);
|
2013-09-14 03:27:19 +04:00
|
|
|
},
|
|
|
|
|
2014-07-10 03:27:37 +04:00
|
|
|
// When the child receives messages with results of requestMatchesCount,
|
|
|
|
// it passes them forward to the parent.
|
|
|
|
onMatchesCountResult: function (aData) {
|
|
|
|
this._global.sendAsyncMessage("Finder:MatchesResult", aData);
|
|
|
|
},
|
|
|
|
|
2016-03-25 17:26:14 +03:00
|
|
|
onHighlightFinished: function(aData) {
|
|
|
|
this._global.sendAsyncMessage("Finder:HighlightFinished", aData);
|
|
|
|
},
|
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
receiveMessage: function (aMessage) {
|
|
|
|
let data = aMessage.data;
|
|
|
|
|
|
|
|
switch (aMessage.name) {
|
2016-06-09 14:30:49 +03:00
|
|
|
case "Finder:Destroy":
|
|
|
|
this._finder.destroy();
|
|
|
|
break;
|
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
case "Finder:CaseSensitive":
|
|
|
|
this._finder.caseSensitive = data.caseSensitive;
|
|
|
|
break;
|
|
|
|
|
2016-06-28 16:13:53 +03:00
|
|
|
case "Finder:EntireWord":
|
|
|
|
this._finder.entireWord = data.entireWord;
|
|
|
|
break;
|
|
|
|
|
2015-01-16 17:11:00 +03:00
|
|
|
case "Finder:SetSearchStringToSelection": {
|
|
|
|
let selection = this._finder.setSearchStringToSelection();
|
|
|
|
this._global.sendAsyncMessage("Finder:CurrentSelectionResult",
|
|
|
|
{ selection: selection,
|
|
|
|
initial: false });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "Finder:GetInitialSelection": {
|
|
|
|
let selection = this._finder.getActiveSelectionText();
|
|
|
|
this._global.sendAsyncMessage("Finder:CurrentSelectionResult",
|
|
|
|
{ selection: selection,
|
|
|
|
initial: true });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
case "Finder:FastFind":
|
2015-03-25 00:58:32 +03:00
|
|
|
this._finder.fastFind(data.searchString, data.linksOnly, data.drawOutline);
|
2013-09-14 03:27:19 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "Finder:FindAgain":
|
2015-03-25 00:58:32 +03:00
|
|
|
this._finder.findAgain(data.findBackwards, data.linksOnly, data.drawOutline);
|
2013-09-14 03:27:19 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "Finder:Highlight":
|
2016-07-28 13:34:13 +03:00
|
|
|
this._finder.highlight(data.highlight, data.word, data.linksOnly);
|
2013-09-14 03:27:19 +04:00
|
|
|
break;
|
|
|
|
|
2016-07-27 17:37:26 +03:00
|
|
|
case "Finder:HighlightAllChange":
|
|
|
|
this._finder.onHighlightAllChange(data.highlightAll);
|
|
|
|
break;
|
|
|
|
|
2015-06-12 20:09:00 +03:00
|
|
|
case "Finder:EnableSelection":
|
|
|
|
this._finder.enableSelection();
|
|
|
|
break;
|
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
case "Finder:RemoveSelection":
|
|
|
|
this._finder.removeSelection();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Finder:FocusContent":
|
|
|
|
this._finder.focusContent();
|
|
|
|
break;
|
|
|
|
|
2016-06-09 14:30:49 +03:00
|
|
|
case "Finder:FindbarClose":
|
|
|
|
this._finder.onFindbarClose();
|
|
|
|
break;
|
|
|
|
|
2016-08-10 12:23:47 +03:00
|
|
|
case "Finder:FindbarOpen":
|
|
|
|
this._finder.onFindbarOpen();
|
|
|
|
break;
|
|
|
|
|
2013-09-14 03:27:19 +04:00
|
|
|
case "Finder:KeyPress":
|
|
|
|
this._finder.keyPress(data);
|
|
|
|
break;
|
2014-07-10 03:27:37 +04:00
|
|
|
|
|
|
|
case "Finder:MatchesCount":
|
|
|
|
this._finder.requestMatchesCount(data.searchString, data.matchLimit, data.linksOnly);
|
|
|
|
break;
|
2016-06-09 14:30:49 +03:00
|
|
|
|
|
|
|
case "Finder:ModalHighlightChange":
|
2016-06-22 20:22:07 +03:00
|
|
|
this._finder.onModalHighlightChange(data.useModalHighlight);
|
2016-06-09 14:30:49 +03:00
|
|
|
break;
|
2013-09-14 03:27:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|