Bug 1728955: part 8) Refactor `DisplayErrCode` in Windows' `nsClipboard`. r=masayuki
[gecko.git] / uriloader / exthandler / nsLocalHandlerApp.cpp
blobf1a9a779cf7d135b05ea322cd07db43a4401f910
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) {
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);
23 } else {
24 aName.Assign(mName);
27 return NS_OK;
30 NS_IMETHODIMP nsLocalHandlerApp::SetName(const nsAString& aName) {
31 mName.Assign(aName);
33 return NS_OK;
36 NS_IMETHODIMP
37 nsLocalHandlerApp::SetDetailedDescription(const nsAString& aDescription) {
38 mDetailedDescription.Assign(aDescription);
40 return NS_OK;
43 NS_IMETHODIMP
44 nsLocalHandlerApp::GetDetailedDescription(nsAString& aDescription) {
45 aDescription.Assign(mDetailedDescription);
47 return NS_OK;
50 NS_IMETHODIMP
51 nsLocalHandlerApp::Equals(nsIHandlerApp* aHandlerApp, bool* _retval) {
52 NS_ENSURE_ARG_POINTER(aHandlerApp);
54 *_retval = false;
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
61 // the same app.
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) {
68 *_retval = true;
69 return NS_OK;
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
76 uint32_t len;
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++) {
82 nsAutoString param;
83 if (NS_FAILED(localHandlerApp->GetParameter(idx, param)) ||
84 !param.Equals(mParameters[idx]))
85 return NS_OK;
88 return executable->Equals(mExecutable, _retval);
91 NS_IMETHODIMP
92 nsLocalHandlerApp::LaunchWithURI(
93 nsIURI* aURI, mozilla::dom::BrowsingContext* aBrowsingContext) {
94 // pass the entire URI to the handler.
95 nsAutoCString spec;
96 aURI->GetAsciiSpec(spec);
97 return LaunchWithIProcess(spec);
100 nsresult nsLocalHandlerApp::LaunchWithIProcess(const nsCString& aArg) {
101 nsresult rv;
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
115 NS_IMETHODIMP
116 nsLocalHandlerApp::GetExecutable(nsIFile** aExecutable) {
117 NS_IF_ADDREF(*aExecutable = mExecutable);
118 return NS_OK;
121 NS_IMETHODIMP
122 nsLocalHandlerApp::SetExecutable(nsIFile* aExecutable) {
123 mExecutable = aExecutable;
124 return NS_OK;
127 NS_IMETHODIMP
128 nsLocalHandlerApp::GetParameterCount(uint32_t* aParameterCount) {
129 *aParameterCount = mParameters.Length();
130 return NS_OK;
133 NS_IMETHODIMP
134 nsLocalHandlerApp::ClearParameters() {
135 mParameters.Clear();
136 return NS_OK;
139 NS_IMETHODIMP
140 nsLocalHandlerApp::AppendParameter(const nsAString& aParam) {
141 mParameters.AppendElement(aParam);
142 return NS_OK;
145 NS_IMETHODIMP
146 nsLocalHandlerApp::GetParameter(uint32_t parameterIndex, nsAString& _retval) {
147 if (mParameters.Length() <= parameterIndex) return NS_ERROR_INVALID_ARG;
149 _retval.Assign(mParameters[parameterIndex]);
150 return NS_OK;
153 NS_IMETHODIMP
154 nsLocalHandlerApp::ParameterExists(const nsAString& aParam, bool* _retval) {
155 *_retval = mParameters.Contains(aParam);
156 return NS_OK;