Bug 1869043 assert that graph set access is main thread only r=padenot
[gecko.git] / uriloader / exthandler / nsLocalHandlerApp.cpp
blob6212d2aeb441488ca92d8906ac3d0d108b3e707c
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"
10 #include "nsComponentManagerUtils.h"
12 // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
13 // here too?
14 NS_IMPL_ISUPPORTS(nsLocalHandlerApp, nsILocalHandlerApp, nsIHandlerApp)
16 ////////////////////////////////////////////////////////////////////////////////
17 //// nsIHandlerApp
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);
24 } else {
25 aName.Assign(mName);
28 return NS_OK;
31 NS_IMETHODIMP nsLocalHandlerApp::SetName(const nsAString& aName) {
32 mName.Assign(aName);
34 return NS_OK;
37 NS_IMETHODIMP
38 nsLocalHandlerApp::SetDetailedDescription(const nsAString& aDescription) {
39 mDetailedDescription.Assign(aDescription);
41 return NS_OK;
44 NS_IMETHODIMP
45 nsLocalHandlerApp::GetDetailedDescription(nsAString& aDescription) {
46 aDescription.Assign(mDetailedDescription);
48 return NS_OK;
51 NS_IMETHODIMP
52 nsLocalHandlerApp::Equals(nsIHandlerApp* aHandlerApp, bool* _retval) {
53 NS_ENSURE_ARG_POINTER(aHandlerApp);
55 *_retval = false;
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
62 // the same app.
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) {
69 *_retval = true;
70 return NS_OK;
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
77 uint32_t len;
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++) {
83 nsAutoString param;
84 if (NS_FAILED(localHandlerApp->GetParameter(idx, param)) ||
85 !param.Equals(mParameters[idx]))
86 return NS_OK;
89 return executable->Equals(mExecutable, _retval);
92 NS_IMETHODIMP
93 nsLocalHandlerApp::LaunchWithURI(
94 nsIURI* aURI, mozilla::dom::BrowsingContext* aBrowsingContext) {
95 // pass the entire URI to the handler.
96 nsAutoCString spec;
97 aURI->GetAsciiSpec(spec);
98 return LaunchWithIProcess(spec);
101 nsresult nsLocalHandlerApp::LaunchWithIProcess(const nsCString& aArg) {
102 nsresult rv;
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
116 NS_IMETHODIMP
117 nsLocalHandlerApp::GetExecutable(nsIFile** aExecutable) {
118 NS_IF_ADDREF(*aExecutable = mExecutable);
119 return NS_OK;
122 NS_IMETHODIMP
123 nsLocalHandlerApp::SetExecutable(nsIFile* aExecutable) {
124 mExecutable = aExecutable;
125 return NS_OK;
128 NS_IMETHODIMP
129 nsLocalHandlerApp::GetParameterCount(uint32_t* aParameterCount) {
130 *aParameterCount = mParameters.Length();
131 return NS_OK;
134 NS_IMETHODIMP
135 nsLocalHandlerApp::ClearParameters() {
136 mParameters.Clear();
137 return NS_OK;
140 NS_IMETHODIMP
141 nsLocalHandlerApp::AppendParameter(const nsAString& aParam) {
142 mParameters.AppendElement(aParam);
143 return NS_OK;
146 NS_IMETHODIMP
147 nsLocalHandlerApp::GetParameter(uint32_t parameterIndex, nsAString& _retval) {
148 if (mParameters.Length() <= parameterIndex) return NS_ERROR_INVALID_ARG;
150 _retval.Assign(mParameters[parameterIndex]);
151 return NS_OK;
154 NS_IMETHODIMP
155 nsLocalHandlerApp::ParameterExists(const nsAString& aParam, bool* _retval) {
156 *_retval = mParameters.Contains(aParam);
157 return NS_OK;