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"
12 // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
14 NS_IMPL_ISUPPORTS(nsLocalHandlerApp
, nsILocalHandlerApp
, nsIHandlerApp
)
16 ////////////////////////////////////////////////////////////////////////////////
19 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
) {
38 nsLocalHandlerApp::SetDetailedDescription(const nsAString
& aDescription
) {
39 mDetailedDescription
.Assign(aDescription
);
45 nsLocalHandlerApp::GetDetailedDescription(nsAString
& aDescription
) {
46 aDescription
.Assign(mDetailedDescription
);
52 nsLocalHandlerApp::Equals(nsIHandlerApp
* aHandlerApp
, bool* _retval
) {
53 NS_ENSURE_ARG_POINTER(aHandlerApp
);
57 // If the handler app isn't a local handler app, then it's not the same app.
58 nsCOMPtr
<nsILocalHandlerApp
> localHandlerApp
= do_QueryInterface(aHandlerApp
);
59 if (!localHandlerApp
) return NS_OK
;
61 // If either handler app doesn't have an executable, then they aren't
63 nsCOMPtr
<nsIFile
> executable
;
64 nsresult rv
= localHandlerApp
->GetExecutable(getter_AddRefs(executable
));
65 if (NS_FAILED(rv
)) return rv
;
67 // Equality for two empty nsIHandlerApp
68 if (!executable
&& !mExecutable
) {
73 // At least one is set so they are not equal
74 if (!mExecutable
|| !executable
) return NS_OK
;
76 // Check the command line parameter list lengths
78 localHandlerApp
->GetParameterCount(&len
);
79 if (mParameters
.Length() != len
) return NS_OK
;
81 // Check the command line params lists
82 for (uint32_t idx
= 0; idx
< mParameters
.Length(); idx
++) {
84 if (NS_FAILED(localHandlerApp
->GetParameter(idx
, param
)) ||
85 !param
.Equals(mParameters
[idx
]))
89 return executable
->Equals(mExecutable
, _retval
);
93 nsLocalHandlerApp::LaunchWithURI(
94 nsIURI
* aURI
, mozilla::dom::BrowsingContext
* aBrowsingContext
) {
95 // pass the entire URI to the handler.
97 aURI
->GetAsciiSpec(spec
);
98 return LaunchWithIProcess(spec
);
101 nsresult
nsLocalHandlerApp::LaunchWithIProcess(const nsCString
& aArg
) {
103 nsCOMPtr
<nsIProcess
> process
= do_CreateInstance(NS_PROCESS_CONTRACTID
, &rv
);
104 if (NS_FAILED(rv
)) return rv
;
106 if (NS_FAILED(rv
= process
->Init(mExecutable
))) return rv
;
108 const char* string
= aArg
.get();
110 return process
->Run(false, &string
, 1);
113 ////////////////////////////////////////////////////////////////////////////////
114 //// nsILocalHandlerApp
117 nsLocalHandlerApp::GetExecutable(nsIFile
** aExecutable
) {
118 NS_IF_ADDREF(*aExecutable
= mExecutable
);
123 nsLocalHandlerApp::SetExecutable(nsIFile
* aExecutable
) {
124 mExecutable
= aExecutable
;
129 nsLocalHandlerApp::GetParameterCount(uint32_t* aParameterCount
) {
130 *aParameterCount
= mParameters
.Length();
135 nsLocalHandlerApp::ClearParameters() {
141 nsLocalHandlerApp::AppendParameter(const nsAString
& aParam
) {
142 mParameters
.AppendElement(aParam
);
147 nsLocalHandlerApp::GetParameter(uint32_t parameterIndex
, nsAString
& _retval
) {
148 if (mParameters
.Length() <= parameterIndex
) return NS_ERROR_INVALID_ARG
;
150 _retval
.Assign(mParameters
[parameterIndex
]);
155 nsLocalHandlerApp::ParameterExists(const nsAString
& aParam
, bool* _retval
) {
156 *_retval
= mParameters
.Contains(aParam
);