home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / components / nsSidebar.js < prev    next >
Text File  |  2012-06-08  |  15KB  |  383 lines

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