widget/windows/winrt/MetroUtils.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: 4; 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 "MetroUtils.h"
michael@0 7 #include <windows.h>
michael@0 8 #include "nsICommandLineRunner.h"
michael@0 9 #include "nsNetUtil.h"
michael@0 10 #include "nsIBrowserDOMWindow.h"
michael@0 11 #include "nsIWebNavigation.h"
michael@0 12 #include "nsIDocShellTreeItem.h"
michael@0 13 #include "nsIDOMWindow.h"
michael@0 14 #include "nsIDOMChromeWindow.h"
michael@0 15 #include "nsIWindowMediator.h"
michael@0 16 #include "nsIURI.h"
michael@0 17 #include "prlog.h"
michael@0 18 #include "nsIObserverService.h"
michael@0 19 #include "nsRect.h"
michael@0 20
michael@0 21 #include <wrl/wrappers/corewrappers.h>
michael@0 22 #include <windows.ui.applicationsettings.h>
michael@0 23 #include <windows.graphics.display.h>
michael@0 24 #include "DisplayInfo_sdk81.h"
michael@0 25
michael@0 26 using namespace ABI::Windows::UI::ApplicationSettings;
michael@0 27
michael@0 28 using namespace mozilla;
michael@0 29
michael@0 30 using namespace ABI::Windows::Foundation;
michael@0 31 using namespace Microsoft::WRL;
michael@0 32 using namespace Microsoft::WRL::Wrappers;
michael@0 33 using namespace ABI::Windows::UI::ViewManagement;
michael@0 34 using namespace ABI::Windows::Graphics::Display;
michael@0 35
michael@0 36 // Conversion between logical and physical coordinates
michael@0 37
michael@0 38 double
michael@0 39 MetroUtils::LogToPhysFactor()
michael@0 40 {
michael@0 41 ComPtr<IDisplayInformationStatics> dispInfoStatics;
michael@0 42 if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayInformation).Get(),
michael@0 43 dispInfoStatics.GetAddressOf()))) {
michael@0 44 ComPtr<IDisplayInformation> dispInfo;
michael@0 45 if (SUCCEEDED(dispInfoStatics->GetForCurrentView(&dispInfo))) {
michael@0 46 FLOAT dpi;
michael@0 47 if (SUCCEEDED(dispInfo->get_LogicalDpi(&dpi))) {
michael@0 48 return (double)dpi / 96.0f;
michael@0 49 }
michael@0 50 }
michael@0 51 }
michael@0 52
michael@0 53 ComPtr<IDisplayPropertiesStatics> dispProps;
michael@0 54 if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayProperties).Get(),
michael@0 55 dispProps.GetAddressOf()))) {
michael@0 56 FLOAT dpi;
michael@0 57 if (SUCCEEDED(dispProps->get_LogicalDpi(&dpi))) {
michael@0 58 return (double)dpi / 96.0f;
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62 return 1.0;
michael@0 63 }
michael@0 64
michael@0 65 double
michael@0 66 MetroUtils::PhysToLogFactor()
michael@0 67 {
michael@0 68 return 1.0 / LogToPhysFactor();
michael@0 69 }
michael@0 70
michael@0 71 double
michael@0 72 MetroUtils::ScaleFactor()
michael@0 73 {
michael@0 74 // Return the resolution scale factor reported by the metro environment.
michael@0 75 // XXX TODO: also consider the desktop resolution setting, as IE appears to do?
michael@0 76 ComPtr<IDisplayInformationStatics> dispInfoStatics;
michael@0 77 if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayInformation).Get(),
michael@0 78 dispInfoStatics.GetAddressOf()))) {
michael@0 79 ComPtr<IDisplayInformation> dispInfo;
michael@0 80 if (SUCCEEDED(dispInfoStatics->GetForCurrentView(&dispInfo))) {
michael@0 81 ResolutionScale scale;
michael@0 82 if (SUCCEEDED(dispInfo->get_ResolutionScale(&scale))) {
michael@0 83 return (double)scale / 100.0;
michael@0 84 }
michael@0 85 }
michael@0 86 }
michael@0 87
michael@0 88 ComPtr<IDisplayPropertiesStatics> dispProps;
michael@0 89 if (SUCCEEDED(GetActivationFactory(HStringReference(RuntimeClass_Windows_Graphics_Display_DisplayProperties).Get(),
michael@0 90 dispProps.GetAddressOf()))) {
michael@0 91 ResolutionScale scale;
michael@0 92 if (SUCCEEDED(dispProps->get_ResolutionScale(&scale))) {
michael@0 93 return (double)scale / 100.0;
michael@0 94 }
michael@0 95 }
michael@0 96
michael@0 97 return 1.0;
michael@0 98 }
michael@0 99
michael@0 100 nsIntPoint
michael@0 101 MetroUtils::LogToPhys(const Point& aPt)
michael@0 102 {
michael@0 103 double factor = LogToPhysFactor();
michael@0 104 return nsIntPoint(int32_t(NS_round(aPt.X * factor)), int32_t(NS_round(aPt.Y * factor)));
michael@0 105 }
michael@0 106
michael@0 107 nsIntRect
michael@0 108 MetroUtils::LogToPhys(const Rect& aRect)
michael@0 109 {
michael@0 110 double factor = LogToPhysFactor();
michael@0 111 return nsIntRect(int32_t(NS_round(aRect.X * factor)),
michael@0 112 int32_t(NS_round(aRect.Y * factor)),
michael@0 113 int32_t(NS_round(aRect.Width * factor)),
michael@0 114 int32_t(NS_round(aRect.Height * factor)));
michael@0 115 }
michael@0 116
michael@0 117 Point
michael@0 118 MetroUtils::PhysToLog(const nsIntPoint& aPt)
michael@0 119 {
michael@0 120 // Points contain FLOATs
michael@0 121 FLOAT factor = (FLOAT)PhysToLogFactor();
michael@0 122 Point p = { FLOAT(aPt.x) * factor, FLOAT(aPt.y) * factor };
michael@0 123 return p;
michael@0 124 }
michael@0 125
michael@0 126 nsresult
michael@0 127 MetroUtils::FireObserver(const char* aMessage, const char16_t* aData)
michael@0 128 {
michael@0 129 nsCOMPtr<nsIObserverService> observerService =
michael@0 130 mozilla::services::GetObserverService();
michael@0 131 if (observerService) {
michael@0 132 return observerService->NotifyObservers(nullptr, aMessage, aData);
michael@0 133 }
michael@0 134 return NS_ERROR_FAILURE;
michael@0 135 }
michael@0 136
michael@0 137 HRESULT MetroUtils::CreateUri(HSTRING aUriStr, ComPtr<IUriRuntimeClass>& aUriOut)
michael@0 138 {
michael@0 139 HRESULT hr;
michael@0 140 ComPtr<IUriRuntimeClassFactory> uriFactory;
michael@0 141 hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriFactory);
michael@0 142 AssertRetHRESULT(hr, hr);
michael@0 143 ComPtr<IUriRuntimeClass> uri;
michael@0 144 return uriFactory->CreateUri(aUriStr, &aUriOut);
michael@0 145 }
michael@0 146
michael@0 147 HRESULT MetroUtils::CreateUri(HString& aHString, ComPtr<IUriRuntimeClass>& aUriOut)
michael@0 148 {
michael@0 149 return MetroUtils::CreateUri(aHString.Get(), aUriOut);
michael@0 150 }
michael@0 151
michael@0 152 HRESULT
michael@0 153 MetroUtils::GetViewState(ApplicationViewState& aState)
michael@0 154 {
michael@0 155 HRESULT hr;
michael@0 156 ComPtr<IApplicationViewStatics> appViewStatics;
michael@0 157 hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_ViewManagement_ApplicationView).Get(),
michael@0 158 appViewStatics.GetAddressOf());
michael@0 159 AssertRetHRESULT(hr, hr);
michael@0 160 hr = appViewStatics->get_Value(&aState);
michael@0 161 return hr;
michael@0 162 }
michael@0 163
michael@0 164 HRESULT
michael@0 165 MetroUtils::TryUnsnap(bool* aResult)
michael@0 166 {
michael@0 167 HRESULT hr;
michael@0 168 ComPtr<IApplicationViewStatics> appViewStatics;
michael@0 169 hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_ViewManagement_ApplicationView).Get(),
michael@0 170 appViewStatics.GetAddressOf());
michael@0 171 AssertRetHRESULT(hr, hr);
michael@0 172 boolean success = false;
michael@0 173 hr = appViewStatics->TryUnsnap(&success);
michael@0 174 if (aResult)
michael@0 175 *aResult = success;
michael@0 176 return hr;
michael@0 177 }
michael@0 178
michael@0 179 HRESULT
michael@0 180 MetroUtils::ShowSettingsFlyout()
michael@0 181 {
michael@0 182 ComPtr<ISettingsPaneStatics> settingsPaneStatics;
michael@0 183 HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_ApplicationSettings_SettingsPane).Get(),
michael@0 184 settingsPaneStatics.GetAddressOf());
michael@0 185 if (SUCCEEDED(hr)) {
michael@0 186 settingsPaneStatics->Show();
michael@0 187 }
michael@0 188
michael@0 189 return hr;
michael@0 190 }

mercurial