Bug 1389848: Make isServiceInstantiated*() return false rather than throw for uninstantiated services. r?ehsan draft
authorKris Maglione <maglione.k@gmail.com>
Sat, 12 Aug 2017 16:15:19 -0700
changeset 645474 9990d47ac1bf949d2bb147c40a8d55a973ba8d2d
parent 645473 ca237e2a9f3c82743d1834adcdb230f17fe37b57
child 645475 363309f141e4f43990b38bc50895c3b4868bce4d
push id73759
push usermaglione.k@gmail.com
push dateSat, 12 Aug 2017 23:15:51 +0000
reviewersehsan
bugs1389848
milestone57.0a1
Bug 1389848: Make isServiceInstantiated*() return false rather than throw for uninstantiated services. r?ehsan MozReview-Commit-ID: J8w7eQ6R3eS
browser/components/tests/startupRecorder.js
xpcom/components/nsComponentManager.cpp
xpcom/components/nsIServiceManager.idl
--- 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++