2004-10-29 23:28:38 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-10-29 23:28:38 +04:00
|
|
|
|
|
|
|
#include "nsCommandLineServiceMac.h"
|
2011-01-03 19:08:36 +03:00
|
|
|
#include "MacApplicationDelegate.h"
|
2004-10-29 23:28:38 +04:00
|
|
|
|
2010-06-09 02:26:12 +04:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#include <Carbon/Carbon.h>
|
2004-10-29 23:28:38 +04:00
|
|
|
|
2010-06-09 02:26:12 +04:00
|
|
|
namespace CommandLineServiceMac {
|
2004-10-29 23:28:38 +04:00
|
|
|
|
2010-06-09 02:26:12 +04:00
|
|
|
static const int kArgsGrowSize = 20;
|
2004-10-29 23:28:38 +04:00
|
|
|
|
2013-10-11 00:36:42 +04:00
|
|
|
static char** sArgs = nullptr;
|
2010-06-09 02:26:12 +04:00
|
|
|
static int sArgsAllocated = 0;
|
|
|
|
static int sArgsUsed = 0;
|
2004-10-29 23:28:38 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool sBuildingCommandLine = false;
|
2011-01-03 19:08:36 +03:00
|
|
|
|
2010-06-09 02:26:12 +04:00
|
|
|
void AddToCommandLine(const char* inArgText)
|
2004-10-29 23:28:38 +04:00
|
|
|
{
|
2010-06-09 02:26:12 +04:00
|
|
|
if (sArgsUsed >= sArgsAllocated - 1) {
|
|
|
|
// realloc does not free the given pointer if allocation fails
|
|
|
|
char **temp = static_cast<char**>(realloc(sArgs, (sArgsAllocated + kArgsGrowSize) * sizeof(char*)));
|
|
|
|
if (!temp)
|
|
|
|
return;
|
|
|
|
sArgs = temp;
|
|
|
|
sArgsAllocated += kArgsGrowSize;
|
2004-10-29 23:28:38 +04:00
|
|
|
}
|
|
|
|
|
2010-06-09 02:26:12 +04:00
|
|
|
char *temp2 = strdup(inArgText);
|
|
|
|
if (!temp2)
|
|
|
|
return;
|
2004-10-29 23:28:38 +04:00
|
|
|
|
2010-06-09 02:26:12 +04:00
|
|
|
sArgs[sArgsUsed++] = temp2;
|
2013-10-11 00:36:42 +04:00
|
|
|
sArgs[sArgsUsed] = nullptr;
|
2010-06-09 02:26:12 +04:00
|
|
|
|
|
|
|
return;
|
2004-10-29 23:28:38 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void SetupMacCommandLine(int& argc, char**& argv, bool forRestart)
|
2004-10-29 23:28:38 +04:00
|
|
|
{
|
2010-06-09 02:26:12 +04:00
|
|
|
sArgs = static_cast<char **>(malloc(kArgsGrowSize * sizeof(char*)));
|
|
|
|
if (!sArgs)
|
|
|
|
return;
|
|
|
|
sArgsAllocated = kArgsGrowSize;
|
2013-10-11 00:36:42 +04:00
|
|
|
sArgs[0] = nullptr;
|
2010-06-09 02:26:12 +04:00
|
|
|
sArgsUsed = 0;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
sBuildingCommandLine = true;
|
2011-01-03 19:08:36 +03:00
|
|
|
|
2010-06-09 02:26:12 +04:00
|
|
|
// Copy args, stripping anything we don't want.
|
2006-10-23 19:08:20 +04:00
|
|
|
for (int arg = 0; arg < argc; arg++) {
|
|
|
|
char* flag = argv[arg];
|
2010-06-09 02:26:12 +04:00
|
|
|
// Don't pass on the psn (Process Serial Number) flag from the OS.
|
2006-10-23 19:08:20 +04:00
|
|
|
if (strncmp(flag, "-psn_", 5) != 0)
|
|
|
|
AddToCommandLine(flag);
|
|
|
|
}
|
2004-10-29 23:28:38 +04:00
|
|
|
|
2011-01-03 19:08:36 +03:00
|
|
|
// Force processing of any pending Apple GetURL Events while we're building
|
|
|
|
// the command line. The handlers will append to the command line rather than
|
|
|
|
// act directly so there is no chance we'll process them during a XUL window
|
|
|
|
// load and accidentally open unnecessary windows and home pages.
|
|
|
|
ProcessPendingGetURLAppleEvents();
|
|
|
|
|
|
|
|
// If the process will be relaunched, the child should be in the foreground
|
|
|
|
// if the parent is in the foreground. This will be communicated in a
|
|
|
|
// command-line argument to the child.
|
2010-04-17 13:50:17 +04:00
|
|
|
if (forRestart) {
|
2010-06-09 02:26:12 +04:00
|
|
|
Boolean isForeground = false;
|
|
|
|
ProcessSerialNumber psnSelf, psnFront;
|
2010-04-17 13:50:17 +04:00
|
|
|
if (::GetCurrentProcess(&psnSelf) == noErr &&
|
|
|
|
::GetFrontProcess(&psnFront) == noErr &&
|
|
|
|
::SameProcess(&psnSelf, &psnFront, &isForeground) == noErr &&
|
|
|
|
isForeground) {
|
|
|
|
AddToCommandLine("-foreground");
|
|
|
|
}
|
2006-07-20 01:08:19 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
sBuildingCommandLine = false;
|
2011-01-03 19:08:36 +03:00
|
|
|
|
2010-06-09 02:26:12 +04:00
|
|
|
argc = sArgsUsed;
|
|
|
|
argv = sArgs;
|
2004-10-29 23:28:38 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool AddURLToCurrentCommandLine(const char* aURL)
|
2011-01-03 19:08:36 +03:00
|
|
|
{
|
|
|
|
if (!sBuildingCommandLine) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-01-03 19:08:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AddToCommandLine("-url");
|
|
|
|
AddToCommandLine(aURL);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-01-03 19:08:36 +03:00
|
|
|
}
|
|
|
|
|
2010-06-09 02:26:12 +04:00
|
|
|
} // namespace CommandLineServiceMac
|