NeckoCommon.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_net_NeckoCommon_h
  6. #define mozilla_net_NeckoCommon_h
  7. #include "nsXULAppAPI.h"
  8. #include "prenv.h"
  9. #include "nsPrintfCString.h"
  10. #include "mozilla/Preferences.h"
  11. namespace mozilla { namespace dom {
  12. class TabChild;
  13. } // namespace dom
  14. } // namespace mozilla
  15. #if defined(DEBUG)
  16. # define NECKO_ERRORS_ARE_FATAL_DEFAULT true
  17. #else
  18. # define NECKO_ERRORS_ARE_FATAL_DEFAULT false
  19. #endif
  20. // TODO: Eventually remove NECKO_MAYBE_ABORT and DROP_DEAD (bug 575494).
  21. // Still useful for catching listener interfaces we don't yet support across
  22. // processes, etc.
  23. #define NECKO_MAYBE_ABORT(msg) \
  24. do { \
  25. bool abort = NECKO_ERRORS_ARE_FATAL_DEFAULT; \
  26. const char *e = PR_GetEnv("NECKO_ERRORS_ARE_FATAL"); \
  27. if (e) \
  28. abort = (*e == '0') ? false : true; \
  29. if (abort) { \
  30. msg.Append(" (set NECKO_ERRORS_ARE_FATAL=0 in your environment to " \
  31. "convert this error into a warning.)"); \
  32. NS_RUNTIMEABORT(msg.get()); \
  33. } else { \
  34. msg.Append(" (set NECKO_ERRORS_ARE_FATAL=1 in your environment to " \
  35. "convert this warning into a fatal error.)"); \
  36. NS_WARNING(msg.get()); \
  37. } \
  38. } while (0)
  39. #define DROP_DEAD() \
  40. do { \
  41. nsPrintfCString msg("NECKO ERROR: '%s' UNIMPLEMENTED", \
  42. __FUNCTION__); \
  43. NECKO_MAYBE_ABORT(msg); \
  44. return NS_ERROR_NOT_IMPLEMENTED; \
  45. } while (0)
  46. #define ENSURE_CALLED_BEFORE_ASYNC_OPEN() \
  47. do { \
  48. if (mIsPending || mWasOpened) { \
  49. nsPrintfCString msg("'%s' called after AsyncOpen: %s +%d", \
  50. __FUNCTION__, __FILE__, __LINE__); \
  51. NECKO_MAYBE_ABORT(msg); \
  52. } \
  53. NS_ENSURE_TRUE(!mIsPending, NS_ERROR_IN_PROGRESS); \
  54. NS_ENSURE_TRUE(!mWasOpened, NS_ERROR_ALREADY_OPENED); \
  55. } while (0)
  56. // Fails call if made after request observers (on-modify-request, etc) have been
  57. // called
  58. #define ENSURE_CALLED_BEFORE_CONNECT() \
  59. do { \
  60. if (mRequestObserversCalled) { \
  61. nsPrintfCString msg("'%s' called too late: %s +%d", \
  62. __FUNCTION__, __FILE__, __LINE__); \
  63. NECKO_MAYBE_ABORT(msg); \
  64. if (mIsPending) \
  65. return NS_ERROR_IN_PROGRESS; \
  66. MOZ_ASSERT(mWasOpened); \
  67. return NS_ERROR_ALREADY_OPENED; \
  68. } \
  69. } while (0)
  70. namespace mozilla {
  71. namespace net {
  72. inline bool
  73. IsNeckoChild()
  74. {
  75. static bool didCheck = false;
  76. static bool amChild = false;
  77. if (!didCheck) {
  78. didCheck = true;
  79. amChild = (XRE_GetProcessType() == GeckoProcessType_Content);
  80. }
  81. return amChild;
  82. }
  83. namespace NeckoCommonInternal {
  84. extern bool gSecurityDisabled;
  85. extern bool gRegisteredBool;
  86. } // namespace NeckoCommonInternal
  87. // This should always return true unless xpcshell tests are being used
  88. inline bool
  89. UsingNeckoIPCSecurity()
  90. {
  91. return !NeckoCommonInternal::gSecurityDisabled;
  92. }
  93. inline bool
  94. MissingRequiredTabChild(mozilla::dom::TabChild* tabChild,
  95. const char* context)
  96. {
  97. if (UsingNeckoIPCSecurity()) {
  98. if (!tabChild) {
  99. printf_stderr("WARNING: child tried to open %s IPDL channel w/o "
  100. "security info\n", context);
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. } // namespace net
  107. } // namespace mozilla
  108. #endif // mozilla_net_NeckoCommon_h