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"
11 // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
13 NS_IMPL_ISUPPORTS(nsLocalHandlerApp
, nsILocalHandlerApp
, nsIHandlerApp
)
15 ////////////////////////////////////////////////////////////////////////////////
18 NS_IMETHODIMP
nsLocalHandlerApp::GetName(nsAString
& aName
)
20 if (mName
.IsEmpty() && mExecutable
) {
21 // Don't want to cache this, just in case someone resets the app
22 // without changing the description....
23 mExecutable
->GetLeafName(aName
);
31 NS_IMETHODIMP
nsLocalHandlerApp::SetName(const nsAString
& aName
)
39 nsLocalHandlerApp::SetDetailedDescription(const nsAString
& aDescription
)
41 mDetailedDescription
.Assign(aDescription
);
47 nsLocalHandlerApp::GetDetailedDescription(nsAString
& aDescription
)
49 aDescription
.Assign(mDetailedDescription
);
55 nsLocalHandlerApp::Equals(nsIHandlerApp
*aHandlerApp
, bool *_retval
)
57 NS_ENSURE_ARG_POINTER(aHandlerApp
);
61 // If the handler app isn't a local handler app, then it's not the same app.
62 nsCOMPtr
<nsILocalHandlerApp
> localHandlerApp
= do_QueryInterface(aHandlerApp
);
66 // If either handler app doesn't have an executable, then they aren't
68 nsCOMPtr
<nsIFile
> executable
;
69 nsresult rv
= localHandlerApp
->GetExecutable(getter_AddRefs(executable
));
73 // Equality for two empty nsIHandlerApp
74 if (!executable
&& !mExecutable
) {
79 // At least one is set so they are not equal
80 if (!mExecutable
|| !executable
)
83 // Check the command line parameter list lengths
85 localHandlerApp
->GetParameterCount(&len
);
86 if (mParameters
.Length() != len
)
89 // Check the command line params lists
90 for (uint32_t idx
= 0; idx
< mParameters
.Length(); idx
++) {
92 if (NS_FAILED(localHandlerApp
->GetParameter(idx
, param
)) ||
93 !param
.Equals(mParameters
[idx
]))
97 return executable
->Equals(mExecutable
, _retval
);
101 nsLocalHandlerApp::LaunchWithURI(nsIURI
*aURI
,
102 nsIInterfaceRequestor
*aWindowContext
)
104 // pass the entire URI to the handler.
106 aURI
->GetAsciiSpec(spec
);
107 return LaunchWithIProcess(spec
);
111 nsLocalHandlerApp::LaunchWithIProcess(const nsCString
& aArg
)
114 nsCOMPtr
<nsIProcess
> process
= do_CreateInstance(NS_PROCESS_CONTRACTID
, &rv
);
118 if (NS_FAILED(rv
= process
->Init(mExecutable
)))
121 const char *string
= aArg
.get();
123 return process
->Run(false, &string
, 1);
126 ////////////////////////////////////////////////////////////////////////////////
127 //// nsILocalHandlerApp
129 /* attribute nsIFile executable; */
131 nsLocalHandlerApp::GetExecutable(nsIFile
**aExecutable
)
133 NS_IF_ADDREF(*aExecutable
= mExecutable
);
138 nsLocalHandlerApp::SetExecutable(nsIFile
*aExecutable
)
140 mExecutable
= aExecutable
;
144 /* readonly attribute unsigned long parameterCount; */
146 nsLocalHandlerApp::GetParameterCount(uint32_t *aParameterCount
)
148 *aParameterCount
= mParameters
.Length();
152 /* void clearParameters (); */
154 nsLocalHandlerApp::ClearParameters()
160 /* void appendParameter (in AString param); */
162 nsLocalHandlerApp::AppendParameter(const nsAString
& aParam
)
164 mParameters
.AppendElement(aParam
);
168 /* AString getParameter (in unsigned long parameterIndex); */
170 nsLocalHandlerApp::GetParameter(uint32_t parameterIndex
, nsAString
& _retval
)
172 if (mParameters
.Length() <= parameterIndex
)
173 return NS_ERROR_INVALID_ARG
;
175 _retval
.Assign(mParameters
[parameterIndex
]);
179 /* boolean parameterExists (in AString param); */
181 nsLocalHandlerApp::ParameterExists(const nsAString
& aParam
, bool *_retval
)
183 *_retval
= mParameters
.Contains(aParam
);