toolkit/xre/nsNativeAppSupportCocoa.mm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsString.h"
michael@0 7
michael@0 8 #import <CoreServices/CoreServices.h>
michael@0 9 #import <Cocoa/Cocoa.h>
michael@0 10
michael@0 11 #include "nsCOMPtr.h"
michael@0 12 #include "nsNativeAppSupportBase.h"
michael@0 13
michael@0 14 #include "nsIAppShellService.h"
michael@0 15 #include "nsIAppStartup.h"
michael@0 16 #include "nsIBaseWindow.h"
michael@0 17 #include "nsICommandLineRunner.h"
michael@0 18 #include "nsIDOMWindow.h"
michael@0 19 #include "nsIDocShellTreeItem.h"
michael@0 20 #include "nsIDocShellTreeOwner.h"
michael@0 21 #include "nsIInterfaceRequestorUtils.h"
michael@0 22 #include "nsIObserver.h"
michael@0 23 #include "nsIServiceManager.h"
michael@0 24 #include "nsIWebNavigation.h"
michael@0 25 #include "nsIWidget.h"
michael@0 26 #include "nsIWindowMediator.h"
michael@0 27
michael@0 28 // This must be included last:
michael@0 29 #include "nsObjCExceptions.h"
michael@0 30
michael@0 31 nsresult
michael@0 32 GetNativeWindowPointerFromDOMWindow(nsIDOMWindow *a_window, NSWindow **a_nativeWindow)
michael@0 33 {
michael@0 34 *a_nativeWindow = nil;
michael@0 35 if (!a_window)
michael@0 36 return NS_ERROR_INVALID_ARG;
michael@0 37
michael@0 38 nsCOMPtr<nsIWebNavigation> mruWebNav(do_GetInterface(a_window));
michael@0 39 if (mruWebNav) {
michael@0 40 nsCOMPtr<nsIDocShellTreeItem> mruTreeItem(do_QueryInterface(mruWebNav));
michael@0 41 nsCOMPtr<nsIDocShellTreeOwner> mruTreeOwner = nullptr;
michael@0 42 mruTreeItem->GetTreeOwner(getter_AddRefs(mruTreeOwner));
michael@0 43 if(mruTreeOwner) {
michael@0 44 nsCOMPtr<nsIBaseWindow> mruBaseWindow(do_QueryInterface(mruTreeOwner));
michael@0 45 if (mruBaseWindow) {
michael@0 46 nsCOMPtr<nsIWidget> mruWidget = nullptr;
michael@0 47 mruBaseWindow->GetMainWidget(getter_AddRefs(mruWidget));
michael@0 48 if (mruWidget) {
michael@0 49 *a_nativeWindow = (NSWindow*)mruWidget->GetNativeData(NS_NATIVE_WINDOW);
michael@0 50 }
michael@0 51 }
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 return NS_OK;
michael@0 56 }
michael@0 57
michael@0 58 class nsNativeAppSupportCocoa : public nsNativeAppSupportBase
michael@0 59 {
michael@0 60 public:
michael@0 61 nsNativeAppSupportCocoa() :
michael@0 62 mCanShowUI(false) { }
michael@0 63
michael@0 64 NS_IMETHOD Start(bool* aRetVal);
michael@0 65 NS_IMETHOD ReOpen();
michael@0 66 NS_IMETHOD Enable();
michael@0 67
michael@0 68 private:
michael@0 69 bool mCanShowUI;
michael@0 70 };
michael@0 71
michael@0 72 NS_IMETHODIMP
michael@0 73 nsNativeAppSupportCocoa::Enable()
michael@0 74 {
michael@0 75 mCanShowUI = true;
michael@0 76 return NS_OK;
michael@0 77 }
michael@0 78
michael@0 79 #define MAC_OS_X_VERSION_10_6_HEX 0x00001060
michael@0 80
michael@0 81 NS_IMETHODIMP nsNativeAppSupportCocoa::Start(bool *_retval)
michael@0 82 {
michael@0 83 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 84
michael@0 85 SInt32 response = 0;
michael@0 86 OSErr err = ::Gestalt (gestaltSystemVersion, &response);
michael@0 87 response &= 0xFFFF; // The system version is in the low order word
michael@0 88
michael@0 89 // Check that the OS version is supported, if not return false,
michael@0 90 // which will make the browser quit. In principle we could display an
michael@0 91 // alert here. But the alert's message and buttons would require custom
michael@0 92 // localization. So (for now at least) we just log an English message
michael@0 93 // to the console before quitting.
michael@0 94 if ((err != noErr) || response < MAC_OS_X_VERSION_10_6_HEX) {
michael@0 95 NSLog(@"Minimum OS version requirement not met!");
michael@0 96 return NS_OK;
michael@0 97 }
michael@0 98
michael@0 99 *_retval = true;
michael@0 100 return NS_OK;
michael@0 101
michael@0 102 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 103 }
michael@0 104
michael@0 105 NS_IMETHODIMP
michael@0 106 nsNativeAppSupportCocoa::ReOpen()
michael@0 107 {
michael@0 108 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 109
michael@0 110 if (!mCanShowUI)
michael@0 111 return NS_ERROR_FAILURE;
michael@0 112
michael@0 113 bool haveNonMiniaturized = false;
michael@0 114 bool haveOpenWindows = false;
michael@0 115 bool done = false;
michael@0 116
michael@0 117 nsCOMPtr<nsIWindowMediator>
michael@0 118 wm(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
michael@0 119 if (!wm) {
michael@0 120 return NS_ERROR_FAILURE;
michael@0 121 }
michael@0 122 else {
michael@0 123 nsCOMPtr<nsISimpleEnumerator> windowList;
michael@0 124 wm->GetXULWindowEnumerator(nullptr, getter_AddRefs(windowList));
michael@0 125 bool more;
michael@0 126 windowList->HasMoreElements(&more);
michael@0 127 while (more) {
michael@0 128 nsCOMPtr<nsISupports> nextWindow = nullptr;
michael@0 129 windowList->GetNext(getter_AddRefs(nextWindow));
michael@0 130 nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(nextWindow));
michael@0 131 if (!baseWindow) {
michael@0 132 windowList->HasMoreElements(&more);
michael@0 133 continue;
michael@0 134 }
michael@0 135 else {
michael@0 136 haveOpenWindows = true;
michael@0 137 }
michael@0 138
michael@0 139 nsCOMPtr<nsIWidget> widget = nullptr;
michael@0 140 baseWindow->GetMainWidget(getter_AddRefs(widget));
michael@0 141 if (!widget) {
michael@0 142 windowList->HasMoreElements(&more);
michael@0 143 continue;
michael@0 144 }
michael@0 145 NSWindow *cocoaWindow = (NSWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);
michael@0 146 if (![cocoaWindow isMiniaturized]) {
michael@0 147 haveNonMiniaturized = true;
michael@0 148 break; //have un-minimized windows, nothing to do
michael@0 149 }
michael@0 150 windowList->HasMoreElements(&more);
michael@0 151 } // end while
michael@0 152
michael@0 153 if (!haveNonMiniaturized) {
michael@0 154 // Deminiaturize the most recenty used window
michael@0 155 nsCOMPtr<nsIDOMWindow> mru;
michael@0 156 wm->GetMostRecentWindow(nullptr, getter_AddRefs(mru));
michael@0 157
michael@0 158 if (mru) {
michael@0 159 NSWindow *cocoaMru = nil;
michael@0 160 GetNativeWindowPointerFromDOMWindow(mru, &cocoaMru);
michael@0 161 if (cocoaMru) {
michael@0 162 [cocoaMru deminiaturize:nil];
michael@0 163 done = true;
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 } // end if have non miniaturized
michael@0 168
michael@0 169 if (!haveOpenWindows && !done) {
michael@0 170 char* argv[] = { nullptr };
michael@0 171
michael@0 172 // use an empty command line to make the right kind(s) of window open
michael@0 173 nsCOMPtr<nsICommandLineRunner> cmdLine
michael@0 174 (do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
michael@0 175 NS_ENSURE_TRUE(cmdLine, NS_ERROR_FAILURE);
michael@0 176
michael@0 177 nsresult rv;
michael@0 178 rv = cmdLine->Init(0, argv, nullptr,
michael@0 179 nsICommandLine::STATE_REMOTE_EXPLICIT);
michael@0 180 NS_ENSURE_SUCCESS(rv, rv);
michael@0 181
michael@0 182 return cmdLine->Run();
michael@0 183 }
michael@0 184
michael@0 185 } // got window mediator
michael@0 186 return NS_OK;
michael@0 187
michael@0 188 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 189 }
michael@0 190
michael@0 191 #pragma mark -
michael@0 192
michael@0 193 // Create and return an instance of class nsNativeAppSupportCocoa.
michael@0 194 nsresult NS_CreateNativeAppSupport(nsINativeAppSupport**aResult)
michael@0 195 {
michael@0 196 *aResult = new nsNativeAppSupportCocoa;
michael@0 197 if (!*aResult) return NS_ERROR_OUT_OF_MEMORY;
michael@0 198
michael@0 199 NS_ADDREF(*aResult);
michael@0 200 return NS_OK;
michael@0 201 }

mercurial