nsCommandLineServiceMac.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsCommandLineServiceMac.h"
  6. #include "MacApplicationDelegate.h"
  7. #include <CoreFoundation/CoreFoundation.h>
  8. #include <Carbon/Carbon.h>
  9. namespace CommandLineServiceMac {
  10. static const int kArgsGrowSize = 20;
  11. static char** sArgs = nullptr;
  12. static int sArgsAllocated = 0;
  13. static int sArgsUsed = 0;
  14. static bool sBuildingCommandLine = false;
  15. void AddToCommandLine(const char* inArgText)
  16. {
  17. if (sArgsUsed >= sArgsAllocated - 1) {
  18. // realloc does not free the given pointer if allocation fails
  19. char **temp = static_cast<char**>(realloc(sArgs, (sArgsAllocated + kArgsGrowSize) * sizeof(char*)));
  20. if (!temp)
  21. return;
  22. sArgs = temp;
  23. sArgsAllocated += kArgsGrowSize;
  24. }
  25. char *temp2 = strdup(inArgText);
  26. if (!temp2)
  27. return;
  28. sArgs[sArgsUsed++] = temp2;
  29. sArgs[sArgsUsed] = nullptr;
  30. return;
  31. }
  32. void SetupMacCommandLine(int& argc, char**& argv, bool forRestart)
  33. {
  34. sArgs = static_cast<char **>(malloc(kArgsGrowSize * sizeof(char*)));
  35. if (!sArgs)
  36. return;
  37. sArgsAllocated = kArgsGrowSize;
  38. sArgs[0] = nullptr;
  39. sArgsUsed = 0;
  40. sBuildingCommandLine = true;
  41. // Copy args, stripping anything we don't want.
  42. for (int arg = 0; arg < argc; arg++) {
  43. char* flag = argv[arg];
  44. // Don't pass on the psn (Process Serial Number) flag from the OS, or
  45. // the "-foreground" flag since it will be set below if necessary.
  46. if (strncmp(flag, "-psn_", 5) != 0 &&
  47. strncmp(flag, "-foreground", 11) != 0)
  48. AddToCommandLine(flag);
  49. }
  50. // Force processing of any pending Apple GetURL Events while we're building
  51. // the command line. The handlers will append to the command line rather than
  52. // act directly so there is no chance we'll process them during a XUL window
  53. // load and accidentally open unnecessary windows and home pages.
  54. ProcessPendingGetURLAppleEvents();
  55. // If the process will be relaunched, the child should be in the foreground
  56. // if the parent is in the foreground. This will be communicated in a
  57. // command-line argument to the child.
  58. if (forRestart) {
  59. Boolean isForeground = false;
  60. ProcessSerialNumber psnSelf, psnFront;
  61. if (::GetCurrentProcess(&psnSelf) == noErr &&
  62. ::GetFrontProcess(&psnFront) == noErr &&
  63. ::SameProcess(&psnSelf, &psnFront, &isForeground) == noErr &&
  64. isForeground) {
  65. AddToCommandLine("-foreground");
  66. }
  67. }
  68. sBuildingCommandLine = false;
  69. argc = sArgsUsed;
  70. argv = sArgs;
  71. }
  72. bool AddURLToCurrentCommandLine(const char* aURL)
  73. {
  74. if (!sBuildingCommandLine) {
  75. return false;
  76. }
  77. AddToCommandLine("-url");
  78. AddToCommandLine(aURL);
  79. return true;
  80. }
  81. } // namespace CommandLineServiceMac