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