mfbt/WindowsVersion.h
author Kyle Huey <khuey@kylehuey.com>
Sat, 15 Mar 2014 12:00:15 -0700
changeset 190962 32f48d6d3389ea5db45cfc6e452ec52595c11a43
parent 188298 da8693a783a8e44e9093070cb12cc8590e812100
child 214333 91d7659bb35145e10ff737753688e0972c1a79b5
permissions -rw-r--r--
Bug 967364: Rename already_AddRefed::get to take. r=bsmedberg

/* 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/. */

#ifndef mozilla_WindowsVersion_h
#define mozilla_WindowsVersion_h

#include "mozilla/Attributes.h"
#include <stdint.h>
#include <windows.h>

namespace mozilla
{
  inline bool
  IsWindowsVersionOrLater(uint32_t aVersion)
  {
    static uint32_t minVersion = 0;
    static uint32_t maxVersion = UINT32_MAX;

    if (minVersion >= aVersion) {
      return true;
    }

    if (aVersion >= maxVersion) {
      return false;
    }

    OSVERSIONINFOEX info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    info.dwMajorVersion = aVersion >> 24;
    info.dwMinorVersion = (aVersion >> 16) & 0xFF;
    info.wServicePackMajor = (aVersion >> 8) & 0xFF;
    info.wServicePackMinor = aVersion & 0xFF;

    DWORDLONG conditionMask = 0;
    VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
    VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
    VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
    VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);

    if (VerifyVersionInfo(&info,
                          VER_MAJORVERSION | VER_MINORVERSION |
                          VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
                          conditionMask)) {
      minVersion = aVersion;
      return true;
    }

    maxVersion = aVersion;
    return false;
  }

  inline bool
  IsWindowsBuildOrLater(uint32_t aBuild)
  {
    static uint32_t minBuild = 0;
    static uint32_t maxBuild = UINT32_MAX;

    if (minBuild >= aBuild) {
      return true;
    }

    if (aBuild >= maxBuild) {
      return false;
    }

    OSVERSIONINFOEX info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    info.dwBuildNumber = aBuild;

    DWORDLONG conditionMask = 0;
    VER_SET_CONDITION(conditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);

    if (VerifyVersionInfo(&info, VER_BUILDNUMBER, conditionMask)) {
      minBuild = aBuild;
      return true;
    }

    maxBuild = aBuild;
    return false;
  }

  MOZ_ALWAYS_INLINE bool
  IsXPSP3OrLater()
  { return IsWindowsVersionOrLater(0x05010300ul); }

  MOZ_ALWAYS_INLINE bool
  IsWin2003OrLater()
  { return IsWindowsVersionOrLater(0x05020000ul); }

  MOZ_ALWAYS_INLINE bool
  IsWin2003SP2OrLater()
  { return IsWindowsVersionOrLater(0x05020200ul); }

  MOZ_ALWAYS_INLINE bool
  IsVistaOrLater()
  { return IsWindowsVersionOrLater(0x06000000ul); }

  MOZ_ALWAYS_INLINE bool
  IsVistaSP1OrLater()
  { return IsWindowsVersionOrLater(0x06000100ul); }

  MOZ_ALWAYS_INLINE bool
  IsWin7OrLater()
  { return IsWindowsVersionOrLater(0x06010000ul); }

  MOZ_ALWAYS_INLINE bool
  IsWin7SP1OrLater()
  { return IsWindowsVersionOrLater(0x06010100ul); }

  MOZ_ALWAYS_INLINE bool
  IsWin8OrLater()
  { return IsWindowsVersionOrLater(0x06020000ul); }

  MOZ_ALWAYS_INLINE bool
  IsNotWin7PreRTM()
  {
    return IsWin7SP1OrLater() || !IsWin7OrLater() ||
           IsWindowsBuildOrLater(7600);
  }
}

#endif /* mozilla_WindowsVersion_h */