2012-05-21 11:12:37 +00: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 18:38:29 +00:00
|
|
|
|
|
|
|
#include "ScopedXREEmbed.h"
|
|
|
|
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/string_util.h"
|
|
|
|
|
|
|
|
#include "nsIFile.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-23 17:25:00 +00:00
|
|
|
#include "nsString.h"
|
2009-06-29 18:38:29 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
using mozilla::ipc::ScopedXREEmbed;
|
|
|
|
|
|
|
|
ScopedXREEmbed::ScopedXREEmbed()
|
|
|
|
: mShouldKillEmbedding(false)
|
|
|
|
{
|
2009-08-12 18:31:48 +00:00
|
|
|
NS_LogInit();
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedXREEmbed::~ScopedXREEmbed()
|
|
|
|
{
|
|
|
|
Stop();
|
2009-08-12 18:31:48 +00:00
|
|
|
NS_LogTerm();
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 16:24:42 +00: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 18:38:29 +00:00
|
|
|
void
|
|
|
|
ScopedXREEmbed::Start()
|
|
|
|
{
|
2009-11-06 00:24:27 +00: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 18:38:29 +00:00
|
|
|
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2009-06-29 18:38:29 +00:00
|
|
|
nsresult rv = XRE_GetBinaryPath(path.c_str(), getter_AddRefs(localFile));
|
2010-02-22 17:41:21 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
rv = localFile->GetParent(getter_AddRefs(parent));
|
2010-02-22 17:41:21 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
localFile = do_QueryInterface(parent);
|
2012-09-14 10:00:31 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(localFile);
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2010-09-23 23:28:48 +00:00
|
|
|
#ifdef OS_MACOSX
|
2011-02-01 19:53:53 +00: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 23:28:48 +00:00
|
|
|
|
2011-02-01 19:53:53 +00:00
|
|
|
localFile = do_QueryInterface(parent);
|
2012-09-14 10:00:31 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(localFile);
|
2010-09-23 23:28:48 +00:00
|
|
|
|
2011-02-01 19:53:53 +00:00
|
|
|
rv = localFile->GetParent(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2010-09-23 23:28:48 +00:00
|
|
|
|
2011-02-01 19:53:53 +00:00
|
|
|
localFile = do_QueryInterface(parent);
|
2012-09-14 10:00:31 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(localFile);
|
2010-09-23 23:28:48 +00:00
|
|
|
|
2011-02-01 19:53:53 +00:00
|
|
|
rv = localFile->GetParent(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2010-09-23 23:28:48 +00:00
|
|
|
|
2011-02-01 19:53:53 +00:00
|
|
|
localFile = do_QueryInterface(parent);
|
2012-09-14 10:00:31 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(localFile);
|
2011-02-01 19:53:53 +00:00
|
|
|
}
|
2010-09-23 23:28:48 +00:00
|
|
|
#endif
|
|
|
|
|
2012-12-18 16:24:42 +00:00
|
|
|
if (mAppDir)
|
|
|
|
rv = XRE_InitEmbedding2(localFile, mAppDir, nullptr);
|
|
|
|
else
|
|
|
|
rv = XRE_InitEmbedding2(localFile, localFile, nullptr);
|
2010-02-22 17:41:21 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
mShouldKillEmbedding = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScopedXREEmbed::Stop()
|
|
|
|
{
|
|
|
|
if (mShouldKillEmbedding) {
|
|
|
|
XRE_TermEmbedding();
|
|
|
|
mShouldKillEmbedding = false;
|
|
|
|
}
|
|
|
|
}
|