Bug 1389848: Make isServiceInstantiated*() return false rather than throw for uninstantiated services. r?ehsan
MozReview-Commit-ID: J8w7eQ6R3eS
--- a/browser/components/tests/startupRecorder.js
+++ b/browser/components/tests/startupRecorder.js
@@ -41,18 +41,17 @@ startupRecorder.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
record(name) {
this.data.code[name] = {
components: this.loader.loadedComponents(),
modules: this.loader.loadedModules(),
services: Object.keys(Cc).filter(c => {
try {
- Cm.isServiceInstantiatedByContractID(c, Ci.nsISupports);
- return true;
+ return Cm.isServiceInstantiatedByContractID(c, Ci.nsISupports);
} catch (e) {
return false;
}
})
};
},
observe(subject, topic, data) {
--- a/xpcom/components/nsComponentManager.cpp
+++ b/xpcom/components/nsComponentManager.cpp
@@ -1306,28 +1306,30 @@ nsComponentManagerImpl::IsServiceInstant
aClass.ToProvidedString(cid);
aIID.ToProvidedString(iid);
fprintf(stderr, "Checking for service on shutdown. Denied.\n"
" CID: %s\n IID: %s\n", cid, iid);
#endif /* SHOW_DENIED_ON_SHUTDOWN */
return NS_ERROR_UNEXPECTED;
}
- nsresult rv = NS_ERROR_SERVICE_NOT_AVAILABLE;
+ nsresult rv = NS_OK;
nsFactoryEntry* entry;
{
SafeMutexAutoLock lock(mLock);
entry = mFactories.Get(aClass);
}
if (entry && entry->mServiceObject) {
nsCOMPtr<nsISupports> service;
rv = entry->mServiceObject->QueryInterface(aIID, getter_AddRefs(service));
*aResult = (service != nullptr);
+ } else {
+ *aResult = false;
}
return rv;
}
NS_IMETHODIMP
nsComponentManagerImpl::IsServiceInstantiatedByContractID(
const char* aContractID,
@@ -1346,27 +1348,29 @@ nsComponentManagerImpl::IsServiceInstant
char iid[NSID_LENGTH];
aIID.ToProvidedString(iid);
fprintf(stderr, "Checking for service on shutdown. Denied.\n"
" ContractID: %s\n IID: %s\n", aContractID, iid);
#endif /* SHOW_DENIED_ON_SHUTDOWN */
return NS_ERROR_UNEXPECTED;
}
- nsresult rv = NS_ERROR_SERVICE_NOT_AVAILABLE;
+ nsresult rv = NS_OK;
nsFactoryEntry* entry;
{
SafeMutexAutoLock lock(mLock);
entry = mContractIDs.Get(nsDependentCString(aContractID));
}
if (entry && entry->mServiceObject) {
nsCOMPtr<nsISupports> service;
rv = entry->mServiceObject->QueryInterface(aIID, getter_AddRefs(service));
*aResult = (service != nullptr);
+ } else {
+ *aResult = false;
}
return rv;
}
NS_IMETHODIMP
nsComponentManagerImpl::GetServiceByContractID(const char* aContractID,
const nsIID& aIID,
--- a/xpcom/components/nsIServiceManager.idl
+++ b/xpcom/components/nsIServiceManager.idl
@@ -40,23 +40,22 @@ interface nsIServiceManager : nsISupport
void getServiceByContractID(in string aContractID,
in nsIIDRef aIID,
[iid_is(aIID),retval] out nsQIResult result);
/**
* isServiceInstantiated
*
* isServiceInstantiated will return a true if the service has already
- * been created, or throw otherwise
+ * been created, or false otherwise. Throws if the service does not
+ * implement the given IID.
*
* @param aClass or aContractID : aClass or aContractID of object
* instance requested
* @param aIID : IID of interface requested
- * @throws NS_ERROR_SERVICE_NOT_AVAILABLE if the service hasn't been
- * instantiated
* @throws NS_NOINTERFACE if the IID given isn't supported by the object
*/
boolean isServiceInstantiated(in nsCIDRef aClass, in nsIIDRef aIID);
boolean isServiceInstantiatedByContractID(in string aContractID, in nsIIDRef aIID);
};
%{C++