netwerk/ipc/NeckoCommon.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/ipc/NeckoCommon.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,140 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set sw=2 ts=8 et tw=80 : */
     1.6 +
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#ifndef mozilla_net_NeckoCommon_h
    1.12 +#define mozilla_net_NeckoCommon_h
    1.13 +
    1.14 +#include "nsXULAppAPI.h"
    1.15 +#include "prenv.h"
    1.16 +#include "nsPrintfCString.h"
    1.17 +#include "mozilla/Preferences.h"
    1.18 +
    1.19 +namespace mozilla { namespace dom {
    1.20 +class TabChild;
    1.21 +}}
    1.22 +
    1.23 +#if defined(DEBUG) || defined(ENABLE_TESTS)
    1.24 +# define NECKO_ERRORS_ARE_FATAL_DEFAULT true
    1.25 +#else
    1.26 +# define NECKO_ERRORS_ARE_FATAL_DEFAULT false
    1.27 +#endif 
    1.28 +
    1.29 +// TODO: Eventually remove NECKO_MAYBE_ABORT and DROP_DEAD (bug 575494).
    1.30 +// Still useful for catching listener interfaces we don't yet support across
    1.31 +// processes, etc.
    1.32 +
    1.33 +#define NECKO_MAYBE_ABORT(msg)                                                 \
    1.34 +  do {                                                                         \
    1.35 +    bool abort = NECKO_ERRORS_ARE_FATAL_DEFAULT;                               \
    1.36 +    const char *e = PR_GetEnv("NECKO_ERRORS_ARE_FATAL");                       \
    1.37 +    if (e)                                                                     \
    1.38 +      abort = (*e == '0') ? false : true;                                      \
    1.39 +    if (abort) {                                                               \
    1.40 +      msg.Append(" (set NECKO_ERRORS_ARE_FATAL=0 in your environment to "      \
    1.41 +                      "convert this error into a warning.)");                  \
    1.42 +      NS_RUNTIMEABORT(msg.get());                                              \
    1.43 +    } else {                                                                   \
    1.44 +      msg.Append(" (set NECKO_ERRORS_ARE_FATAL=1 in your environment to "      \
    1.45 +                      "convert this warning into a fatal error.)");            \
    1.46 +      NS_WARNING(msg.get());                                                   \
    1.47 +    }                                                                          \
    1.48 +  } while (0)
    1.49 +
    1.50 +#define DROP_DEAD()                                                            \
    1.51 +  do {                                                                         \
    1.52 +    nsPrintfCString msg("NECKO ERROR: '%s' UNIMPLEMENTED",                     \
    1.53 +                        __FUNCTION__);                                         \
    1.54 +    NECKO_MAYBE_ABORT(msg);                                                    \
    1.55 +    return NS_ERROR_NOT_IMPLEMENTED;                                           \
    1.56 +  } while (0)
    1.57 +
    1.58 +#define ENSURE_CALLED_BEFORE_ASYNC_OPEN()                                      \
    1.59 +  do {                                                                         \
    1.60 +    if (mIsPending || mWasOpened) {                                            \
    1.61 +      nsPrintfCString msg("'%s' called after AsyncOpen: %s +%d",               \
    1.62 +                          __FUNCTION__, __FILE__, __LINE__);                   \
    1.63 +      NECKO_MAYBE_ABORT(msg);                                                  \
    1.64 +    }                                                                          \
    1.65 +    NS_ENSURE_TRUE(!mIsPending, NS_ERROR_IN_PROGRESS);                         \
    1.66 +    NS_ENSURE_TRUE(!mWasOpened, NS_ERROR_ALREADY_OPENED);                      \
    1.67 +  } while (0)
    1.68 +
    1.69 +// Fails call if made after request observers (on-modify-request, etc) have been
    1.70 +// called
    1.71 +
    1.72 +#define ENSURE_CALLED_BEFORE_CONNECT()                                         \
    1.73 +  do {                                                                         \
    1.74 +    if (mRequestObserversCalled) {                                             \
    1.75 +      nsPrintfCString msg("'%s' called too late: %s +%d",                      \
    1.76 +                          __FUNCTION__, __FILE__, __LINE__);                   \
    1.77 +      NECKO_MAYBE_ABORT(msg);                                                  \
    1.78 +      if (mIsPending)                                                          \
    1.79 +        return NS_ERROR_IN_PROGRESS;                                           \
    1.80 +      MOZ_ASSERT(mWasOpened);                                                  \
    1.81 +      return NS_ERROR_ALREADY_OPENED;                                          \
    1.82 +    }                                                                          \
    1.83 +  } while (0)
    1.84 +
    1.85 +namespace mozilla {
    1.86 +namespace net {
    1.87 +
    1.88 +inline bool 
    1.89 +IsNeckoChild() 
    1.90 +{
    1.91 +  static bool didCheck = false;
    1.92 +  static bool amChild = false;
    1.93 +
    1.94 +  if (!didCheck) {
    1.95 +    // This allows independent necko-stacks (instead of single stack in chrome)
    1.96 +    // to still be run.  
    1.97 +    // TODO: Remove eventually when no longer supported (bug 571126)
    1.98 +    const char * e = PR_GetEnv("NECKO_SEPARATE_STACKS");
    1.99 +    if (!e) 
   1.100 +      amChild = (XRE_GetProcessType() == GeckoProcessType_Content);
   1.101 +    didCheck = true;
   1.102 +  }
   1.103 +  return amChild;
   1.104 +}
   1.105 +
   1.106 +namespace NeckoCommonInternal {
   1.107 +  extern bool gSecurityDisabled;
   1.108 +  extern bool gRegisteredBool;
   1.109 +}
   1.110 +
   1.111 +// This should always return true unless xpcshell tests are being used
   1.112 +inline bool
   1.113 +UsingNeckoIPCSecurity()
   1.114 +{
   1.115 +
   1.116 +  if (!NeckoCommonInternal::gRegisteredBool) {
   1.117 +    Preferences::AddBoolVarCache(&NeckoCommonInternal::gSecurityDisabled,
   1.118 +                                 "network.disable.ipc.security");
   1.119 +    NeckoCommonInternal::gRegisteredBool = true;
   1.120 +  }
   1.121 +  return !NeckoCommonInternal::gSecurityDisabled;
   1.122 +}
   1.123 +
   1.124 +inline bool
   1.125 +MissingRequiredTabChild(mozilla::dom::TabChild* tabChild,
   1.126 +                        const char* context)
   1.127 +{
   1.128 +  if (UsingNeckoIPCSecurity()) {
   1.129 +    if (!tabChild) {
   1.130 +      printf_stderr("WARNING: child tried to open %s IPDL channel w/o "
   1.131 +                    "security info\n", context);
   1.132 +      return true;
   1.133 +    }
   1.134 +  }
   1.135 +  return false;
   1.136 +}
   1.137 +
   1.138 +
   1.139 +} // namespace net
   1.140 +} // namespace mozilla
   1.141 +
   1.142 +#endif // mozilla_net_NeckoCommon_h
   1.143 +

mercurial