BackgroundUtils.h (6115B)
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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_ipc_backgroundutils_h__ 8 #define mozilla_ipc_backgroundutils_h__ 9 10 #include "ipc/IPCMessageUtils.h" 11 #include "mozilla/OriginAttributes.h" 12 #include "nsCOMPtr.h" 13 #include "nscore.h" 14 15 class nsIContentSecurityPolicy; 16 class nsILoadInfo; 17 class nsINode; 18 class nsIPrincipal; 19 class nsIRedirectHistoryEntry; 20 21 namespace IPC { 22 23 namespace detail { 24 template <class ParamType> 25 struct OriginAttributesParamTraits { 26 typedef ParamType paramType; 27 28 static void Write(MessageWriter* aWriter, const paramType& aParam) { 29 nsAutoCString suffix; 30 aParam.CreateSuffix(suffix); 31 WriteParam(aWriter, suffix); 32 } 33 34 static bool Read(MessageReader* aReader, paramType* aResult) { 35 nsAutoCString suffix; 36 return ReadParam(aReader, &suffix) && aResult->PopulateFromSuffix(suffix); 37 } 38 }; 39 } // namespace detail 40 41 template <> 42 struct ParamTraits<mozilla::OriginAttributes> 43 : public detail::OriginAttributesParamTraits<mozilla::OriginAttributes> {}; 44 45 } // namespace IPC 46 47 namespace mozilla { 48 49 namespace dom { 50 class Document; 51 } 52 53 namespace net { 54 class ChildLoadInfoForwarderArgs; 55 class LoadInfoArgs; 56 class LoadInfo; 57 class ParentLoadInfoForwarderArgs; 58 class RedirectHistoryEntryInfo; 59 } // namespace net 60 61 namespace ipc { 62 63 class ContentSecurityPolicy; 64 class CSPInfo; 65 class PrincipalInfo; 66 67 /** 68 * Convert a PrincipalInfo to an nsIPrincipal. 69 * 70 * MUST be called on the main thread. 71 */ 72 Result<nsCOMPtr<nsIPrincipal>, nsresult> PrincipalInfoToPrincipal( 73 const PrincipalInfo& aPrincipalInfo); 74 75 /** 76 * Convert an nsIPrincipal to a PrincipalInfo. 77 * 78 * MUST be called on the main thread only. 79 */ 80 nsresult PrincipalToPrincipalInfo(nsIPrincipal* aPrincipal, 81 PrincipalInfo* aPrincipalInfo, 82 bool aSkipBaseDomain = false); 83 84 /** 85 * Compare storage keys for equivalence. 86 * 87 * Only use with storage keys retrieved from nsIGlobalObject::GetStorageKey! 88 * Bug 1776271 tracks enhancing this into a proper type. 89 */ 90 bool StorageKeysEqual(const PrincipalInfo& aLeft, const PrincipalInfo& aRight); 91 92 /** 93 * Convert a CSPInfo to an nsIContentSecurityPolicy. 94 * 95 * MUST be called on the main thread only. 96 * 97 * If possible, provide a requesting doc, so policy violation events can 98 * be dispatched correctly. If aRequestingDoc is null, then the CSPInfo holds 99 * the necessary fallback information, like a serialized requestPrincipal, 100 * to generate a valid nsIContentSecurityPolicy. 101 */ 102 already_AddRefed<nsIContentSecurityPolicy> CSPInfoToCSP( 103 const CSPInfo& aCSPInfo, mozilla::dom::Document* aRequestingDoc, 104 nsresult* aOptionalResult = nullptr); 105 106 /** 107 * Convert an nsIContentSecurityPolicy to a CSPInfo. 108 * 109 * MUST be called on the main thread only. 110 */ 111 nsresult CSPToCSPInfo(nsIContentSecurityPolicy* aCSP, CSPInfo* aCSPInfo); 112 113 /** 114 * Return true if this PrincipalInfo is a content principal and it has 115 * a privateBrowsing id in its OriginAttributes 116 */ 117 bool IsPrincipalInfoPrivate(const PrincipalInfo& aPrincipalInfo); 118 119 /** 120 * Convert an RedirectHistoryEntryInfo to a nsIRedirectHistoryEntry. 121 */ 122 123 already_AddRefed<nsIRedirectHistoryEntry> RHEntryInfoToRHEntry( 124 const mozilla::net::RedirectHistoryEntryInfo& aRHEntryInfo); 125 126 /** 127 * Convert an nsIRedirectHistoryEntry to a RedirectHistoryEntryInfo. 128 */ 129 130 nsresult RHEntryToRHEntryInfo( 131 nsIRedirectHistoryEntry* aRHEntry, 132 mozilla::net::RedirectHistoryEntryInfo* aRHEntryInfo); 133 134 /** 135 * Convert a LoadInfo to LoadInfoArgs struct. 136 */ 137 nsresult LoadInfoToLoadInfoArgs(nsILoadInfo* aLoadInfo, 138 mozilla::net::LoadInfoArgs* outLoadInfoArgs); 139 140 /** 141 * Convert LoadInfoArgs to a LoadInfo. 142 */ 143 nsresult LoadInfoArgsToLoadInfo(const mozilla::net::LoadInfoArgs& aLoadInfoArgs, 144 const nsACString& aOriginRemoteType, 145 nsILoadInfo** outLoadInfo); 146 nsresult LoadInfoArgsToLoadInfo(const mozilla::net::LoadInfoArgs& aLoadInfoArgs, 147 const nsACString& aOriginRemoteType, 148 nsINode* aCspToInheritLoadingContext, 149 nsILoadInfo** outLoadInfo); 150 nsresult LoadInfoArgsToLoadInfo(const net::LoadInfoArgs& aLoadInfoArgs, 151 const nsACString& aOriginRemoteType, 152 mozilla::net::LoadInfo** outLoadInfo); 153 nsresult LoadInfoArgsToLoadInfo(const net::LoadInfoArgs& aLoadInfoArgs, 154 const nsACString& aOriginRemoteType, 155 nsINode* aCspToInheritLoadingContext, 156 mozilla::net::LoadInfo** outLoadInfo); 157 158 /** 159 * Fills ParentLoadInfoForwarderArgs with properties we want to carry to child 160 * processes. 161 */ 162 void LoadInfoToParentLoadInfoForwarder( 163 nsILoadInfo* aLoadInfo, 164 mozilla::net::ParentLoadInfoForwarderArgs* aForwarderArgsOut); 165 166 /** 167 * Merges (replaces) properties of an existing LoadInfo on a child process 168 * with properties carried down through ParentLoadInfoForwarderArgs. 169 */ 170 nsresult MergeParentLoadInfoForwarder( 171 mozilla::net::ParentLoadInfoForwarderArgs const& aForwarderArgs, 172 nsILoadInfo* aLoadInfo); 173 174 /** 175 * Fills ChildLoadInfoForwarderArgs with properties we want to carry to the 176 * parent process after the initial channel creation. 177 */ 178 void LoadInfoToChildLoadInfoForwarder( 179 nsILoadInfo* aLoadInfo, 180 mozilla::net::ChildLoadInfoForwarderArgs* aForwarderArgsOut); 181 182 /** 183 * Merges (replaces) properties of an existing LoadInfo on the parent process 184 * with properties contained in a ChildLoadInfoForwarderArgs. 185 */ 186 nsresult MergeChildLoadInfoForwarder( 187 const mozilla::net::ChildLoadInfoForwarderArgs& aForwardArgs, 188 nsILoadInfo* aLoadInfo); 189 190 } // namespace ipc 191 } // namespace mozilla 192 193 #endif // mozilla_ipc_backgroundutils_h__