tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

URLExtraData.h (2601B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 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 /* thread-safe container of information for resolving url values */
      8 
      9 #ifndef mozilla_URLExtraData_h
     10 #define mozilla_URLExtraData_h
     11 
     12 #include <utility>
     13 
     14 #include "mozilla/BuiltInStyleSheets.h"
     15 #include "mozilla/StaticPtr.h"
     16 #include "nsCOMPtr.h"
     17 #include "nsIPrincipal.h"
     18 #include "nsIReferrerInfo.h"
     19 #include "nsIURI.h"
     20 
     21 namespace mozilla {
     22 
     23 struct URLExtraData {
     24  static bool ChromeRulesEnabled(nsIURI* aURI);
     25 
     26  URLExtraData(already_AddRefed<nsIURI> aBaseURI,
     27               already_AddRefed<nsIReferrerInfo> aReferrerInfo,
     28               already_AddRefed<nsIPrincipal> aPrincipal)
     29      : mBaseURI(std::move(aBaseURI)),
     30        mReferrerInfo(std::move(aReferrerInfo)),
     31        mPrincipal(std::move(aPrincipal)) {
     32    MOZ_ASSERT(mBaseURI);
     33    MOZ_ASSERT(mPrincipal);
     34    MOZ_ASSERT(mReferrerInfo);
     35    // When we hold the URI data of a style sheet, referrer is always
     36    // equal to the sheet URI.
     37    nsCOMPtr<nsIURI> referrer = mReferrerInfo->GetOriginalReferrer();
     38    mChromeRulesEnabled = ChromeRulesEnabled(referrer);
     39  }
     40 
     41  URLExtraData(nsIURI* aBaseURI, nsIReferrerInfo* aReferrerInfo,
     42               nsIPrincipal* aPrincipal)
     43      : URLExtraData(do_AddRef(aBaseURI), do_AddRef(aReferrerInfo),
     44                     do_AddRef(aPrincipal)) {}
     45 
     46  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(URLExtraData)
     47 
     48  nsIURI* BaseURI() const { return mBaseURI; }
     49  nsIReferrerInfo* ReferrerInfo() const { return mReferrerInfo; }
     50  nsIPrincipal* Principal() const { return mPrincipal; }
     51 
     52  bool ChromeRulesEnabled() const { return mChromeRulesEnabled; }
     53 
     54  static URLExtraData* Dummy() {
     55    MOZ_ASSERT(sDummy);
     56    return sDummy;
     57  }
     58 
     59  static URLExtraData* DummyChrome() {
     60    MOZ_ASSERT(sDummyChrome);
     61    return sDummyChrome;
     62  }
     63 
     64  static void Init();
     65  static void Shutdown();
     66 
     67  // URLExtraData objects that shared style sheets use a sheet ID index to
     68  // refer to.
     69  static StaticRefPtr<URLExtraData> sShared[size_t(BuiltInStyleSheet::Count)];
     70 
     71 private:
     72  ~URLExtraData();
     73 
     74  nsCOMPtr<nsIURI> mBaseURI;
     75  nsCOMPtr<nsIReferrerInfo> mReferrerInfo;
     76  nsCOMPtr<nsIPrincipal> mPrincipal;
     77 
     78  bool mChromeRulesEnabled;
     79 
     80  static StaticRefPtr<URLExtraData> sDummy;
     81  static StaticRefPtr<URLExtraData> sDummyChrome;
     82 };
     83 
     84 }  // namespace mozilla
     85 
     86 #endif  // mozilla_URLExtraData_h