DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (31ec81b5d7bb)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsWinMetroUtils.h"
#include "MetroUtils.h"
#include "nsXULAppAPI.h"
#include "FrameworkView.h"
#include "MetroApp.h"
#include "nsIWindowsRegKey.h"
#include "ToastNotificationHandler.h"

#include <shldisp.h>
#include <shellapi.h>
#include <windows.ui.viewmanagement.h>
#include <windows.ui.startscreen.h>
#include <Wincrypt.h>

using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::UI::StartScreen;
using namespace ABI::Windows::UI::ViewManagement;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace mozilla::widget::winrt;

namespace mozilla {
namespace widget {
namespace winrt {
extern ComPtr<MetroApp> sMetroApp;
extern nsTArray<nsString>* sSettingsArray;
extern ComPtr<FrameworkView> sFrameworkView;
} } }

namespace mozilla {
namespace widget {

static LPCWSTR sSyncEmailField = L"sync-e";
static LPCWSTR sSyncPasswordField = L"sync-p";
static LPCWSTR sSyncKeyField = L"sync-k";
static LPCSTR sRegPath = "Software\\Mozilla\\Firefox";

NS_IMPL_ISUPPORTS1(nsWinMetroUtils, nsIWinMetroUtils)

nsWinMetroUtils::nsWinMetroUtils()
{
}

nsWinMetroUtils::~nsWinMetroUtils()
{
}

/**
 * Pins a new tile to the Windows 8 start screen.
 *
 * @param aTileID         An ID which can later be used to remove the tile
 * @param aShortName      A short name for the tile
 * @param aDiplayName     The name that will be displayed on the tile
 * @param aActivationArgs The arguments to pass to the browser upon
 *                        activation of the tile
 * @param aTileImage An image for the normal tile view
 * @param aSmallTileImage An image for the small tile view
 */
NS_IMETHODIMP
nsWinMetroUtils::PinTileAsync(const nsAString &aTileID,
                              const nsAString &aShortName,
                              const nsAString &aDisplayName,
                              const nsAString &aActivationArgs,
                              const nsAString &aTileImage,
                              const nsAString &aSmallTileImage)
{
  if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
    NS_WARNING("PinTileAsync can't be called on the desktop.");
    return NS_ERROR_FAILURE;
  }
  HRESULT hr;

  HString logoStr, smallLogoStr, displayName, shortName;

  logoStr.Set(aTileImage.BeginReading());
  smallLogoStr.Set(aSmallTileImage.BeginReading());
  displayName.Set(aDisplayName.BeginReading());
  shortName.Set(aShortName.BeginReading());

  ComPtr<IUriRuntimeClass> logo, smallLogo;
  AssertRetHRESULT(MetroUtils::CreateUri(logoStr, logo), NS_ERROR_FAILURE);
  AssertRetHRESULT(MetroUtils::CreateUri(smallLogoStr, smallLogo), NS_ERROR_FAILURE);

  HString tileActivationArgumentsStr, tileIdStr;
  tileActivationArgumentsStr.Set(aActivationArgs.BeginReading());
  tileIdStr.Set(aTileID.BeginReading());

  ComPtr<ISecondaryTileFactory> tileFactory;
  ComPtr<ISecondaryTile> secondaryTile;
  hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_StartScreen_SecondaryTile).Get(),
                            tileFactory.GetAddressOf());
  AssertRetHRESULT(hr, NS_ERROR_FAILURE);
  hr = tileFactory->CreateWithId(tileIdStr.Get(), secondaryTile.GetAddressOf());
  AssertRetHRESULT(hr, NS_ERROR_FAILURE);

  secondaryTile->put_Logo(logo.Get());
  secondaryTile->put_SmallLogo(smallLogo.Get());
  secondaryTile->put_DisplayName(displayName.Get());
  secondaryTile->put_ShortName(shortName.Get());
  secondaryTile->put_Arguments(tileActivationArgumentsStr.Get());
  secondaryTile->put_TileOptions(TileOptions::TileOptions_ShowNameOnLogo);

  // The tile is created and we can now attempt to pin the tile.
  ComPtr<IAsyncOperationCompletedHandler<bool>> callback(Callback<IAsyncOperationCompletedHandler<bool>>(
    sMetroApp.Get(), &MetroApp::OnAsyncTileCreated));
  ComPtr<IAsyncOperation<bool>> operation;
  AssertRetHRESULT(secondaryTile->RequestCreateAsync(operation.GetAddressOf()), NS_ERROR_FAILURE);
  operation->put_Completed(callback.Get());
  return NS_OK;
}

/**
 * Unpins a tile from the Windows 8 start screen.
 *
 * @param aTileID An existing ID which was previously pinned
 */
NS_IMETHODIMP
nsWinMetroUtils::UnpinTileAsync(const nsAString &aTileID)
{
  if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
    NS_WARNING("UnpinTileAsync can't be called on the desktop.");
    return NS_ERROR_FAILURE;
  }
  HRESULT hr;
  HString tileIdStr;
  tileIdStr.Set(aTileID.BeginReading());

  ComPtr<ISecondaryTileFactory> tileFactory;
  ComPtr<ISecondaryTile> secondaryTile;
  hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_StartScreen_SecondaryTile).Get(),
                            tileFactory.GetAddressOf());
  AssertRetHRESULT(hr, NS_ERROR_FAILURE);
  hr = tileFactory->CreateWithId(tileIdStr.Get(), secondaryTile.GetAddressOf());
  AssertRetHRESULT(hr, NS_ERROR_FAILURE);

  // Attempt to unpin the tile
  ComPtr<IAsyncOperationCompletedHandler<bool>> callback(Callback<IAsyncOperationCompletedHandler<bool>>(
    sMetroApp.Get(), &MetroApp::OnAsyncTileCreated));
  ComPtr<IAsyncOperation<bool>> operation;
  AssertRetHRESULT(secondaryTile->RequestDeleteAsync(operation.GetAddressOf()), NS_ERROR_FAILURE);
  operation->put_Completed(callback.Get());
  return NS_OK;
}

/**
 * Determines if a tile is pinned to the Windows 8 start screen.
 *
 * @param aTileID   An ID which may have been pinned with pinTileAsync
 * @param aIsPinned Out parameter for determining if the tile is pinned or not
 */
NS_IMETHODIMP
nsWinMetroUtils::IsTilePinned(const nsAString &aTileID, bool *aIsPinned)
{
  if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
    NS_WARNING("IsTilePinned can't be called on the desktop.");
    return NS_ERROR_FAILURE;
  }
  NS_ENSURE_ARG_POINTER(aIsPinned);

  HRESULT hr;
  HString tileIdStr;
  tileIdStr.Set(aTileID.BeginReading());

  ComPtr<ISecondaryTileStatics> tileStatics;
  hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_StartScreen_SecondaryTile).Get(),
                            tileStatics.GetAddressOf());
  AssertRetHRESULT(hr, NS_ERROR_FAILURE);
  boolean result = false;
  tileStatics->Exists(tileIdStr.Get(), &result);
  *aIsPinned = result;
  return NS_OK;
}

/**
  * Stores the sync info securely in Windows
  *
  * @param aEmail The sync account email
  * @param aPassword The sync account password
  * @param aKey The sync account key
  */
NS_IMETHODIMP
nsWinMetroUtils::StoreSyncInfo(const nsAString &aEmail,
                               const nsAString &aPassword,
                               const nsAString &aKey)
{
  DATA_BLOB emailIn = {
    (aEmail.Length() + 1) * 2,
    (BYTE *)aEmail.BeginReading()},
  passwordIn = {
    (aPassword.Length() + 1) * 2,
    (BYTE *)aPassword.BeginReading()},
  keyIn = {
    (aKey.Length() + 1) * 2,
    (BYTE *)aKey.BeginReading()};
  DATA_BLOB emailOut = { 0, nullptr }, passwordOut = {0, nullptr }, keyOut = { 0, nullptr };
  bool succeeded = CryptProtectData(&emailIn, nullptr, nullptr, nullptr,
                                    nullptr, 0, &emailOut) &&
                   CryptProtectData(&passwordIn, nullptr, nullptr, nullptr,
                                    nullptr, 0, &passwordOut) &&
                   CryptProtectData(&keyIn, nullptr, nullptr, nullptr,
                                    nullptr, 0, &keyOut);

  if (succeeded) {
    nsresult rv;
    nsCOMPtr<nsIWindowsRegKey> regKey
      (do_CreateInstance("@mozilla.org/windows-registry-key;1", &rv));
    NS_ENSURE_SUCCESS(rv, rv);
    regKey->Create(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER,
                  NS_ConvertUTF8toUTF16(sRegPath),
                  nsIWindowsRegKey::ACCESS_SET_VALUE);

    if (NS_FAILED(regKey->WriteBinaryValue(nsDependentString(sSyncEmailField),
                                           nsAutoCString((const char *)emailOut.pbData,
                                                         emailOut.cbData)))) {
      succeeded = false;
    }

    if (succeeded &&
        NS_FAILED(regKey->WriteBinaryValue(nsDependentString(sSyncPasswordField),
                                           nsAutoCString((const char *)passwordOut.pbData,
                                                         passwordOut.cbData)))) {
      succeeded = false;
    }

    if (succeeded &&
        NS_FAILED(regKey->WriteBinaryValue(nsDependentString(sSyncKeyField),
                                           nsAutoCString((const char *)keyOut.pbData,
                                                         keyOut.cbData)))) {
      succeeded = false;
    }
    regKey->Close();
  }

  LocalFree(emailOut.pbData);
  LocalFree(passwordOut.pbData);
  LocalFree(keyOut.pbData);

  return succeeded ? NS_OK : NS_ERROR_FAILURE;
}

/**
  * Loads the sync info securely in Windows
  *
  * @param aEmail The sync account email
  * @param aPassword The sync account password
  * @param aKey The sync account key
  */
NS_IMETHODIMP
nsWinMetroUtils::LoadSyncInfo(nsAString &aEmail, nsAString &aPassword,
                              nsAString &aKey)
{
  nsresult rv;
  nsCOMPtr<nsIWindowsRegKey> regKey
    (do_CreateInstance("@mozilla.org/windows-registry-key;1", &rv));
  NS_ENSURE_SUCCESS(rv, rv);
  regKey->Create(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER,
                 NS_ConvertUTF8toUTF16(sRegPath),
                 nsIWindowsRegKey::ACCESS_QUERY_VALUE);

  nsAutoCString email, password, key;
  if (NS_FAILED(regKey->ReadBinaryValue(nsDependentString(sSyncEmailField), email)) ||
      NS_FAILED(regKey->ReadBinaryValue(nsDependentString(sSyncPasswordField), password)) ||
      NS_FAILED(regKey->ReadBinaryValue(nsDependentString(sSyncKeyField), key))) {
    return NS_ERROR_FAILURE;
  }
  regKey->Close();

  DATA_BLOB emailIn = { email.Length(), (BYTE*)email.BeginReading() },
            passwordIn = { password.Length(), (BYTE*)password.BeginReading() },
            keyIn = { key.Length(), (BYTE*)key.BeginReading() };
  DATA_BLOB emailOut = { 0, nullptr }, passwordOut = { 0, nullptr }, keyOut = { 0, nullptr };
  bool succeeded = CryptUnprotectData(&emailIn, nullptr, nullptr, nullptr,
                                      nullptr, 0, &emailOut) &&
                   CryptUnprotectData(&passwordIn, nullptr, nullptr, nullptr,
                                      nullptr, 0, &passwordOut) &&
                   CryptUnprotectData(&keyIn, nullptr, nullptr, nullptr,
                                      nullptr, 0, &keyOut);
  if (succeeded) {
    aEmail = reinterpret_cast<wchar_t*>(emailOut.pbData);
    aPassword = reinterpret_cast<wchar_t*>(passwordOut.pbData);
    aKey = reinterpret_cast<wchar_t*>(keyOut.pbData);
  }

  LocalFree(emailOut.pbData);
  LocalFree(passwordOut.pbData);
  LocalFree(keyOut.pbData);

  return succeeded ? NS_OK : NS_ERROR_FAILURE;
}

/**
  * Clears the stored sync info if any.
  */
NS_IMETHODIMP
nsWinMetroUtils::ClearSyncInfo()
{
  nsresult rv;
  nsCOMPtr<nsIWindowsRegKey> regKey
    (do_CreateInstance("@mozilla.org/windows-registry-key;1", &rv));
  NS_ENSURE_SUCCESS(rv, rv);
  regKey->Create(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER,
                 NS_ConvertUTF8toUTF16(sRegPath),
                 nsIWindowsRegKey::ACCESS_WRITE);
  nsresult rv1 = regKey->RemoveValue(nsDependentString(sSyncEmailField));
  nsresult rv2 = regKey->RemoveValue(nsDependentString(sSyncPasswordField));
  nsresult rv3 = regKey->RemoveValue(nsDependentString(sSyncKeyField));
  regKey->Close();

  if (NS_FAILED(rv1) || NS_FAILED(rv2) || NS_FAILED(rv3)) {
      return NS_ERROR_FAILURE;
  }
  return NS_OK;
}

/**
 * Launches the specified application with the specified arguments and
 * switches to Desktop mode if in metro mode.
*/
NS_IMETHODIMP
nsWinMetroUtils::LaunchInDesktop(const nsAString &aPath, const nsAString &aArguments)
{
  SHELLEXECUTEINFOW sinfo;
  memset(&sinfo, 0, sizeof(SHELLEXECUTEINFOW));
  sinfo.cbSize       = sizeof(SHELLEXECUTEINFOW);
  // Per the Metro style enabled desktop browser, for some reason,
  // SEE_MASK_FLAG_LOG_USAGE is needed to change from immersive mode
  // to desktop.
  sinfo.fMask        = SEE_MASK_FLAG_LOG_USAGE;
  sinfo.hwnd         = NULL;
  sinfo.lpFile       = aPath.BeginReading();
  sinfo.lpParameters = aArguments.BeginReading();
  sinfo.lpVerb       = L"open";
  sinfo.nShow        = SW_SHOWNORMAL;

  if (!ShellExecuteEx(&sinfo)) {
    return NS_ERROR_FAILURE;
  }
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::ShowNativeToast(const nsAString &aTitle,
  const nsAString &aMessage, const nsAString &anImage)
{
  // Firefox is in the foreground, no need for a notification.
  if (::GetActiveWindow() == ::GetForegroundWindow()) {
    return NS_OK;
  }

  ToastNotificationHandler* notification_handler =
      new ToastNotificationHandler;

  HSTRING title = HStringReference(aTitle.BeginReading()).Get();
  HSTRING msg = HStringReference(aMessage.BeginReading()).Get();
  HSTRING imagePath = HStringReference(anImage.BeginReading()).Get();
  notification_handler->DisplayNotification(title, msg, imagePath);

  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::GetSnappedState(int32_t *aSnappedState)
{
  if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
    NS_WARNING("GetSnappedState can't be called on the desktop.");
    return NS_ERROR_FAILURE;
  }
  NS_ENSURE_ARG_POINTER(aSnappedState);
  ApplicationViewState viewState;
  AssertRetHRESULT(MetroUtils::GetViewState(viewState), NS_ERROR_UNEXPECTED);
  *aSnappedState = (int32_t) viewState;
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::Unsnap()
{
  if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
    NS_WARNING("Unsnap can't be called on the desktop.");
    return NS_ERROR_FAILURE;
  }

  HRESULT hr = MetroUtils::TryUnsnap();
  return SUCCEEDED(hr) ? NS_OK : NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsWinMetroUtils::ShowSettingsFlyout()
{
  if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
    NS_WARNING("Settings flyout can't be shown on the desktop.");
    return NS_ERROR_FAILURE;
  }

  HRESULT hr = MetroUtils::ShowSettingsFlyout();
  return SUCCEEDED(hr) ? NS_OK : NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsWinMetroUtils::GetImmersive(bool *aImersive)
{
  *aImersive =
    XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Metro;
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::GetHandPreference(int32_t *aHandPreference)
{
  if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Desktop) {
    *aHandPreference = nsIWinMetroUtils::handPreferenceRight;
    return NS_OK;
  }

  ComPtr<IUISettings> uiSettings;
  AssertRetHRESULT(ActivateGenericInstance(RuntimeClass_Windows_UI_ViewManagement_UISettings, uiSettings), NS_ERROR_UNEXPECTED);

  HandPreference value;
  uiSettings->get_HandPreference(&value);
  if (value == HandPreference::HandPreference_LeftHanded)
    *aHandPreference = nsIWinMetroUtils::handPreferenceLeft;
  else
    *aHandPreference = nsIWinMetroUtils::handPreferenceRight;

  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::GetActivationURI(nsAString &aActivationURI)
{
  if (!sFrameworkView) {
    NS_WARNING("GetActivationURI used before view is created!");
    return NS_OK;
  }
  sFrameworkView->GetActivationURI(aActivationURI);
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::GetKeyboardVisible(bool *aImersive)
{
  *aImersive = mozilla::widget::winrt::FrameworkView::IsKeyboardVisible();
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::GetKeyboardX(uint32_t *aX)
{
  *aX = (uint32_t)floor(mozilla::widget::winrt::FrameworkView::KeyboardVisibleRect().X);
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::GetKeyboardY(uint32_t *aY)
{
  *aY = (uint32_t)floor(mozilla::widget::winrt::FrameworkView::KeyboardVisibleRect().Y);
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::GetKeyboardWidth(uint32_t *aWidth)
{
  *aWidth = (uint32_t)ceil(mozilla::widget::winrt::FrameworkView::KeyboardVisibleRect().Width);
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::GetKeyboardHeight(uint32_t *aHeight)
{
  *aHeight = (uint32_t)ceil(mozilla::widget::winrt::FrameworkView::KeyboardVisibleRect().Height);
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::AddSettingsPanelEntry(const nsAString &aLabel, uint32_t *aId)
{
  NS_ENSURE_ARG_POINTER(aId);
  if (!sSettingsArray)
    return NS_ERROR_UNEXPECTED;

  *aId = sSettingsArray->Length();
  sSettingsArray->AppendElement(nsString(aLabel));
  return NS_OK;
}

NS_IMETHODIMP
nsWinMetroUtils::SwapMouseButton(bool aValue, bool *aOriginalValue)
{
  *aOriginalValue = ::SwapMouseButton(aValue);
  return NS_OK;
}

} // widget
} // mozilla