home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / phishing-afterload-displayer.js < prev    next >
Text File  |  2006-08-07  |  13KB  |  339 lines

  1. function PROT_PhishMsgDisplayer(msgDesc, browser, doc, url) {
  2. return new PROT_PhishMsgDisplayerCanvas(msgDesc, browser, doc, url);
  3. }
  4. function PROT_PhishMsgDisplayerBase(msgDesc, browser, doc, url) {
  5. this.debugZone = "phisdisplayer";
  6. this.msgDesc_ = msgDesc;                                // currently unused
  7. this.browser_ = browser;
  8. this.doc_ = doc;
  9. this.url_ = url;
  10. this.messageId_ = "amulet-palm-message";
  11. this.messageTailId_ = "amulet-palm-message-tail-container";
  12. this.messageContentId_ = "amulet-palm-message-content";
  13. this.extendedMessageId_ = "amulet-palm-extended-message";
  14. this.showmoreLinkId_ = "amulet-palm-showmore-link";
  15. this.urlbarIconId_ = "amulet-urlbar-icon";
  16. this.refElementId_ = this.urlbarIconId_;
  17. this.reporter_ = new PROT_Reporter();
  18. this.commandHandlers_ = {
  19. "amulet-palm-showmore":
  20. BindToObject(this.showMore_, this),
  21. "amulet-palm-phishingorg":
  22. BindToObject(this.showURL_, this, PROT_globalStore.getAntiPhishingURL()),
  23. "amulet-palm-phishingfaq":
  24. BindToObject(this.showURL_, this, PROT_globalStore.getPhishingFaqURL()),
  25. "amulet-palm-fraudpage" :
  26. BindToObject(this.showURL_, this, PROT_globalStore.getHomePageURL()),
  27. "amulet-palm-falsepositive":
  28. BindToObject(this.onUserSubmitFalsePositive, this),
  29. "amulet-submit-blacklist" :
  30. BindToObject(this.onUserSubmitToBlacklist, this),
  31. "amulet-submit-generic-phishing" :
  32. BindToObject(this.onUserSubmitToGenericPhish, this),
  33. };
  34. this.windowWatcher_ =
  35. Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  36. .getService(Components.interfaces.nsIWindowWatcher);
  37. }
  38. PROT_PhishMsgDisplayerBase.prototype.getBackgroundColor_ = function() {
  39. var pref = Components.classes["@mozilla.org/preferences-service;1"].
  40. getService(Components.interfaces.nsIPrefBranch);
  41. return pref.getCharPref("browser.display.background_color");
  42. }
  43. PROT_PhishMsgDisplayerBase.prototype.declineAction = function() {
  44. G_Debug(this, "User declined warning.");
  45. G_Assert(this, this.started_, "Decline on a non-active displayer?");
  46. this.reporter_.report("phishdecline", this.url_);
  47. this.messageShouldShow_ = false;
  48. if (this.messageShowing_)
  49. this.hideMessage_();
  50. }
  51. PROT_PhishMsgDisplayerBase.prototype.acceptAction = function() {
  52. G_Assert(this, this.started_, "Accept on an unstarted displayer?");
  53. G_Assert(this, this.done_, "Accept on a finished displayer?");
  54. G_Debug(this, "User accepted warning.");
  55. this.reporter_.report("phishaccept", this.url_);
  56. var url = PROT_globalStore.getGetMeOutOfHereURL();
  57. this.browser_.loadURI(url);
  58. }
  59. PROT_PhishMsgDisplayerBase.prototype.onBrowserResized_ = function(event) {
  60. G_Debug(this, "Got resize for " + event.target.nodeName);
  61. if (event.target == this.doc_) {
  62. G_Debug(this, "User resized browser.");
  63. if (this.messageShowing_) {
  64. this.hideMessage_();
  65. this.showMessage_();
  66. }
  67. }
  68. }
  69. PROT_PhishMsgDisplayerBase.prototype.browserSelected = function() {
  70. G_Assert(this, this.started_, "Displayer selected before being started???");
  71. if (this.messageShowing_ === undefined) {
  72. this.messageShouldShow_ = true;
  73. this.ensureNavBarShowing_();
  74. }
  75. this.hideLockIcon_();        // Comes back when we are unselected or unloaded
  76. this.addWarningInUrlbar_();  // Goes away when we are unselected or unloaded
  77. if (this.messageShouldShow_)
  78. this.showMessage_();
  79. }
  80. PROT_PhishMsgDisplayerBase.prototype.explicitShow = function() {
  81. this.messageShouldShow_ = true;
  82. if (!this.messageShowing_)
  83. this.showMessage_();
  84. }
  85. PROT_PhishMsgDisplayerBase.prototype.browserUnselected = function() {
  86. this.removeWarningInUrlbar_();
  87. this.unhideLockIcon_();
  88. if (this.messageShowing_)
  89. this.hideMessage_();
  90. }
  91. PROT_PhishMsgDisplayerBase.prototype.start = function() {
  92. G_Assert(this, this.started_ == undefined, "Displayer started twice?");
  93. this.started_ = true;
  94. this.commandController_ = new PROT_CommandController(this.commandHandlers_);
  95. this.doc_.defaultView.controllers.appendController(this.commandController_);
  96. this.resizeHandler_ = BindToObject(this.onBrowserResized_, this);
  97. this.doc_.defaultView.addEventListener("resize",
  98. this.resizeHandler_,
  99. true);
  100. }
  101. PROT_PhishMsgDisplayerBase.prototype.isActive = function() {
  102. return !!this.started_;
  103. }
  104. PROT_PhishMsgDisplayerBase.prototype.done = function() {
  105. G_Assert(this, !this.done_, "Called done more than once?");
  106. this.done_ = true;
  107. if (this.started_) {
  108. this.removeWarningInUrlbar_();
  109. this.unhideLockIcon_();
  110. if (this.messageShowing_)
  111. this.hideMessage_();
  112. if (this.resizeHandler_) {
  113. this.doc_.defaultView.removeEventListener("resize",
  114. this.resizeHandler_,
  115. true);
  116. this.resizeHandler_ = null;
  117. }
  118. var win = this.doc_.defaultView;
  119. win.controllers.removeController(this.commandController_);
  120. this.commandController_ = null;
  121. }
  122. }
  123. PROT_PhishMsgDisplayerBase.prototype.removeIfExists_ = function(orig,
  124. toRemove) {
  125. var pos = orig.indexOf(toRemove);
  126. if (pos != -1)
  127. orig = orig.substring(0, pos) + orig.substring(pos + toRemove.length);
  128. return orig;
  129. }
  130. PROT_PhishMsgDisplayerBase.prototype.ensureNavBarShowing_ = function() {
  131. var navbar = this.doc_.getElementById("nav-bar");
  132. navbar.setAttribute("collapsed", false);
  133. var win = this.doc_.getElementById("main-window");
  134. var hidden = win.getAttribute("chromehidden");
  135. G_Debug(this, "Old chromehidden string: " + hidden);
  136. var newHidden = this.removeIfExists_(hidden, "toolbar");
  137. newHidden = this.removeIfExists_(newHidden, "location");
  138. if (newHidden != hidden) {
  139. G_Debug(this, "New chromehidden string: " + newHidden);
  140. win.setAttribute("chromehidden", newHidden);
  141. var urlbar = this.doc_.getElementById("urlbar");
  142. urlbar.value = this.doc_.getElementById("content")
  143. .contentDocument.location.href;
  144. }
  145. }
  146. PROT_PhishMsgDisplayerBase.prototype.hideLockIcon_ = function() {
  147. var lockIcon = this.doc_.getElementById("lock-icon");
  148. lockIcon.hidden = true;
  149. }
  150. PROT_PhishMsgDisplayerBase.prototype.unhideLockIcon_ = function() {
  151. var lockIcon = this.doc_.getElementById("lock-icon");
  152. lockIcon.hidden = false;
  153. }
  154. PROT_PhishMsgDisplayerBase.prototype.addWarningInUrlbar_ = function() {
  155. var urlbarIcon = this.doc_.getElementById(this.urlbarIconId_);
  156. urlbarIcon.style.display = "";
  157. }
  158. PROT_PhishMsgDisplayerBase.prototype.removeWarningInUrlbar_ = function() {
  159. var urlbarIcon = this.doc_.getElementById(this.urlbarIconId_);
  160. urlbarIcon.style.display = "none";
  161. }
  162. PROT_PhishMsgDisplayerBase.prototype.showMessage_ = function() { };
  163. PROT_PhishMsgDisplayerBase.prototype.hideMessage_ = function() { };
  164. PROT_PhishMsgDisplayerBase.prototype.adjustLocation_ = function(message,
  165. tail,
  166. refElement) {
  167. var refX = refElement.boxObject.x;
  168. var refY = refElement.boxObject.y;
  169. var refHeight = refElement.boxObject.height;
  170. var refWidth = refElement.boxObject.width;
  171. G_Debug(this, "Ref element is at [window-relative] (" + refX + ", " +
  172. refY + ")");
  173. var pixelsIntoRefY = -2;
  174. var tailY = refY + refHeight - pixelsIntoRefY;
  175. var tailPixelsLeftOfRefX = tail.boxObject.width;
  176. var tailPixelsIntoRefX = Math.round(refWidth / 2);
  177. var tailX = refX - tailPixelsLeftOfRefX + tailPixelsIntoRefX;
  178. var messageY = tailY + tail.boxObject.height + 3;
  179. var messagePixelsLeftOfRefX = 375;
  180. var messageX = refX - messagePixelsLeftOfRefX;
  181. G_Debug(this, "Message is at [window-relative] (" + messageX + ", " +
  182. messageY + ")");
  183. G_Debug(this, "Tail is at [window-relative] (" + tailX + ", " +
  184. tailY + ")");
  185. tail.style.top = tailY + "px";
  186. tail.style.left = tailX + "px";
  187. message.style.top = messageY + "px";
  188. message.style.left = messageX + "px";
  189. }
  190. PROT_PhishMsgDisplayerBase.prototype.showMore_ = function() {
  191. this.doc_.getElementById(this.extendedMessageId_).hidden = false;
  192. this.doc_.getElementById(this.showmoreLinkId_).style.display = "none";
  193. }
  194. PROT_PhishMsgDisplayerBase.prototype.showURL_ = function(url) {
  195. this.windowWatcher_.openWindow(this.windowWatcher_.activeWindow,
  196. url,
  197. "_blank",
  198. null,
  199. null);
  200. }
  201. PROT_PhishMsgDisplayerBase.prototype.onUserSubmitFalsePositive = function() {
  202. var badUrl = this.url_;
  203. G_Debug(this, "User wants to submit as false positive: " + badUrl);
  204. var url = PROT_globalStore.getFalsePositiveURL();
  205. url.query += "&url=" + encodeURIComponent(badUrl);
  206. this.windowWatcher_.openWindow(
  207. this.windowWatcher_.activeWindow /* parent */,
  208. url.asciiSpec,
  209. "_blank",
  210. "height=400em,width=800,scrollbars=yes,resizable=yes," +
  211. "menubar,toolbar,location,directories,personalbar,status",
  212. null /* args */);
  213. }
  214. PROT_PhishMsgDisplayerBase.prototype.onUserSubmitToBlacklist = function() {
  215. var badUrl = this.url_;
  216. G_Debug(this, "User wants to submit to blacklist: " + badUrl);
  217. var url = PROT_globalStore.getSubmitUrl();
  218. url.query += "&url=" + encodeURIComponent(badUrl);
  219. this.windowWatcher_.openWindow(
  220. this.windowWatcher_.activeWindow /* parent */,
  221. url.asciiSpec,
  222. "_blank",
  223. "height=400em,width=800,scrollbars=yes,resizable=yes," +
  224. "menubar,toolbar,location,directories,personalbar,status",
  225. null /* args */);
  226. return true;
  227. }
  228. PROT_PhishMsgDisplayerBase.prototype.onUserSubmitToGenericPhish = function() {
  229. var badUrl = this.url_;
  230. G_Debug(this, "User wants to submit something about: " + badUrl);
  231. var url = PROT_globalStore.getGenericPhishSubmitURL();
  232. url.query += "&url=" + encodeURIComponent(badUrl);
  233. this.windowWatcher_.openWindow(
  234. this.windowWatcher_.activeWindow /* parent */,
  235. url.asciiSpec,
  236. "_blank",
  237. "height=400em,width=800,scrollbars=yes,resizable=yes," +
  238. "menubar,toolbar,location,directories,personalbar,status",
  239. null /* args */);
  240. return true;
  241. }
  242. function PROT_PhishMsgDisplayerCanvas(msgDesc, browser, doc, url) {
  243. PROT_PhishMsgDisplayerBase.call(this, msgDesc, browser, doc, url);
  244. this.dimAreaId_ = "amulet-dim-area-canvas";
  245. this.contentStackId_ = "amulet-content-stack";
  246. this.pageCanvasId_ = "amulet-page-canvas";
  247. this.xhtmlNS_ = "http://www.w3.org/1999/xhtml";     // we create html:canvas
  248. }
  249. PROT_PhishMsgDisplayerCanvas.inherits(PROT_PhishMsgDisplayerBase);
  250. PROT_PhishMsgDisplayerCanvas.prototype.showMessage_ = function() {
  251. G_Debug(this, "Showing message.");
  252. this.messageShowing_ = true;
  253. var pageCanvas = this.doc_.createElementNS(this.xhtmlNS_, "html:canvas");
  254. pageCanvas.id = this.pageCanvasId_;
  255. pageCanvas.hidden = "true";
  256. var contentStack = this.doc_.getElementById(this.contentStackId_);
  257. contentStack.insertBefore(pageCanvas, contentStack.firstChild);
  258. contentStack.hidden = false;
  259. var w = this.browser_.boxObject.width;
  260. G_Debug(this, "browser w=" + w);
  261. var h = this.browser_.boxObject.height;
  262. G_Debug(this, "browser h=" + h);
  263. var win = this.browser_.contentWindow;
  264. var scrollX = win.scrollX;
  265. G_Debug(this, "win scrollx=" + scrollX);
  266. var scrollY = win.scrollY;
  267. G_Debug(this, "win scrolly=" + scrollY);
  268. var dimarea = this.doc_.getElementById(this.dimAreaId_);
  269. dimarea.hidden = false;
  270. this.browser_.parentNode.collapsed = true;   // And now hide the browser
  271. pageCanvas.setAttribute("width", w);
  272. pageCanvas.setAttribute("height", h);
  273. var bgcolor = this.getBackgroundColor_();
  274. var cx = pageCanvas.getContext("2d");
  275. cx.drawWindow(win, scrollX, scrollY, w, h, bgcolor);
  276. var debZone = this.debugZone;
  277. function repaint() {
  278. G_Debug(debZone, "Repainting canvas...");
  279. cx.drawWindow(win, scrollX, scrollY, w, h, bgcolor);
  280. };
  281. this.repainter_ = new PROT_PhishMsgCanvasRepainter(repaint);
  282. var refElement = this.doc_.getElementById(this.refElementId_);
  283. var message = this.doc_.getElementById(this.messageId_);
  284. var tail = this.doc_.getElementById(this.messageTailId_);
  285. message.hidden = false;
  286. message.style.display = "block";
  287. tail.hidden = false;
  288. tail.style.display = "block";
  289. this.adjustLocation_(message, tail, refElement);
  290. }
  291. PROT_PhishMsgDisplayerCanvas.prototype.hideMessage_ = function() {
  292. G_Debug(this, "Hiding phishing warning.");
  293. G_Assert(this, this.messageShowing_, "Hide message called but not showing?");
  294. this.messageShowing_ = false;
  295. this.repainter_.cancel();
  296. this.repainter_ = null;
  297. var message = this.doc_.getElementById(this.messageId_);
  298. message.hidden = true;
  299. message.style.display = "none";
  300. var tail = this.doc_.getElementById(this.messageTailId_);
  301. tail.hidden = true;
  302. tail.style.display = "none";
  303. this.browser_.parentNode.collapsed = false;
  304. var contentStack = this.doc_.getElementById(this.contentStackId_);
  305. contentStack.hidden = true;
  306. var dimarea = this.doc_.getElementById(this.dimAreaId_);
  307. dimarea.hidden = true;
  308. var pageCanvas = this.doc_.getElementById(this.pageCanvasId_);
  309. contentStack.removeChild(pageCanvas);
  310. }
  311. function PROT_PhishMsgCanvasRepainter(repaintFunc) {
  312. this.count_ = 0;
  313. this.repaintFunc_ = repaintFunc;
  314. this.initPeriodMS_ = 500;             // Initially repaint every 500ms
  315. this.steadyStateAtMS_ = 10 * 1000;    // Go slowly after 10 seconds,
  316. this.steadyStatePeriodMS_ = 3 * 1000; // repainting every 3 seconds, and
  317. this.quitAtMS_ = 20 * 1000;           // stop after 20 seconds
  318. this.startMS_ = (new Date).getTime();
  319. this.alarm_ = new G_Alarm(BindToObject(this.repaint, this),
  320. this.initPeriodMS_);
  321. }
  322. PROT_PhishMsgCanvasRepainter.prototype.repaint = function() {
  323. this.repaintFunc_();
  324. var nextRepaint;
  325. if ((new Date).getTime() - this.startMS_ > this.steadyStateAtMS_)
  326. nextRepaint = this.steadyStatePeriodMS_;
  327. else
  328. nextRepaint = this.initPeriodMS_;
  329. if (!((new Date).getTime() - this.startMS_ > this.quitAtMS_))
  330. this.alarm_ = new G_Alarm(BindToObject(this.repaint, this), nextRepaint);
  331. }
  332. PROT_PhishMsgCanvasRepainter.prototype.cancel = function() {
  333. if (this.alarm_) {
  334. this.alarm_.cancel();
  335. this.alarm_ = null;
  336. }
  337. this.repaintFunc_ = null;
  338. }
  339.