1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/xre/nsCommandLineServiceMac.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,100 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsCommandLineServiceMac.h" 1.10 +#include "MacApplicationDelegate.h" 1.11 + 1.12 +#include <CoreFoundation/CoreFoundation.h> 1.13 +#include <Carbon/Carbon.h> 1.14 + 1.15 +namespace CommandLineServiceMac { 1.16 + 1.17 +static const int kArgsGrowSize = 20; 1.18 + 1.19 +static char** sArgs = nullptr; 1.20 +static int sArgsAllocated = 0; 1.21 +static int sArgsUsed = 0; 1.22 + 1.23 +static bool sBuildingCommandLine = false; 1.24 + 1.25 +void AddToCommandLine(const char* inArgText) 1.26 +{ 1.27 + if (sArgsUsed >= sArgsAllocated - 1) { 1.28 + // realloc does not free the given pointer if allocation fails 1.29 + char **temp = static_cast<char**>(realloc(sArgs, (sArgsAllocated + kArgsGrowSize) * sizeof(char*))); 1.30 + if (!temp) 1.31 + return; 1.32 + sArgs = temp; 1.33 + sArgsAllocated += kArgsGrowSize; 1.34 + } 1.35 + 1.36 + char *temp2 = strdup(inArgText); 1.37 + if (!temp2) 1.38 + return; 1.39 + 1.40 + sArgs[sArgsUsed++] = temp2; 1.41 + sArgs[sArgsUsed] = nullptr; 1.42 + 1.43 + return; 1.44 +} 1.45 + 1.46 +void SetupMacCommandLine(int& argc, char**& argv, bool forRestart) 1.47 +{ 1.48 + sArgs = static_cast<char **>(malloc(kArgsGrowSize * sizeof(char*))); 1.49 + if (!sArgs) 1.50 + return; 1.51 + sArgsAllocated = kArgsGrowSize; 1.52 + sArgs[0] = nullptr; 1.53 + sArgsUsed = 0; 1.54 + 1.55 + sBuildingCommandLine = true; 1.56 + 1.57 + // Copy args, stripping anything we don't want. 1.58 + for (int arg = 0; arg < argc; arg++) { 1.59 + char* flag = argv[arg]; 1.60 + // Don't pass on the psn (Process Serial Number) flag from the OS. 1.61 + if (strncmp(flag, "-psn_", 5) != 0) 1.62 + AddToCommandLine(flag); 1.63 + } 1.64 + 1.65 + // Force processing of any pending Apple GetURL Events while we're building 1.66 + // the command line. The handlers will append to the command line rather than 1.67 + // act directly so there is no chance we'll process them during a XUL window 1.68 + // load and accidentally open unnecessary windows and home pages. 1.69 + ProcessPendingGetURLAppleEvents(); 1.70 + 1.71 + // If the process will be relaunched, the child should be in the foreground 1.72 + // if the parent is in the foreground. This will be communicated in a 1.73 + // command-line argument to the child. 1.74 + if (forRestart) { 1.75 + Boolean isForeground = false; 1.76 + ProcessSerialNumber psnSelf, psnFront; 1.77 + if (::GetCurrentProcess(&psnSelf) == noErr && 1.78 + ::GetFrontProcess(&psnFront) == noErr && 1.79 + ::SameProcess(&psnSelf, &psnFront, &isForeground) == noErr && 1.80 + isForeground) { 1.81 + AddToCommandLine("-foreground"); 1.82 + } 1.83 + } 1.84 + 1.85 + sBuildingCommandLine = false; 1.86 + 1.87 + argc = sArgsUsed; 1.88 + argv = sArgs; 1.89 +} 1.90 + 1.91 +bool AddURLToCurrentCommandLine(const char* aURL) 1.92 +{ 1.93 + if (!sBuildingCommandLine) { 1.94 + return false; 1.95 + } 1.96 + 1.97 + AddToCommandLine("-url"); 1.98 + AddToCommandLine(aURL); 1.99 + 1.100 + return true; 1.101 +} 1.102 + 1.103 +} // namespace CommandLineServiceMac