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
) {
19 if (mName
.IsEmpty() && mExecutable
) {
20 // Don't want to cache this, just in case someone resets the app
21 // without changing the description....
22 mExecutable
->GetLeafName(aName
);
30 NS_IMETHODIMP
nsLocalHandlerApp::SetName(const nsAString
& aName
) {
37 nsLocalHandlerApp::SetDetailedDescription(const nsAString
& aDescription
) {
38 mDetailedDescription
.Assign(aDescription
);
44 nsLocalHandlerApp::GetDetailedDescription(nsAString
& aDescription
) {
45 aDescription
.Assign(mDetailedDescription
);
51 nsLocalHandlerApp::Equals(nsIHandlerApp
* aHandlerApp
, bool* _retval
) {
52 NS_ENSURE_ARG_POINTER(aHandlerApp
);
56 // If the handler app isn't a local handler app, then it's not the same app.
57 nsCOMPtr
<nsILocalHandlerApp
> localHandlerApp
= do_QueryInterface(aHandlerApp
);
58 if (!localHandlerApp
) return NS_OK
;
60 // If either handler app doesn't have an executable, then they aren't
62 nsCOMPtr
<nsIFile
> executable
;
63 nsresult rv
= localHandlerApp
->GetExecutable(getter_AddRefs(executable
));
64 if (NS_FAILED(rv
)) return rv
;
66 // Equality for two empty nsIHandlerApp
67 if (!executable
&& !mExecutable
) {
72 // At least one is set so they are not equal
73 if (!mExecutable
|| !executable
) return NS_OK
;
75 // Check the command line parameter list lengths
77 localHandlerApp
->GetParameterCount(&len
);
78 if (mParameters
.Length() != len
) return NS_OK
;
80 // Check the command line params lists
81 for (uint32_t idx
= 0; idx
< mParameters
.Length(); idx
++) {
83 if (NS_FAILED(localHandlerApp
->GetParameter(idx
, param
)) ||
84 !param
.Equals(mParameters
[idx
]))
88 return executable
->Equals(mExecutable
, _retval
);
92 nsLocalHandlerApp::LaunchWithURI(
93 nsIURI
* aURI
, mozilla::dom::BrowsingContext
* aBrowsingContext
) {
94 // pass the entire URI to the handler.
96 aURI
->GetAsciiSpec(spec
);
97 return LaunchWithIProcess(spec
);
100 nsresult
nsLocalHandlerApp::LaunchWithIProcess(const nsCString
& aArg
) {
102 nsCOMPtr
<nsIProcess
> process
= do_CreateInstance(NS_PROCESS_CONTRACTID
, &rv
);
103 if (NS_FAILED(rv
)) return rv
;
105 if (NS_FAILED(rv
= process
->Init(mExecutable
))) return rv
;
107 const char* string
= aArg
.get();
109 return process
->Run(false, &string
, 1);
112 ////////////////////////////////////////////////////////////////////////////////
113 //// nsILocalHandlerApp
116 nsLocalHandlerApp::GetExecutable(nsIFile
** aExecutable
) {
117 NS_IF_ADDREF(*aExecutable
= mExecutable
);
122 nsLocalHandlerApp::SetExecutable(nsIFile
* aExecutable
) {
123 mExecutable
= aExecutable
;
128 nsLocalHandlerApp::GetParameterCount(uint32_t* aParameterCount
) {
129 *aParameterCount
= mParameters
.Length();
134 nsLocalHandlerApp::ClearParameters() {
140 nsLocalHandlerApp::AppendParameter(const nsAString
& aParam
) {
141 mParameters
.AppendElement(aParam
);
146 nsLocalHandlerApp::GetParameter(uint32_t parameterIndex
, nsAString
& _retval
) {
147 if (mParameters
.Length() <= parameterIndex
) return NS_ERROR_INVALID_ARG
;
149 _retval
.Assign(mParameters
[parameterIndex
]);
154 nsLocalHandlerApp::ParameterExists(const nsAString
& aParam
, bool* _retval
) {
155 *_retval
= mParameters
.Contains(aParam
);