Bumping manifests a=b2g-bump
[gecko.git] / uriloader / exthandler / nsLocalHandlerApp.cpp
blobe06cc39a5effdb7176d64caef998c3502be7715a
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"
8 #include "nsIURI.h"
9 #include "nsIProcess.h"
11 // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
12 // here too?
13 NS_IMPL_ISUPPORTS(nsLocalHandlerApp, nsILocalHandlerApp, nsIHandlerApp)
15 ////////////////////////////////////////////////////////////////////////////////
16 //// nsIHandlerApp
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);
24 } else {
25 aName.Assign(mName);
28 return NS_OK;
31 NS_IMETHODIMP nsLocalHandlerApp::SetName(const nsAString & aName)
33 mName.Assign(aName);
35 return NS_OK;
38 NS_IMETHODIMP
39 nsLocalHandlerApp::SetDetailedDescription(const nsAString & aDescription)
41 mDetailedDescription.Assign(aDescription);
43 return NS_OK;
46 NS_IMETHODIMP
47 nsLocalHandlerApp::GetDetailedDescription(nsAString& aDescription)
49 aDescription.Assign(mDetailedDescription);
51 return NS_OK;
54 NS_IMETHODIMP
55 nsLocalHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval)
57 NS_ENSURE_ARG_POINTER(aHandlerApp);
59 *_retval = false;
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);
63 if (!localHandlerApp)
64 return NS_OK;
66 // If either handler app doesn't have an executable, then they aren't
67 // the same app.
68 nsCOMPtr<nsIFile> executable;
69 nsresult rv = localHandlerApp->GetExecutable(getter_AddRefs(executable));
70 if (NS_FAILED(rv))
71 return rv;
73 // Equality for two empty nsIHandlerApp
74 if (!executable && !mExecutable) {
75 *_retval = true;
76 return NS_OK;
79 // At least one is set so they are not equal
80 if (!mExecutable || !executable)
81 return NS_OK;
83 // Check the command line parameter list lengths
84 uint32_t len;
85 localHandlerApp->GetParameterCount(&len);
86 if (mParameters.Length() != len)
87 return NS_OK;
89 // Check the command line params lists
90 for (uint32_t idx = 0; idx < mParameters.Length(); idx++) {
91 nsAutoString param;
92 if (NS_FAILED(localHandlerApp->GetParameter(idx, param)) ||
93 !param.Equals(mParameters[idx]))
94 return NS_OK;
97 return executable->Equals(mExecutable, _retval);
100 NS_IMETHODIMP
101 nsLocalHandlerApp::LaunchWithURI(nsIURI *aURI,
102 nsIInterfaceRequestor *aWindowContext)
104 // pass the entire URI to the handler.
105 nsAutoCString spec;
106 aURI->GetAsciiSpec(spec);
107 return LaunchWithIProcess(spec);
110 nsresult
111 nsLocalHandlerApp::LaunchWithIProcess(const nsCString& aArg)
113 nsresult rv;
114 nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID, &rv);
115 if (NS_FAILED(rv))
116 return rv;
118 if (NS_FAILED(rv = process->Init(mExecutable)))
119 return rv;
121 const char *string = aArg.get();
123 return process->Run(false, &string, 1);
126 ////////////////////////////////////////////////////////////////////////////////
127 //// nsILocalHandlerApp
129 /* attribute nsIFile executable; */
130 NS_IMETHODIMP
131 nsLocalHandlerApp::GetExecutable(nsIFile **aExecutable)
133 NS_IF_ADDREF(*aExecutable = mExecutable);
134 return NS_OK;
137 NS_IMETHODIMP
138 nsLocalHandlerApp::SetExecutable(nsIFile *aExecutable)
140 mExecutable = aExecutable;
141 return NS_OK;
144 /* readonly attribute unsigned long parameterCount; */
145 NS_IMETHODIMP
146 nsLocalHandlerApp::GetParameterCount(uint32_t *aParameterCount)
148 *aParameterCount = mParameters.Length();
149 return NS_OK;
152 /* void clearParameters (); */
153 NS_IMETHODIMP
154 nsLocalHandlerApp::ClearParameters()
156 mParameters.Clear();
157 return NS_OK;
160 /* void appendParameter (in AString param); */
161 NS_IMETHODIMP
162 nsLocalHandlerApp::AppendParameter(const nsAString & aParam)
164 mParameters.AppendElement(aParam);
165 return NS_OK;
168 /* AString getParameter (in unsigned long parameterIndex); */
169 NS_IMETHODIMP
170 nsLocalHandlerApp::GetParameter(uint32_t parameterIndex, nsAString & _retval)
172 if (mParameters.Length() <= parameterIndex)
173 return NS_ERROR_INVALID_ARG;
175 _retval.Assign(mParameters[parameterIndex]);
176 return NS_OK;
179 /* boolean parameterExists (in AString param); */
180 NS_IMETHODIMP
181 nsLocalHandlerApp::ParameterExists(const nsAString & aParam, bool *_retval)
183 *_retval = mParameters.Contains(aParam);
184 return NS_OK;