Bumping manifests a=b2g-bump
[gecko.git] / ipc / glue / ScopedXREEmbed.cpp
blob342decf42b3462dd6eb7e507252462f2bd3e921a
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"
10 #include "nsIFile.h"
12 #include "nsCOMPtr.h"
13 #include "nsServiceManagerUtils.h"
14 #include "nsString.h"
15 #include "nsXULAppAPI.h"
17 using mozilla::ipc::ScopedXREEmbed;
19 ScopedXREEmbed::ScopedXREEmbed()
20 : mShouldKillEmbedding(false)
22 NS_LogInit();
25 ScopedXREEmbed::~ScopedXREEmbed()
27 Stop();
28 NS_LogTerm();
31 void
32 ScopedXREEmbed::SetAppDir(const nsACString& aPath)
34 bool flag;
35 nsresult rv =
36 XRE_GetFileFromPath(aPath.BeginReading(), getter_AddRefs(mAppDir));
37 if (NS_FAILED(rv) ||
38 NS_FAILED(mAppDir->Exists(&flag)) || !flag) {
39 NS_WARNING("Invalid application directory passed to content process.");
40 mAppDir = nullptr;
44 void
45 ScopedXREEmbed::Start()
47 std::string path;
48 #if defined(OS_WIN)
49 path = WideToUTF8(CommandLine::ForCurrentProcess()->program());
50 #elif defined(OS_POSIX)
51 path = CommandLine::ForCurrentProcess()->argv()[0];
52 #else
53 # error Sorry
54 #endif
56 nsCOMPtr<nsIFile> localFile;
57 nsresult rv = XRE_GetBinaryPath(path.c_str(), getter_AddRefs(localFile));
58 if (NS_FAILED(rv))
59 return;
61 nsCOMPtr<nsIFile> parent;
62 rv = localFile->GetParent(getter_AddRefs(parent));
63 if (NS_FAILED(rv))
64 return;
66 localFile = do_QueryInterface(parent);
67 NS_ENSURE_TRUE_VOID(localFile);
69 #ifdef OS_MACOSX
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));
74 if (NS_FAILED(rv))
75 return;
77 localFile = do_QueryInterface(parent);
78 NS_ENSURE_TRUE_VOID(localFile);
80 rv = localFile->GetParent(getter_AddRefs(parent));
81 if (NS_FAILED(rv))
82 return;
84 localFile = do_QueryInterface(parent);
85 NS_ENSURE_TRUE_VOID(localFile);
87 rv = localFile->GetParent(getter_AddRefs(parent));
88 if (NS_FAILED(rv))
89 return;
91 localFile = do_QueryInterface(parent);
92 NS_ENSURE_TRUE_VOID(localFile);
94 rv = localFile->SetNativeLeafName(NS_LITERAL_CSTRING("Resources"));
95 if (NS_FAILED(rv)) {
96 return;
99 #endif
101 if (mAppDir)
102 rv = XRE_InitEmbedding2(localFile, mAppDir, nullptr);
103 else
104 rv = XRE_InitEmbedding2(localFile, localFile, nullptr);
105 if (NS_FAILED(rv))
106 return;
108 mShouldKillEmbedding = true;
111 void
112 ScopedXREEmbed::Stop()
114 if (mShouldKillEmbedding) {
115 XRE_TermEmbedding();
116 mShouldKillEmbedding = false;