Simplify nsCommandLineServiceMac, make it easier to understand and faster. Remove unused code. b=542936 r=mstange r=bsmedberg

This commit is contained in:
Josh Aas 2010-06-08 18:26:12 -04:00
parent 65c3a85ff2
commit e2b3518473
6 changed files with 87 additions and 353 deletions

View File

@ -51,7 +51,6 @@
#include "nsINativeAppSupport.h"
#include "nsAppRunner.h"
#include "nsComponentManagerUtils.h"
#include "nsCommandLineServiceMac.h"
#include "nsIServiceManager.h"
#include "nsServiceManagerUtils.h"
#include "nsIAppStartup.h"
@ -63,6 +62,8 @@
#include "nsICommandLineRunner.h"
#include "nsIMacDockSupport.h"
#include "nsIStandaloneNativeMenu.h"
#include "nsILocalFileMac.h"
#include "nsString.h"
@interface MacApplicationDelegate : NSObject
{
@ -152,14 +153,9 @@ SetupMacApplicationDelegate()
NS_OBJC_END_TRY_ABORT_BLOCK;
}
// Opening the application is handled specially elsewhere,
// don't define applicationOpenUntitledFile: .
// The method that NSApplication calls upon a request to reopen, such as when
// the Dock icon is clicked and no windows are open.
// A "visible" window may be miniaturized, so we can't skip
// nsCocoaNativeReOpen() if 'flag' is 'true'.
// the Dock icon is clicked and no windows are open. A "visible" window may be
// miniaturized, so we can't skip nsCocoaNativeReOpen() if 'flag' is 'true'.
- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApp hasVisibleWindows:(BOOL)flag
{
nsCOMPtr<nsINativeAppSupport> nas = do_CreateInstance(NS_NATIVEAPPSUPPORT_CONTRACTID);
@ -175,19 +171,42 @@ SetupMacApplicationDelegate()
// The method that NSApplication calls when documents are requested to be opened.
// It will be called once for each selected document.
- (BOOL)application:(NSApplication*)theApplication openFile:(NSString*)filename
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
// Take advantage of the existing "command line" code for Macs.
nsMacCommandLine& cmdLine = nsMacCommandLine::GetMacCommandLine();
// URLWithString expects our string to be a legal URL with percent escapes.
filename = [filename stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// We don't actually care about Mac filetypes in this context, just pass a placeholder.
cmdLine.HandleOpenOneDoc((CFURLRef)[NSURL URLWithString:filename], 'abcd');
NSString *escapedPath = [filename stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return YES;
nsCOMPtr<nsILocalFileMac> inFile;
nsresult rv = NS_NewLocalFileWithCFURL((CFURLRef)[NSURL URLWithString:escapedPath], PR_TRUE, getter_AddRefs(inFile));
if (NS_FAILED(rv))
return NO;
nsCOMPtr<nsICommandLineRunner> cmdLine(do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
if (!cmdLine) {
NS_ERROR("Couldn't create command line!");
return NO;
}
nsCString filePath;
rv = inFile->GetNativePath(filePath);
if (NS_FAILED(rv))
return NO;
nsCOMPtr<nsIFile> workingDir;
rv = NS_GetSpecialDirectory(NS_OS_CURRENT_WORKING_DIR, getter_AddRefs(workingDir));
if (NS_FAILED(rv))
return NO;
const char *argv[3] = {nsnull, "-file", filePath.get()};
rv = cmdLine->Init(3, const_cast<char**>(argv), workingDir, nsICommandLine::STATE_REMOTE_EXPLICIT);
if (NS_FAILED(rv))
return NO;
if (NS_SUCCEEDED(cmdLine->Run()))
return YES;
return NO;
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
}
@ -195,23 +214,12 @@ SetupMacApplicationDelegate()
// The method that NSApplication calls when documents are requested to be printed
// from the Finder (under the "File" menu).
// It will be called once for each selected document.
- (BOOL)application:(NSApplication*)theApplication printFile:(NSString*)filename
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
// Take advantage of the existing "command line" code for Macs.
nsMacCommandLine& cmdLine = nsMacCommandLine::GetMacCommandLine();
// We don't actually care about Mac filetypes in this context, just pass a placeholder.
cmdLine.HandlePrintOneDoc((CFURLRef)[NSURL URLWithString:filename], 'abcd');
return YES;
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
return NO;
}
// Create the menu that shows up in the Dock.
- (NSMenu*)applicationDockMenu:(NSApplication*)sender
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;

View File

@ -117,12 +117,10 @@ endif
ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
CMMSRCS += MacLaunchHelper.mm
CPPSRCS += nsCommandLineServiceMac.cpp
OS_CXXFLAGS += -fexceptions
endif
ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
CMMSRCS += MacApplicationDelegate.mm
CMMSRCS += MacAutoreleasePool.mm
CPPSRCS += nsCommandLineServiceMac.cpp
OS_CXXFLAGS += -fexceptions
endif
ifdef MOZ_X11

View File

@ -1757,7 +1757,7 @@ static nsresult LaunchChild(nsINativeAppSupport* aNative,
mozilla::AndroidBridge::Bridge()->ScheduleRestart();
#else
#if defined(XP_MACOSX)
SetupMacCommandLine(gRestartArgc, gRestartArgv, PR_TRUE);
CommandLineServiceMac::SetupMacCommandLine(gRestartArgc, gRestartArgv, PR_TRUE);
LaunchChildMac(gRestartArgc, gRestartArgv);
#else
nsCOMPtr<nsILocalFile> lf;
@ -1963,7 +1963,7 @@ ShowProfileManager(nsIToolkitProfileService* aProfileSvc,
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
#ifdef XP_MACOSX
SetupMacCommandLine(gRestartArgc, gRestartArgv, PR_TRUE);
CommandLineServiceMac::SetupMacCommandLine(gRestartArgc, gRestartArgv, PR_TRUE);
#endif
#ifdef XP_WIN
@ -2053,7 +2053,7 @@ ImportProfiles(nsIToolkitProfileService* aPService,
xpcom.RegisterProfileService();
#ifdef XP_MACOSX
SetupMacCommandLine(gRestartArgc, gRestartArgv, PR_TRUE);
CommandLineServiceMac::SetupMacCommandLine(gRestartArgc, gRestartArgv, PR_TRUE);
#endif
nsCOMPtr<nsIProfileMigrator> migrator
@ -3571,14 +3571,13 @@ XRE_main(int argc, char* argv[], const nsXREAppData* aAppData)
cmdLine = do_CreateInstance("@mozilla.org/toolkit/command-line;1");
NS_ENSURE_TRUE(cmdLine, 1);
SetupMacCommandLine(gArgc, gArgv, PR_FALSE);
CommandLineServiceMac::SetupMacCommandLine(gArgc, gArgv, PR_FALSE);
rv = cmdLine->Init(gArgc, gArgv,
workingDir, nsICommandLine::STATE_INITIAL_LAUNCH);
NS_ENSURE_SUCCESS(rv, 1);
#endif
#ifdef MOZ_WIDGET_COCOA
// Prepare Cocoa's form of Apple Event handling.
// Set up ability to respond to system (Apple) events.
SetupMacApplicationDelegate();
#endif
@ -3614,7 +3613,6 @@ XRE_main(int argc, char* argv[], const nsXREAppData* aAppData)
PromiseFlatCString(profileName).get());
#endif /* MOZ_ENABLE_XREMOTE */
// enable win32 DDE responses and Mac appleevents responses
nativeApp->Enable();
}
@ -3670,7 +3668,7 @@ XRE_main(int argc, char* argv[], const nsXREAppData* aAppData)
#endif
#ifdef XP_MACOSX
SetupMacCommandLine(gRestartArgc, gRestartArgv, PR_TRUE);
CommandLineServiceMac::SetupMacCommandLine(gRestartArgc, gRestartArgv, PR_TRUE);
#endif
}
}

View File

@ -38,293 +38,73 @@
*
* ***** END LICENSE BLOCK ***** */
// Special stuff for the Macintosh implementation of command-line service.
#include "nsCommandLineServiceMac.h"
#include "nsDebug.h"
#include "nsILocalFileMac.h"
#include "nsDebug.h"
#include "nsNetUtil.h"
#include "nsIAppStartup.h"
#include "nsIServiceManager.h"
#include "nsIURL.h"
#include "nsIIOService.h"
#include "nsIURL.h"
#include "nsIServiceManager.h"
#include "nsNetCID.h"
#include "nsIDOMWindow.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsIWindowWatcher.h"
#include "jsapi.h"
#include "nsReadableUtils.h"
#include "nsIObserverService.h"
#include "nsIPrefService.h"
#include "nsICommandLineRunner.h"
#include "nsDirectoryServiceDefs.h"
#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h>
#include "prmem.h"
#include "plstr.h"
#include "prenv.h"
namespace CommandLineServiceMac {
// the static instance
nsMacCommandLine nsMacCommandLine::sMacCommandLine;
static const int kArgsGrowSize = 20;
/*
* ReadLine --
*
* Read in a line of text, terminated by CR or LF, from inStream into buf.
* The terminating CR or LF is not included. The text in buf is terminated
* by a null byte.
* Returns the number of bytes in buf. If EOF and zero bytes were read, returns -1.
*/
static char** sArgs = NULL;
static int sArgsAllocated = 0;
static int sArgsUsed = 0;
static PRInt32 ReadLine(FILE* inStream, char* buf, PRInt32 bufSize)
void AddToCommandLine(const char* inArgText)
{
PRInt32 charsRead = 0;
int c;
if (bufSize < 2)
return -1;
while (charsRead < (bufSize-1)) {
c = getc(inStream);
if (c == EOF || c == '\n' || c == '\r')
break;
buf[charsRead++] = c;
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;
}
buf[charsRead] = '\0';
return (c == EOF && !charsRead) ? -1 : charsRead;
char *temp2 = strdup(inArgText);
if (!temp2)
return;
sArgs[sArgsUsed++] = temp2;
sArgs[sArgsUsed] = NULL;
return;
}
nsMacCommandLine::nsMacCommandLine()
: mArgs(NULL)
, mArgsAllocated(0)
, mArgsUsed(0)
, mStartedUp(PR_FALSE)
void SetupMacCommandLine(int& argc, char**& argv, PRBool forRestart)
{
}
sArgs = static_cast<char **>(malloc(kArgsGrowSize * sizeof(char*)));
if (!sArgs)
return;
sArgsAllocated = kArgsGrowSize;
sArgs[0] = NULL;
sArgsUsed = 0;
nsMacCommandLine::~nsMacCommandLine()
{
if (mArgs) {
for (PRUint32 i = 0; i < mArgsUsed; i++)
free(mArgs[i]);
free(mArgs);
}
}
nsresult nsMacCommandLine::Initialize(int& argc, char**& argv)
{
mArgs = static_cast<char **>(malloc(kArgsGrowSize * sizeof(char *)));
if (!mArgs)
return NS_ERROR_FAILURE;
mArgs[0] = nsnull;
mArgsAllocated = kArgsGrowSize;
mArgsUsed = 0;
// Here, we may actually get useful args.
// Copy them first to mArgv.
// Copy args, stripping anything we don't want.
for (int arg = 0; arg < argc; arg++) {
char* flag = argv[arg];
// don't pass on the psn (Process Serial Number) flag from the OS
// Don't pass on the psn (Process Serial Number) flag from the OS.
if (strncmp(flag, "-psn_", 5) != 0)
AddToCommandLine(flag);
}
// we've started up now
mStartedUp = PR_TRUE;
argc = mArgsUsed;
argv = mArgs;
return NS_OK;
}
void nsMacCommandLine::SetupCommandLine(int& argc, char**& argv, PRBool forRestart)
{
// Initializes the command line from Apple Events and other sources,
// as appropriate for OS X.
//
// IMPORTANT: This must be done before XPCOM shutdown if the app is to
// relaunch (i.e. before the ScopedXPCOMStartup object goes out of scope).
// XPCOM shutdown can cause other things to process native events, and
// native event processing can cause the waiting Apple Events to be
// discarded.
// Process Apple Events and put them into the arguments.
Initialize(argc, argv);
if (forRestart) {
Boolean isForeground = PR_FALSE;
ProcessSerialNumber psnSelf, psnFront;
// 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.
Boolean isForeground = false;
ProcessSerialNumber psnSelf, psnFront;
if (::GetCurrentProcess(&psnSelf) == noErr &&
::GetFrontProcess(&psnFront) == noErr &&
::SameProcess(&psnSelf, &psnFront, &isForeground) == noErr &&
isForeground) {
// The process is currently in the foreground. The relaunched
// process should come to the front, too.
AddToCommandLine("-foreground");
}
}
argc = mArgsUsed;
argv = mArgs;
argc = sArgsUsed;
argv = sArgs;
}
nsresult nsMacCommandLine::AddToCommandLine(const char* inArgText)
{
if (mArgsUsed >= mArgsAllocated - 1) {
// realloc does not free the given pointer if allocation fails.
char **temp = static_cast<char **>(realloc(mArgs, (mArgsAllocated + kArgsGrowSize) * sizeof(char *)));
if (!temp)
return NS_ERROR_OUT_OF_MEMORY;
mArgs = temp;
mArgsAllocated += kArgsGrowSize;
}
char *temp2 = strdup(inArgText);
if (!temp2)
return NS_ERROR_OUT_OF_MEMORY;
mArgs[mArgsUsed++] = temp2;
mArgs[mArgsUsed] = nsnull;
return NS_OK;
}
nsresult nsMacCommandLine::AddToCommandLine(const char* inOptionString, const CFURLRef file)
{
CFStringRef string = ::CFURLGetString(file);
if (!string)
return NS_ERROR_FAILURE;
CFIndex length = ::CFStringGetLength(string);
CFIndex bufLen = 0;
::CFStringGetBytes(string, CFRangeMake(0, length), kCFStringEncodingUTF8,
0, PR_FALSE, nsnull, 0, &bufLen);
UInt8 buffer[bufLen + 1];
if (!buffer)
return NS_ERROR_OUT_OF_MEMORY;
::CFStringGetBytes(string, CFRangeMake(0, length), kCFStringEncodingUTF8,
0, PR_FALSE, buffer, bufLen, nsnull);
buffer[bufLen] = 0;
AddToCommandLine(inOptionString);
AddToCommandLine((char*)buffer);
return NS_OK;
}
nsresult nsMacCommandLine::AddToEnvironmentVars(const char* inArgText)
{
(void)PR_SetEnv(inArgText);
return NS_OK;
}
nsresult nsMacCommandLine::HandleOpenOneDoc(const CFURLRef file, OSType inFileType)
{
nsCOMPtr<nsILocalFileMac> inFile;
nsresult rv = NS_NewLocalFileWithCFURL(file, PR_TRUE, getter_AddRefs(inFile));
if (NS_FAILED(rv))
return rv;
if (!mStartedUp) {
// Is it the right type to be a command-line file?
if (inFileType == 'TEXT' || inFileType == 'CMDL') {
// Can we open the file?
FILE *fp = 0;
rv = inFile->OpenANSIFileDesc("r", &fp);
if (NS_SUCCEEDED(rv)) {
Boolean foundArgs = false;
Boolean foundEnv = false;
char chars[1024];
static const char kCommandLinePrefix[] = "ARGS:";
static const char kEnvVarLinePrefix[] = "ENV:";
while (ReadLine(fp, chars, sizeof(chars)) != -1) {
// See if there are any command line or environment var settings
if (PL_strstr(chars, kCommandLinePrefix) == chars) {
AddToCommandLine(chars + sizeof(kCommandLinePrefix) - 1);
foundArgs = true;
}
else if (PL_strstr(chars, kEnvVarLinePrefix) == chars) {
AddToEnvironmentVars(chars + sizeof(kEnvVarLinePrefix) - 1);
foundEnv = true;
}
}
fclose(fp);
// If we found a command line or environment vars we want to return now
// rather than trying to open the file as a URL
if (foundArgs || foundEnv)
return NS_OK;
}
}
// If it's not a command-line argument, and we are starting up the application,
// add a command-line "-url" argument to the global list. This means that if
// the app is opened with documents on the mac, they'll be handled the same
// way as if they had been typed on the command line in Unix or DOS.
return AddToCommandLine("-url", file);
}
// Final case: we're not just starting up, use the arg as a -file <arg>
nsCOMPtr<nsICommandLineRunner> cmdLine
(do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
if (!cmdLine) {
NS_ERROR("Couldn't create command line!");
return NS_ERROR_FAILURE;
}
nsCString filePath;
rv = inFile->GetNativePath(filePath);
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIFile> workingDir;
rv = NS_GetSpecialDirectory(NS_OS_CURRENT_WORKING_DIR, getter_AddRefs(workingDir));
if (NS_FAILED(rv))
return rv;
const char *argv[3] = {nsnull, "-file", filePath.get()};
rv = cmdLine->Init(3, const_cast<char**>(argv), workingDir, nsICommandLine::STATE_REMOTE_EXPLICIT);
if (NS_FAILED(rv))
return rv;
rv = cmdLine->Run();
return rv;
}
nsresult nsMacCommandLine::HandlePrintOneDoc(const CFURLRef file, OSType fileType)
{
// If we are starting up the application,
// add a command-line "-print" argument to the global list. This means that if
// the app is opened with documents on the mac, they'll be handled the same
// way as if they had been typed on the command line in Unix or DOS.
if (!mStartedUp)
return AddToCommandLine("-print", file);
// Final case: we're not just starting up. How do we handle this?
NS_NOTYETIMPLEMENTED("Write Me");
return NS_ERROR_FAILURE;
}
nsresult nsMacCommandLine::DispatchURLToNewBrowser(const char* url)
{
nsresult rv = AddToCommandLine("-url");
if (NS_SUCCEEDED(rv))
rv = AddToCommandLine(url);
return rv;
}
#pragma mark -
void SetupMacCommandLine(int& argc, char**& argv, PRBool forRestart)
{
nsMacCommandLine& cmdLine = nsMacCommandLine::GetMacCommandLine();
return cmdLine.SetupCommandLine(argc, argv, forRestart);
}
} // namespace CommandLineServiceMac

View File

@ -35,63 +35,13 @@
*
* ***** END LICENSE BLOCK ***** */
// Special stuff for the Macintosh implementation of command-line service.
#ifndef nsCommandLineServiceMac_h_
#define nsCommandLineServiceMac_h_
#include <CoreFoundation/CoreFoundation.h>
#include "nscore.h"
#include "nsError.h"
#include "nsString.h"
class nsMacCommandLine
{
public:
enum
{
kArgsGrowSize = 20
};
nsMacCommandLine();
~nsMacCommandLine();
nsresult Initialize(int& argc, char**& argv);
void SetupCommandLine(int& argc, char**& argv, PRBool forRestart);
nsresult AddToCommandLine(const char* inArgText);
nsresult AddToCommandLine(const char* inOptionString, const CFURLRef file);
nsresult AddToEnvironmentVars(const char* inArgText);
nsresult HandleOpenOneDoc(const CFURLRef file, OSType inFileType);
nsresult HandlePrintOneDoc(const CFURLRef file, OSType fileType);
nsresult DispatchURLToNewBrowser(const char* url);
protected:
nsresult OpenURL(const char* aURL);
nsresult OpenWindow(const char *chrome, const PRUnichar *url);
char** mArgs; // array of arg pointers (augmented argv)
PRUint32 mArgsAllocated; // number of slots available in mArgs
PRUint32 mArgsUsed; // number of slots used in mArgs
PRBool mStartedUp;
public:
static nsMacCommandLine& GetMacCommandLine() { return sMacCommandLine; }
private:
static nsMacCommandLine sMacCommandLine;
};
void SetupMacCommandLine(int& argc, char**& argv, PRBool forRestart);
namespace CommandLineServiceMac {
void SetupMacCommandLine(int& argc, char**& argv, PRBool forRestart);
}
#endif // nsCommandLineServiceMac_h_

View File

@ -542,7 +542,7 @@ ApplyUpdate(nsIFile *greDir, nsIFile *updateDir, nsILocalFile *statusFile,
goto end;
#ifdef XP_MACOSX
SetupMacCommandLine(argc, argv, PR_TRUE);
CommandLineServiceMac::SetupMacCommandLine(argc, argv, PR_TRUE);
#endif
PR_CreateProcessDetached(updaterPath.get(), argv, nsnull, attr);