widget/windows/nsToolkit.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 "nsToolkit.h"
michael@0 7 #include "nsAppShell.h"
michael@0 8 #include "nsWindow.h"
michael@0 9 #include "nsWidgetsCID.h"
michael@0 10 #include "prmon.h"
michael@0 11 #include "prtime.h"
michael@0 12 #include "nsIServiceManager.h"
michael@0 13 #include "nsComponentManagerUtils.h"
michael@0 14 #include <objbase.h>
michael@0 15 #include "WinUtils.h"
michael@0 16
michael@0 17 #include "nsUXThemeData.h"
michael@0 18
michael@0 19 // unknwn.h is needed to build with WIN32_LEAN_AND_MEAN
michael@0 20 #include <unknwn.h>
michael@0 21
michael@0 22 using namespace mozilla::widget;
michael@0 23
michael@0 24 nsToolkit* nsToolkit::gToolkit = nullptr;
michael@0 25 HINSTANCE nsToolkit::mDllInstance = 0;
michael@0 26 static const unsigned long kD3DUsageDelay = 5000;
michael@0 27
michael@0 28 static void
michael@0 29 StartAllowingD3D9(nsITimer *aTimer, void *aClosure)
michael@0 30 {
michael@0 31 if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
michael@0 32 nsWindow::StartAllowingD3D9(true);
michael@0 33 }
michael@0 34 }
michael@0 35
michael@0 36 MouseTrailer* nsToolkit::gMouseTrailer;
michael@0 37
michael@0 38 //-------------------------------------------------------------------------
michael@0 39 //
michael@0 40 // constructor
michael@0 41 //
michael@0 42 //-------------------------------------------------------------------------
michael@0 43 nsToolkit::nsToolkit()
michael@0 44 {
michael@0 45 MOZ_COUNT_CTOR(nsToolkit);
michael@0 46
michael@0 47 #if defined(MOZ_STATIC_COMPONENT_LIBS)
michael@0 48 nsToolkit::Startup(GetModuleHandle(nullptr));
michael@0 49 #endif
michael@0 50
michael@0 51 gMouseTrailer = &mMouseTrailer;
michael@0 52
michael@0 53 if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
michael@0 54 mD3D9Timer = do_CreateInstance("@mozilla.org/timer;1");
michael@0 55 mD3D9Timer->InitWithFuncCallback(::StartAllowingD3D9,
michael@0 56 nullptr,
michael@0 57 kD3DUsageDelay,
michael@0 58 nsITimer::TYPE_ONE_SHOT);
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62
michael@0 63 //-------------------------------------------------------------------------
michael@0 64 //
michael@0 65 // destructor
michael@0 66 //
michael@0 67 //-------------------------------------------------------------------------
michael@0 68 nsToolkit::~nsToolkit()
michael@0 69 {
michael@0 70 MOZ_COUNT_DTOR(nsToolkit);
michael@0 71 gMouseTrailer = nullptr;
michael@0 72 }
michael@0 73
michael@0 74 void
michael@0 75 nsToolkit::Startup(HMODULE hModule)
michael@0 76 {
michael@0 77 nsToolkit::mDllInstance = hModule;
michael@0 78 WinUtils::Initialize();
michael@0 79 nsUXThemeData::Initialize();
michael@0 80 }
michael@0 81
michael@0 82 void
michael@0 83 nsToolkit::Shutdown()
michael@0 84 {
michael@0 85 delete gToolkit;
michael@0 86 gToolkit = nullptr;
michael@0 87 }
michael@0 88
michael@0 89 void
michael@0 90 nsToolkit::StartAllowingD3D9()
michael@0 91 {
michael@0 92 if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
michael@0 93 nsToolkit::GetToolkit()->mD3D9Timer->Cancel();
michael@0 94 nsWindow::StartAllowingD3D9(false);
michael@0 95 }
michael@0 96 }
michael@0 97
michael@0 98 //-------------------------------------------------------------------------
michael@0 99 //
michael@0 100 // Return the nsToolkit for the current thread. If a toolkit does not
michael@0 101 // yet exist, then one will be created...
michael@0 102 //
michael@0 103 //-------------------------------------------------------------------------
michael@0 104 // static
michael@0 105 nsToolkit* nsToolkit::GetToolkit()
michael@0 106 {
michael@0 107 if (!gToolkit) {
michael@0 108 gToolkit = new nsToolkit();
michael@0 109 }
michael@0 110
michael@0 111 return gToolkit;
michael@0 112 }
michael@0 113
michael@0 114
michael@0 115 //-------------------------------------------------------------------------
michael@0 116 //
michael@0 117 //
michael@0 118 //-------------------------------------------------------------------------
michael@0 119 MouseTrailer::MouseTrailer() : mMouseTrailerWindow(nullptr), mCaptureWindow(nullptr),
michael@0 120 mIsInCaptureMode(false), mEnabled(true)
michael@0 121 {
michael@0 122 }
michael@0 123 //-------------------------------------------------------------------------
michael@0 124 //
michael@0 125 //
michael@0 126 //-------------------------------------------------------------------------
michael@0 127 MouseTrailer::~MouseTrailer()
michael@0 128 {
michael@0 129 DestroyTimer();
michael@0 130 }
michael@0 131 //-------------------------------------------------------------------------
michael@0 132 //
michael@0 133 //
michael@0 134 //-------------------------------------------------------------------------
michael@0 135 void MouseTrailer::SetMouseTrailerWindow(HWND aWnd)
michael@0 136 {
michael@0 137 if (mMouseTrailerWindow != aWnd && mTimer) {
michael@0 138 // Make sure TimerProc is fired at least once for the old window
michael@0 139 TimerProc(nullptr, nullptr);
michael@0 140 }
michael@0 141 mMouseTrailerWindow = aWnd;
michael@0 142 CreateTimer();
michael@0 143 }
michael@0 144
michael@0 145 //-------------------------------------------------------------------------
michael@0 146 //
michael@0 147 //
michael@0 148 //-------------------------------------------------------------------------
michael@0 149 void MouseTrailer::SetCaptureWindow(HWND aWnd)
michael@0 150 {
michael@0 151 mCaptureWindow = aWnd;
michael@0 152 if (mCaptureWindow) {
michael@0 153 mIsInCaptureMode = true;
michael@0 154 }
michael@0 155 }
michael@0 156
michael@0 157 //-------------------------------------------------------------------------
michael@0 158 //
michael@0 159 //
michael@0 160 //-------------------------------------------------------------------------
michael@0 161 nsresult MouseTrailer::CreateTimer()
michael@0 162 {
michael@0 163 if (mTimer || !mEnabled) {
michael@0 164 return NS_OK;
michael@0 165 }
michael@0 166
michael@0 167 nsresult rv;
michael@0 168 mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
michael@0 169 NS_ENSURE_SUCCESS(rv, rv);
michael@0 170
michael@0 171 return mTimer->InitWithFuncCallback(TimerProc, nullptr, 200,
michael@0 172 nsITimer::TYPE_REPEATING_SLACK);
michael@0 173 }
michael@0 174
michael@0 175 //-------------------------------------------------------------------------
michael@0 176 //
michael@0 177 //
michael@0 178 //-------------------------------------------------------------------------
michael@0 179 void MouseTrailer::DestroyTimer()
michael@0 180 {
michael@0 181 if (mTimer) {
michael@0 182 mTimer->Cancel();
michael@0 183 mTimer = nullptr;
michael@0 184 }
michael@0 185 }
michael@0 186
michael@0 187 //-------------------------------------------------------------------------
michael@0 188 //
michael@0 189 //
michael@0 190 //-------------------------------------------------------------------------
michael@0 191 void MouseTrailer::TimerProc(nsITimer* aTimer, void* aClosure)
michael@0 192 {
michael@0 193 MouseTrailer *mtrailer = nsToolkit::gMouseTrailer;
michael@0 194 NS_ASSERTION(mtrailer, "MouseTrailer still firing after deletion!");
michael@0 195
michael@0 196 // Check to see if we are in mouse capture mode,
michael@0 197 // Once capture ends we could still get back one more timer event.
michael@0 198 // Capture could end outside our window.
michael@0 199 // Also, for some reason when the mouse is on the frame it thinks that
michael@0 200 // it is inside the window that is being captured.
michael@0 201 if (mtrailer->mCaptureWindow) {
michael@0 202 if (mtrailer->mCaptureWindow != mtrailer->mMouseTrailerWindow) {
michael@0 203 return;
michael@0 204 }
michael@0 205 } else {
michael@0 206 if (mtrailer->mIsInCaptureMode) {
michael@0 207 // mMouseTrailerWindow could be bad from rolling over the frame, so clear
michael@0 208 // it if we were capturing and now this is the first timer callback
michael@0 209 // since we canceled the capture
michael@0 210 mtrailer->mMouseTrailerWindow = nullptr;
michael@0 211 mtrailer->mIsInCaptureMode = false;
michael@0 212 return;
michael@0 213 }
michael@0 214 }
michael@0 215
michael@0 216 if (mtrailer->mMouseTrailerWindow && ::IsWindow(mtrailer->mMouseTrailerWindow)) {
michael@0 217 POINT mp;
michael@0 218 DWORD pos = ::GetMessagePos();
michael@0 219 mp.x = GET_X_LPARAM(pos);
michael@0 220 mp.y = GET_Y_LPARAM(pos);
michael@0 221 HWND mouseWnd = ::WindowFromPoint(mp);
michael@0 222 if (mtrailer->mMouseTrailerWindow != mouseWnd) {
michael@0 223 // Notify someone that a mouse exit happened.
michael@0 224 PostMessage(mtrailer->mMouseTrailerWindow, WM_MOUSELEAVE, 0, 0);
michael@0 225
michael@0 226 // we are out of this window, destroy timer
michael@0 227 mtrailer->DestroyTimer();
michael@0 228 mtrailer->mMouseTrailerWindow = nullptr;
michael@0 229 }
michael@0 230 } else {
michael@0 231 mtrailer->DestroyTimer();
michael@0 232 mtrailer->mMouseTrailerWindow = nullptr;
michael@0 233 }
michael@0 234 }
michael@0 235

mercurial