ipc/glue/ScopedXREEmbed.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial