caps/src/nsNullPrincipalURI.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  * vim: sw=2 ts=2 sts=2 expandtab
     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/. */
     7 #include "nsNullPrincipalURI.h"
     9 #include "mozilla/MemoryReporting.h"
    11 #include "nsNetUtil.h"
    12 #include "nsEscape.h"
    13 #include "nsCRT.h"
    15 ////////////////////////////////////////////////////////////////////////////////
    16 //// nsNullPrincipalURI
    18 nsNullPrincipalURI::nsNullPrincipalURI(const nsCString &aSpec)
    19 {
    20   int32_t dividerPosition = aSpec.FindChar(':');
    21   NS_ASSERTION(dividerPosition != -1, "Malformed URI!");
    23   int32_t n = aSpec.Left(mScheme, dividerPosition);
    24   NS_ASSERTION(n == dividerPosition, "Storing the scheme failed!");
    26   int32_t count = aSpec.Length() - dividerPosition - 1;
    27   n = aSpec.Mid(mPath, dividerPosition + 1, count);
    28   NS_ASSERTION(n == count, "Storing the path failed!");
    30   ToLowerCase(mScheme);
    31 }
    33 static NS_DEFINE_CID(kNullPrincipalURIImplementationCID,
    34                      NS_NULLPRINCIPALURI_IMPLEMENTATION_CID);
    36 NS_IMPL_ADDREF(nsNullPrincipalURI)
    37 NS_IMPL_RELEASE(nsNullPrincipalURI)
    39 NS_INTERFACE_MAP_BEGIN(nsNullPrincipalURI)
    40   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIURI)
    41   if (aIID.Equals(kNullPrincipalURIImplementationCID))
    42     foundInterface = static_cast<nsIURI *>(this);
    43   else
    44   NS_INTERFACE_MAP_ENTRY(nsIURI)
    45   NS_INTERFACE_MAP_ENTRY(nsISizeOf)
    46 NS_INTERFACE_MAP_END
    48 ////////////////////////////////////////////////////////////////////////////////
    49 //// nsIURI
    51 NS_IMETHODIMP
    52 nsNullPrincipalURI::GetAsciiHost(nsACString &_host)
    53 {
    54   _host.Truncate();
    55   return NS_OK;
    56 }
    58 NS_IMETHODIMP
    59 nsNullPrincipalURI::GetAsciiSpec(nsACString &_spec)
    60 {
    61   nsAutoCString buffer;
    62   (void)GetSpec(buffer);
    63   NS_EscapeURL(buffer, esc_OnlyNonASCII | esc_AlwaysCopy, _spec);
    64   return NS_OK;
    65 }
    67 NS_IMETHODIMP
    68 nsNullPrincipalURI::GetHost(nsACString &_host)
    69 {
    70   _host.Truncate();
    71   return NS_OK;
    72 }
    74 NS_IMETHODIMP
    75 nsNullPrincipalURI::SetHost(const nsACString &aHost)
    76 {
    77   return NS_ERROR_NOT_IMPLEMENTED;
    78 }
    80 NS_IMETHODIMP
    81 nsNullPrincipalURI::GetHostPort(nsACString &_host)
    82 {
    83   return NS_ERROR_NOT_IMPLEMENTED;
    84 }
    86 NS_IMETHODIMP
    87 nsNullPrincipalURI::SetHostPort(const nsACString &aHost)
    88 {
    89   return NS_ERROR_NOT_IMPLEMENTED;
    90 }
    92 NS_IMETHODIMP
    93 nsNullPrincipalURI::GetOriginCharset(nsACString &_charset)
    94 {
    95   _charset.Truncate();
    96   return NS_OK;
    97 }
    99 NS_IMETHODIMP
   100 nsNullPrincipalURI::GetPassword(nsACString &_password)
   101 {
   102   return NS_ERROR_NOT_IMPLEMENTED;
   103 }
   105 NS_IMETHODIMP
   106 nsNullPrincipalURI::SetPassword(const nsACString &aPassword)
   107 {
   108   return NS_ERROR_NOT_IMPLEMENTED;
   109 }
   111 NS_IMETHODIMP
   112 nsNullPrincipalURI::GetPath(nsACString &_path)
   113 {
   114   _path = mPath;
   115   return NS_OK;
   116 }
   118 NS_IMETHODIMP
   119 nsNullPrincipalURI::SetPath(const nsACString &aPath)
   120 {
   121   return NS_ERROR_NOT_IMPLEMENTED;
   122 }
   124 NS_IMETHODIMP
   125 nsNullPrincipalURI::GetRef(nsACString &_ref)
   126 {
   127   _ref.Truncate();
   128   return NS_ERROR_NOT_IMPLEMENTED;
   129 }
   131 NS_IMETHODIMP
   132 nsNullPrincipalURI::SetRef(const nsACString &aRef)
   133 {
   134   return NS_ERROR_NOT_IMPLEMENTED;
   135 }
   137 NS_IMETHODIMP
   138 nsNullPrincipalURI::GetPrePath(nsACString &_prePath)
   139 {
   140   _prePath = mScheme + NS_LITERAL_CSTRING(":");
   141   return NS_OK;
   142 }
   144 NS_IMETHODIMP
   145 nsNullPrincipalURI::GetPort(int32_t *_port)
   146 {
   147   return NS_ERROR_NOT_IMPLEMENTED;
   148 }
   150 NS_IMETHODIMP
   151 nsNullPrincipalURI::SetPort(int32_t aPort)
   152 {
   153   return NS_ERROR_NOT_IMPLEMENTED;
   154 }
   156 NS_IMETHODIMP
   157 nsNullPrincipalURI::GetScheme(nsACString &_scheme)
   158 {
   159   _scheme = mScheme;
   160   return NS_OK;
   161 }
   163 NS_IMETHODIMP
   164 nsNullPrincipalURI::SetScheme(const nsACString &aScheme)
   165 {
   166   return NS_ERROR_NOT_IMPLEMENTED;
   167 }
   169 NS_IMETHODIMP
   170 nsNullPrincipalURI::GetSpec(nsACString &_spec)
   171 {
   172   _spec = mScheme + NS_LITERAL_CSTRING(":") + mPath;
   173   return NS_OK;
   174 }
   176 // result may contain unescaped UTF-8 characters
   177 NS_IMETHODIMP
   178 nsNullPrincipalURI::GetSpecIgnoringRef(nsACString &result)
   179 {
   180   return GetSpec(result);
   181 }
   183 NS_IMETHODIMP
   184 nsNullPrincipalURI::GetHasRef(bool *result)
   185 {
   186   *result = false;
   187   return NS_OK;
   188 }
   190 NS_IMETHODIMP
   191 nsNullPrincipalURI::SetSpec(const nsACString &aSpec)
   192 {
   193   return NS_ERROR_NOT_IMPLEMENTED;
   194 }
   196 NS_IMETHODIMP
   197 nsNullPrincipalURI::GetUsername(nsACString &_username)
   198 {
   199   return NS_ERROR_NOT_IMPLEMENTED;
   200 }
   202 NS_IMETHODIMP
   203 nsNullPrincipalURI::SetUsername(const nsACString &aUsername)
   204 {
   205   return NS_ERROR_NOT_IMPLEMENTED;
   206 }
   208 NS_IMETHODIMP
   209 nsNullPrincipalURI::GetUserPass(nsACString &_userPass)
   210 {
   211   return NS_ERROR_NOT_IMPLEMENTED;
   212 }
   214 NS_IMETHODIMP
   215 nsNullPrincipalURI::SetUserPass(const nsACString &aUserPass)
   216 {
   217   return NS_ERROR_NOT_IMPLEMENTED;
   218 }
   220 NS_IMETHODIMP
   221 nsNullPrincipalURI::Clone(nsIURI **_newURI)
   222 {
   223   nsCOMPtr<nsIURI> uri =
   224     new nsNullPrincipalURI(mScheme + NS_LITERAL_CSTRING(":") + mPath);
   225   uri.forget(_newURI);
   226   return NS_OK;
   227 }
   229 NS_IMETHODIMP
   230 nsNullPrincipalURI::CloneIgnoringRef(nsIURI **_newURI)
   231 {
   232   // GetRef/SetRef not supported by nsNullPrincipalURI, so
   233   // CloneIgnoringRef() is the same as Clone().
   234   return Clone(_newURI);
   235 }
   237 NS_IMETHODIMP
   238 nsNullPrincipalURI::Equals(nsIURI *aOther, bool *_equals)
   239 {
   240   *_equals = false;
   241   nsNullPrincipalURI *otherURI;
   242   nsresult rv = aOther->QueryInterface(kNullPrincipalURIImplementationCID,
   243                                        (void **)&otherURI);
   244   if (NS_SUCCEEDED(rv)) {
   245     *_equals = (mScheme == otherURI->mScheme && mPath == otherURI->mPath);
   246     NS_RELEASE(otherURI);
   247   }
   248   return NS_OK;
   249 }
   251 NS_IMETHODIMP
   252 nsNullPrincipalURI::EqualsExceptRef(nsIURI *aOther, bool *_equals)
   253 {
   254   // GetRef/SetRef not supported by nsNullPrincipalURI, so
   255   // EqualsExceptRef() is the same as Equals().
   256   return Equals(aOther, _equals);
   257 }
   259 NS_IMETHODIMP
   260 nsNullPrincipalURI::Resolve(const nsACString &aRelativePath,
   261                             nsACString &_resolvedURI)
   262 {
   263   _resolvedURI = aRelativePath;
   264   return NS_OK;
   265 }
   267 NS_IMETHODIMP
   268 nsNullPrincipalURI::SchemeIs(const char *aScheme, bool *_schemeIs)
   269 {
   270   *_schemeIs = (0 == nsCRT::strcasecmp(mScheme.get(), aScheme));
   271   return NS_OK;
   272 }
   274 ////////////////////////////////////////////////////////////////////////////////
   275 //// nsISizeOf
   277 size_t
   278 nsNullPrincipalURI::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
   279 {
   280   return mScheme.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
   281          mPath.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
   282 }
   284 size_t
   285 nsNullPrincipalURI::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
   286   return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
   287 }

mercurial