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