2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-06-29 11:38:29 -07:00
|
|
|
|
|
|
|
#include "ScopedXREEmbed.h"
|
|
|
|
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/string_util.h"
|
|
|
|
|
|
|
|
#include "nsIFile.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
using mozilla::ipc::ScopedXREEmbed;
|
|
|
|
|
|
|
|
ScopedXREEmbed::ScopedXREEmbed()
|
|
|
|
: mShouldKillEmbedding(false)
|
|
|
|
{
|
2009-08-12 11:31:48 -07:00
|
|
|
NS_LogInit();
|
2009-06-29 11:38:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedXREEmbed::~ScopedXREEmbed()
|
|
|
|
{
|
|
|
|
Stop();
|
2009-08-12 11:31:48 -07:00
|
|
|
NS_LogTerm();
|
2009-06-29 11:38:29 -07:00
|
|
|
}
|
|
|
|
|
2012-12-18 08:24:42 -08:00
|
|
|
void
|
|
|
|
ScopedXREEmbed::SetAppDir(const nsACString& aPath)
|
|
|
|
{
|
|
|
|
bool flag;
|
|
|
|
nsresult rv =
|
|
|
|
XRE_GetFileFromPath(aPath.BeginReading(), getter_AddRefs(mAppDir));
|
|
|
|
if (NS_FAILED(rv) ||
|
|
|
|
NS_FAILED(mAppDir->Exists(&flag)) || !flag) {
|
|
|
|
NS_WARNING("Invalid application directory passed to content process.");
|
|
|
|
mAppDir = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 11:38:29 -07:00
|
|
|
void
|
|
|
|
ScopedXREEmbed::Start()
|
|
|
|
{
|
2009-11-05 16:24:27 -08:00
|
|
|
std::string path;
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
path = WideToUTF8(CommandLine::ForCurrentProcess()->program());
|
|
|
|
#elif defined(OS_POSIX)
|
|
|
|
path = CommandLine::ForCurrentProcess()->argv()[0];
|
|
|
|
#else
|
|
|
|
# error Sorry
|
|
|
|
#endif
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2009-06-29 11:38:29 -07:00
|
|
|
nsresult rv = XRE_GetBinaryPath(path.c_str(), getter_AddRefs(localFile));
|
2010-02-22 09:41:21 -08:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2009-06-29 11:38:29 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
rv = localFile->GetParent(getter_AddRefs(parent));
|
2010-02-22 09:41:21 -08:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2009-06-29 11:38:29 -07:00
|
|
|
|
|
|
|
localFile = do_QueryInterface(parent);
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_TRUE_VOID(localFile);
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2010-09-23 16:28:48 -07:00
|
|
|
#ifdef OS_MACOSX
|
2011-02-01 11:53:53 -08:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
// We're an XPCOM-using subprocess. Walk out of
|
|
|
|
// [subprocess].app/Contents/MacOS to the real GRE dir.
|
|
|
|
rv = localFile->GetParent(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2010-09-23 16:28:48 -07:00
|
|
|
|
2011-02-01 11:53:53 -08:00
|
|
|
localFile = do_QueryInterface(parent);
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_TRUE_VOID(localFile);
|
2010-09-23 16:28:48 -07:00
|
|
|
|
2011-02-01 11:53:53 -08:00
|
|
|
rv = localFile->GetParent(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2010-09-23 16:28:48 -07:00
|
|
|
|
2011-02-01 11:53:53 -08:00
|
|
|
localFile = do_QueryInterface(parent);
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_TRUE_VOID(localFile);
|
2010-09-23 16:28:48 -07:00
|
|
|
|
2011-02-01 11:53:53 -08:00
|
|
|
rv = localFile->GetParent(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2010-09-23 16:28:48 -07:00
|
|
|
|
2011-02-01 11:53:53 -08:00
|
|
|
localFile = do_QueryInterface(parent);
|
2012-09-14 03:00:31 -07:00
|
|
|
NS_ENSURE_TRUE_VOID(localFile);
|
2011-02-01 11:53:53 -08:00
|
|
|
}
|
2010-09-23 16:28:48 -07:00
|
|
|
#endif
|
|
|
|
|
2012-12-18 08:24:42 -08:00
|
|
|
if (mAppDir)
|
|
|
|
rv = XRE_InitEmbedding2(localFile, mAppDir, nullptr);
|
|
|
|
else
|
|
|
|
rv = XRE_InitEmbedding2(localFile, localFile, nullptr);
|
2010-02-22 09:41:21 -08:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2009-06-29 11:38:29 -07:00
|
|
|
|
|
|
|
mShouldKillEmbedding = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScopedXREEmbed::Stop()
|
|
|
|
{
|
|
|
|
if (mShouldKillEmbedding) {
|
|
|
|
XRE_TermEmbedding();
|
|
|
|
mShouldKillEmbedding = false;
|
|
|
|
}
|
|
|
|
}
|