1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include "nsExternalSharingAppService.h"
6 
7 #include "mozilla/ModuleUtils.h"
8 #include "nsIClassInfoImpl.h"
9 
10 #include "AndroidBridge.h"
11 #include "nsArrayUtils.h"
12 #include "nsISupportsUtils.h"
13 #include "nsComponentManagerUtils.h"
14 
15 using namespace mozilla;
16 
NS_IMPL_ISUPPORTS(nsExternalSharingAppService,nsIExternalSharingAppService)17 NS_IMPL_ISUPPORTS(nsExternalSharingAppService, nsIExternalSharingAppService)
18 
19 nsExternalSharingAppService::nsExternalSharingAppService() {}
20 
~nsExternalSharingAppService()21 nsExternalSharingAppService::~nsExternalSharingAppService() {}
22 
23 NS_IMETHODIMP
ShareWithDefault(const nsAString & data,const nsAString & mime,const nsAString & title)24 nsExternalSharingAppService::ShareWithDefault(const nsAString &data,
25                                               const nsAString &mime,
26                                               const nsAString &title) {
27   NS_NAMED_LITERAL_STRING(sendAction, "android.intent.action.SEND");
28   const nsString emptyString = EmptyString();
29   return java::GeckoAppShell::OpenUriExternal(data, mime, emptyString,
30                                               emptyString, sendAction, title)
31              ? NS_OK
32              : NS_ERROR_FAILURE;
33 }
34 
35 NS_IMETHODIMP
GetSharingApps(const nsAString & aMIMEType,uint32_t * aLen,nsISharingHandlerApp *** aHandlers)36 nsExternalSharingAppService::GetSharingApps(const nsAString &aMIMEType,
37                                             uint32_t *aLen,
38                                             nsISharingHandlerApp ***aHandlers) {
39   nsresult rv;
40   NS_NAMED_LITERAL_STRING(sendAction, "android.intent.action.SEND");
41   nsCOMPtr<nsIMutableArray> array = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
42   NS_ENSURE_SUCCESS(rv, rv);
43   if (!AndroidBridge::Bridge()) return NS_OK;
44   AndroidBridge::Bridge()->GetHandlersForMimeType(aMIMEType, array, nullptr,
45                                                   sendAction);
46   array->GetLength(aLen);
47   *aHandlers = static_cast<nsISharingHandlerApp **>(
48       moz_xmalloc(sizeof(nsISharingHandlerApp *) * *aLen));
49   for (uint32_t i = 0; i < *aLen; i++) {
50     rv = array->QueryElementAt(i, NS_GET_IID(nsISharingHandlerApp),
51                                (void **)(*aHandlers + i));
52     NS_ENSURE_SUCCESS(rv, rv);
53   }
54   return NS_OK;
55 }
56