NeckoCommon.h (5680B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et tw=80 : */ 3 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #ifndef mozilla_net_NeckoCommon_h 9 #define mozilla_net_NeckoCommon_h 10 11 #include "mozilla/Preferences.h" 12 #include "mozilla/Variant.h" 13 #include "nsIRequest.h" 14 #include "nsPrintfCString.h" 15 #include "nsXULAppAPI.h" 16 #include "prenv.h" 17 18 class nsIStreamListener; 19 20 namespace mozilla { 21 namespace dom { 22 class BrowserChild; 23 } // namespace dom 24 } // namespace mozilla 25 26 #if defined(DEBUG) 27 # define NECKO_ERRORS_ARE_FATAL_DEFAULT true 28 #else 29 # define NECKO_ERRORS_ARE_FATAL_DEFAULT false 30 #endif 31 32 // TODO: Eventually remove NECKO_MAYBE_ABORT and DROP_DEAD (bug 575494). 33 // Still useful for catching listener interfaces we don't yet support across 34 // processes, etc. 35 36 #define NECKO_MAYBE_ABORT(msg) \ 37 do { \ 38 bool abort = NECKO_ERRORS_ARE_FATAL_DEFAULT; \ 39 const char* e = PR_GetEnv("NECKO_ERRORS_ARE_FATAL"); \ 40 if (e) abort = (*e == '0') ? false : true; \ 41 if (abort) { \ 42 msg.AppendLiteral( \ 43 " (set NECKO_ERRORS_ARE_FATAL=0 in your environment " \ 44 "to convert this error into a warning.)"); \ 45 MOZ_CRASH_UNSAFE(msg.get()); \ 46 } else { \ 47 msg.AppendLiteral( \ 48 " (set NECKO_ERRORS_ARE_FATAL=1 in your environment " \ 49 "to convert this warning into a fatal error.)"); \ 50 NS_WARNING(msg.get()); \ 51 } \ 52 } while (0) 53 54 #define DROP_DEAD() \ 55 do { \ 56 nsPrintfCString msg("NECKO ERROR: '%s' UNIMPLEMENTED", __FUNCTION__); \ 57 NECKO_MAYBE_ABORT(msg); \ 58 return NS_ERROR_NOT_IMPLEMENTED; \ 59 } while (0) 60 61 #define ENSURE_CALLED_BEFORE_ASYNC_OPEN() \ 62 do { \ 63 if (LoadIsPending() || LoadWasOpened()) { \ 64 nsPrintfCString msg("'%s' called after AsyncOpen: %s +%d", __FUNCTION__, \ 65 __FILE__, __LINE__); \ 66 NECKO_MAYBE_ABORT(msg); \ 67 } \ 68 NS_ENSURE_TRUE(!LoadIsPending(), NS_ERROR_IN_PROGRESS); \ 69 NS_ENSURE_TRUE(!LoadWasOpened(), NS_ERROR_ALREADY_OPENED); \ 70 } while (0) 71 72 // Fails call if made after request observers (on-modify-request, etc) have been 73 // called 74 75 #define ENSURE_CALLED_BEFORE_CONNECT() \ 76 do { \ 77 if (LoadRequestObserversCalled()) { \ 78 nsPrintfCString msg("'%s' called too late: %s +%d", __FUNCTION__, \ 79 __FILE__, __LINE__); \ 80 NECKO_MAYBE_ABORT(msg); \ 81 if (LoadIsPending()) return NS_ERROR_IN_PROGRESS; \ 82 MOZ_ASSERT(LoadWasOpened()); \ 83 return NS_ERROR_ALREADY_OPENED; \ 84 } \ 85 } while (0) 86 87 namespace mozilla { 88 namespace net { 89 90 inline bool IsNeckoChild() { 91 static bool didCheck = false; 92 static bool amChild = false; 93 94 if (!didCheck) { 95 didCheck = true; 96 amChild = (XRE_GetProcessType() == GeckoProcessType_Content); 97 } 98 return amChild; 99 } 100 101 inline bool IsSocketProcessChild() { 102 static bool amChild = (XRE_GetProcessType() == GeckoProcessType_Socket); 103 return amChild; 104 } 105 106 class HttpChannelSecurityWarningReporter : public nsISupports { 107 public: 108 [[nodiscard]] virtual nsresult ReportSecurityMessage( 109 const nsAString& aMessageTag, const nsAString& aMessageCategory) = 0; 110 [[nodiscard]] virtual nsresult LogBlockedCORSRequest( 111 const nsAString& aMessage, const nsACString& aCategory, 112 bool aIsWarning = false) = 0; 113 [[nodiscard]] virtual nsresult LogMimeTypeMismatch( 114 const nsACString& aMessageName, bool aWarning, const nsAString& aURL, 115 const nsAString& aContentType) = 0; 116 }; 117 118 struct OnStartRequestParams { 119 nsCOMPtr<nsIRequest> request; 120 }; 121 struct OnDataAvailableParams { 122 nsCOMPtr<nsIRequest> request; 123 nsCString data; 124 uint64_t offset; 125 uint32_t count; 126 }; 127 struct OnStopRequestParams { 128 nsCOMPtr<nsIRequest> request; 129 nsresult status; 130 }; 131 struct OnAfterLastPartParams { 132 nsresult status; 133 }; 134 using StreamListenerFunction = 135 mozilla::Variant<OnStartRequestParams, OnDataAvailableParams, 136 OnStopRequestParams, OnAfterLastPartParams>; 137 138 nsresult ForwardStreamListenerFunctions(nsTArray<StreamListenerFunction> aCalls, 139 nsIStreamListener* aParent); 140 141 } // namespace net 142 } // namespace mozilla 143 144 #endif // mozilla_net_NeckoCommon_h