1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef SHELL_WINDOWSUSERCHOICE_H__
7 #define SHELL_WINDOWSUSERCHOICE_H__
8 
9 #include <windows.h>
10 
11 #include "mozilla/UniquePtr.h"
12 
13 /*
14  * Check the UserChoice Hashes for https, http, .html, .htm
15  *
16  * This should be checked before attempting to set a new default browser via
17  * the UserChoice key, to confirm our understanding of the existing hash.
18  * If an incorrect hash is written, Windows will prompt the user to choose a
19  * new default (or, in recent versions, it will just reset the default to Edge).
20  *
21  * Assuming that the existing hash value is correct (since Windows is fairly
22  * diligent about replacing bad keys), if we can recompute it from scratch,
23  * then we should be able to compute a correct hash for our new UserChoice key.
24  *
25  * @return true if we matched all the hashes, false otherwise.
26  */
27 bool CheckBrowserUserChoiceHashes();
28 
29 /*
30  * Result from CheckUserChoiceHash()
31  *
32  * NOTE: Currently the only positive result is OK_V1 , but the enum
33  * could be extended to indicate different versions of the hash.
34  */
35 enum class CheckUserChoiceHashResult {
36   OK_V1,         // Matched the current version of the hash (as of Win10 20H2).
37   ERR_MISMATCH,  // The hash did not match.
38   ERR_OTHER,     // Error reading or generating the hash.
39 };
40 
41 /*
42  * Generate a UserChoice Hash, compare it with the one that is stored.
43  *
44  * See comments on CheckBrowserUserChoiceHashes(), which calls this to check
45  * each of the browser associations.
46  *
47  * @param aExt      File extension or protocol association to check
48  * @param aUserSid  String SID of the current user
49  *
50  * @return Result of the check, see CheckUserChoiceHashResult
51  */
52 CheckUserChoiceHashResult CheckUserChoiceHash(const wchar_t* aExt,
53                                               const wchar_t* aUserSid);
54 
55 /*
56  * Get the registry path for the given association, file extension or protocol.
57  *
58  * @return The path, or nullptr on failure.
59  */
60 mozilla::UniquePtr<wchar_t[]> GetAssociationKeyPath(const wchar_t* aExt);
61 
62 /*
63  * Get the current user's SID
64  *
65  * @return String SID for the user of the current process, nullptr on failure.
66  */
67 mozilla::UniquePtr<wchar_t[]> GetCurrentUserStringSid();
68 
69 /*
70  * Generate the UserChoice Hash
71  *
72  * @param aExt          file extension or protocol being registered
73  * @param aUserSid      string SID of the current user
74  * @param aProgId       ProgId to associate with aExt
75  * @param aTimestamp    approximate write time of the UserChoice key (within
76  *                      the same minute)
77  *
78  * @return UserChoice Hash, nullptr on failure.
79  */
80 mozilla::UniquePtr<wchar_t[]> GenerateUserChoiceHash(const wchar_t* aExt,
81                                                      const wchar_t* aUserSid,
82                                                      const wchar_t* aProgId,
83                                                      SYSTEMTIME aTimestamp);
84 
85 /*
86  * Build a ProgID from a base and AUMI
87  *
88  * @param aProgIDBase   A base, such as FirefoxHTML or FirefoxURL
89  * @param aAumi         The AUMI of the installation
90  *
91  * @return Formatted ProgID.
92  */
93 mozilla::UniquePtr<wchar_t[]> FormatProgID(const wchar_t* aProgIDBase,
94                                            const wchar_t* aAumi);
95 
96 /*
97  * Check that the given ProgID exists in HKCR
98  *
99  * @return true if it could be opened for reading, false otherwise.
100  */
101 bool CheckProgIDExists(const wchar_t* aProgID);
102 
103 #endif  // SHELL_WINDOWSUSERCHOICE_H__
104