toolkit/xre/nsCommandLineServiceMac.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:16df918a415a
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
6 #include "nsCommandLineServiceMac.h"
7 #include "MacApplicationDelegate.h"
8
9 #include <CoreFoundation/CoreFoundation.h>
10 #include <Carbon/Carbon.h>
11
12 namespace CommandLineServiceMac {
13
14 static const int kArgsGrowSize = 20;
15
16 static char** sArgs = nullptr;
17 static int sArgsAllocated = 0;
18 static int sArgsUsed = 0;
19
20 static bool sBuildingCommandLine = false;
21
22 void AddToCommandLine(const char* inArgText)
23 {
24 if (sArgsUsed >= sArgsAllocated - 1) {
25 // realloc does not free the given pointer if allocation fails
26 char **temp = static_cast<char**>(realloc(sArgs, (sArgsAllocated + kArgsGrowSize) * sizeof(char*)));
27 if (!temp)
28 return;
29 sArgs = temp;
30 sArgsAllocated += kArgsGrowSize;
31 }
32
33 char *temp2 = strdup(inArgText);
34 if (!temp2)
35 return;
36
37 sArgs[sArgsUsed++] = temp2;
38 sArgs[sArgsUsed] = nullptr;
39
40 return;
41 }
42
43 void SetupMacCommandLine(int& argc, char**& argv, bool forRestart)
44 {
45 sArgs = static_cast<char **>(malloc(kArgsGrowSize * sizeof(char*)));
46 if (!sArgs)
47 return;
48 sArgsAllocated = kArgsGrowSize;
49 sArgs[0] = nullptr;
50 sArgsUsed = 0;
51
52 sBuildingCommandLine = true;
53
54 // Copy args, stripping anything we don't want.
55 for (int arg = 0; arg < argc; arg++) {
56 char* flag = argv[arg];
57 // Don't pass on the psn (Process Serial Number) flag from the OS.
58 if (strncmp(flag, "-psn_", 5) != 0)
59 AddToCommandLine(flag);
60 }
61
62 // Force processing of any pending Apple GetURL Events while we're building
63 // the command line. The handlers will append to the command line rather than
64 // act directly so there is no chance we'll process them during a XUL window
65 // load and accidentally open unnecessary windows and home pages.
66 ProcessPendingGetURLAppleEvents();
67
68 // If the process will be relaunched, the child should be in the foreground
69 // if the parent is in the foreground. This will be communicated in a
70 // command-line argument to the child.
71 if (forRestart) {
72 Boolean isForeground = false;
73 ProcessSerialNumber psnSelf, psnFront;
74 if (::GetCurrentProcess(&psnSelf) == noErr &&
75 ::GetFrontProcess(&psnFront) == noErr &&
76 ::SameProcess(&psnSelf, &psnFront, &isForeground) == noErr &&
77 isForeground) {
78 AddToCommandLine("-foreground");
79 }
80 }
81
82 sBuildingCommandLine = false;
83
84 argc = sArgsUsed;
85 argv = sArgs;
86 }
87
88 bool AddURLToCurrentCommandLine(const char* aURL)
89 {
90 if (!sBuildingCommandLine) {
91 return false;
92 }
93
94 AddToCommandLine("-url");
95 AddToCommandLine(aURL);
96
97 return true;
98 }
99
100 } // namespace CommandLineServiceMac

mercurial