Line data Source code
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/. */
6 :
7 : #include "ScopedXREEmbed.h"
8 :
9 : #include "base/command_line.h"
10 : #include "base/string_util.h"
11 :
12 : #include "nsIFile.h"
13 :
14 : #include "nsCOMPtr.h"
15 : #include "nsServiceManagerUtils.h"
16 : #include "nsString.h"
17 : #include "nsXULAppAPI.h"
18 :
19 : using mozilla::ipc::ScopedXREEmbed;
20 :
21 2 : ScopedXREEmbed::ScopedXREEmbed()
22 2 : : mShouldKillEmbedding(false)
23 : {
24 2 : NS_LogInit();
25 2 : }
26 :
27 0 : ScopedXREEmbed::~ScopedXREEmbed()
28 : {
29 0 : Stop();
30 0 : NS_LogTerm();
31 0 : }
32 :
33 : void
34 2 : ScopedXREEmbed::SetAppDir(const nsACString& aPath)
35 : {
36 : bool flag;
37 : nsresult rv =
38 2 : XRE_GetFileFromPath(aPath.BeginReading(), getter_AddRefs(mAppDir));
39 6 : if (NS_FAILED(rv) ||
40 4 : NS_FAILED(mAppDir->Exists(&flag)) || !flag) {
41 0 : NS_WARNING("Invalid application directory passed to content process.");
42 0 : mAppDir = nullptr;
43 : }
44 2 : }
45 :
46 : void
47 2 : ScopedXREEmbed::Start()
48 : {
49 4 : std::string path;
50 : #if defined(OS_WIN)
51 : path = WideToUTF8(CommandLine::ForCurrentProcess()->program());
52 : #elif defined(OS_POSIX)
53 2 : path = CommandLine::ForCurrentProcess()->argv()[0];
54 : #else
55 : # error Sorry
56 : #endif
57 :
58 4 : nsCOMPtr<nsIFile> localFile;
59 2 : nsresult rv = XRE_GetBinaryPath(path.c_str(), getter_AddRefs(localFile));
60 2 : if (NS_FAILED(rv))
61 0 : return;
62 :
63 4 : nsCOMPtr<nsIFile> parent;
64 2 : rv = localFile->GetParent(getter_AddRefs(parent));
65 2 : if (NS_FAILED(rv))
66 0 : return;
67 :
68 2 : localFile = do_QueryInterface(parent);
69 2 : NS_ENSURE_TRUE_VOID(localFile);
70 :
71 : #ifdef OS_MACOSX
72 : if (XRE_IsContentProcess()) {
73 : // We're an XPCOM-using subprocess. Walk out of
74 : // [subprocess].app/Contents/MacOS to the real GRE dir.
75 : rv = localFile->GetParent(getter_AddRefs(parent));
76 : if (NS_FAILED(rv))
77 : return;
78 :
79 : localFile = do_QueryInterface(parent);
80 : NS_ENSURE_TRUE_VOID(localFile);
81 :
82 : rv = localFile->GetParent(getter_AddRefs(parent));
83 : if (NS_FAILED(rv))
84 : return;
85 :
86 : localFile = do_QueryInterface(parent);
87 : NS_ENSURE_TRUE_VOID(localFile);
88 :
89 : rv = localFile->GetParent(getter_AddRefs(parent));
90 : if (NS_FAILED(rv))
91 : return;
92 :
93 : localFile = do_QueryInterface(parent);
94 : NS_ENSURE_TRUE_VOID(localFile);
95 :
96 : rv = localFile->SetNativeLeafName(NS_LITERAL_CSTRING("Resources"));
97 : if (NS_FAILED(rv)) {
98 : return;
99 : }
100 : }
101 : #endif
102 :
103 2 : if (mAppDir)
104 2 : rv = XRE_InitEmbedding2(localFile, mAppDir, nullptr);
105 : else
106 0 : rv = XRE_InitEmbedding2(localFile, localFile, nullptr);
107 2 : if (NS_FAILED(rv))
108 0 : return;
109 :
110 2 : mShouldKillEmbedding = true;
111 : }
112 :
113 : void
114 0 : ScopedXREEmbed::Stop()
115 : {
116 0 : if (mShouldKillEmbedding) {
117 0 : XRE_TermEmbedding();
118 0 : mShouldKillEmbedding = false;
119 : }
120 0 : }
|