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"
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 ///////////////////////////////////////////////////////////////////////////////
22 NS_IMPL_ISUPPORTS(nsSharePicker
, nsISharePicker
)
25 inline NS_ConvertUTF8toUTF16
NS_ConvertUTF8toUTF16_MaybeVoid(
26 const nsACString
& aStr
) {
27 auto str
= NS_ConvertUTF8toUTF16(aStr
);
28 str
.SetIsVoid(aStr
.IsVoid());
31 inline nsIGlobalObject
* GetGlobalObject() {
32 return xpc::NativeGlobal(xpc::PrivilegedJunkScope());
37 nsSharePicker::Init(mozIDOMWindowProxy
* aOpenerWindow
) {
39 return NS_ERROR_FAILURE
;
41 mOpenerWindow
= aOpenerWindow
;
47 nsSharePicker::GetOpenerWindow(mozIDOMWindowProxy
** aOpenerWindow
) {
48 *aOpenerWindow
= mOpenerWindow
;
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
;
62 nsresult rv
= aUrl
->GetSpec(urlString
);
63 MOZ_ASSERT(NS_SUCCEEDED(rv
));
64 mozilla::Unused
<< rv
;
66 urlString
.SetIsVoid(true);
70 WindowsUIUtils::Share(NS_ConvertUTF8toUTF16_MaybeVoid(aTitle
),
71 NS_ConvertUTF8toUTF16_MaybeVoid(aText
),
72 NS_ConvertUTF8toUTF16_MaybeVoid(urlString
));
74 mozilla::GetCurrentSerialEventTarget(), __func__
,
75 [promise
]() { promise
->MaybeResolveWithUndefined(); },
76 [promise
]() { promise
->MaybeReject(NS_ERROR_DOM_ABORT_ERR
); });
78 promise
.forget(aPromise
);