Bug 1171175 - Improve BasePrincipal::IsCodebasePrincipal. r=baku
authorBobby Holley <bobbyholley@gmail.com>
Wed, 03 Jun 2015 13:40:46 -0700
changeset 247168 b4aac564d1e066866be2bd83a6aad9d8778d9cbf
parent 247167 144d803669f22256968c0f4d7c062faacd04c172
child 247169 66bab785635f3b108bc88c07228848463a07d7f6
push id60627
push userbobbyholley@gmail.com
push dateThu, 04 Jun 2015 17:02:04 +0000
treeherdermozilla-inbound@b4aac564d1e0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1171175
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1171175 - Improve BasePrincipal::IsCodebasePrincipal. r=baku
caps/BasePrincipal.cpp
caps/BasePrincipal.h
caps/nsPrincipal.h
dom/workers/ServiceWorkerManager.cpp
--- a/caps/BasePrincipal.cpp
+++ b/caps/BasePrincipal.cpp
@@ -254,34 +254,9 @@ BasePrincipal::CreateCodebasePrincipal(n
 
   // Mint a codebase principal.
   nsRefPtr<nsPrincipal> codebase = new nsPrincipal();
   rv = codebase->Init(aURI, aAttrs);
   NS_ENSURE_SUCCESS(rv, nullptr);
   return codebase.forget();
 }
 
-/* static */ bool
-BasePrincipal::IsCodebasePrincipal(nsIPrincipal* aPrincipal)
-{
-  MOZ_ASSERT(aPrincipal);
-
-  bool isNullPrincipal = true;
-  nsresult rv = aPrincipal->GetIsNullPrincipal(&isNullPrincipal);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return false;
-  }
-
-  if (isNullPrincipal || nsContentUtils::IsSystemPrincipal(aPrincipal)) {
-    return false;
-  }
-
-  // No expanded principals.
-  nsCOMPtr<nsIExpandedPrincipal> expandedPrincipal =
-    do_QueryInterface(aPrincipal);
-  if (expandedPrincipal) {
-    return false;
-  }
-
-  return true;
-}
-
 } // namespace mozilla
--- a/caps/BasePrincipal.h
+++ b/caps/BasePrincipal.h
@@ -78,17 +78,17 @@ public:
   NS_IMETHOD GetCookieJar(nsACString& aCookieJar) final;
   NS_IMETHOD GetAppStatus(uint16_t* aAppStatus) final;
   NS_IMETHOD GetAppId(uint32_t* aAppStatus) final;
   NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement) final;
   NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) final;
 
   virtual bool IsOnCSSUnprefixingWhitelist() override { return false; }
 
-  static bool IsCodebasePrincipal(nsIPrincipal* aPrincipal);
+  virtual bool IsCodebasePrincipal() const { return false; };
 
   static BasePrincipal* Cast(nsIPrincipal* aPrin) { return static_cast<BasePrincipal*>(aPrin); }
   static already_AddRefed<BasePrincipal> CreateCodebasePrincipal(nsIURI* aURI, OriginAttributes& aAttrs);
 
   const OriginAttributes& OriginAttributesRef() { return mOriginAttributes; }
   uint32_t AppId() const { return mOriginAttributes.mAppId; }
   bool IsInBrowserElement() const { return mOriginAttributes.mInBrowser; }
 
--- a/caps/nsPrincipal.h
+++ b/caps/nsPrincipal.h
@@ -24,16 +24,17 @@ public:
   NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
   NS_IMETHOD GetHashValue(uint32_t* aHashValue) override;
   NS_IMETHOD GetURI(nsIURI** aURI) override;
   NS_IMETHOD GetDomain(nsIURI** aDomain) override;
   NS_IMETHOD SetDomain(nsIURI* aDomain) override;
   NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal) override;
   NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
   virtual bool IsOnCSSUnprefixingWhitelist() override;
+  bool IsCodebasePrincipal() const override { return true; }
   nsresult GetOriginInternal(nsACString& aOrigin) override;
 
   nsPrincipal();
 
   // Init() must be called before the principal is in a usable state.
   nsresult Init(nsIURI* aCodebase, const mozilla::OriginAttributes& aOriginAttributes);
 
   virtual void GetScriptLocation(nsACString& aStr) override;
--- a/dom/workers/ServiceWorkerManager.cpp
+++ b/dom/workers/ServiceWorkerManager.cpp
@@ -224,17 +224,17 @@ namespace {
 nsresult
 PopulateRegistrationData(nsIPrincipal* aPrincipal,
                          const ServiceWorkerRegistrationInfo* aRegistration,
                          ServiceWorkerRegistrationData& aData)
 {
   MOZ_ASSERT(aPrincipal);
   MOZ_ASSERT(aRegistration);
 
-  if (NS_WARN_IF(!BasePrincipal::IsCodebasePrincipal(aPrincipal))) {
+  if (NS_WARN_IF(!BasePrincipal::Cast(aPrincipal)->IsCodebasePrincipal())) {
     return NS_ERROR_FAILURE;
   }
 
   nsresult rv = PrincipalToPrincipalInfo(aPrincipal, &aData.principal());
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
@@ -1586,17 +1586,17 @@ public:
     nsCOMPtr<nsIPrincipal> principal = doc->NodePrincipal();
     if (!principal) {
       mPromise->MaybeReject(NS_ERROR_UNEXPECTED);
       return NS_OK;
     }
 
     nsTArray<nsRefPtr<ServiceWorkerRegistrationMainThread>> array;
 
-    if (NS_WARN_IF(!BasePrincipal::IsCodebasePrincipal(principal))) {
+    if (NS_WARN_IF(!BasePrincipal::Cast(principal)->IsCodebasePrincipal())) {
       return NS_OK;
     }
 
     nsAutoCString scopeKey;
     nsresult rv = swm->PrincipalToScopeKey(principal, scopeKey);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
@@ -2583,17 +2583,17 @@ ServiceWorkerManager::GetServiceWorkerRe
 }
 
 /* static */ nsresult
 ServiceWorkerManager::PrincipalToScopeKey(nsIPrincipal* aPrincipal,
                                           nsACString& aKey)
 {
   MOZ_ASSERT(aPrincipal);
 
-  if (NS_WARN_IF(!BasePrincipal::IsCodebasePrincipal(aPrincipal))) {
+  if (NS_WARN_IF(!BasePrincipal::Cast(aPrincipal)->IsCodebasePrincipal())) {
     return NS_ERROR_FAILURE;
   }
 
   nsresult rv = aPrincipal->GetOriginSuffix(aKey);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }