summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutthirdparty/MsiDatabase.h
blob: 91c1370b8d611f4fe598982dc10c1dc38c7de908 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 __MsiDatabase_h__
#define __MsiDatabase_h__

#include "mozilla/Maybe.h"
#include "mozilla/UniquePtr.h"
#include "nsWindowsHelpers.h"

#include <windows.h>
#include <msi.h>
#include <msiquery.h>

namespace mozilla {

class MsiDatabase final {
  static UniquePtr<wchar_t[]> GetRecordString(MSIHANDLE aRecord,
                                              UINT aFieldIndex);

  nsAutoMsiHandle mDatabase;

  MsiDatabase() = default;
  explicit MsiDatabase(const wchar_t* aDatabasePath);

 public:
  // A callback function passed to ExecuteSingleColumnQuery uses this type
  // to control the enumeration loop.
  enum class CallbackResult { Continue, Stop };

  static Maybe<MsiDatabase> FromProductId(const wchar_t* aProductId);

  MsiDatabase(const MsiDatabase&) = delete;
  MsiDatabase& operator=(const MsiDatabase&) = delete;
  MsiDatabase(MsiDatabase&& aOther) : mDatabase(aOther.mDatabase.disown()) {}
  MsiDatabase& operator=(MsiDatabase&& aOther) {
    if (this != &aOther) {
      mDatabase.own(aOther.mDatabase.disown());
    }
    return *this;
  }

  explicit operator bool() const;

  template <typename CallbackT>
  bool ExecuteSingleColumnQuery(const wchar_t* aQuery,
                                const CallbackT& aCallback) const {
    MSIHANDLE handle;
    UINT ret = ::MsiDatabaseOpenViewW(mDatabase, aQuery, &handle);
    if (ret != ERROR_SUCCESS) {
      return false;
    }

    nsAutoMsiHandle view(handle);

    ret = ::MsiViewExecute(view, 0);
    if (ret != ERROR_SUCCESS) {
      return false;
    }

    for (;;) {
      ret = ::MsiViewFetch(view, &handle);
      if (ret == ERROR_NO_MORE_ITEMS) {
        break;
      } else if (ret != ERROR_SUCCESS) {
        return false;
      }

      nsAutoMsiHandle record(handle);
      UniquePtr<wchar_t[]> guidStr = GetRecordString(record, 1);
      if (!guidStr) {
        continue;
      }

      CallbackResult result = aCallback(guidStr.get());
      if (result == CallbackResult::Continue) {
        continue;
      } else if (result == CallbackResult::Stop) {
        break;
      } else {
        MOZ_ASSERT_UNREACHABLE("Unexpected CallbackResult.");
      }
    }

    return true;
  }
};

}  // namespace mozilla

#endif  // __MsiDatabase_h__