Bug 1826326 [wpt PR 39360] - Get rid of ResourceWarnings produced by the tests, a...
[gecko.git] / widget / windows / nsSharePicker.cpp
blob9cdc7927180ad89a3a9b7ffc55c69762744e925e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 http://mozilla.org/MPL/2.0/. */
7 #include "nsSharePicker.h"
9 #include "nsString.h"
10 #include "nsThreadUtils.h"
11 #include "WindowsUIUtils.h"
12 #include "nsPIDOMWindow.h"
13 #include "mozilla/dom/Promise.h"
14 #include "mozilla/ErrorResult.h"
15 #include "mozilla/Unused.h"
17 using mozilla::dom::Promise;
19 ///////////////////////////////////////////////////////////////////////////////
20 // nsISharePicker
22 NS_IMPL_ISUPPORTS(nsSharePicker, nsISharePicker)
24 namespace {
25 inline NS_ConvertUTF8toUTF16 NS_ConvertUTF8toUTF16_MaybeVoid(
26 const nsACString& aStr) {
27 auto str = NS_ConvertUTF8toUTF16(aStr);
28 str.SetIsVoid(aStr.IsVoid());
29 return str;
31 inline nsIGlobalObject* GetGlobalObject() {
32 return xpc::NativeGlobal(xpc::PrivilegedJunkScope());
34 } // namespace
36 NS_IMETHODIMP
37 nsSharePicker::Init(mozIDOMWindowProxy* aOpenerWindow) {
38 if (mInited) {
39 return NS_ERROR_FAILURE;
41 mOpenerWindow = aOpenerWindow;
42 mInited = true;
43 return NS_OK;
46 NS_IMETHODIMP
47 nsSharePicker::GetOpenerWindow(mozIDOMWindowProxy** aOpenerWindow) {
48 *aOpenerWindow = mOpenerWindow;
49 return NS_OK;
52 NS_IMETHODIMP
53 nsSharePicker::Share(const nsACString& aTitle, const nsACString& aText,
54 nsIURI* aUrl, Promise** aPromise) {
55 mozilla::ErrorResult result;
56 RefPtr<Promise> promise = Promise::Create(GetGlobalObject(), result);
57 if (NS_WARN_IF(result.Failed())) {
58 return result.StealNSResult();
60 nsAutoCString urlString;
61 if (aUrl) {
62 nsresult rv = aUrl->GetSpec(urlString);
63 MOZ_ASSERT(NS_SUCCEEDED(rv));
64 mozilla::Unused << rv;
65 } else {
66 urlString.SetIsVoid(true);
69 auto mozPromise =
70 WindowsUIUtils::Share(NS_ConvertUTF8toUTF16_MaybeVoid(aTitle),
71 NS_ConvertUTF8toUTF16_MaybeVoid(aText),
72 NS_ConvertUTF8toUTF16_MaybeVoid(urlString));
73 mozPromise->Then(
74 mozilla::GetCurrentSerialEventTarget(), __func__,
75 [promise]() { promise->MaybeResolveWithUndefined(); },
76 [promise]() { promise->MaybeReject(NS_ERROR_DOM_ABORT_ERR); });
78 promise.forget(aPromise);
80 return NS_OK;