1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ScopedXREEmbed.h"
9 #include "base/command_line.h"
10 #include "base/string_util.h"
15 #include "nsServiceManagerUtils.h"
17 #include "nsXULAppAPI.h"
19 using mozilla::ipc::ScopedXREEmbed
;
21 ScopedXREEmbed::ScopedXREEmbed() : mShouldKillEmbedding(false) { NS_LogInit(); }
23 ScopedXREEmbed::~ScopedXREEmbed() {
28 void ScopedXREEmbed::SetAppDir(const nsACString
& aPath
) {
31 XRE_GetFileFromPath(aPath
.BeginReading(), getter_AddRefs(mAppDir
));
32 if (NS_FAILED(rv
) || NS_FAILED(mAppDir
->Exists(&flag
)) || !flag
) {
33 NS_WARNING("Invalid application directory passed to content process.");
38 void ScopedXREEmbed::Start() {
39 nsCOMPtr
<nsIFile
> localFile
;
40 nsresult rv
= XRE_GetBinaryPath(getter_AddRefs(localFile
));
41 if (NS_FAILED(rv
)) return;
43 nsCOMPtr
<nsIFile
> parent
;
44 rv
= localFile
->GetParent(getter_AddRefs(parent
));
45 if (NS_FAILED(rv
)) return;
48 NS_ENSURE_TRUE_VOID(localFile
);
51 if (XRE_IsContentProcess()) {
52 // We're an XPCOM-using subprocess. Walk out of
53 // [subprocess].app/Contents/MacOS to the real GRE dir.
54 rv
= localFile
->GetParent(getter_AddRefs(parent
));
55 if (NS_FAILED(rv
)) return;
58 NS_ENSURE_TRUE_VOID(localFile
);
60 rv
= localFile
->GetParent(getter_AddRefs(parent
));
61 if (NS_FAILED(rv
)) return;
64 NS_ENSURE_TRUE_VOID(localFile
);
66 rv
= localFile
->GetParent(getter_AddRefs(parent
));
67 if (NS_FAILED(rv
)) return;
70 NS_ENSURE_TRUE_VOID(localFile
);
72 rv
= localFile
->SetNativeLeafName("Resources"_ns
);
80 rv
= XRE_InitEmbedding2(localFile
, mAppDir
, nullptr);
82 rv
= XRE_InitEmbedding2(localFile
, localFile
, nullptr);
83 if (NS_FAILED(rv
)) return;
85 mShouldKillEmbedding
= true;
88 void ScopedXREEmbed::Stop() {
89 if (mShouldKillEmbedding
) {
91 mShouldKillEmbedding
= false;