|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
7 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
8 |
|
9 const DEBUG = false; /* set to false to suppress debug messages */ |
|
10 |
|
11 const SIDEBAR_CONTRACTID = "@mozilla.org/sidebar;1"; |
|
12 const SIDEBAR_CID = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}"); |
|
13 |
|
14 // File extension for Sherlock search plugin description files |
|
15 const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i; |
|
16 |
|
17 function nsSidebar() |
|
18 { |
|
19 } |
|
20 |
|
21 nsSidebar.prototype.classID = SIDEBAR_CID; |
|
22 |
|
23 nsSidebar.prototype.validateSearchEngine = |
|
24 function (engineURL, iconURL) |
|
25 { |
|
26 try |
|
27 { |
|
28 // Make sure the URLs are HTTP, HTTPS, or FTP. |
|
29 var isWeb = /^(https?|ftp):\/\//i; |
|
30 |
|
31 if (!isWeb.test(engineURL)) |
|
32 throw "Unsupported search engine URL"; |
|
33 |
|
34 if (iconURL && !isWeb.test(iconURL)) |
|
35 throw "Unsupported search icon URL."; |
|
36 } |
|
37 catch(ex) |
|
38 { |
|
39 debug(ex); |
|
40 Components.utils.reportError("Invalid argument passed to window.sidebar.addSearchEngine: " + ex); |
|
41 |
|
42 var searchBundle = Services.strings.createBundle("chrome://global/locale/search/search.properties"); |
|
43 var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties"); |
|
44 var brandName = brandBundle.GetStringFromName("brandShortName"); |
|
45 var title = searchBundle.GetStringFromName("error_invalid_engine_title"); |
|
46 var msg = searchBundle.formatStringFromName("error_invalid_engine_msg", |
|
47 [brandName], 1); |
|
48 Services.ww.getNewPrompter(null).alert(title, msg); |
|
49 return false; |
|
50 } |
|
51 |
|
52 return true; |
|
53 } |
|
54 |
|
55 // The suggestedTitle and suggestedCategory parameters are ignored, but remain |
|
56 // for backward compatibility. |
|
57 nsSidebar.prototype.addSearchEngine = |
|
58 function (engineURL, iconURL, suggestedTitle, suggestedCategory) |
|
59 { |
|
60 debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " + |
|
61 suggestedCategory + ", " + suggestedTitle + ")"); |
|
62 |
|
63 if (!this.validateSearchEngine(engineURL, iconURL)) |
|
64 return; |
|
65 |
|
66 // OpenSearch files will likely be far more common than Sherlock files, and |
|
67 // have less consistent suffixes, so we assume that ".src" is a Sherlock |
|
68 // (text) file, and anything else is OpenSearch (XML). |
|
69 var dataType; |
|
70 if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL)) |
|
71 dataType = Components.interfaces.nsISearchEngine.DATA_TEXT; |
|
72 else |
|
73 dataType = Components.interfaces.nsISearchEngine.DATA_XML; |
|
74 |
|
75 Services.search.addEngine(engineURL, dataType, iconURL, true); |
|
76 } |
|
77 |
|
78 // This function exists largely to implement window.external.AddSearchProvider(), |
|
79 // to match other browsers' APIs. The capitalization, although nonstandard here, |
|
80 // is therefore important. |
|
81 nsSidebar.prototype.AddSearchProvider = |
|
82 function (aDescriptionURL) |
|
83 { |
|
84 // Get the favicon URL for the current page, or our best guess at the current |
|
85 // page since we don't have easy access to the active document. Most search |
|
86 // engines will override this with an icon specified in the OpenSearch |
|
87 // description anyway. |
|
88 var win = Services.wm.getMostRecentWindow("navigator:browser"); |
|
89 var browser = win.gBrowser; |
|
90 var iconURL = ""; |
|
91 // Use documentURIObject in the check for shouldLoadFavIcon so that we |
|
92 // do the right thing with about:-style error pages. Bug 453442 |
|
93 if (browser.shouldLoadFavIcon(browser.selectedBrowser |
|
94 .contentDocument |
|
95 .documentURIObject)) |
|
96 iconURL = browser.getIcon(); |
|
97 |
|
98 if (!this.validateSearchEngine(aDescriptionURL, iconURL)) |
|
99 return; |
|
100 |
|
101 const typeXML = Components.interfaces.nsISearchEngine.DATA_XML; |
|
102 Services.search.addEngine(aDescriptionURL, typeXML, iconURL, true); |
|
103 } |
|
104 |
|
105 // This function exists to implement window.external.IsSearchProviderInstalled(), |
|
106 // for compatibility with other browsers. It will return an integer value |
|
107 // indicating whether the given engine is installed for the current user. |
|
108 // However, it is currently stubbed out due to security/privacy concerns |
|
109 // stemming from difficulties in determining what domain issued the request. |
|
110 // See bug 340604 and |
|
111 // http://msdn.microsoft.com/en-us/library/aa342526%28VS.85%29.aspx . |
|
112 // XXX Implement this! |
|
113 nsSidebar.prototype.IsSearchProviderInstalled = |
|
114 function (aSearchURL) |
|
115 { |
|
116 return 0; |
|
117 } |
|
118 |
|
119 nsSidebar.prototype.QueryInterface = XPCOMUtils.generateQI([Components.interfaces.nsISupports]); |
|
120 |
|
121 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsSidebar]); |
|
122 |
|
123 /* static functions */ |
|
124 if (DEBUG) |
|
125 debug = function (s) { dump("-*- sidebar component: " + s + "\n"); } |
|
126 else |
|
127 debug = function (s) {} |