Line data Source code
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/. */
6 :
7 : #include "nsLocalHandlerApp.h"
8 : #include "nsIURI.h"
9 : #include "nsIProcess.h"
10 :
11 : // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
12 : // here too?
13 0 : NS_IMPL_ISUPPORTS(nsLocalHandlerApp, nsILocalHandlerApp, nsIHandlerApp)
14 :
15 : ////////////////////////////////////////////////////////////////////////////////
16 : //// nsIHandlerApp
17 :
18 0 : NS_IMETHODIMP nsLocalHandlerApp::GetName(nsAString& aName)
19 : {
20 0 : if (mName.IsEmpty() && mExecutable) {
21 : // Don't want to cache this, just in case someone resets the app
22 : // without changing the description....
23 0 : mExecutable->GetLeafName(aName);
24 : } else {
25 0 : aName.Assign(mName);
26 : }
27 :
28 0 : return NS_OK;
29 : }
30 :
31 0 : NS_IMETHODIMP nsLocalHandlerApp::SetName(const nsAString & aName)
32 : {
33 0 : mName.Assign(aName);
34 :
35 0 : return NS_OK;
36 : }
37 :
38 : NS_IMETHODIMP
39 0 : nsLocalHandlerApp::SetDetailedDescription(const nsAString & aDescription)
40 : {
41 0 : mDetailedDescription.Assign(aDescription);
42 :
43 0 : return NS_OK;
44 : }
45 :
46 : NS_IMETHODIMP
47 0 : nsLocalHandlerApp::GetDetailedDescription(nsAString& aDescription)
48 : {
49 0 : aDescription.Assign(mDetailedDescription);
50 :
51 0 : return NS_OK;
52 : }
53 :
54 : NS_IMETHODIMP
55 0 : nsLocalHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval)
56 : {
57 0 : NS_ENSURE_ARG_POINTER(aHandlerApp);
58 :
59 0 : *_retval = false;
60 :
61 : // If the handler app isn't a local handler app, then it's not the same app.
62 0 : nsCOMPtr <nsILocalHandlerApp> localHandlerApp = do_QueryInterface(aHandlerApp);
63 0 : if (!localHandlerApp)
64 0 : return NS_OK;
65 :
66 : // If either handler app doesn't have an executable, then they aren't
67 : // the same app.
68 0 : nsCOMPtr<nsIFile> executable;
69 0 : nsresult rv = localHandlerApp->GetExecutable(getter_AddRefs(executable));
70 0 : if (NS_FAILED(rv))
71 0 : return rv;
72 :
73 : // Equality for two empty nsIHandlerApp
74 0 : if (!executable && !mExecutable) {
75 0 : *_retval = true;
76 0 : return NS_OK;
77 : }
78 :
79 : // At least one is set so they are not equal
80 0 : if (!mExecutable || !executable)
81 0 : return NS_OK;
82 :
83 : // Check the command line parameter list lengths
84 : uint32_t len;
85 0 : localHandlerApp->GetParameterCount(&len);
86 0 : if (mParameters.Length() != len)
87 0 : return NS_OK;
88 :
89 : // Check the command line params lists
90 0 : for (uint32_t idx = 0; idx < mParameters.Length(); idx++) {
91 0 : nsAutoString param;
92 0 : if (NS_FAILED(localHandlerApp->GetParameter(idx, param)) ||
93 0 : !param.Equals(mParameters[idx]))
94 0 : return NS_OK;
95 : }
96 :
97 0 : return executable->Equals(mExecutable, _retval);
98 : }
99 :
100 : NS_IMETHODIMP
101 0 : nsLocalHandlerApp::LaunchWithURI(nsIURI *aURI,
102 : nsIInterfaceRequestor *aWindowContext)
103 : {
104 : // pass the entire URI to the handler.
105 0 : nsAutoCString spec;
106 0 : aURI->GetAsciiSpec(spec);
107 0 : return LaunchWithIProcess(spec);
108 : }
109 :
110 : nsresult
111 0 : nsLocalHandlerApp::LaunchWithIProcess(const nsCString& aArg)
112 : {
113 : nsresult rv;
114 0 : nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID, &rv);
115 0 : if (NS_FAILED(rv))
116 0 : return rv;
117 :
118 0 : if (NS_FAILED(rv = process->Init(mExecutable)))
119 0 : return rv;
120 :
121 0 : const char *string = aArg.get();
122 :
123 0 : return process->Run(false, &string, 1);
124 : }
125 :
126 : ////////////////////////////////////////////////////////////////////////////////
127 : //// nsILocalHandlerApp
128 :
129 : NS_IMETHODIMP
130 0 : nsLocalHandlerApp::GetExecutable(nsIFile **aExecutable)
131 : {
132 0 : NS_IF_ADDREF(*aExecutable = mExecutable);
133 0 : return NS_OK;
134 : }
135 :
136 : NS_IMETHODIMP
137 0 : nsLocalHandlerApp::SetExecutable(nsIFile *aExecutable)
138 : {
139 0 : mExecutable = aExecutable;
140 0 : return NS_OK;
141 : }
142 :
143 : NS_IMETHODIMP
144 0 : nsLocalHandlerApp::GetParameterCount(uint32_t *aParameterCount)
145 : {
146 0 : *aParameterCount = mParameters.Length();
147 0 : return NS_OK;
148 : }
149 :
150 : NS_IMETHODIMP
151 0 : nsLocalHandlerApp::ClearParameters()
152 : {
153 0 : mParameters.Clear();
154 0 : return NS_OK;
155 : }
156 :
157 : NS_IMETHODIMP
158 0 : nsLocalHandlerApp::AppendParameter(const nsAString & aParam)
159 : {
160 0 : mParameters.AppendElement(aParam);
161 0 : return NS_OK;
162 : }
163 :
164 : NS_IMETHODIMP
165 0 : nsLocalHandlerApp::GetParameter(uint32_t parameterIndex, nsAString & _retval)
166 : {
167 0 : if (mParameters.Length() <= parameterIndex)
168 0 : return NS_ERROR_INVALID_ARG;
169 :
170 0 : _retval.Assign(mParameters[parameterIndex]);
171 0 : return NS_OK;
172 : }
173 :
174 : NS_IMETHODIMP
175 0 : nsLocalHandlerApp::ParameterExists(const nsAString & aParam, bool *_retval)
176 : {
177 0 : *_retval = mParameters.Contains(aParam);
178 0 : return NS_OK;
179 : }
|