LCOV - code coverage report
Current view: top level - caps - ExpandedPrincipal.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 1 95 1.1 %
Date: 2017-07-14 16:53:18 Functions: 0 21 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* vim: set ts=2 sw=2 et tw=80: */
       3             : /* This Source Code Form is subject to the terms of the Mozilla Public
       4             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       5             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       6             : 
       7             : #include "ExpandedPrincipal.h"
       8             : #include "nsIClassInfoImpl.h"
       9             : 
      10             : using namespace mozilla;
      11             : 
      12           3 : NS_IMPL_CLASSINFO(ExpandedPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
      13             :                   NS_EXPANDEDPRINCIPAL_CID)
      14           0 : NS_IMPL_QUERY_INTERFACE_CI(ExpandedPrincipal,
      15             :                            nsIPrincipal,
      16             :                            nsIExpandedPrincipal)
      17           0 : NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal,
      18             :                             nsIPrincipal,
      19             :                             nsIExpandedPrincipal)
      20             : 
      21             : struct OriginComparator
      22             : {
      23           0 :   bool LessThan(nsIPrincipal* a, nsIPrincipal* b) const
      24             :   {
      25           0 :     nsAutoCString originA;
      26           0 :     DebugOnly<nsresult> rv = a->GetOrigin(originA);
      27           0 :     MOZ_ASSERT(NS_SUCCEEDED(rv));
      28           0 :     nsAutoCString originB;
      29           0 :     rv = b->GetOrigin(originB);
      30           0 :     MOZ_ASSERT(NS_SUCCEEDED(rv));
      31           0 :     return originA < originB;
      32             :   }
      33             : 
      34           0 :   bool Equals(nsIPrincipal* a, nsIPrincipal* b) const
      35             :   {
      36           0 :     nsAutoCString originA;
      37           0 :     DebugOnly<nsresult> rv = a->GetOrigin(originA);
      38           0 :     MOZ_ASSERT(NS_SUCCEEDED(rv));
      39           0 :     nsAutoCString originB;
      40           0 :     rv = b->GetOrigin(originB);
      41           0 :     MOZ_ASSERT(NS_SUCCEEDED(rv));
      42           0 :     return a == b;
      43             :   }
      44             : };
      45             : 
      46           0 : ExpandedPrincipal::ExpandedPrincipal(nsTArray<nsCOMPtr<nsIPrincipal>> &aWhiteList)
      47           0 :   : BasePrincipal(eExpandedPrincipal)
      48             : {
      49             :   // We force the principals to be sorted by origin so that ExpandedPrincipal
      50             :   // origins can have a canonical form.
      51             :   OriginComparator c;
      52           0 :   for (size_t i = 0; i < aWhiteList.Length(); ++i) {
      53           0 :     mPrincipals.InsertElementSorted(aWhiteList[i], c);
      54             :   }
      55           0 : }
      56             : 
      57           0 : ExpandedPrincipal::~ExpandedPrincipal()
      58           0 : { }
      59             : 
      60             : already_AddRefed<ExpandedPrincipal>
      61           0 : ExpandedPrincipal::Create(nsTArray<nsCOMPtr<nsIPrincipal>>& aWhiteList,
      62             :                           const OriginAttributes& aAttrs)
      63             : {
      64           0 :   RefPtr<ExpandedPrincipal> ep = new ExpandedPrincipal(aWhiteList);
      65             : 
      66           0 :   nsAutoCString origin;
      67           0 :   origin.AssignLiteral("[Expanded Principal [");
      68           0 :   for (size_t i = 0; i < ep->mPrincipals.Length(); ++i) {
      69           0 :     if (i != 0) {
      70           0 :       origin.AppendLiteral(", ");
      71             :     }
      72             : 
      73           0 :     nsAutoCString subOrigin;
      74           0 :     DebugOnly<nsresult> rv = ep->mPrincipals.ElementAt(i)->GetOrigin(subOrigin);
      75           0 :     MOZ_ASSERT(NS_SUCCEEDED(rv));
      76           0 :     origin.Append(subOrigin);
      77             :   }
      78           0 :   origin.Append("]]");
      79             : 
      80           0 :   ep->FinishInit(origin, aAttrs);
      81           0 :   return ep.forget();
      82             : }
      83             : 
      84             : NS_IMETHODIMP
      85           0 : ExpandedPrincipal::GetDomain(nsIURI** aDomain)
      86             : {
      87           0 :   *aDomain = nullptr;
      88           0 :   return NS_OK;
      89             : }
      90             : 
      91             : NS_IMETHODIMP
      92           0 : ExpandedPrincipal::SetDomain(nsIURI* aDomain)
      93             : {
      94           0 :   return NS_OK;
      95             : }
      96             : 
      97             : bool
      98           0 : ExpandedPrincipal::SubsumesInternal(nsIPrincipal* aOther,
      99             :                                     BasePrincipal::DocumentDomainConsideration aConsideration)
     100             : {
     101             :   // If aOther is an ExpandedPrincipal too, we break it down into its component
     102             :   // nsIPrincipals, and check subsumes on each one.
     103           0 :   nsCOMPtr<nsIExpandedPrincipal> expanded = do_QueryInterface(aOther);
     104           0 :   if (expanded) {
     105             :     nsTArray< nsCOMPtr<nsIPrincipal> >* otherList;
     106           0 :     expanded->GetWhiteList(&otherList);
     107           0 :     for (uint32_t i = 0; i < otherList->Length(); ++i){
     108             :       // Use SubsumesInternal rather than Subsumes here, since OriginAttribute
     109             :       // checks are only done between non-expanded sub-principals, and we don't
     110             :       // need to incur the extra virtual call overhead.
     111           0 :       if (!SubsumesInternal((*otherList)[i], aConsideration)) {
     112           0 :         return false;
     113             :       }
     114             :     }
     115           0 :     return true;
     116             :   }
     117             : 
     118             :   // We're dealing with a regular principal. One of our principals must subsume
     119             :   // it.
     120           0 :   for (uint32_t i = 0; i < mPrincipals.Length(); ++i) {
     121           0 :     if (Cast(mPrincipals[i])->Subsumes(aOther, aConsideration)) {
     122           0 :       return true;
     123             :     }
     124             :   }
     125             : 
     126           0 :   return false;
     127             : }
     128             : 
     129             : bool
     130           0 : ExpandedPrincipal::MayLoadInternal(nsIURI* uri)
     131             : {
     132           0 :   for (uint32_t i = 0; i < mPrincipals.Length(); ++i){
     133           0 :     if (BasePrincipal::Cast(mPrincipals[i])->MayLoadInternal(uri)) {
     134           0 :       return true;
     135             :     }
     136             :   }
     137             : 
     138           0 :   return false;
     139             : }
     140             : 
     141             : NS_IMETHODIMP
     142           0 : ExpandedPrincipal::GetHashValue(uint32_t* result)
     143             : {
     144           0 :   MOZ_CRASH("extended principal should never be used as key in a hash map");
     145             : }
     146             : 
     147             : NS_IMETHODIMP
     148           0 : ExpandedPrincipal::GetURI(nsIURI** aURI)
     149             : {
     150           0 :   *aURI = nullptr;
     151           0 :   return NS_OK;
     152             : }
     153             : 
     154             : NS_IMETHODIMP
     155           0 : ExpandedPrincipal::GetWhiteList(nsTArray<nsCOMPtr<nsIPrincipal> >** aWhiteList)
     156             : {
     157           0 :   *aWhiteList = &mPrincipals;
     158           0 :   return NS_OK;
     159             : }
     160             : 
     161             : NS_IMETHODIMP
     162           0 : ExpandedPrincipal::GetBaseDomain(nsACString& aBaseDomain)
     163             : {
     164           0 :   return NS_ERROR_NOT_AVAILABLE;
     165             : }
     166             : 
     167             : NS_IMETHODIMP
     168           0 : ExpandedPrincipal::GetAddonId(nsAString& aAddonId)
     169             : {
     170           0 :   aAddonId.Truncate();
     171           0 :   return NS_OK;
     172             : };
     173             : 
     174             : bool
     175           0 : ExpandedPrincipal::AddonHasPermission(const nsAString& aPerm)
     176             : {
     177           0 :   for (size_t i = 0; i < mPrincipals.Length(); ++i) {
     178           0 :     if (BasePrincipal::Cast(mPrincipals[i])->AddonHasPermission(aPerm)) {
     179           0 :       return true;
     180             :     }
     181             :   }
     182           0 :   return false;
     183             : }
     184             : 
     185             : nsresult
     186           0 : ExpandedPrincipal::GetScriptLocation(nsACString& aStr)
     187             : {
     188           0 :   aStr.Assign("[Expanded Principal [");
     189           0 :   for (size_t i = 0; i < mPrincipals.Length(); ++i) {
     190           0 :     if (i != 0) {
     191           0 :       aStr.AppendLiteral(", ");
     192             :     }
     193             : 
     194           0 :     nsAutoCString spec;
     195             :     nsresult rv =
     196           0 :       nsJSPrincipals::get(mPrincipals.ElementAt(i))->GetScriptLocation(spec);
     197           0 :     NS_ENSURE_SUCCESS(rv, rv);
     198             : 
     199           0 :     aStr.Append(spec);
     200             :   }
     201           0 :   aStr.Append("]]");
     202           0 :   return NS_OK;
     203             : }
     204             : 
     205             : //////////////////////////////////////////
     206             : // Methods implementing nsISerializable //
     207             : //////////////////////////////////////////
     208             : 
     209             : NS_IMETHODIMP
     210           0 : ExpandedPrincipal::Read(nsIObjectInputStream* aStream)
     211             : {
     212           0 :   return NS_ERROR_NOT_IMPLEMENTED;
     213             : }
     214             : 
     215             : NS_IMETHODIMP
     216           0 : ExpandedPrincipal::Write(nsIObjectOutputStream* aStream)
     217             : {
     218           0 :   return NS_ERROR_NOT_IMPLEMENTED;
     219             : }

Generated by: LCOV version 1.13