|
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/. */ |
|
4 |
|
5 #include "ScopedXREEmbed.h" |
|
6 |
|
7 #include "base/command_line.h" |
|
8 #include "base/string_util.h" |
|
9 |
|
10 #include "nsIFile.h" |
|
11 |
|
12 #include "nsCOMPtr.h" |
|
13 #include "nsServiceManagerUtils.h" |
|
14 #include "nsString.h" |
|
15 #include "nsXULAppAPI.h" |
|
16 |
|
17 using mozilla::ipc::ScopedXREEmbed; |
|
18 |
|
19 ScopedXREEmbed::ScopedXREEmbed() |
|
20 : mShouldKillEmbedding(false) |
|
21 { |
|
22 NS_LogInit(); |
|
23 } |
|
24 |
|
25 ScopedXREEmbed::~ScopedXREEmbed() |
|
26 { |
|
27 Stop(); |
|
28 NS_LogTerm(); |
|
29 } |
|
30 |
|
31 void |
|
32 ScopedXREEmbed::SetAppDir(const nsACString& aPath) |
|
33 { |
|
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; |
|
41 } |
|
42 } |
|
43 |
|
44 void |
|
45 ScopedXREEmbed::Start() |
|
46 { |
|
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 |
|
55 |
|
56 nsCOMPtr<nsIFile> localFile; |
|
57 nsresult rv = XRE_GetBinaryPath(path.c_str(), getter_AddRefs(localFile)); |
|
58 if (NS_FAILED(rv)) |
|
59 return; |
|
60 |
|
61 nsCOMPtr<nsIFile> parent; |
|
62 rv = localFile->GetParent(getter_AddRefs(parent)); |
|
63 if (NS_FAILED(rv)) |
|
64 return; |
|
65 |
|
66 localFile = do_QueryInterface(parent); |
|
67 NS_ENSURE_TRUE_VOID(localFile); |
|
68 |
|
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; |
|
76 |
|
77 localFile = do_QueryInterface(parent); |
|
78 NS_ENSURE_TRUE_VOID(localFile); |
|
79 |
|
80 rv = localFile->GetParent(getter_AddRefs(parent)); |
|
81 if (NS_FAILED(rv)) |
|
82 return; |
|
83 |
|
84 localFile = do_QueryInterface(parent); |
|
85 NS_ENSURE_TRUE_VOID(localFile); |
|
86 |
|
87 rv = localFile->GetParent(getter_AddRefs(parent)); |
|
88 if (NS_FAILED(rv)) |
|
89 return; |
|
90 |
|
91 localFile = do_QueryInterface(parent); |
|
92 NS_ENSURE_TRUE_VOID(localFile); |
|
93 } |
|
94 #endif |
|
95 |
|
96 if (mAppDir) |
|
97 rv = XRE_InitEmbedding2(localFile, mAppDir, nullptr); |
|
98 else |
|
99 rv = XRE_InitEmbedding2(localFile, localFile, nullptr); |
|
100 if (NS_FAILED(rv)) |
|
101 return; |
|
102 |
|
103 mShouldKillEmbedding = true; |
|
104 } |
|
105 |
|
106 void |
|
107 ScopedXREEmbed::Stop() |
|
108 { |
|
109 if (mShouldKillEmbedding) { |
|
110 XRE_TermEmbedding(); |
|
111 mShouldKillEmbedding = false; |
|
112 } |
|
113 } |