optimize/modernize Mac OS X toolkit/xre code. b=486733 r=mstange sr=bsmedberg

This commit is contained in:
Josh Aas 2009-04-09 20:54:11 -04:00
Родитель 43db6bc8ab
Коммит 499bb62851
5 изменённых файлов: 55 добавлений и 79 удалений

Просмотреть файл

@ -157,18 +157,10 @@ SetupMacApplicationDelegate()
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
FSRef ref;
// The cast is kind of freaky, but apparently it's what all the beautiful people do.
OSStatus status = FSPathMakeRef((UInt8 *)[filename fileSystemRepresentation], &ref, NULL);
if (status != noErr) {
NS_WARNING("FSPathMakeRef in openFile failed, skipping file open");
return NO;
}
// 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.HandleOpenOneDoc(&ref, 'abcd');
cmdLine.HandleOpenOneDoc((CFURLRef)[NSURL URLWithString:filename], 'abcd');
return YES;
@ -183,18 +175,10 @@ SetupMacApplicationDelegate()
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
FSRef ref;
// The cast is kind of freaky, but apparently it's what all the beautiful people do.
OSStatus status = FSPathMakeRef((UInt8 *)[filename fileSystemRepresentation], &ref, NULL);
if (status != noErr) {
NS_WARNING("FSPathMakeRef in printFile failed, skipping printing");
return NO;
}
// 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(&ref, 'abcd');
cmdLine.HandlePrintOneDoc((CFURLRef)[NSURL URLWithString:filename], 'abcd');
return YES;

Просмотреть файл

@ -195,35 +195,25 @@ nsresult nsMacCommandLine::AddToCommandLine(const char* inArgText)
return NS_OK;
}
nsresult nsMacCommandLine::AddToCommandLine(const char* inOptionString, const FSRef* inFSRef)
nsresult nsMacCommandLine::AddToCommandLine(const char* inOptionString, const CFURLRef file)
{
CFURLRef url = ::CFURLCreateFromFSRef(nsnull, inFSRef);
if (!url)
CFStringRef string = ::CFURLGetString(file);
if (!string)
return NS_ERROR_FAILURE;
CFStringRef string = ::CFURLGetString(url);
if (!string) {
::CFRelease(url);
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) {
::CFRelease(url);
return NS_ERROR_FAILURE;
}
if (!buffer)
return NS_ERROR_OUT_OF_MEMORY;
::CFStringGetBytes(string, CFRangeMake(0, length), kCFStringEncodingUTF8,
0, PR_FALSE, buffer, bufLen, nsnull);
buffer[bufLen] = 0;
::CFRelease(url);
AddToCommandLine(inOptionString);
AddToCommandLine((char*)buffer);
@ -236,12 +226,12 @@ nsresult nsMacCommandLine::AddToEnvironmentVars(const char* inArgText)
return NS_OK;
}
OSErr nsMacCommandLine::HandleOpenOneDoc(const FSRef* inFSRef, OSType inFileType)
nsresult nsMacCommandLine::HandleOpenOneDoc(const CFURLRef file, OSType inFileType)
{
nsCOMPtr<nsILocalFileMac> inFile;
nsresult rv = NS_NewLocalFileWithFSRef(inFSRef, PR_TRUE, getter_AddRefs(inFile));
nsresult rv = NS_NewLocalFileWithCFURL(file, PR_TRUE, getter_AddRefs(inFile));
if (NS_FAILED(rv))
return errAEEventNotHandled;
return rv;
if (!mStartedUp) {
// Is it the right type to be a command-line file?
@ -270,17 +260,16 @@ OSErr nsMacCommandLine::HandleOpenOneDoc(const FSRef* inFSRef, OSType inFileType
fclose(fp);
// If we found a command line or environment vars we want to return now
// raather than trying to open the file as a URL
// rather than trying to open the file as a URL
if (foundArgs || foundEnv)
return noErr;
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.
rv = AddToCommandLine("-url", inFSRef);
return (NS_SUCCEEDED(rv)) ? noErr : errAEEventNotHandled;
return AddToCommandLine("-url", file);
}
// Final case: we're not just starting up, use the arg as a -file <arg>
@ -288,48 +277,47 @@ OSErr nsMacCommandLine::HandleOpenOneDoc(const FSRef* inFSRef, OSType inFileType
(do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
if (!cmdLine) {
NS_ERROR("Couldn't create command line!");
return errAEEventNotHandled;
return NS_ERROR_FAILURE;
}
nsCString filePath;
rv = inFile->GetNativePath(filePath);
if (NS_FAILED(rv))
return errAEEventNotHandled;
return rv;
nsCOMPtr<nsIFile> workingDir;
rv = NS_GetSpecialDirectory(NS_OS_CURRENT_WORKING_DIR, getter_AddRefs(workingDir));
if (NS_FAILED(rv))
return errAEEventNotHandled;
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 errAEEventNotHandled;
return rv;
rv = cmdLine->Run();
return (NS_SUCCEEDED(rv)) ? noErr : errAEEventNotHandled;
return rv;
}
OSErr nsMacCommandLine::HandlePrintOneDoc(const FSRef* inFSRef, OSType fileType)
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", inFSRef);
return AddToCommandLine("-print", file);
// Final case: we're not just starting up. How do we handle this?
NS_NOTYETIMPLEMENTED("Write Me");
return errAEEventNotHandled;
return NS_ERROR_FAILURE;
}
OSErr nsMacCommandLine::DispatchURLToNewBrowser(const char* url)
nsresult nsMacCommandLine::DispatchURLToNewBrowser(const char* url)
{
OSErr err = errAEEventNotHandled;
err = AddToCommandLine("-url");
if (err == noErr)
err = AddToCommandLine(url);
return err;
nsresult rv = AddToCommandLine("-url");
if (NS_SUCCEEDED(rv))
rv = AddToCommandLine(url);
return rv;
}
#pragma mark -

Просмотреть файл

@ -40,19 +40,16 @@
#ifndef nsCommandLineServiceMac_h_
#define nsCommandLineServiceMac_h_
#include <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h>
#include "nscore.h"
#include "nsError.h"
#include "nsString.h"
#ifdef __cplusplus
class nsMacCommandLine
{
public:
enum
{
kArgsGrowSize = 20
@ -65,17 +62,17 @@ public:
void SetupCommandLine(int& argc, char**& argv);
nsresult AddToCommandLine(const char* inArgText);
nsresult AddToCommandLine(const char* inOptionString, const FSRef* inFSRef);
nsresult AddToCommandLine(const char* inOptionString, const CFURLRef file);
nsresult AddToEnvironmentVars(const char* inArgText);
OSErr HandleOpenOneDoc(const FSRef* inFSRef, OSType inFileType);
OSErr HandlePrintOneDoc(const FSRef* inFSRef, OSType fileType);
nsresult HandleOpenOneDoc(const CFURLRef file, OSType inFileType);
nsresult HandlePrintOneDoc(const CFURLRef file, OSType fileType);
OSErr DispatchURLToNewBrowser(const char* url);
nsresult DispatchURLToNewBrowser(const char* url);
protected:
OSErr OpenURL(const char* aURL);
nsresult OpenURL(const char* aURL);
nsresult OpenWindow(const char *chrome, const PRUnichar *url);
@ -88,25 +85,13 @@ protected:
public:
static nsMacCommandLine& GetMacCommandLine() { return sMacCommandLine; }
private:
static nsMacCommandLine sMacCommandLine;
};
#endif //__cplusplus
#ifdef __cplusplus
extern "C" {
#endif
void SetupMacCommandLine(int& argc, char**& argv);
#ifdef __cplusplus
}
#endif
#endif // nsCommandLineServiceMac_h_

Просмотреть файл

@ -74,7 +74,7 @@ interface nsILocalFileMac : nsILocalFile
*
* NOTE: Supported only for XP_MACOSX or TARGET_CARBON
*
* @param aFSRef the native file spec
* @param aFSRef the native FSRef
*
*/
[noscript] void initWithFSRef([const] in FSRefPtr aFSRef);
@ -239,5 +239,6 @@ interface nsILocalFileMac : nsILocalFile
extern "C"
{
NS_EXPORT nsresult NS_NewLocalFileWithFSRef(const FSRef* aFSRef, PRBool aFollowSymlinks, nsILocalFileMac** result);
NS_EXPORT nsresult NS_NewLocalFileWithCFURL(const CFURLRef aURL, PRBool aFollowSymlinks, nsILocalFileMac** result);
}
%}

Просмотреть файл

@ -2394,6 +2394,24 @@ nsresult NS_NewLocalFileWithFSRef(const FSRef* aFSRef, PRBool aFollowLinks, nsIL
return NS_OK;
}
nsresult NS_NewLocalFileWithCFURL(const CFURLRef aURL, PRBool aFollowLinks, nsILocalFileMac** result)
{
nsLocalFile* file = new nsLocalFile();
if (file == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(file);
file->SetFollowLinks(aFollowLinks);
nsresult rv = file->InitWithCFURL(aURL);
if (NS_FAILED(rv)) {
NS_RELEASE(file);
return rv;
}
*result = file;
return NS_OK;
}
#pragma mark -
#pragma mark [Static Functions]