Bug 1890689 apply drift correction to input rate instead of output rate r=pehrsons
[gecko.git] / uriloader / exthandler / nsLocalHandlerApp.cpp
blobfa3f27758fd2b03111191421bae7fcfe2d3b4519
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"
11 #include "mozilla/dom/Promise.h"
12 #include "nsProxyRelease.h"
14 // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
15 // here too?
16 NS_IMPL_ISUPPORTS(nsLocalHandlerApp, nsILocalHandlerApp, nsIHandlerApp)
18 using namespace mozilla;
20 ////////////////////////////////////////////////////////////////////////////////
21 //// nsIHandlerApp
23 NS_IMETHODIMP nsLocalHandlerApp::GetName(nsAString& aName) {
24 if (mName.IsEmpty() && mExecutable) {
25 // Don't want to cache this, just in case someone resets the app
26 // without changing the description....
27 mExecutable->GetLeafName(aName);
28 } else {
29 aName.Assign(mName);
32 return NS_OK;
35 /**
36 * This method returns a std::function that will be executed on a thread other
37 * than the main thread. To facilitate things, it should effectively be a global
38 * function that does not maintain a reference to the this pointer. There should
39 * be no reference to any objects that will be shared across threads. Sub-class
40 * implementations should make local copies of everything they need and capture
41 * those in the callback.
43 std::function<nsresult(nsString&)>
44 nsLocalHandlerApp::GetPrettyNameOnNonMainThreadCallback() {
45 nsString name;
47 // Calculate the name now, on the main thread, so as to avoid
48 // doing anything with the this pointer on the other thread
49 auto result = GetName(name);
51 return [name, result](nsString& aName) -> nsresult {
52 aName = name;
53 return result;
57 NS_IMETHODIMP
58 nsLocalHandlerApp::PrettyNameAsync(JSContext* aCx, dom::Promise** aPromise) {
59 NS_ENSURE_ARG_POINTER(aPromise);
61 *aPromise = nullptr;
63 if (!mExecutable) {
64 return NS_ERROR_FAILURE;
67 nsIGlobalObject* global = xpc::CurrentNativeGlobal(aCx);
68 if (NS_WARN_IF(!global)) {
69 return NS_ERROR_FAILURE;
72 ErrorResult err;
73 RefPtr<dom::Promise> outer = dom::Promise::Create(global, err);
74 if (NS_WARN_IF(err.Failed())) {
75 return err.StealNSResult();
78 outer.forget(aPromise);
80 nsAutoString executablePath;
81 nsresult result = mExecutable->GetPath(executablePath);
83 if (NS_FAILED(result) || executablePath.IsEmpty()) {
84 (*aPromise)->MaybeReject(result);
85 return NS_OK;
88 nsMainThreadPtrHandle<dom::Promise> promiseHolder(
89 new nsMainThreadPtrHolder<dom::Promise>(
90 "nsLocalHandlerApp::prettyExecutableName Promise", *aPromise));
92 auto prettyNameGetter = GetPrettyNameOnNonMainThreadCallback();
94 result = NS_DispatchBackgroundTask(
95 NS_NewRunnableFunction(
96 __func__,
97 [promiseHolder /* can't move this because if the dispatch fails, we
98 call reject on the promiseHolder */
100 prettyNameGetter = std::move(prettyNameGetter)]() mutable -> void {
101 nsAutoString prettyExecutableName;
103 nsresult result = prettyNameGetter(prettyExecutableName);
105 DebugOnly<nsresult> rv =
106 NS_DispatchToMainThread(NS_NewRunnableFunction(
107 __func__,
108 [promiseHolder = std::move(promiseHolder),
109 prettyExecutableName = std::move(prettyExecutableName),
110 result]() {
111 if (NS_FAILED(result)) {
112 promiseHolder.get()->MaybeReject(result);
113 } else {
114 promiseHolder.get()->MaybeResolve(prettyExecutableName);
116 }));
117 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
118 "NS_DispatchToMainThread failed");
120 NS_DISPATCH_EVENT_MAY_BLOCK);
122 if (NS_FAILED(result)) {
123 promiseHolder.get()->MaybeReject(result);
126 return NS_OK;
129 NS_IMETHODIMP nsLocalHandlerApp::SetName(const nsAString& aName) {
130 mName.Assign(aName);
132 return NS_OK;
135 NS_IMETHODIMP
136 nsLocalHandlerApp::SetDetailedDescription(const nsAString& aDescription) {
137 mDetailedDescription.Assign(aDescription);
139 return NS_OK;
142 NS_IMETHODIMP
143 nsLocalHandlerApp::GetDetailedDescription(nsAString& aDescription) {
144 aDescription.Assign(mDetailedDescription);
146 return NS_OK;
149 NS_IMETHODIMP
150 nsLocalHandlerApp::Equals(nsIHandlerApp* aHandlerApp, bool* _retval) {
151 NS_ENSURE_ARG_POINTER(aHandlerApp);
153 *_retval = false;
155 // If the handler app isn't a local handler app, then it's not the same app.
156 nsCOMPtr<nsILocalHandlerApp> localHandlerApp = do_QueryInterface(aHandlerApp);
157 if (!localHandlerApp) return NS_OK;
159 // If either handler app doesn't have an executable, then they aren't
160 // the same app.
161 nsCOMPtr<nsIFile> executable;
162 nsresult rv = localHandlerApp->GetExecutable(getter_AddRefs(executable));
163 if (NS_FAILED(rv)) return rv;
165 // Equality for two empty nsIHandlerApp
166 if (!executable && !mExecutable) {
167 *_retval = true;
168 return NS_OK;
171 // At least one is set so they are not equal
172 if (!mExecutable || !executable) return NS_OK;
174 // Check the command line parameter list lengths
175 uint32_t len;
176 localHandlerApp->GetParameterCount(&len);
177 if (mParameters.Length() != len) return NS_OK;
179 // Check the command line params lists
180 for (uint32_t idx = 0; idx < mParameters.Length(); idx++) {
181 nsAutoString param;
182 if (NS_FAILED(localHandlerApp->GetParameter(idx, param)) ||
183 !param.Equals(mParameters[idx]))
184 return NS_OK;
187 return executable->Equals(mExecutable, _retval);
190 NS_IMETHODIMP
191 nsLocalHandlerApp::LaunchWithURI(
192 nsIURI* aURI, mozilla::dom::BrowsingContext* aBrowsingContext) {
193 // pass the entire URI to the handler.
194 nsAutoCString spec;
195 aURI->GetAsciiSpec(spec);
196 return LaunchWithIProcess(spec);
199 nsresult nsLocalHandlerApp::LaunchWithIProcess(const nsCString& aArg) {
200 nsresult rv;
201 nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID, &rv);
202 if (NS_FAILED(rv)) return rv;
204 if (NS_FAILED(rv = process->Init(mExecutable))) return rv;
206 const char* string = aArg.get();
208 return process->Run(false, &string, 1);
211 ////////////////////////////////////////////////////////////////////////////////
212 //// nsILocalHandlerApp
214 NS_IMETHODIMP
215 nsLocalHandlerApp::GetExecutable(nsIFile** aExecutable) {
216 NS_IF_ADDREF(*aExecutable = mExecutable);
217 return NS_OK;
220 NS_IMETHODIMP
221 nsLocalHandlerApp::SetExecutable(nsIFile* aExecutable) {
222 mExecutable = aExecutable;
223 return NS_OK;
226 NS_IMETHODIMP
227 nsLocalHandlerApp::GetParameterCount(uint32_t* aParameterCount) {
228 *aParameterCount = mParameters.Length();
229 return NS_OK;
232 NS_IMETHODIMP
233 nsLocalHandlerApp::ClearParameters() {
234 mParameters.Clear();
235 return NS_OK;
238 NS_IMETHODIMP
239 nsLocalHandlerApp::AppendParameter(const nsAString& aParam) {
240 mParameters.AppendElement(aParam);
241 return NS_OK;
244 NS_IMETHODIMP
245 nsLocalHandlerApp::GetParameter(uint32_t parameterIndex, nsAString& _retval) {
246 if (mParameters.Length() <= parameterIndex) return NS_ERROR_INVALID_ARG;
248 _retval.Assign(mParameters[parameterIndex]);
249 return NS_OK;
252 NS_IMETHODIMP
253 nsLocalHandlerApp::ParameterExists(const nsAString& aParam, bool* _retval) {
254 *_retval = mParameters.Contains(aParam);
255 return NS_OK;