2004-06-18 01:23:51 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-10-30 07:48:16 +04:00
|
|
|
|
2004-06-18 01:23:51 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
|
2010-03-10 22:30:11 +03:00
|
|
|
#import <CoreServices/CoreServices.h>
|
2007-08-16 18:53:10 +04:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2004-06-18 01:23:51 +04:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsNativeAppSupportBase.h"
|
|
|
|
|
|
|
|
#include "nsIAppShellService.h"
|
2004-10-30 03:34:15 +04:00
|
|
|
#include "nsIAppStartup.h"
|
2004-06-18 01:23:51 +04:00
|
|
|
#include "nsIBaseWindow.h"
|
2005-01-17 21:50:18 +03:00
|
|
|
#include "nsICommandLineRunner.h"
|
2011-07-15 14:31:34 +04:00
|
|
|
#include "nsIDOMWindow.h"
|
2004-06-18 01:23:51 +04:00
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIDocShellTreeOwner.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsIWindowMediator.h"
|
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
// This must be included last:
|
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
|
2004-06-18 01:23:51 +04:00
|
|
|
nsresult
|
2011-07-15 14:31:34 +04:00
|
|
|
GetNativeWindowPointerFromDOMWindow(nsIDOMWindow *a_window, NSWindow **a_nativeWindow)
|
2010-03-10 22:30:11 +03:00
|
|
|
{
|
|
|
|
*a_nativeWindow = nil;
|
|
|
|
if (!a_window)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWebNavigation> mruWebNav(do_GetInterface(a_window));
|
|
|
|
if (mruWebNav) {
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> mruTreeItem(do_QueryInterface(mruWebNav));
|
2012-07-30 18:20:58 +04:00
|
|
|
nsCOMPtr<nsIDocShellTreeOwner> mruTreeOwner = nullptr;
|
2010-03-10 22:30:11 +03:00
|
|
|
mruTreeItem->GetTreeOwner(getter_AddRefs(mruTreeOwner));
|
|
|
|
if(mruTreeOwner) {
|
|
|
|
nsCOMPtr<nsIBaseWindow> mruBaseWindow(do_QueryInterface(mruTreeOwner));
|
|
|
|
if (mruBaseWindow) {
|
2012-07-30 18:20:58 +04:00
|
|
|
nsCOMPtr<nsIWidget> mruWidget = nullptr;
|
2010-03-10 22:30:11 +03:00
|
|
|
mruBaseWindow->GetMainWidget(getter_AddRefs(mruWidget));
|
|
|
|
if (mruWidget) {
|
|
|
|
*a_nativeWindow = (NSWindow*)mruWidget->GetNativeData(NS_NATIVE_WINDOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2010-03-10 22:30:11 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2007-08-16 18:53:10 +04:00
|
|
|
class nsNativeAppSupportCocoa : public nsNativeAppSupportBase
|
2004-06-18 01:23:51 +04:00
|
|
|
{
|
|
|
|
public:
|
2007-08-16 18:53:10 +04:00
|
|
|
nsNativeAppSupportCocoa() :
|
2011-10-17 18:59:28 +04:00
|
|
|
mCanShowUI(false) { }
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHOD Start(bool* aRetVal);
|
2004-06-18 01:23:51 +04:00
|
|
|
NS_IMETHOD ReOpen();
|
2004-10-29 23:28:38 +04:00
|
|
|
NS_IMETHOD Enable();
|
2004-06-18 01:23:51 +04:00
|
|
|
|
|
|
|
private:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mCanShowUI;
|
2004-06-18 01:23:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-08-16 18:53:10 +04:00
|
|
|
nsNativeAppSupportCocoa::Enable()
|
2004-06-18 01:23:51 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mCanShowUI = true;
|
2004-10-29 23:28:38 +04:00
|
|
|
return NS_OK;
|
2004-06-18 01:23:51 +04:00
|
|
|
}
|
|
|
|
|
2013-03-08 03:00:07 +04:00
|
|
|
#define MAC_OS_X_VERSION_10_6_HEX 0x00001060
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsNativeAppSupportCocoa::Start(bool *_retval)
|
2004-06-18 01:23:51 +04:00
|
|
|
{
|
2008-02-20 20:34:21 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-09-18 20:37:22 +04:00
|
|
|
SInt32 response = 0;
|
2007-06-27 08:25:30 +04:00
|
|
|
OSErr err = ::Gestalt (gestaltSystemVersion, &response);
|
2007-08-16 18:53:10 +04:00
|
|
|
response &= 0xFFFF; // The system version is in the low order word
|
2007-06-27 08:25:30 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
// Check that the OS version is supported, if not return false,
|
2007-08-16 18:53:10 +04:00
|
|
|
// which will make the browser quit. In principle we could display an
|
|
|
|
// alert here. But the alert's message and buttons would require custom
|
|
|
|
// localization. So (for now at least) we just log an English message
|
|
|
|
// to the console before quitting.
|
2013-03-08 03:00:07 +04:00
|
|
|
if ((err != noErr) || response < MAC_OS_X_VERSION_10_6_HEX) {
|
2010-03-25 19:07:50 +03:00
|
|
|
NSLog(@"Minimum OS version requirement not met!");
|
2012-08-16 15:58:11 +04:00
|
|
|
return NS_OK;
|
2004-06-18 01:23:51 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = true;
|
2004-06-18 01:23:51 +04:00
|
|
|
return NS_OK;
|
2008-02-20 20:34:21 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2004-06-18 01:23:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-08-16 18:53:10 +04:00
|
|
|
nsNativeAppSupportCocoa::ReOpen()
|
2004-06-18 01:23:51 +04:00
|
|
|
{
|
2008-02-20 20:34:21 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2004-06-18 01:23:51 +04:00
|
|
|
if (!mCanShowUI)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool haveNonMiniaturized = false;
|
|
|
|
bool haveOpenWindows = false;
|
|
|
|
bool done = false;
|
2004-06-18 01:23:51 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWindowMediator>
|
|
|
|
wm(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
|
2009-03-10 00:27:48 +03:00
|
|
|
if (!wm) {
|
2004-06-18 01:23:51 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-03-10 00:27:48 +03:00
|
|
|
else {
|
2004-06-18 01:23:51 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> windowList;
|
2012-07-30 18:20:58 +04:00
|
|
|
wm->GetXULWindowEnumerator(nullptr, getter_AddRefs(windowList));
|
2011-09-29 10:19:26 +04:00
|
|
|
bool more;
|
2004-06-18 01:23:51 +04:00
|
|
|
windowList->HasMoreElements(&more);
|
2009-03-10 00:27:48 +03:00
|
|
|
while (more) {
|
2012-07-30 18:20:58 +04:00
|
|
|
nsCOMPtr<nsISupports> nextWindow = nullptr;
|
2004-06-18 01:23:51 +04:00
|
|
|
windowList->GetNext(getter_AddRefs(nextWindow));
|
|
|
|
nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(nextWindow));
|
2009-03-10 00:27:48 +03:00
|
|
|
if (!baseWindow) {
|
2004-06-18 01:23:51 +04:00
|
|
|
windowList->HasMoreElements(&more);
|
|
|
|
continue;
|
|
|
|
}
|
2009-03-10 00:27:48 +03:00
|
|
|
else {
|
2011-10-17 18:59:28 +04:00
|
|
|
haveOpenWindows = true;
|
2004-06-18 01:23:51 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = nullptr;
|
2004-06-18 01:23:51 +04:00
|
|
|
baseWindow->GetMainWidget(getter_AddRefs(widget));
|
2009-03-10 00:27:48 +03:00
|
|
|
if (!widget) {
|
2004-06-18 01:23:51 +04:00
|
|
|
windowList->HasMoreElements(&more);
|
|
|
|
continue;
|
|
|
|
}
|
2007-08-16 18:53:10 +04:00
|
|
|
NSWindow *cocoaWindow = (NSWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);
|
|
|
|
if (![cocoaWindow isMiniaturized]) {
|
2011-10-17 18:59:28 +04:00
|
|
|
haveNonMiniaturized = true;
|
2004-06-18 01:23:51 +04:00
|
|
|
break; //have un-minimized windows, nothing to do
|
|
|
|
}
|
|
|
|
windowList->HasMoreElements(&more);
|
|
|
|
} // end while
|
|
|
|
|
2009-03-10 00:27:48 +03:00
|
|
|
if (!haveNonMiniaturized) {
|
2007-08-16 18:53:10 +04:00
|
|
|
// Deminiaturize the most recenty used window
|
2011-07-15 14:31:34 +04:00
|
|
|
nsCOMPtr<nsIDOMWindow> mru;
|
2012-07-30 18:20:58 +04:00
|
|
|
wm->GetMostRecentWindow(nullptr, getter_AddRefs(mru));
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2009-03-10 00:27:48 +03:00
|
|
|
if (mru) {
|
2007-08-16 18:53:10 +04:00
|
|
|
NSWindow *cocoaMru = nil;
|
|
|
|
GetNativeWindowPointerFromDOMWindow(mru, &cocoaMru);
|
|
|
|
if (cocoaMru) {
|
|
|
|
[cocoaMru deminiaturize:nil];
|
2011-10-17 18:59:28 +04:00
|
|
|
done = true;
|
2004-06-18 01:23:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-16 18:53:10 +04:00
|
|
|
} // end if have non miniaturized
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2009-03-10 00:27:48 +03:00
|
|
|
if (!haveOpenWindows && !done) {
|
2012-07-30 18:20:58 +04:00
|
|
|
char* argv[] = { nullptr };
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2005-01-17 21:50:18 +03:00
|
|
|
// use an empty command line to make the right kind(s) of window open
|
|
|
|
nsCOMPtr<nsICommandLineRunner> cmdLine
|
|
|
|
(do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
|
|
|
|
NS_ENSURE_TRUE(cmdLine, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = cmdLine->Init(0, argv, nullptr,
|
2005-01-17 21:50:18 +03:00
|
|
|
nsICommandLine::STATE_REMOTE_EXPLICIT);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return cmdLine->Run();
|
2004-06-18 01:23:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // got window mediator
|
|
|
|
return NS_OK;
|
2008-02-20 20:34:21 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2004-06-18 01:23:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2007-08-16 18:53:10 +04:00
|
|
|
// Create and return an instance of class nsNativeAppSupportCocoa.
|
2004-06-18 01:23:51 +04:00
|
|
|
nsresult NS_CreateNativeAppSupport(nsINativeAppSupport**aResult)
|
|
|
|
{
|
2007-08-16 18:53:10 +04:00
|
|
|
*aResult = new nsNativeAppSupportCocoa;
|
2004-06-18 01:23:51 +04:00
|
|
|
if (!*aResult) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2007-08-16 18:53:10 +04:00
|
|
|
NS_ADDREF(*aResult);
|
2004-06-18 01:23:51 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|