home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2010 April / PCA177.iso / ESSENTIALS / Firefox Setup.exe / nonlocalized / components / nsSidebar.js < prev    next >
Encoding:
Text File  |  2009-07-15  |  12.1 KB  |  334 lines

  1. //@line 43 "e:\builds\moz2_slave\win32_build\build\browser\components\sidebar\src\nsSidebar.js"
  2.  
  3. /*
  4.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  5.  * If you must perform some initialization, and it could possibly fail (even
  6.  * due to an out-of-memory condition), you should use an Init method, which
  7.  * can convey failure appropriately (thrown exception in JS,
  8.  * NS_FAILED(nsresult) return in C++).
  9.  *
  10.  * In JS, you can actually cheat, because a thrown exception will cause the
  11.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  12.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  13.  * for portability reasons -- and even when you're building completely
  14.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  15.  */
  16.  
  17. const DEBUG = false; /* set to false to suppress debug messages */
  18.  
  19. const SIDEBAR_CONTRACTID        = "@mozilla.org/sidebar;1";
  20. const SIDEBAR_CID               = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
  21. const nsISupports               = Components.interfaces.nsISupports;
  22. const nsIFactory                = Components.interfaces.nsIFactory;
  23. const nsISidebar                = Components.interfaces.nsISidebar;
  24. const nsISidebarExternal        = Components.interfaces.nsISidebarExternal;
  25. const nsIClassInfo              = Components.interfaces.nsIClassInfo;
  26.  
  27. // File extension for Sherlock search plugin description files
  28. const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i;
  29.  
  30. function nsSidebar()
  31. {
  32.     const PROMPTSERVICE_CONTRACTID = "@mozilla.org/embedcomp/prompt-service;1";
  33.     const nsIPromptService = Components.interfaces.nsIPromptService;
  34.     this.promptService =
  35.         Components.classes[PROMPTSERVICE_CONTRACTID].getService(nsIPromptService);
  36.  
  37.     const SEARCHSERVICE_CONTRACTID = "@mozilla.org/browser/search-service;1";
  38.     const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
  39.     this.searchService =
  40.       Components.classes[SEARCHSERVICE_CONTRACTID].getService(nsIBrowserSearchService);
  41. }
  42.  
  43. nsSidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
  44.  
  45. function sidebarURLSecurityCheck(url)
  46. {
  47.     if (!/^(https?:|ftp:)/i.test(url)) {
  48.         Components.utils.reportError("Invalid argument passed to window.sidebar.addPanel: Unsupported panel URL." );
  49.         return false;
  50.     }
  51.     return true;
  52. }
  53.  
  54. /* decorate prototype to provide ``class'' methods and property accessors */
  55. nsSidebar.prototype.addPanel =
  56. function (aTitle, aContentURL, aCustomizeURL)
  57. {
  58.     debug("addPanel(" + aTitle + ", " + aContentURL + ", " +
  59.           aCustomizeURL + ")");
  60.  
  61.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, false);
  62. }
  63.  
  64. nsSidebar.prototype.addPersistentPanel =
  65. function(aTitle, aContentURL, aCustomizeURL)
  66. {
  67.     debug("addPersistentPanel(" + aTitle + ", " + aContentURL + ", " +
  68.            aCustomizeURL + ")\n");
  69.  
  70.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, true);
  71. }
  72.  
  73. nsSidebar.prototype.addPanelInternal =
  74. function (aTitle, aContentURL, aCustomizeURL, aPersist)
  75. {
  76.     var WINMEDSVC = Components.classes['@mozilla.org/appshell/window-mediator;1']
  77.                               .getService(Components.interfaces.nsIWindowMediator);
  78.     var win = WINMEDSVC.getMostRecentWindow( "navigator:browser" );
  79.                                                                                 
  80.     if (!sidebarURLSecurityCheck(aContentURL))
  81.       return;
  82.  
  83.     var uri = null;
  84.     var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  85.                               .getService(Components.interfaces.nsIIOService);
  86.     try {
  87.       uri = ioService.newURI(aContentURL, null, null);
  88.     }
  89.     catch(ex) { return; }
  90.  
  91.     win.PlacesUIUtils.showMinimalAddBookmarkUI(uri, aTitle, null, null, true, true);
  92. }
  93.  
  94. nsSidebar.prototype.validateSearchEngine =
  95. function (engineURL, iconURL)
  96. {
  97.   try
  98.   {
  99.     // Make sure we're using HTTP, HTTPS, or FTP.
  100.     if (! /^(https?|ftp):\/\//i.test(engineURL))
  101.       throw "Unsupported search engine URL";
  102.   
  103.     // Make sure we're using HTTP, HTTPS, or FTP and refering to a
  104.     // .gif/.jpg/.jpeg/.png/.ico file for the icon.
  105.     if (iconURL &&
  106.         ! /^(https?|ftp):\/\/.+\.(gif|jpg|jpeg|png|ico)$/i.test(iconURL))
  107.       throw "Unsupported search icon URL.";
  108.   }
  109.   catch(ex)
  110.   {
  111.     debug(ex);
  112.     Components.utils.reportError("Invalid argument passed to window.sidebar.addSearchEngine: " + ex);
  113.     
  114.     var searchBundle = srGetStrBundle("chrome://global/locale/search/search.properties");
  115.     var brandBundle = srGetStrBundle("chrome://branding/locale/brand.properties");
  116.     var brandName = brandBundle.GetStringFromName("brandShortName");
  117.     var title = searchBundle.GetStringFromName("error_invalid_engine_title");
  118.     var msg = searchBundle.formatStringFromName("error_invalid_engine_msg",
  119.                                                 [brandName], 1);
  120.     var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].
  121.              getService(Components.interfaces.nsIWindowWatcher);
  122.     ww.getNewPrompter(null).alert(title, msg);
  123.     return false;
  124.   }
  125.   
  126.   return true;
  127. }
  128.  
  129. // The suggestedTitle and suggestedCategory parameters are ignored, but remain
  130. // for backward compatibility.
  131. nsSidebar.prototype.addSearchEngine =
  132. function (engineURL, iconURL, suggestedTitle, suggestedCategory)
  133. {
  134.   debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
  135.         suggestedCategory + ", " + suggestedTitle + ")");
  136.  
  137.   if (!this.validateSearchEngine(engineURL, iconURL))
  138.     return;
  139.  
  140.   // OpenSearch files will likely be far more common than Sherlock files, and
  141.   // have less consistent suffixes, so we assume that ".src" is a Sherlock
  142.   // (text) file, and anything else is OpenSearch (XML).
  143.   var dataType;
  144.   if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL))
  145.     dataType = Components.interfaces.nsISearchEngine.DATA_TEXT;
  146.   else
  147.     dataType = Components.interfaces.nsISearchEngine.DATA_XML;
  148.  
  149.   this.searchService.addEngine(engineURL, dataType, iconURL, true);
  150. }
  151.  
  152. // This function exists largely to implement window.external.AddSearchProvider(),
  153. // to match other browsers' APIs.  The capitalization, although nonstandard here,
  154. // is therefore important.
  155. nsSidebar.prototype.AddSearchProvider =
  156. function (aDescriptionURL)
  157. {
  158.   // Get the favicon URL for the current page, or our best guess at the current
  159.   // page since we don't have easy access to the active document.  Most search
  160.   // engines will override this with an icon specified in the OpenSearch
  161.   // description anyway.
  162.   var WINMEDSVC = Components.classes['@mozilla.org/appshell/window-mediator;1']
  163.                             .getService(Components.interfaces.nsIWindowMediator);
  164.   var win = WINMEDSVC.getMostRecentWindow("navigator:browser");
  165.   var browser = win.document.getElementById("content");
  166.   var iconURL = "";
  167.   // Use documentURIObject in the check for shouldLoadFavIcon so that we
  168.   // do the right thing with about:-style error pages.  Bug 453442
  169.   if (browser.shouldLoadFavIcon(browser.selectedBrowser
  170.                                        .contentDocument
  171.                                        .documentURIObject))
  172.     iconURL = win.gProxyFavIcon.getAttribute("src");
  173.   
  174.   if (!this.validateSearchEngine(aDescriptionURL, iconURL))
  175.     return;
  176.  
  177.   const typeXML = Components.interfaces.nsISearchEngine.DATA_XML;
  178.   this.searchService.addEngine(aDescriptionURL, typeXML, iconURL, true);
  179. }
  180.  
  181. // This function exists to implement window.external.IsSearchProviderInstalled(),
  182. // for compatibility with other browsers.  It will return an integer value
  183. // indicating whether the given engine is installed for the current user.
  184. // However, it is currently stubbed out due to security/privacy concerns
  185. // stemming from difficulties in determining what domain issued the request.
  186. // See bug 340604 and
  187. // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/issearchproviderinstalled.asp .
  188. // XXX Implement this!
  189. nsSidebar.prototype.IsSearchProviderInstalled =
  190. function (aSearchURL)
  191. {
  192.   return 0;
  193. }
  194.  
  195. nsSidebar.prototype.addMicrosummaryGenerator =
  196. function (generatorURL)
  197. {
  198.     debug("addMicrosummaryGenerator(" + generatorURL + ")");
  199.  
  200.     if (!/^https?:/i.test(generatorURL))
  201.       return;
  202.  
  203.     var stringBundle = srGetStrBundle("chrome://browser/locale/sidebar/sidebar.properties");
  204.     var titleMessage = stringBundle.GetStringFromName("addMicsumGenConfirmTitle");
  205.     var dialogMessage = stringBundle.formatStringFromName("addMicsumGenConfirmText", [generatorURL], 1);
  206.       
  207.     if (!this.promptService.confirm(null, titleMessage, dialogMessage))
  208.         return;
  209.  
  210.     var ioService = Components.classes["@mozilla.org/network/io-service;1"].
  211.                     getService(Components.interfaces.nsIIOService);
  212.     var generatorURI = ioService.newURI(generatorURL, null, null);
  213.  
  214.     var microsummaryService = Components.classes["@mozilla.org/microsummary/service;1"].
  215.                               getService(Components.interfaces.nsIMicrosummaryService);
  216.     if (microsummaryService)
  217.       microsummaryService.addGenerator(generatorURI);
  218. }
  219.  
  220. // property of nsIClassInfo
  221. nsSidebar.prototype.flags = nsIClassInfo.DOM_OBJECT;
  222.  
  223. // property of nsIClassInfo
  224. nsSidebar.prototype.classDescription = "Sidebar";
  225.  
  226. // method of nsIClassInfo
  227. nsSidebar.prototype.getInterfaces = function(count) {
  228.     var interfaceList = [nsISidebar, nsISidebarExternal, nsIClassInfo];
  229.     count.value = interfaceList.length;
  230.     return interfaceList;
  231. }
  232.  
  233. // method of nsIClassInfo
  234. nsSidebar.prototype.getHelperForLanguage = function(count) {return null;}
  235.  
  236. nsSidebar.prototype.QueryInterface =
  237. function (iid) {
  238.     if (iid.equals(nsISidebar) ||
  239.         iid.equals(nsISidebarExternal) ||
  240.         iid.equals(nsIClassInfo) ||
  241.         iid.equals(nsISupports))
  242.         return this;
  243.  
  244.     throw Components.results.NS_ERROR_NO_INTERFACE;
  245. }
  246.  
  247. var sidebarModule = new Object();
  248.  
  249. sidebarModule.registerSelf =
  250. function (compMgr, fileSpec, location, type)
  251. {
  252.     debug("registering (all right -- a JavaScript module!)");
  253.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  254.  
  255.     compMgr.registerFactoryLocation(SIDEBAR_CID,
  256.                                     "Sidebar JS Component",
  257.                                     SIDEBAR_CONTRACTID,
  258.                                     fileSpec,
  259.                                     location,
  260.                                     type);
  261.  
  262.     const CATMAN_CONTRACTID = "@mozilla.org/categorymanager;1";
  263.     const nsICategoryManager = Components.interfaces.nsICategoryManager;
  264.     var catman = Components.classes[CATMAN_CONTRACTID].
  265.                             getService(nsICategoryManager);
  266.  
  267.     const JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY = "JavaScript global property";
  268.     catman.addCategoryEntry(JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY,
  269.                             "sidebar",
  270.                             SIDEBAR_CONTRACTID,
  271.                             true,
  272.                             true);
  273.  
  274.     catman.addCategoryEntry(JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY,
  275.                             "external",
  276.                             SIDEBAR_CONTRACTID,
  277.                             true,
  278.                             true);
  279. }
  280.  
  281. sidebarModule.getClassObject =
  282. function (compMgr, cid, iid) {
  283.     if (!cid.equals(SIDEBAR_CID))
  284.         throw Components.results.NS_ERROR_NO_INTERFACE;
  285.  
  286.     if (!iid.equals(Components.interfaces.nsIFactory))
  287.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  288.  
  289.     return sidebarFactory;
  290. }
  291.  
  292. sidebarModule.canUnload =
  293. function(compMgr)
  294. {
  295.     debug("Unloading component.");
  296.     return true;
  297. }
  298.  
  299. /* factory object */
  300. var sidebarFactory = new Object();
  301.  
  302. sidebarFactory.createInstance =
  303. function (outer, iid) {
  304.     debug("CI: " + iid);
  305.     if (outer != null)
  306.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  307.  
  308.     return (new nsSidebar()).QueryInterface(iid);
  309. }
  310.  
  311. /* entrypoint */
  312. function NSGetModule(compMgr, fileSpec) {
  313.     return sidebarModule;
  314. }
  315.  
  316. /* static functions */
  317. if (DEBUG)
  318.     debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
  319. else
  320.     debug = function (s) {}
  321.  
  322. // String bundle service
  323. var gStrBundleService = null;
  324.  
  325. function srGetStrBundle(path)
  326. {
  327.   if (!gStrBundleService)
  328.     gStrBundleService =
  329.       Components.classes["@mozilla.org/intl/stringbundle;1"]
  330.                 .getService(Components.interfaces.nsIStringBundleService);
  331.  
  332.   return gStrBundleService.createBundle(path);
  333. }
  334.