Bug 1826326 [wpt PR 39360] - Get rid of ResourceWarnings produced by the tests, a...
[gecko.git] / widget / windows / UrlmonHeaderOnlyUtils.h
blobdd9209f78f043f5d636f4a3eda35ced0139a33d5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_UrlmonHeaderOnlyUtils_h
8 #define mozilla_UrlmonHeaderOnlyUtils_h
10 #include "mozilla/ShellHeaderOnlyUtils.h"
12 namespace mozilla {
14 /**
15 * We used to validate a uri with SHParseDisplayName to mitigate the Windows
16 * bug (Bug 394974). However, Bug 1573051 revealed an issue that a fragment,
17 * a string following a hash mark (#), is dropped when we extract a string
18 * from PIDL. This is the intended behavior of Windows.
20 * To deal with the fragment issue as well as keeping our mitigation, we
21 * decided to use CreateUri to validate a uri string, but we also keep using
22 * SHParseDisplayName as a pre-check. This is because there are several
23 * cases where CreateUri succeeds while SHParseDisplayName fails such as
24 * a non-existent file: uri.
26 * To minimize the impact of introducing CreateUri into the validation logic,
27 * we try to mimic the logic of windows_storage!IUriToPidl (ieframe!IUriToPidl
28 * in Win7) which is executed behind SHParseDisplayName.
29 * What IUriToPidl does is:
30 * 1) If a given uri has a fragment, removes a fragment.
31 * 2) Takes an absolute uri if it's available in the given uri, otherwise
32 * takes a raw uri.
34 * As we need to get a full uri including a fragment, this function does 2).
36 inline LauncherResult<_bstr_t> UrlmonValidateUri(const wchar_t* aUri) {
37 LauncherResult<UniqueAbsolutePidl> pidlResult = ShellParseDisplayName(aUri);
38 if (pidlResult.isErr()) {
39 return pidlResult.propagateErr();
42 // The value of |flags| is the same value as used in ieframe!_EnsureIUri in
43 // Win7, which is called behind SHParseDisplayName. In Win10, on the other
44 // hand, an flag 0x03000000 is also passed to CreateUri, but we don't
45 // specify it because it's undocumented and unknown.
46 constexpr DWORD flags =
47 Uri_CREATE_NO_DECODE_EXTRA_INFO | Uri_CREATE_CANONICALIZE |
48 Uri_CREATE_CRACK_UNKNOWN_SCHEMES | Uri_CREATE_PRE_PROCESS_HTML_URI |
49 Uri_CREATE_IE_SETTINGS;
50 RefPtr<IUri> uri;
51 HRESULT hr;
52 SAFECALL_URLMON_FUNC(CreateUri, aUri, flags, 0, getter_AddRefs(uri));
53 if (FAILED(hr)) {
54 return LAUNCHER_ERROR_FROM_HRESULT(hr);
57 _bstr_t bstrUri;
59 hr = uri->GetAbsoluteUri(bstrUri.GetAddress());
60 if (FAILED(hr)) {
61 return LAUNCHER_ERROR_FROM_HRESULT(hr);
64 if (hr == S_FALSE) {
65 hr = uri->GetRawUri(bstrUri.GetAddress());
66 if (FAILED(hr)) {
67 return LAUNCHER_ERROR_FROM_HRESULT(hr);
71 return bstrUri;
74 } // namespace mozilla
76 #endif // mozilla_UrlmonHeaderOnlyUtils_h