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

  1. function PROT_Controller(win,
  2. tabWatcher,
  3. listManager,
  4. phishingWarden,
  5. contentAnalyzer) {
  6. this.debugZone = "controller";
  7. this.win_ = win;
  8. this.listManager_ = listManager;
  9. this.phishingWarden_ = phishingWarden;
  10. this.contentAnalyzer_ = contentAnalyzer;
  11. this.prefs_ = new G_Preferences();
  12. this.checkRemotePrefName_ = PROT_globalStore.getServerCheckEnabledPrefName();
  13. this.checkRemote_ = this.prefs_.getPref(this.checkRemotePrefName_, null);
  14. this.checkRemotePrefObserver = BindToObject(this.onCheckRemotePrefChanged,
  15. this);
  16. this.prefs_.addObserver(this.checkRemotePrefName_,
  17. this.checkRemotePrefObserver);
  18. this.phishWardenPrefName_ = PROT_globalStore.getPhishWardenEnabledPrefName();
  19. this.phishWardenEnabled_ = this.prefs_.getPref(this.phishWardenPrefName_,
  20. null);
  21. this.phishWardenPrefObserver =
  22. BindToObject(this.onPhishWardenEnabledPrefChanged, this);
  23. this.prefs_.addObserver(this.phishWardenPrefName_,
  24. this.phishWardenPrefObserver);
  25. this.tabWatcher_ = tabWatcher;
  26. this.onShutdown_ = BindToObject(this.shutdown, this);
  27. this.win_.addEventListener("unload", this.onShutdown_, false);
  28. this.onTabSwitchCallback_ = BindToObject(this.onTabSwitch, this);
  29. this.tabWatcher_.registerListener("tabswitch",
  30. this.onTabSwitchCallback_);
  31. this.onLoadCallback_ = BindToObject(this.onLoad, this);
  32. this.tabWatcher_.registerListener("domcontentloaded",
  33. this.onLoadCallback_);
  34. var commandHandlers = {
  35. "amulet-show-warning" :
  36. BindToObject(this.onUserShowWarning, this),
  37. "amulet-accept-warning" :
  38. BindToObject(this.onUserAcceptWarning, this),
  39. "amulet-decline-warning" :
  40. BindToObject(this.onUserDeclineWarning, this),
  41. "amulet-submit-blacklist-urlbar" :
  42. BindToObject(this.onUserSubmitToBlacklist, this),
  43. "amulet-preferences" :
  44. BindToObject(this.onUserPreferences, this),
  45. "amulet-test-link" :
  46. BindToObject(this.showURL_, this, PROT_globalStore.getLocalizedTestURL()),
  47. "amulet-preferences-home-link":
  48. BindToObject(this.showURL_, this, PROT_globalStore.getHomePageURL()),
  49. "amulet-preferences-policy-link":
  50. BindToObject(this.showURL_, this, PROT_globalStore.getPolicyURL()),
  51. "amulet-preferences-home-link-nochrome":
  52. BindToObject(this.showURL_, this, PROT_globalStore.getHomePageURL(),
  53. true /* chromeless */),
  54. "amulet-preferences-policy-link-nochrome":
  55. BindToObject(this.showURL_, this, PROT_globalStore.getPolicyURL(),
  56. true /* chromeless */),
  57. };
  58. this.commandController_ = new PROT_CommandController(commandHandlers);
  59. this.win_.controllers.appendController(this.commandController_);
  60. this.browserView_ = new PROT_BrowserView(this.tabWatcher_,
  61. this.win_.document);
  62. this.phishingWarden_.addBrowserView(this.browserView_);
  63. this.windowWatcher_ =
  64. Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  65. .getService(Components.interfaces.nsIWindowWatcher);
  66. G_Debug(this, "Controller initialized.");
  67. }
  68. PROT_Controller.prototype.shutdown = function(e) {
  69. G_Debug(this, "Browser window closing. Shutting controller down.");
  70. this.phishingWarden_.removeBrowserView(this.browserView_);
  71. this.win_.controllers.removeController(this.commandController_);
  72. this.commandController_ = null;
  73. this.browserView_ = null;
  74. this.prefs_.removeObserver(this.checkRemotePrefName_,
  75. this.checkRemotePrefObserver);
  76. this.prefs_.removeObserver(this.phishWardenPrefName_,
  77. this.phishWardenPrefObserver);
  78. this.tabWatcher_.removeListener("load",
  79. this.onLoadCallback_);
  80. this.tabWatcher_.removeListener("tabswitch",
  81. this.onTabSwitchCallback_);
  82. this.win_.removeEventListener("unload", this.onShutdown_, false);
  83. this.prefs_ = null;
  84. this.windowWatcher_ = null;
  85. G_Debug(this, "Controller shut down.");
  86. }
  87. PROT_Controller.prototype.onCheckRemotePrefChanged = function(prefName) {
  88. this.checkRemote_ = this.prefs_.getBoolPrefOrDefault(prefName,
  89. this.checkRemote_);
  90. }
  91. PROT_Controller.prototype.onPhishWardenEnabledPrefChanged = function(
  92. prefName) {
  93. this.phishWardenEnabled_ =
  94. this.prefs_.getBoolPrefOrDefault(prefName, this.phishWardenEnabled_);
  95. }
  96. PROT_Controller.prototype.onUserShowWarning = function() {
  97. var browser = this.tabWatcher_.getCurrentBrowser();
  98. this.browserView_.explicitShow(browser);
  99. }
  100. PROT_Controller.prototype.onUserPreferences = function() {
  101. G_Debug(this, "User wants preferences.");
  102. var features = "chrome,titlebar,toolbar,centerscreen,modal";
  103. this.windowWatcher_.openWindow(
  104. this.win_,
  105. "chrome://amulet-of-protection/content/protection-preferences.xul",
  106. "amulet-prefs-dialog",
  107. features,
  108. null /* args */);
  109. return true;
  110. }
  111. PROT_Controller.prototype.showURL_ = function(url, opt_chromeless) {
  112. var features = opt_chromeless ? "status,scrollbars=yes,resizable=yes" : null;
  113. this.windowWatcher_.openWindow(this.win_,
  114. url,
  115. "_blank",
  116. features,
  117. null);
  118. }
  119. PROT_Controller.prototype.onUserSubmitToBlacklist = function() {
  120. var current_window = this.tabWatcher_.getCurrentWindow();
  121. var badUrl = current_window.location.href;
  122. G_Debug(this, "User wants to submit to blacklist: " + badUrl);
  123. var url = PROT_globalStore.getSubmitUrl();
  124. url.query += "&url=" + encodeURIComponent(badUrl);
  125. this.windowWatcher_.openWindow(
  126. this.windowWatcher_.activeWindow /* parent */,
  127. url.asciiSpec,
  128. "_blank",
  129. "height=400em,width=800,scrollbars=yes,resizable=yes," +
  130. "menubar,toolbar,location,directories,personalbar,status",
  131. null /* args */);
  132. return true;
  133. }
  134. PROT_Controller.prototype.onUserAcceptWarning = function() {
  135. G_Debug(this, "User accepted warning.");
  136. var browser = this.tabWatcher_.getCurrentBrowser();
  137. G_Assert(this, !!browser, "Couldn't get current browser?!?");
  138. G_Assert(this, this.browserView_.hasProblem(browser),
  139. "User accept fired, but browser doesn't have warning showing?!?");
  140. this.browserView_.acceptAction(browser);
  141. this.browserView_.problemResolved(browser);
  142. }
  143. PROT_Controller.prototype.onUserDeclineWarning = function() {
  144. G_Debug(this, "User declined warning.");
  145. var browser = this.tabWatcher_.getCurrentBrowser();
  146. G_Assert(this, this.browserView_.hasProblem(browser),
  147. "User decline fired, but browser doesn't have warning showing?!?");
  148. this.browserView_.declineAction(browser);
  149. }
  150. PROT_Controller.prototype.onTabSwitch = function(e) {
  151. if (this.browserView_.hasProblem(e.fromBrowser))
  152. this.browserView_.problemBrowserUnselected(e.fromBrowser);
  153. if (this.browserView_.hasProblem(e.toBrowser))
  154. this.browserView_.problemBrowserSelected(e.toBrowser);
  155. }
  156. PROT_Controller.prototype.onLoad = function(e) {
  157. if (e.doc)
  158. this.contentAnalyzer_.maybeAnalyzeDoc(e.doc);
  159. }
  160. PROT_Controller.prototype.maybeShowOptDialog = function() {
  161. if (this.checkRemote_ === null && !PROT_application.opting) {
  162. PROT_application.opting = true;   // Reset in protection-opt.xul
  163. this.showOptDialog();
  164. }
  165. }
  166. PROT_Controller.prototype.loadURI = function(browser, url) {
  167. browser.loadURI(url, null, null);
  168. }
  169. PROT_Controller.prototype.reloadPage = function(browser) {
  170. var normalReload = browser.webNavigation.LOAD_FLAGS_NORMAL;
  171. browser.reload(normalReload);
  172. }
  173. PROT_Controller.prototype.showOptDialog = function() {
  174. while (this.checkRemote_ === null)
  175. this.win_.openDialog(
  176. "chrome://amulet-of-protection/content/protection-opt.xul",
  177. "amulet-opt-dialog",
  178. "chrome,modal=yes,centerscreen,resizable=no",
  179. this.win_);
  180. if (this.optChecker_) {
  181. this.tabWatcher_.removeListener("load", this.optChecker_);
  182. this.optChecker_ = null;
  183. }
  184. }
  185.