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"
|
2014-11-29 01:28:59 +03:00
|
|
|
#include "nsCocoaFeatures.h"
|
2004-06-18 01:23:51 +04:00
|
|
|
#include "nsNativeAppSupportBase.h"
|
|
|
|
|
|
|
|
#include "nsIBaseWindow.h"
|
2018-09-15 12:00:49 +03:00
|
|
|
#include "nsCommandLine.h"
|
2016-01-30 20:05:36 +03:00
|
|
|
#include "mozIDOMWindow.h"
|
2004-06-18 01:23:51 +04:00
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsIWindowMediator.h"
|
2020-04-21 15:31:51 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "WidgetUtils.h"
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
// This must be included last:
|
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
|
2020-04-21 15:31:51 +03:00
|
|
|
using mozilla::widget::WidgetUtils;
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsresult GetNativeWindowPointerFromDOMWindow(mozIDOMWindowProxy* a_window,
|
|
|
|
NSWindow** a_nativeWindow) {
|
2010-03-10 22:30:11 +03:00
|
|
|
*a_nativeWindow = nil;
|
|
|
|
if (!a_window) return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2020-04-21 15:31:51 +03:00
|
|
|
nsPIDOMWindowOuter* win = nsPIDOMWindowOuter::From(a_window);
|
|
|
|
nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(win);
|
|
|
|
if (!widget) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-03-10 22:30:11 +03:00
|
|
|
}
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2020-04-21 15:31:51 +03:00
|
|
|
*a_nativeWindow = (NSWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);
|
|
|
|
|
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() : mCanShowUI(false) {}
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
NS_IMETHOD Start(bool* aRetVal) override;
|
|
|
|
NS_IMETHOD ReOpen() override;
|
|
|
|
NS_IMETHOD Enable() override;
|
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() {
|
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
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsNativeAppSupportCocoa::Start(bool* _retval) {
|
2014-11-29 01:28:59 +03:00
|
|
|
int major, minor, bugfix;
|
|
|
|
nsCocoaFeatures::GetSystemVersion(major, minor, bugfix);
|
2008-02-20 20:34:21 +03:00
|
|
|
|
2014-11-29 01:28:59 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
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.
|
2020-08-14 17:20:37 +03:00
|
|
|
if (major < 10 || (major == 10 && minor < 12)) {
|
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;
|
2019-03-28 22:00:17 +03:00
|
|
|
|
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() {
|
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;
|
2019-01-21 20:18:16 +03:00
|
|
|
|
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;
|
2019-11-05 20:56:28 +03:00
|
|
|
wm->GetAppWindowEnumerator(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
|
2016-01-30 20:05:36 +03:00
|
|
|
}
|
2004-06-18 01:23:51 +04:00
|
|
|
windowList->HasMoreElements(&more);
|
|
|
|
} // end while
|
2016-01-30 20:05:36 +03:00
|
|
|
|
2009-03-10 00:27:48 +03:00
|
|
|
if (!haveNonMiniaturized) {
|
2007-08-16 18:53:10 +04:00
|
|
|
// Deminiaturize the most recenty used window
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<mozIDOMWindowProxy> mru;
|
2012-07-30 18:20:58 +04:00
|
|
|
wm->GetMostRecentWindow(nullptr, getter_AddRefs(mru));
|
2016-01-30 20:05:36 +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
|
2016-01-30 20:05:36 +03:00
|
|
|
|
2009-03-10 00:27:48 +03:00
|
|
|
if (!haveOpenWindows && !done) {
|
2012-07-30 18:20:58 +04:00
|
|
|
char* argv[] = {nullptr};
|
2016-01-30 20:05:36 +03:00
|
|
|
|
2005-01-17 21:50:18 +03:00
|
|
|
// use an empty command line to make the right kind(s) of window open
|
2018-09-15 12:00:49 +03:00
|
|
|
nsCOMPtr<nsICommandLineRunner> cmdLine(new nsCommandLine());
|
2005-01-17 21:50:18 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = cmdLine->Init(0, argv, nullptr, nsICommandLine::STATE_REMOTE_EXPLICIT);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return cmdLine->Run();
|
2004-06-18 01:23:51 +04:00
|
|
|
}
|
2019-01-21 20:18:16 +03:00
|
|
|
|
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;
|
|
|
|
}
|