Bug 1578501 [wpt PR 18803] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / ipc / glue / ScopedXREEmbed.cpp
blob24c0132f3594d5d1088e9db040de27ae5f4366f2
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"
12 #include "nsIFile.h"
14 #include "nsCOMPtr.h"
15 #include "nsServiceManagerUtils.h"
16 #include "nsString.h"
17 #include "nsXULAppAPI.h"
19 using mozilla::ipc::ScopedXREEmbed;
21 ScopedXREEmbed::ScopedXREEmbed() : mShouldKillEmbedding(false) { NS_LogInit(); }
23 ScopedXREEmbed::~ScopedXREEmbed() {
24 Stop();
25 NS_LogTerm();
28 void ScopedXREEmbed::SetAppDir(const nsACString& aPath) {
29 bool flag;
30 nsresult rv =
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.");
34 mAppDir = nullptr;
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;
47 localFile = parent;
48 NS_ENSURE_TRUE_VOID(localFile);
50 #ifdef OS_MACOSX
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;
57 localFile = parent;
58 NS_ENSURE_TRUE_VOID(localFile);
60 rv = localFile->GetParent(getter_AddRefs(parent));
61 if (NS_FAILED(rv)) return;
63 localFile = parent;
64 NS_ENSURE_TRUE_VOID(localFile);
66 rv = localFile->GetParent(getter_AddRefs(parent));
67 if (NS_FAILED(rv)) return;
69 localFile = parent;
70 NS_ENSURE_TRUE_VOID(localFile);
72 rv = localFile->SetNativeLeafName(NS_LITERAL_CSTRING("Resources"));
73 if (NS_FAILED(rv)) {
74 return;
77 #endif
79 if (mAppDir)
80 rv = XRE_InitEmbedding2(localFile, mAppDir, nullptr);
81 else
82 rv = XRE_InitEmbedding2(localFile, localFile, nullptr);
83 if (NS_FAILED(rv)) return;
85 mShouldKillEmbedding = true;
88 void ScopedXREEmbed::Stop() {
89 if (mShouldKillEmbedding) {
90 XRE_TermEmbedding();
91 mShouldKillEmbedding = false;