"use strict";
const EXPORTED_SYMBOLS = ["ProvidersManager"];
const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
const VERSION = "1.0";
var SuggestDataService = {
SERVICE_NAME: "yaOmniBoxSuggestDataService",
getVersion: function SuggestDataService_getVerion() VERSION,
registerDataProvider: function SuggestDataService_registerDataProvider(aProvider) {
return ProvidersManager.registerDataProvider(aProvider);
}
,
handleResponse: function SuggestDataService_handleResponse(aTopic, aData, aProvider) {
if (aTopic == "start-collecting")
ProvidersManager.onSearchResult(aProvider,aData);
}
,
removeDataProvider: function SuggestDataService_removeDataProvider(aProvider) {
ProvidersManager.removeDataProvider(aProvider);
}
};
var ProvidersManager = {
init: function ProvidersManager_init(core) {
this.core = core;
core.api.Services.registerService(SuggestDataService.SERVICE_NAME,SuggestDataService);
this._clearProvidersQueue();
this.loadDataProviders();
}
,
finalize: function ProvidersManager_finalize() {
this.stopSearch();
this._dataProviders = Object.create(null);
this.api.Services.unregisterService(SuggestDataService.SERVICE_NAME);
this.core = null;
}
,
get api() this.core.api,
_dataProviders: Object.create(null),
startSearch: function ProvidersManager_startSearch(aSearchStrings, aListener) {
this.stopSearch();
this._searchListener = aListener;
this.api.Services.notifyServiceUsers(SuggestDataService.SERVICE_NAME,"start-collecting",aSearchStrings);
}
,
stopSearch: function ProvidersManager_stopSearch() {
this.api.Services.notifyServiceUsers(SuggestDataService.SERVICE_NAME,"stop-collecting");
this._searchListener = null;
this._searchString = null;
this._searchStartTime = null;
}
,
_clearProvidersQueue: function ProvidersManager__clearProvidersQueue() {
this._results = {
};
this._stats = {
};
this._searchResult = {
};
this._resultsStrings = {
};
}
,
onSearchResult: function ProvidersManager_onSearchResult(aDataProvider, aResult) {
this._stats[aDataProvider.PROVIDER_NAME] = aResult.stat;
this._searchResult[aDataProvider.PROVIDER_NAME] = aResult.searchResult;
this._results[aDataProvider.PROVIDER_NAME] = aResult.suggestions;
this._resultsStrings[aDataProvider.PROVIDER_NAME] = aResult.originalSearchStrings;
if (this._searchListener)
this._searchListener._onSearchResult([this._results, this._stats, this._searchResult, aResult.originalSearchStrings]);
}
,
registerDataProvider: function ProvidersManager_registerDataProvider(aProvider) {
if (this._dataProviders[aProvider.PROVIDER_NAME])
return false;
this._dataProviders[aProvider.PROVIDER_NAME] = aProvider;
return true;
}
,
removeDataProvider: function ProvidersManager_removeDataProvider(aProvider) {
if (this._dataProviders[aProvider.PROVIDER_NAME])
delete this._dataProviders[aProvider.PROVIDER_NAME];
}
,
loadDataProviders: function ProvidersManager_loadDataProviders() {
var providersPath = "/native/providers/";
["places", "suggests", "formHistory"].forEach(function (aProviderName) {
Cu.import(this.api.Package.resolvePath(providersPath + aProviderName + ".js"),{
}).DataProvider.init(this.core);
}
,this);
}
};
