ipc/glue/ScopedXREEmbed.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/glue/ScopedXREEmbed.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "ScopedXREEmbed.h"
     1.9 +
    1.10 +#include "base/command_line.h"
    1.11 +#include "base/string_util.h"
    1.12 +
    1.13 +#include "nsIFile.h"
    1.14 +
    1.15 +#include "nsCOMPtr.h"
    1.16 +#include "nsServiceManagerUtils.h"
    1.17 +#include "nsString.h"
    1.18 +#include "nsXULAppAPI.h"
    1.19 +
    1.20 +using mozilla::ipc::ScopedXREEmbed;
    1.21 +
    1.22 +ScopedXREEmbed::ScopedXREEmbed()
    1.23 +: mShouldKillEmbedding(false)
    1.24 +{
    1.25 +  NS_LogInit();
    1.26 +}
    1.27 +
    1.28 +ScopedXREEmbed::~ScopedXREEmbed()
    1.29 +{
    1.30 +  Stop();
    1.31 +  NS_LogTerm();
    1.32 +}
    1.33 +
    1.34 +void
    1.35 +ScopedXREEmbed::SetAppDir(const nsACString& aPath)
    1.36 +{
    1.37 +  bool flag;
    1.38 +  nsresult rv =
    1.39 +    XRE_GetFileFromPath(aPath.BeginReading(), getter_AddRefs(mAppDir));
    1.40 +  if (NS_FAILED(rv) ||
    1.41 +      NS_FAILED(mAppDir->Exists(&flag)) || !flag) {
    1.42 +    NS_WARNING("Invalid application directory passed to content process.");
    1.43 +    mAppDir = nullptr;
    1.44 +  }
    1.45 +}
    1.46 +
    1.47 +void
    1.48 +ScopedXREEmbed::Start()
    1.49 +{
    1.50 +  std::string path;
    1.51 +#if defined(OS_WIN)
    1.52 +  path = WideToUTF8(CommandLine::ForCurrentProcess()->program());
    1.53 +#elif defined(OS_POSIX)
    1.54 +  path = CommandLine::ForCurrentProcess()->argv()[0];
    1.55 +#else
    1.56 +#  error Sorry
    1.57 +#endif
    1.58 +
    1.59 +  nsCOMPtr<nsIFile> localFile;
    1.60 +  nsresult rv = XRE_GetBinaryPath(path.c_str(), getter_AddRefs(localFile));
    1.61 +  if (NS_FAILED(rv))
    1.62 +    return;
    1.63 +
    1.64 +  nsCOMPtr<nsIFile> parent;
    1.65 +  rv = localFile->GetParent(getter_AddRefs(parent));
    1.66 +  if (NS_FAILED(rv))
    1.67 +    return;
    1.68 +
    1.69 +  localFile = do_QueryInterface(parent);
    1.70 +  NS_ENSURE_TRUE_VOID(localFile);
    1.71 +
    1.72 +#ifdef OS_MACOSX
    1.73 +  if (XRE_GetProcessType() == GeckoProcessType_Content) {
    1.74 +    // We're an XPCOM-using subprocess.  Walk out of
    1.75 +    // [subprocess].app/Contents/MacOS to the real GRE dir.
    1.76 +    rv = localFile->GetParent(getter_AddRefs(parent));
    1.77 +    if (NS_FAILED(rv))
    1.78 +      return;
    1.79 +
    1.80 +    localFile = do_QueryInterface(parent);
    1.81 +    NS_ENSURE_TRUE_VOID(localFile);
    1.82 +
    1.83 +    rv = localFile->GetParent(getter_AddRefs(parent));
    1.84 +    if (NS_FAILED(rv))
    1.85 +      return;
    1.86 +
    1.87 +    localFile = do_QueryInterface(parent);
    1.88 +    NS_ENSURE_TRUE_VOID(localFile);
    1.89 +
    1.90 +    rv = localFile->GetParent(getter_AddRefs(parent));
    1.91 +    if (NS_FAILED(rv))
    1.92 +      return;
    1.93 +
    1.94 +    localFile = do_QueryInterface(parent);
    1.95 +    NS_ENSURE_TRUE_VOID(localFile);
    1.96 +  }
    1.97 +#endif
    1.98 +
    1.99 +  if (mAppDir)
   1.100 +    rv = XRE_InitEmbedding2(localFile, mAppDir, nullptr);
   1.101 +  else
   1.102 +    rv = XRE_InitEmbedding2(localFile, localFile, nullptr);
   1.103 +  if (NS_FAILED(rv))
   1.104 +    return;
   1.105 +
   1.106 +  mShouldKillEmbedding = true;
   1.107 +}
   1.108 +
   1.109 +void
   1.110 +ScopedXREEmbed::Stop()
   1.111 +{
   1.112 +  if (mShouldKillEmbedding) {
   1.113 +    XRE_TermEmbedding();
   1.114 +    mShouldKillEmbedding = false;
   1.115 +  }
   1.116 +}

mercurial