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