michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ScopedXREEmbed.h" michael@0: michael@0: #include "base/command_line.h" michael@0: #include "base/string_util.h" michael@0: michael@0: #include "nsIFile.h" michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsString.h" michael@0: #include "nsXULAppAPI.h" michael@0: michael@0: using mozilla::ipc::ScopedXREEmbed; michael@0: michael@0: ScopedXREEmbed::ScopedXREEmbed() michael@0: : mShouldKillEmbedding(false) michael@0: { michael@0: NS_LogInit(); michael@0: } michael@0: michael@0: ScopedXREEmbed::~ScopedXREEmbed() michael@0: { michael@0: Stop(); michael@0: NS_LogTerm(); michael@0: } michael@0: michael@0: void michael@0: ScopedXREEmbed::SetAppDir(const nsACString& aPath) michael@0: { michael@0: bool flag; michael@0: nsresult rv = michael@0: XRE_GetFileFromPath(aPath.BeginReading(), getter_AddRefs(mAppDir)); michael@0: if (NS_FAILED(rv) || michael@0: NS_FAILED(mAppDir->Exists(&flag)) || !flag) { michael@0: NS_WARNING("Invalid application directory passed to content process."); michael@0: mAppDir = nullptr; michael@0: } michael@0: } michael@0: michael@0: void michael@0: ScopedXREEmbed::Start() michael@0: { michael@0: std::string path; michael@0: #if defined(OS_WIN) michael@0: path = WideToUTF8(CommandLine::ForCurrentProcess()->program()); michael@0: #elif defined(OS_POSIX) michael@0: path = CommandLine::ForCurrentProcess()->argv()[0]; michael@0: #else michael@0: # error Sorry michael@0: #endif michael@0: michael@0: nsCOMPtr localFile; michael@0: nsresult rv = XRE_GetBinaryPath(path.c_str(), getter_AddRefs(localFile)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: nsCOMPtr parent; michael@0: rv = localFile->GetParent(getter_AddRefs(parent)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: localFile = do_QueryInterface(parent); michael@0: NS_ENSURE_TRUE_VOID(localFile); michael@0: michael@0: #ifdef OS_MACOSX michael@0: if (XRE_GetProcessType() == GeckoProcessType_Content) { michael@0: // We're an XPCOM-using subprocess. Walk out of michael@0: // [subprocess].app/Contents/MacOS to the real GRE dir. michael@0: rv = localFile->GetParent(getter_AddRefs(parent)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: localFile = do_QueryInterface(parent); michael@0: NS_ENSURE_TRUE_VOID(localFile); michael@0: michael@0: rv = localFile->GetParent(getter_AddRefs(parent)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: localFile = do_QueryInterface(parent); michael@0: NS_ENSURE_TRUE_VOID(localFile); michael@0: michael@0: rv = localFile->GetParent(getter_AddRefs(parent)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: localFile = do_QueryInterface(parent); michael@0: NS_ENSURE_TRUE_VOID(localFile); michael@0: } michael@0: #endif michael@0: michael@0: if (mAppDir) michael@0: rv = XRE_InitEmbedding2(localFile, mAppDir, nullptr); michael@0: else michael@0: rv = XRE_InitEmbedding2(localFile, localFile, nullptr); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: mShouldKillEmbedding = true; michael@0: } michael@0: michael@0: void michael@0: ScopedXREEmbed::Stop() michael@0: { michael@0: if (mShouldKillEmbedding) { michael@0: XRE_TermEmbedding(); michael@0: mShouldKillEmbedding = false; michael@0: } michael@0: }