Open Bug 1048127 Opened 5 years ago Updated 4 years ago

addEngine "dataType" parameter documentation is potentially confusing

Categories

(Firefox :: Search, defect, P4)

31 Branch
defect

Tracking

()

People

(Reporter: jason.nichols, Unassigned)

Details

(Whiteboard: [fxsearch])

User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36

Steps to reproduce:

See: http://stackoverflow.com/questions/25083992/firefox-says-could-not-download-the-search-plugin-from

I'm trying to dynamically generate a search plugin for Firefox based on user entered values. I'm not including the forms and cruft surrounding it, because I've narrowed it down to a simple failing test case trying to import any XML.

Code: 
Simplified JS

var browserSearchService = Components
        .classes["@mozilla.org/browser/search-service;1"]
        .getService(Components.interfaces.nsIBrowserSearchService);

var EngineProperties = {
                xml :   'http://localhost/search.xml',
                dataType: 3,
                iconURL : 'http://localhost/logo.png',
                confirm : false,
                callback : function addEngineCallback(){
                    console.log('Jason is the greatest');           
                }
            }

browserSearchService.addEngine( EngineProperties.xml,
                                            EngineProperties.dataType,
                                            EngineProperties.iconURL,
                                            EngineProperties.confirm,
                                            EngineProperties.callback);
Actual XML

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
                       xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  <ShortName>Jason</ShortName>
  <Description>Powered By Jason</Description>
  <InputEncoding>UTF-8</InputEncoding>
  <Image width="16" height="16" type="image/x-icon">http://localhost/logo.png</Image>

  <URL method="get" type="text/html" template="http://search.mywebsearch.com/mywebsearch/GGmain.jhtml?ptb=100000487&amp;ind=1406730191685&amp;n=14787A74345&amp;st=bar&amp;searchfor={searchTerms}" />
  <URL method="get" type="application/x-moz-keywordsearch" 
    template="http://search.mywebsearch.com/mywebsearch/GGmain.jhtml?&amp;ptb=100000487&amp;ind=1406730191685&amp;n=14787A74345&amp;st=bar&amp;searchfor={searchTerms}" />
  <Url method="get" type="application/x-suggestions+json" 
    template="http://ssmsp.ask.com/query?q={searchTerms}&amp;li=ff&amp;sstype=prefix"/>

  <moz:SearchForm>http://search.mywebsearch.com/mywebsearch/GGmain.jhtml</moz:SearchForm>
</OpenSearchDescription>

Have tried as a data URI, chrome URI, and localhost from the browser console,  tried all as part of an extension, have even tried window.external.AddSearchProvider(), and nothing.

XML was validated by the browser, and I've tried changing MIME types in the data URI.


Actual results:

Error Could Not Download plugin.


Expected results:

Literally anything else.

A useful error message, success, anything.
From the console:
Error invoking addEngine install callback: [Exception... "JavaScript component does not have a method named: "onError"'JavaScript component does not have a method named: "onError"' when calling method: [nsISearchInstallCallback::onError]"  nsresult: "0x80570030 (NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED)"  location: "JS frame :: resource://gre/components/nsSearchService.js :: SRCH_SVC_addEngine/engine._installCallback :: line 3907"  data: no] nsSearchService.js:3909

Changed file type to .osdx, and window.external.AddSearchProvider() is now working, but addEngine still fails.
Component: Untriaged → Search
(In reply to Jason Nichols from comment #1)
> From the console:
> Error invoking addEngine install callback: [Exception... "JavaScript
> component does not have a method named: "onError"

This error occurs because your addEngine callback is not a valid nsISearchInstallCallback - you cannot pass just a JS function, you must pass an object with "onError" and "onSuccess" functions as properties, e.g.:

{
  onSuccess: function (engine) { console.log("Successfully installed engine: " + engine.name);  },
  onError: function (error) { console.log("Failed to add engine: " + error); }
}

Fixing that and then reporting the error code here might be useful (though I suspect it will return the not-so-useful error code 1, "ERROR_UNKNOWN_FAILURE").
changed the callback object.

It responded with an error and the number 1.

literally, function(err){console.log(err)} logged 1
Of note, running a Node server, I can get http://localhost/ or a dummied url (http://i.am.awesome/) from my host file to serve either a .osdx file and have it work via window.external.AddSearchProvider(), or an xml file with the mime type forced to 'application/opensearchdescription+xml' and have it work same.

The same URLs fail with .addEngine(). 

Further, neither 'data:application/opensearchdescription+xml,' nor 'chrome://path/to/file.osdx' nor 'chrome://path/to/file.xml' work with either method. Our operating supposition here at work is that addEngine is completely non-functional, and that window.external.AddSearchProvider is limited to http (and possibly though unconfirmed) https protocols.

Confirmed [protocol's are blocked for window.external](https://bugzilla.mozilla.org/show_bug.cgi?id=484636)

not sure what else I can do to test .addEngine()

Also, .addEngineWithDetails() needs to be updated to support search suggest, but I'm sure that's already an open ticket.
(In reply to Jason Nichols from comment #3)
> literally, function(err){console.log(err)} logged 1

Right, that's ERROR_UNKNOWN_FAILURE.

The list of allowed schemes for addEngine is defined at:
http://hg.mozilla.org/mozilla-central/annotate/71497ed2e0db/toolkit/components/search/nsSearchService.js#l1026

Can you set browser.search.log to true using about:config, restart Firefox, and then try again, and report back on any interesting console messages?
Should we add setting browser.search.log to MDN: https://developer.mozilla.org/en-US/Add-ons/Setting_up_extension_development_environment?redirectlocale=en-US&redirectslug=Setting_up_extension_development_environment#Development_preferences


What would be like a good single line description for this pref is it only for opensearch plugin debugging?
Work around:
As indicated by Noitidart on the SO page, dataType should be 1 even though it's opensearch.

Second, Could not pass the iconURL in addEngine. Not sure if that's true for all icons, but definitely passing it a png or a data URI both failed. (Gavin, figured that once I turned broser.search.log on, thanks!)


Changed filetype to .osdx, not .xml


Lastly, Setting confirm to true results in:

[Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)
[nsIURI.host]"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"  
location: "JS frame :: resource://gre/components/nsSearchService.js :: 
SRCH_SVC_confirmAddEngine :: line 1370"  
data: no]

when you use a data uri because it tries to parse location.host. Using a dummied URL through my hosts file worked, as did localhost.
(In reply to Jason Nichols from comment #7)
> Work around:
> As indicated by Noitidart on the SO page, dataType should be 1 even though
> it's opensearch.

OK, sounds like this was the original issue. This is documented in the IDL:

http://hg.mozilla.org/mozilla-central/annotate/5299864050ee/netwerk/base/public/nsIBrowserSearchService.idl#l279

"Must be one of the supported search engine data types defined above."

which refers to the section above labeled "Supported search engine data types.":

http://hg.mozilla.org/mozilla-central/annotate/5299864050ee/netwerk/base/public/nsIBrowserSearchService.idl#l125

The "search engine types" at http://hg.mozilla.org/mozilla-central/annotate/5299864050ee/netwerk/base/public/nsIBrowserSearchService.idl#l119 are an entirely different set of constants not meant to be used for addEngine() (add engine just needs to know XML vs. Sherlock, it determines whether it's OPENSEARCH or MOZSEARCH on its own).

We can try to clarify that documentation, I see how it is confusing.

> Changed filetype to .osdx, not .xml

This shouldn't matter.
Summary: addEngine isn't working → addEngine "dataType" parameter documentation is potentially confusing
Status: UNCONFIRMED → NEW
Ever confirmed: true
OS: Windows 7 → All
Hardware: x86_64 → All
Priority: -- → P4
Whiteboard: [fxsearch]
Rank: 45
You need to log in before you can comment on or make changes to this bug.