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.

     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)
    21 {
    22   NS_LogInit();
    23 }
    25 ScopedXREEmbed::~ScopedXREEmbed()
    26 {
    27   Stop();
    28   NS_LogTerm();
    29 }
    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 }
    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
    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);
    93   }
    94 #endif
    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;
   103   mShouldKillEmbedding = true;
   104 }
   106 void
   107 ScopedXREEmbed::Stop()
   108 {
   109   if (mShouldKillEmbedding) {
   110     XRE_TermEmbedding();
   111     mShouldKillEmbedding = false;
   112   }
   113 }

mercurial