michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set sw=2 ts=8 et tw=80 : */ michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_net_NeckoCommon_h michael@0: #define mozilla_net_NeckoCommon_h michael@0: michael@0: #include "nsXULAppAPI.h" michael@0: #include "prenv.h" michael@0: #include "nsPrintfCString.h" michael@0: #include "mozilla/Preferences.h" michael@0: michael@0: namespace mozilla { namespace dom { michael@0: class TabChild; michael@0: }} michael@0: michael@0: #if defined(DEBUG) || defined(ENABLE_TESTS) michael@0: # define NECKO_ERRORS_ARE_FATAL_DEFAULT true michael@0: #else michael@0: # define NECKO_ERRORS_ARE_FATAL_DEFAULT false michael@0: #endif michael@0: michael@0: // TODO: Eventually remove NECKO_MAYBE_ABORT and DROP_DEAD (bug 575494). michael@0: // Still useful for catching listener interfaces we don't yet support across michael@0: // processes, etc. michael@0: michael@0: #define NECKO_MAYBE_ABORT(msg) \ michael@0: do { \ michael@0: bool abort = NECKO_ERRORS_ARE_FATAL_DEFAULT; \ michael@0: const char *e = PR_GetEnv("NECKO_ERRORS_ARE_FATAL"); \ michael@0: if (e) \ michael@0: abort = (*e == '0') ? false : true; \ michael@0: if (abort) { \ michael@0: msg.Append(" (set NECKO_ERRORS_ARE_FATAL=0 in your environment to " \ michael@0: "convert this error into a warning.)"); \ michael@0: NS_RUNTIMEABORT(msg.get()); \ michael@0: } else { \ michael@0: msg.Append(" (set NECKO_ERRORS_ARE_FATAL=1 in your environment to " \ michael@0: "convert this warning into a fatal error.)"); \ michael@0: NS_WARNING(msg.get()); \ michael@0: } \ michael@0: } while (0) michael@0: michael@0: #define DROP_DEAD() \ michael@0: do { \ michael@0: nsPrintfCString msg("NECKO ERROR: '%s' UNIMPLEMENTED", \ michael@0: __FUNCTION__); \ michael@0: NECKO_MAYBE_ABORT(msg); \ michael@0: return NS_ERROR_NOT_IMPLEMENTED; \ michael@0: } while (0) michael@0: michael@0: #define ENSURE_CALLED_BEFORE_ASYNC_OPEN() \ michael@0: do { \ michael@0: if (mIsPending || mWasOpened) { \ michael@0: nsPrintfCString msg("'%s' called after AsyncOpen: %s +%d", \ michael@0: __FUNCTION__, __FILE__, __LINE__); \ michael@0: NECKO_MAYBE_ABORT(msg); \ michael@0: } \ michael@0: NS_ENSURE_TRUE(!mIsPending, NS_ERROR_IN_PROGRESS); \ michael@0: NS_ENSURE_TRUE(!mWasOpened, NS_ERROR_ALREADY_OPENED); \ michael@0: } while (0) michael@0: michael@0: // Fails call if made after request observers (on-modify-request, etc) have been michael@0: // called michael@0: michael@0: #define ENSURE_CALLED_BEFORE_CONNECT() \ michael@0: do { \ michael@0: if (mRequestObserversCalled) { \ michael@0: nsPrintfCString msg("'%s' called too late: %s +%d", \ michael@0: __FUNCTION__, __FILE__, __LINE__); \ michael@0: NECKO_MAYBE_ABORT(msg); \ michael@0: if (mIsPending) \ michael@0: return NS_ERROR_IN_PROGRESS; \ michael@0: MOZ_ASSERT(mWasOpened); \ michael@0: return NS_ERROR_ALREADY_OPENED; \ michael@0: } \ michael@0: } while (0) michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: inline bool michael@0: IsNeckoChild() michael@0: { michael@0: static bool didCheck = false; michael@0: static bool amChild = false; michael@0: michael@0: if (!didCheck) { michael@0: // This allows independent necko-stacks (instead of single stack in chrome) michael@0: // to still be run. michael@0: // TODO: Remove eventually when no longer supported (bug 571126) michael@0: const char * e = PR_GetEnv("NECKO_SEPARATE_STACKS"); michael@0: if (!e) michael@0: amChild = (XRE_GetProcessType() == GeckoProcessType_Content); michael@0: didCheck = true; michael@0: } michael@0: return amChild; michael@0: } michael@0: michael@0: namespace NeckoCommonInternal { michael@0: extern bool gSecurityDisabled; michael@0: extern bool gRegisteredBool; michael@0: } michael@0: michael@0: // This should always return true unless xpcshell tests are being used michael@0: inline bool michael@0: UsingNeckoIPCSecurity() michael@0: { michael@0: michael@0: if (!NeckoCommonInternal::gRegisteredBool) { michael@0: Preferences::AddBoolVarCache(&NeckoCommonInternal::gSecurityDisabled, michael@0: "network.disable.ipc.security"); michael@0: NeckoCommonInternal::gRegisteredBool = true; michael@0: } michael@0: return !NeckoCommonInternal::gSecurityDisabled; michael@0: } michael@0: michael@0: inline bool michael@0: MissingRequiredTabChild(mozilla::dom::TabChild* tabChild, michael@0: const char* context) michael@0: { michael@0: if (UsingNeckoIPCSecurity()) { michael@0: if (!tabChild) { michael@0: printf_stderr("WARNING: child tried to open %s IPDL channel w/o " michael@0: "security info\n", context); michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: michael@0: } // namespace net michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_net_NeckoCommon_h michael@0: