1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim:expandtab:shiftwidth=2:tabstop=2:cin:
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is the Mozilla browser.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * the Mozilla Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2007
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Dan Mosedale <dmose@mozilla.org>
25 : * Myk Melez <myk@mozilla.org>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsLocalHandlerApp.h"
42 : #include "nsIURI.h"
43 : #include "nsIProcess.h"
44 :
45 : // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
46 : // here too?
47 384 : NS_IMPL_ISUPPORTS2(nsLocalHandlerApp, nsILocalHandlerApp, nsIHandlerApp)
48 :
49 : ////////////////////////////////////////////////////////////////////////////////
50 : //// nsIHandlerApp
51 :
52 3 : NS_IMETHODIMP nsLocalHandlerApp::GetName(nsAString& aName)
53 : {
54 3 : if (mName.IsEmpty() && mExecutable) {
55 : // Don't want to cache this, just in case someone resets the app
56 : // without changing the description....
57 0 : mExecutable->GetLeafName(aName);
58 : } else {
59 3 : aName.Assign(mName);
60 : }
61 :
62 3 : return NS_OK;
63 : }
64 :
65 7 : NS_IMETHODIMP nsLocalHandlerApp::SetName(const nsAString & aName)
66 : {
67 7 : mName.Assign(aName);
68 :
69 7 : return NS_OK;
70 : }
71 :
72 : NS_IMETHODIMP
73 0 : nsLocalHandlerApp::SetDetailedDescription(const nsAString & aDescription)
74 : {
75 0 : mDetailedDescription.Assign(aDescription);
76 :
77 0 : return NS_OK;
78 : }
79 :
80 : NS_IMETHODIMP
81 0 : nsLocalHandlerApp::GetDetailedDescription(nsAString& aDescription)
82 : {
83 0 : aDescription.Assign(mDetailedDescription);
84 :
85 0 : return NS_OK;
86 : }
87 :
88 : NS_IMETHODIMP
89 5 : nsLocalHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval)
90 : {
91 5 : NS_ENSURE_ARG_POINTER(aHandlerApp);
92 :
93 5 : *_retval = false;
94 :
95 : // If the handler app isn't a local handler app, then it's not the same app.
96 10 : nsCOMPtr <nsILocalHandlerApp> localHandlerApp = do_QueryInterface(aHandlerApp);
97 5 : if (!localHandlerApp)
98 0 : return NS_OK;
99 :
100 : // If either handler app doesn't have an executable, then they aren't
101 : // the same app.
102 10 : nsCOMPtr<nsIFile> executable;
103 5 : nsresult rv = localHandlerApp->GetExecutable(getter_AddRefs(executable));
104 5 : if (NS_FAILED(rv))
105 0 : return rv;
106 :
107 : // Equality for two empty nsIHandlerApp
108 5 : if (!executable && !mExecutable) {
109 1 : *_retval = true;
110 1 : return NS_OK;
111 : }
112 :
113 : // At least one is set so they are not equal
114 4 : if (!mExecutable || !executable)
115 0 : return NS_OK;
116 :
117 : // Check the command line parameter list lengths
118 : PRUint32 len;
119 4 : localHandlerApp->GetParameterCount(&len);
120 4 : if (mParameters.Length() != len)
121 0 : return NS_OK;
122 :
123 : // Check the command line params lists
124 7 : for (PRUint32 idx = 0; idx < mParameters.Length(); idx++) {
125 8 : nsAutoString param;
126 8 : if (NS_FAILED(localHandlerApp->GetParameter(idx, param)) ||
127 4 : !param.Equals(mParameters[idx]))
128 1 : return NS_OK;
129 : }
130 :
131 3 : return executable->Equals(mExecutable, _retval);
132 : }
133 :
134 : NS_IMETHODIMP
135 1 : nsLocalHandlerApp::LaunchWithURI(nsIURI *aURI,
136 : nsIInterfaceRequestor *aWindowContext)
137 : {
138 : // pass the entire URI to the handler.
139 2 : nsCAutoString spec;
140 1 : aURI->GetAsciiSpec(spec);
141 1 : return LaunchWithIProcess(spec);
142 : }
143 :
144 : nsresult
145 1 : nsLocalHandlerApp::LaunchWithIProcess(const nsCString& aArg)
146 : {
147 : nsresult rv;
148 2 : nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID, &rv);
149 1 : if (NS_FAILED(rv))
150 0 : return rv;
151 :
152 1 : if (NS_FAILED(rv = process->Init(mExecutable)))
153 0 : return rv;
154 :
155 1 : const char *string = aArg.get();
156 :
157 1 : return process->Run(false, &string, 1);
158 : }
159 :
160 : ////////////////////////////////////////////////////////////////////////////////
161 : //// nsILocalHandlerApp
162 :
163 : /* attribute nsIFile executable; */
164 : NS_IMETHODIMP
165 7 : nsLocalHandlerApp::GetExecutable(nsIFile **aExecutable)
166 : {
167 7 : NS_IF_ADDREF(*aExecutable = mExecutable);
168 7 : return NS_OK;
169 : }
170 :
171 : NS_IMETHODIMP
172 9 : nsLocalHandlerApp::SetExecutable(nsIFile *aExecutable)
173 : {
174 9 : mExecutable = aExecutable;
175 9 : return NS_OK;
176 : }
177 :
178 : /* readonly attribute unsigned long parameterCount; */
179 : NS_IMETHODIMP
180 7 : nsLocalHandlerApp::GetParameterCount(PRUint32 *aParameterCount)
181 : {
182 7 : *aParameterCount = mParameters.Length();
183 7 : return NS_OK;
184 : }
185 :
186 : /* void clearParameters (); */
187 : NS_IMETHODIMP
188 4 : nsLocalHandlerApp::ClearParameters()
189 : {
190 4 : mParameters.Clear();
191 4 : return NS_OK;
192 : }
193 :
194 : /* void appendParameter (in AString param); */
195 : NS_IMETHODIMP
196 14 : nsLocalHandlerApp::AppendParameter(const nsAString & aParam)
197 : {
198 14 : mParameters.AppendElement(aParam);
199 14 : return NS_OK;
200 : }
201 :
202 : /* AString getParameter (in unsigned long parameterIndex); */
203 : NS_IMETHODIMP
204 7 : nsLocalHandlerApp::GetParameter(PRUint32 parameterIndex, nsAString & _retval)
205 : {
206 7 : if (mParameters.Length() <= parameterIndex)
207 0 : return NS_ERROR_INVALID_ARG;
208 :
209 7 : _retval.Assign(mParameters[parameterIndex]);
210 7 : return NS_OK;
211 : }
212 :
213 : /* boolean parameterExists (in AString param); */
214 : NS_IMETHODIMP
215 3 : nsLocalHandlerApp::ParameterExists(const nsAString & aParam, bool *_retval)
216 : {
217 3 : *_retval = mParameters.Contains(aParam);
218 3 : return NS_OK;
219 : }
|