1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "ScopedXREEmbed.h"
7 #include "base/command_line.h"
8 #include "base/string_util.h"
13 #include "nsServiceManagerUtils.h"
15 #include "nsXULAppAPI.h"
17 using mozilla::ipc::ScopedXREEmbed
;
19 ScopedXREEmbed::ScopedXREEmbed()
20 : mShouldKillEmbedding(false)
25 ScopedXREEmbed::~ScopedXREEmbed()
32 ScopedXREEmbed::SetAppDir(const nsACString
& aPath
)
36 XRE_GetFileFromPath(aPath
.BeginReading(), getter_AddRefs(mAppDir
));
38 NS_FAILED(mAppDir
->Exists(&flag
)) || !flag
) {
39 NS_WARNING("Invalid application directory passed to content process.");
45 ScopedXREEmbed::Start()
49 path
= WideToUTF8(CommandLine::ForCurrentProcess()->program());
50 #elif defined(OS_POSIX)
51 path
= CommandLine::ForCurrentProcess()->argv()[0];
56 nsCOMPtr
<nsIFile
> localFile
;
57 nsresult rv
= XRE_GetBinaryPath(path
.c_str(), getter_AddRefs(localFile
));
61 nsCOMPtr
<nsIFile
> parent
;
62 rv
= localFile
->GetParent(getter_AddRefs(parent
));
66 localFile
= do_QueryInterface(parent
);
67 NS_ENSURE_TRUE_VOID(localFile
);
70 if (XRE_GetProcessType() == GeckoProcessType_Content
) {
71 // We're an XPCOM-using subprocess. Walk out of
72 // [subprocess].app/Contents/MacOS to the real GRE dir.
73 rv
= localFile
->GetParent(getter_AddRefs(parent
));
77 localFile
= do_QueryInterface(parent
);
78 NS_ENSURE_TRUE_VOID(localFile
);
80 rv
= localFile
->GetParent(getter_AddRefs(parent
));
84 localFile
= do_QueryInterface(parent
);
85 NS_ENSURE_TRUE_VOID(localFile
);
87 rv
= localFile
->GetParent(getter_AddRefs(parent
));
91 localFile
= do_QueryInterface(parent
);
92 NS_ENSURE_TRUE_VOID(localFile
);
94 rv
= localFile
->SetNativeLeafName(NS_LITERAL_CSTRING("Resources"));
102 rv
= XRE_InitEmbedding2(localFile
, mAppDir
, nullptr);
104 rv
= XRE_InitEmbedding2(localFile
, localFile
, nullptr);
108 mShouldKillEmbedding
= true;
112 ScopedXREEmbed::Stop()
114 if (mShouldKillEmbedding
) {
116 mShouldKillEmbedding
= false;