1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:cin:
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 "nsLocalHandlerApp.h"
9 #include "nsIProcess.h"
10 #include "nsComponentManagerUtils.h"
11 #include "mozilla/dom/Promise.h"
12 #include "nsProxyRelease.h"
14 // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
16 NS_IMPL_ISUPPORTS(nsLocalHandlerApp
, nsILocalHandlerApp
, nsIHandlerApp
)
18 using namespace mozilla
;
20 ////////////////////////////////////////////////////////////////////////////////
23 NS_IMETHODIMP
nsLocalHandlerApp::GetName(nsAString
& aName
) {
24 if (mName
.IsEmpty() && mExecutable
) {
25 // Don't want to cache this, just in case someone resets the app
26 // without changing the description....
27 mExecutable
->GetLeafName(aName
);
36 * This method returns a std::function that will be executed on a thread other
37 * than the main thread. To facilitate things, it should effectively be a global
38 * function that does not maintain a reference to the this pointer. There should
39 * be no reference to any objects that will be shared across threads. Sub-class
40 * implementations should make local copies of everything they need and capture
41 * those in the callback.
43 std::function
<nsresult(nsString
&)>
44 nsLocalHandlerApp::GetPrettyNameOnNonMainThreadCallback() {
47 // Calculate the name now, on the main thread, so as to avoid
48 // doing anything with the this pointer on the other thread
49 auto result
= GetName(name
);
51 return [name
, result
](nsString
& aName
) -> nsresult
{
58 nsLocalHandlerApp::PrettyNameAsync(JSContext
* aCx
, dom::Promise
** aPromise
) {
59 NS_ENSURE_ARG_POINTER(aPromise
);
64 return NS_ERROR_FAILURE
;
67 nsIGlobalObject
* global
= xpc::CurrentNativeGlobal(aCx
);
68 if (NS_WARN_IF(!global
)) {
69 return NS_ERROR_FAILURE
;
73 RefPtr
<dom::Promise
> outer
= dom::Promise::Create(global
, err
);
74 if (NS_WARN_IF(err
.Failed())) {
75 return err
.StealNSResult();
78 outer
.forget(aPromise
);
80 nsAutoString executablePath
;
81 nsresult result
= mExecutable
->GetPath(executablePath
);
83 if (NS_FAILED(result
) || executablePath
.IsEmpty()) {
84 (*aPromise
)->MaybeReject(result
);
88 nsMainThreadPtrHandle
<dom::Promise
> promiseHolder(
89 new nsMainThreadPtrHolder
<dom::Promise
>(
90 "nsLocalHandlerApp::prettyExecutableName Promise", *aPromise
));
92 auto prettyNameGetter
= GetPrettyNameOnNonMainThreadCallback();
94 result
= NS_DispatchBackgroundTask(
95 NS_NewRunnableFunction(
97 [promiseHolder
/* can't move this because if the dispatch fails, we
98 call reject on the promiseHolder */
100 prettyNameGetter
= std::move(prettyNameGetter
)]() mutable -> void {
101 nsAutoString prettyExecutableName
;
103 nsresult result
= prettyNameGetter(prettyExecutableName
);
105 DebugOnly
<nsresult
> rv
=
106 NS_DispatchToMainThread(NS_NewRunnableFunction(
108 [promiseHolder
= std::move(promiseHolder
),
109 prettyExecutableName
= std::move(prettyExecutableName
),
111 if (NS_FAILED(result
)) {
112 promiseHolder
.get()->MaybeReject(result
);
114 promiseHolder
.get()->MaybeResolve(prettyExecutableName
);
117 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
),
118 "NS_DispatchToMainThread failed");
120 NS_DISPATCH_EVENT_MAY_BLOCK
);
122 if (NS_FAILED(result
)) {
123 promiseHolder
.get()->MaybeReject(result
);
129 NS_IMETHODIMP
nsLocalHandlerApp::SetName(const nsAString
& aName
) {
136 nsLocalHandlerApp::SetDetailedDescription(const nsAString
& aDescription
) {
137 mDetailedDescription
.Assign(aDescription
);
143 nsLocalHandlerApp::GetDetailedDescription(nsAString
& aDescription
) {
144 aDescription
.Assign(mDetailedDescription
);
150 nsLocalHandlerApp::Equals(nsIHandlerApp
* aHandlerApp
, bool* _retval
) {
151 NS_ENSURE_ARG_POINTER(aHandlerApp
);
155 // If the handler app isn't a local handler app, then it's not the same app.
156 nsCOMPtr
<nsILocalHandlerApp
> localHandlerApp
= do_QueryInterface(aHandlerApp
);
157 if (!localHandlerApp
) return NS_OK
;
159 // If either handler app doesn't have an executable, then they aren't
161 nsCOMPtr
<nsIFile
> executable
;
162 nsresult rv
= localHandlerApp
->GetExecutable(getter_AddRefs(executable
));
163 if (NS_FAILED(rv
)) return rv
;
165 // Equality for two empty nsIHandlerApp
166 if (!executable
&& !mExecutable
) {
171 // At least one is set so they are not equal
172 if (!mExecutable
|| !executable
) return NS_OK
;
174 // Check the command line parameter list lengths
176 localHandlerApp
->GetParameterCount(&len
);
177 if (mParameters
.Length() != len
) return NS_OK
;
179 // Check the command line params lists
180 for (uint32_t idx
= 0; idx
< mParameters
.Length(); idx
++) {
182 if (NS_FAILED(localHandlerApp
->GetParameter(idx
, param
)) ||
183 !param
.Equals(mParameters
[idx
]))
187 return executable
->Equals(mExecutable
, _retval
);
191 nsLocalHandlerApp::LaunchWithURI(
192 nsIURI
* aURI
, mozilla::dom::BrowsingContext
* aBrowsingContext
) {
193 // pass the entire URI to the handler.
195 aURI
->GetAsciiSpec(spec
);
196 return LaunchWithIProcess(spec
);
199 nsresult
nsLocalHandlerApp::LaunchWithIProcess(const nsCString
& aArg
) {
201 nsCOMPtr
<nsIProcess
> process
= do_CreateInstance(NS_PROCESS_CONTRACTID
, &rv
);
202 if (NS_FAILED(rv
)) return rv
;
204 if (NS_FAILED(rv
= process
->Init(mExecutable
))) return rv
;
206 const char* string
= aArg
.get();
208 return process
->Run(false, &string
, 1);
211 ////////////////////////////////////////////////////////////////////////////////
212 //// nsILocalHandlerApp
215 nsLocalHandlerApp::GetExecutable(nsIFile
** aExecutable
) {
216 NS_IF_ADDREF(*aExecutable
= mExecutable
);
221 nsLocalHandlerApp::SetExecutable(nsIFile
* aExecutable
) {
222 mExecutable
= aExecutable
;
227 nsLocalHandlerApp::GetParameterCount(uint32_t* aParameterCount
) {
228 *aParameterCount
= mParameters
.Length();
233 nsLocalHandlerApp::ClearParameters() {
239 nsLocalHandlerApp::AppendParameter(const nsAString
& aParam
) {
240 mParameters
.AppendElement(aParam
);
245 nsLocalHandlerApp::GetParameter(uint32_t parameterIndex
, nsAString
& _retval
) {
246 if (mParameters
.Length() <= parameterIndex
) return NS_ERROR_INVALID_ARG
;
248 _retval
.Assign(mParameters
[parameterIndex
]);
253 nsLocalHandlerApp::ParameterExists(const nsAString
& aParam
, bool* _retval
) {
254 *_retval
= mParameters
.Contains(aParam
);