1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/xre/nsNativeAppSupportCocoa.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,201 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsString.h" 1.10 + 1.11 +#import <CoreServices/CoreServices.h> 1.12 +#import <Cocoa/Cocoa.h> 1.13 + 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsNativeAppSupportBase.h" 1.16 + 1.17 +#include "nsIAppShellService.h" 1.18 +#include "nsIAppStartup.h" 1.19 +#include "nsIBaseWindow.h" 1.20 +#include "nsICommandLineRunner.h" 1.21 +#include "nsIDOMWindow.h" 1.22 +#include "nsIDocShellTreeItem.h" 1.23 +#include "nsIDocShellTreeOwner.h" 1.24 +#include "nsIInterfaceRequestorUtils.h" 1.25 +#include "nsIObserver.h" 1.26 +#include "nsIServiceManager.h" 1.27 +#include "nsIWebNavigation.h" 1.28 +#include "nsIWidget.h" 1.29 +#include "nsIWindowMediator.h" 1.30 + 1.31 +// This must be included last: 1.32 +#include "nsObjCExceptions.h" 1.33 + 1.34 +nsresult 1.35 +GetNativeWindowPointerFromDOMWindow(nsIDOMWindow *a_window, NSWindow **a_nativeWindow) 1.36 +{ 1.37 + *a_nativeWindow = nil; 1.38 + if (!a_window) 1.39 + return NS_ERROR_INVALID_ARG; 1.40 + 1.41 + nsCOMPtr<nsIWebNavigation> mruWebNav(do_GetInterface(a_window)); 1.42 + if (mruWebNav) { 1.43 + nsCOMPtr<nsIDocShellTreeItem> mruTreeItem(do_QueryInterface(mruWebNav)); 1.44 + nsCOMPtr<nsIDocShellTreeOwner> mruTreeOwner = nullptr; 1.45 + mruTreeItem->GetTreeOwner(getter_AddRefs(mruTreeOwner)); 1.46 + if(mruTreeOwner) { 1.47 + nsCOMPtr<nsIBaseWindow> mruBaseWindow(do_QueryInterface(mruTreeOwner)); 1.48 + if (mruBaseWindow) { 1.49 + nsCOMPtr<nsIWidget> mruWidget = nullptr; 1.50 + mruBaseWindow->GetMainWidget(getter_AddRefs(mruWidget)); 1.51 + if (mruWidget) { 1.52 + *a_nativeWindow = (NSWindow*)mruWidget->GetNativeData(NS_NATIVE_WINDOW); 1.53 + } 1.54 + } 1.55 + } 1.56 + } 1.57 + 1.58 + return NS_OK; 1.59 +} 1.60 + 1.61 +class nsNativeAppSupportCocoa : public nsNativeAppSupportBase 1.62 +{ 1.63 +public: 1.64 + nsNativeAppSupportCocoa() : 1.65 + mCanShowUI(false) { } 1.66 + 1.67 + NS_IMETHOD Start(bool* aRetVal); 1.68 + NS_IMETHOD ReOpen(); 1.69 + NS_IMETHOD Enable(); 1.70 + 1.71 +private: 1.72 + bool mCanShowUI; 1.73 +}; 1.74 + 1.75 +NS_IMETHODIMP 1.76 +nsNativeAppSupportCocoa::Enable() 1.77 +{ 1.78 + mCanShowUI = true; 1.79 + return NS_OK; 1.80 +} 1.81 + 1.82 +#define MAC_OS_X_VERSION_10_6_HEX 0x00001060 1.83 + 1.84 +NS_IMETHODIMP nsNativeAppSupportCocoa::Start(bool *_retval) 1.85 +{ 1.86 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.87 + 1.88 + SInt32 response = 0; 1.89 + OSErr err = ::Gestalt (gestaltSystemVersion, &response); 1.90 + response &= 0xFFFF; // The system version is in the low order word 1.91 + 1.92 + // Check that the OS version is supported, if not return false, 1.93 + // which will make the browser quit. In principle we could display an 1.94 + // alert here. But the alert's message and buttons would require custom 1.95 + // localization. So (for now at least) we just log an English message 1.96 + // to the console before quitting. 1.97 + if ((err != noErr) || response < MAC_OS_X_VERSION_10_6_HEX) { 1.98 + NSLog(@"Minimum OS version requirement not met!"); 1.99 + return NS_OK; 1.100 + } 1.101 + 1.102 + *_retval = true; 1.103 + return NS_OK; 1.104 + 1.105 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.106 +} 1.107 + 1.108 +NS_IMETHODIMP 1.109 +nsNativeAppSupportCocoa::ReOpen() 1.110 +{ 1.111 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 1.112 + 1.113 + if (!mCanShowUI) 1.114 + return NS_ERROR_FAILURE; 1.115 + 1.116 + bool haveNonMiniaturized = false; 1.117 + bool haveOpenWindows = false; 1.118 + bool done = false; 1.119 + 1.120 + nsCOMPtr<nsIWindowMediator> 1.121 + wm(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID)); 1.122 + if (!wm) { 1.123 + return NS_ERROR_FAILURE; 1.124 + } 1.125 + else { 1.126 + nsCOMPtr<nsISimpleEnumerator> windowList; 1.127 + wm->GetXULWindowEnumerator(nullptr, getter_AddRefs(windowList)); 1.128 + bool more; 1.129 + windowList->HasMoreElements(&more); 1.130 + while (more) { 1.131 + nsCOMPtr<nsISupports> nextWindow = nullptr; 1.132 + windowList->GetNext(getter_AddRefs(nextWindow)); 1.133 + nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(nextWindow)); 1.134 + if (!baseWindow) { 1.135 + windowList->HasMoreElements(&more); 1.136 + continue; 1.137 + } 1.138 + else { 1.139 + haveOpenWindows = true; 1.140 + } 1.141 + 1.142 + nsCOMPtr<nsIWidget> widget = nullptr; 1.143 + baseWindow->GetMainWidget(getter_AddRefs(widget)); 1.144 + if (!widget) { 1.145 + windowList->HasMoreElements(&more); 1.146 + continue; 1.147 + } 1.148 + NSWindow *cocoaWindow = (NSWindow*)widget->GetNativeData(NS_NATIVE_WINDOW); 1.149 + if (![cocoaWindow isMiniaturized]) { 1.150 + haveNonMiniaturized = true; 1.151 + break; //have un-minimized windows, nothing to do 1.152 + } 1.153 + windowList->HasMoreElements(&more); 1.154 + } // end while 1.155 + 1.156 + if (!haveNonMiniaturized) { 1.157 + // Deminiaturize the most recenty used window 1.158 + nsCOMPtr<nsIDOMWindow> mru; 1.159 + wm->GetMostRecentWindow(nullptr, getter_AddRefs(mru)); 1.160 + 1.161 + if (mru) { 1.162 + NSWindow *cocoaMru = nil; 1.163 + GetNativeWindowPointerFromDOMWindow(mru, &cocoaMru); 1.164 + if (cocoaMru) { 1.165 + [cocoaMru deminiaturize:nil]; 1.166 + done = true; 1.167 + } 1.168 + } 1.169 + 1.170 + } // end if have non miniaturized 1.171 + 1.172 + if (!haveOpenWindows && !done) { 1.173 + char* argv[] = { nullptr }; 1.174 + 1.175 + // use an empty command line to make the right kind(s) of window open 1.176 + nsCOMPtr<nsICommandLineRunner> cmdLine 1.177 + (do_CreateInstance("@mozilla.org/toolkit/command-line;1")); 1.178 + NS_ENSURE_TRUE(cmdLine, NS_ERROR_FAILURE); 1.179 + 1.180 + nsresult rv; 1.181 + rv = cmdLine->Init(0, argv, nullptr, 1.182 + nsICommandLine::STATE_REMOTE_EXPLICIT); 1.183 + NS_ENSURE_SUCCESS(rv, rv); 1.184 + 1.185 + return cmdLine->Run(); 1.186 + } 1.187 + 1.188 + } // got window mediator 1.189 + return NS_OK; 1.190 + 1.191 + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; 1.192 +} 1.193 + 1.194 +#pragma mark - 1.195 + 1.196 +// Create and return an instance of class nsNativeAppSupportCocoa. 1.197 +nsresult NS_CreateNativeAppSupport(nsINativeAppSupport**aResult) 1.198 +{ 1.199 + *aResult = new nsNativeAppSupportCocoa; 1.200 + if (!*aResult) return NS_ERROR_OUT_OF_MEMORY; 1.201 + 1.202 + NS_ADDREF(*aResult); 1.203 + return NS_OK; 1.204 +}