From d562c70f4efacd2a6cf78ed00b2a987be3a485e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Wed, 4 Aug 2021 19:37:15 +0200 Subject: [PATCH] environment: Fallback to Soup 2.4 We cannot assume yet that Soup3 is available everywhere. Until that is the case, allow falling back to Soup 2.4 by imitating the bits of the Soup3 API we use with monkey-patching. Part-of: --- js/ui/environment.js | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/js/ui/environment.js b/js/ui/environment.js index d3ea54387..bd372ce26 100644 --- a/js/ui/environment.js +++ b/js/ui/environment.js @@ -11,6 +11,14 @@ imports.gi.versions.Soup = '3.0'; imports.gi.versions.TelepathyGLib = '0.12'; imports.gi.versions.TelepathyLogger = '0.2'; +try { + const Soup_ = imports.gi.Soup; +} catch (e) { + imports.gi.versions.Soup = '2.4'; + const { Soup } = imports.gi; + _injectSoup3Compat(Soup); +} + const { Clutter, Gio, GLib, GObject, Meta, Polkit, Shell, St } = imports.gi; const Gettext = imports.gettext; const System = imports.system; @@ -69,6 +77,44 @@ function _patchLayoutClass(layoutClass, styleProps) { } } +/** + * Mimick the Soup 3 APIs we use when falling back to Soup 2.4 + * + * @param {object} Soup 2.4 namespace + * @returns {void} + */ +function _injectSoup3Compat(Soup) { + const ByteArray = imports.byteArray; + + Soup.StatusCode = Soup.KnownStatusCode; + + Soup.Message.new_from_encoded_form = + function (method, uri, form) { + const soupUri = new Soup.URI(uri); + soupUri.set_query(form); + return Soup.Message.new_from_uri(method, soupUri); + }; + Soup.Message.prototype.set_request_body_from_bytes = + function (contentType, bytes) { + this.set_request( + contentType, + Soup.MemoryUse.COPY, + ByteArray.toString(bytes.get_data())); + }; + + Soup.Session.prototype.send_and_read_async = + function (message, prio, cancellable, callback) { + this.queue_message(message, () => callback(this, message)); + }; + Soup.Session.prototype.send_and_read_finish = + function (message) { + if (message.status_code !== Soup.KnownStatusCode.OK) + return null; + + return message.response_body.flatten().get_as_bytes(); + }; +} + function _makeEaseCallback(params, cleanup) { let onComplete = params.onComplete; delete params.onComplete;