diff options
Diffstat (limited to 'toolkit/system')
41 files changed, 4323 insertions, 0 deletions
diff --git a/toolkit/system/androidproxy/components.conf b/toolkit/system/androidproxy/components.conf new file mode 100644 index 0000000000..499674c32b --- /dev/null +++ b/toolkit/system/androidproxy/components.conf @@ -0,0 +1,13 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +Classes = [ + { + 'cid': '{f01f0060-3708-478e-b935-3ec38be294b8}', + 'contract_ids': ['@mozilla.org/system-proxy-settings;1'], + 'type': 'nsAndroidSystemProxySettings', + }, +] diff --git a/toolkit/system/androidproxy/moz.build b/toolkit/system/androidproxy/moz.build new file mode 100644 index 0000000000..3d1a834676 --- /dev/null +++ b/toolkit/system/androidproxy/moz.build @@ -0,0 +1,18 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("GeckoView", "General") + +SOURCES += [ + "nsAndroidSystemProxySettings.cpp", +] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +FINAL_LIBRARY = "xul" diff --git a/toolkit/system/androidproxy/nsAndroidSystemProxySettings.cpp b/toolkit/system/androidproxy/nsAndroidSystemProxySettings.cpp new file mode 100644 index 0000000000..e60d0f3821 --- /dev/null +++ b/toolkit/system/androidproxy/nsAndroidSystemProxySettings.cpp @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsISystemProxySettings.h" +#include "mozilla/Components.h" +#include "nsPrintfCString.h" +#include "nsNetCID.h" +#include "nsISupports.h" + +#include "AndroidBridge.h" + +class nsAndroidSystemProxySettings : public nsISystemProxySettings { + public: + NS_DECL_THREADSAFE_ISUPPORTS + NS_DECL_NSISYSTEMPROXYSETTINGS + + nsAndroidSystemProxySettings(){}; + + private: + virtual ~nsAndroidSystemProxySettings() {} +}; + +NS_IMPL_ISUPPORTS(nsAndroidSystemProxySettings, nsISystemProxySettings) + +NS_IMETHODIMP +nsAndroidSystemProxySettings::GetMainThreadOnly(bool* aMainThreadOnly) { + *aMainThreadOnly = true; + return NS_OK; +} + +nsresult nsAndroidSystemProxySettings::GetPACURI(nsACString& aResult) { + return NS_OK; +} + +nsresult nsAndroidSystemProxySettings::GetProxyForURI(const nsACString& aSpec, + const nsACString& aScheme, + const nsACString& aHost, + const int32_t aPort, + nsACString& aResult) { + return mozilla::AndroidBridge::Bridge()->GetProxyForURI(aSpec, aScheme, aHost, + aPort, aResult); +} + +void test(){}; + +NS_IMPL_COMPONENT_FACTORY(nsAndroidSystemProxySettings) { + return mozilla::MakeAndAddRef<nsAndroidSystemProxySettings>() + .downcast<nsISupports>(); +} diff --git a/toolkit/system/gnome/components.conf b/toolkit/system/gnome/components.conf new file mode 100644 index 0000000000..1aa0ab1048 --- /dev/null +++ b/toolkit/system/gnome/components.conf @@ -0,0 +1,28 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +Classes = [ + { + 'cid': '{e3a1f3c9-3ae1-4b40-a5e0-7b457fc9a9ad}', + 'contract_ids': ['@mozilla.org/gio-service;1'], + 'type': 'nsGIOService', + 'headers': ['/toolkit/system/gnome/nsGIOService.h'], + }, + { + 'cid': '{bfd4a9d8-d886-4161-81ef-8868da114170}', + 'contract_ids': ['@mozilla.org/gsettings-service;1'], + 'type': 'nsGSettingsService', + 'headers': ['/toolkit/system/gnome/nsGSettingsService.h'], + 'init_method': 'Init', + }, + { + 'cid': '{84e11f80-ca55-11dd-ad8b-0800200c9a66}', + 'contract_ids': ['@mozilla.org/system-alerts-service;1'], + 'type': 'nsSystemAlertsService', + 'headers': ['/toolkit/system/gnome/nsSystemAlertsService.h'], + 'init_method': 'Init', + }, +] diff --git a/toolkit/system/gnome/moz.build b/toolkit/system/gnome/moz.build new file mode 100644 index 0000000000..0fc94ed9e3 --- /dev/null +++ b/toolkit/system/gnome/moz.build @@ -0,0 +1,32 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Firefox", "Shell Integration") + +SOURCES += [ + "nsAlertsIconListener.cpp", + "nsSystemAlertsService.cpp", +] + +SOURCES += [ + "nsGIOService.cpp", + "nsGSettingsService.cpp", +] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +FINAL_LIBRARY = "xul" + +LOCAL_INCLUDES += [ + "/toolkit/components/build/", +] + +CXXFLAGS += CONFIG["GLIB_CFLAGS"] +CXXFLAGS += CONFIG["MOZ_DBUS_GLIB_CFLAGS"] +CXXFLAGS += CONFIG["TK_CFLAGS"] diff --git a/toolkit/system/gnome/nsAlertsIconListener.cpp b/toolkit/system/gnome/nsAlertsIconListener.cpp new file mode 100644 index 0000000000..9c3d796c9d --- /dev/null +++ b/toolkit/system/gnome/nsAlertsIconListener.cpp @@ -0,0 +1,352 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsAlertsIconListener.h" +#include "imgIContainer.h" +#include "imgIRequest.h" +#include "nsNetUtil.h" +#include "nsServiceManagerUtils.h" +#include "nsSystemAlertsService.h" +#include "nsIAlertsService.h" +#include "nsICancelable.h" +#include "nsIImageToPixbuf.h" +#include "nsIStringBundle.h" +#include "nsIObserverService.h" +#include "nsCRT.h" +#include "mozilla/XREAppData.h" + +#include <dlfcn.h> +#include <gdk/gdk.h> + +extern const mozilla::StaticXREAppData* gAppData; + +static bool gHasActions = false; +static bool gHasCaps = false; + +void* nsAlertsIconListener::libNotifyHandle = nullptr; +bool nsAlertsIconListener::libNotifyNotAvail = false; +nsAlertsIconListener::notify_is_initted_t + nsAlertsIconListener::notify_is_initted = nullptr; +nsAlertsIconListener::notify_init_t nsAlertsIconListener::notify_init = nullptr; +nsAlertsIconListener::notify_get_server_caps_t + nsAlertsIconListener::notify_get_server_caps = nullptr; +nsAlertsIconListener::notify_notification_new_t + nsAlertsIconListener::notify_notification_new = nullptr; +nsAlertsIconListener::notify_notification_show_t + nsAlertsIconListener::notify_notification_show = nullptr; +nsAlertsIconListener::notify_notification_set_icon_from_pixbuf_t + nsAlertsIconListener::notify_notification_set_icon_from_pixbuf = nullptr; +nsAlertsIconListener::notify_notification_add_action_t + nsAlertsIconListener::notify_notification_add_action = nullptr; +nsAlertsIconListener::notify_notification_close_t + nsAlertsIconListener::notify_notification_close = nullptr; +nsAlertsIconListener::notify_notification_set_hint_t + nsAlertsIconListener::notify_notification_set_hint = nullptr; + +static void notify_action_cb(NotifyNotification* notification, gchar* action, + gpointer user_data) { + nsAlertsIconListener* alert = static_cast<nsAlertsIconListener*>(user_data); + alert->SendCallback(); +} + +static void notify_closed_marshal(GClosure* closure, GValue* return_value, + guint n_param_values, + const GValue* param_values, + gpointer invocation_hint, + gpointer marshal_data) { + MOZ_ASSERT(n_param_values >= 1, "No object in params"); + + nsAlertsIconListener* alert = + static_cast<nsAlertsIconListener*>(closure->data); + alert->SendClosed(); + NS_RELEASE(alert); +} + +static GdkPixbuf* GetPixbufFromImgRequest(imgIRequest* aRequest) { + nsCOMPtr<imgIContainer> image; + nsresult rv = aRequest->GetImage(getter_AddRefs(image)); + if (NS_FAILED(rv)) { + return nullptr; + } + + int32_t width = 0, height = 0; + const int32_t kBytesPerPixel = 4; + // DBUS_MAXIMUM_ARRAY_LENGTH is 64M, there is 60 bytes overhead + // for the hints array with only the image payload, 256 is used to give + // some breathing room. + const int32_t kMaxImageBytes = 64 * 1024 * 1024 - 256; + image->GetWidth(&width); + image->GetHeight(&height); + if (width * height * kBytesPerPixel > kMaxImageBytes) { + // The image won't fit in a dbus array + return nullptr; + } + + nsCOMPtr<nsIImageToPixbuf> imgToPixbuf = + do_GetService("@mozilla.org/widget/image-to-gdk-pixbuf;1"); + + return imgToPixbuf->ConvertImageToPixbuf(image); +} + +NS_IMPL_ISUPPORTS(nsAlertsIconListener, nsIAlertNotificationImageListener, + nsIObserver, nsISupportsWeakReference) + +nsAlertsIconListener::nsAlertsIconListener(nsSystemAlertsService* aBackend, + const nsAString& aAlertName) + : mAlertName(aAlertName), mBackend(aBackend), mNotification(nullptr) { + if (!libNotifyHandle && !libNotifyNotAvail) { + libNotifyHandle = dlopen("libnotify.so.4", RTLD_LAZY); + if (!libNotifyHandle) { + libNotifyHandle = dlopen("libnotify.so.1", RTLD_LAZY); + if (!libNotifyHandle) { + libNotifyNotAvail = true; + return; + } + } + + notify_is_initted = + (notify_is_initted_t)dlsym(libNotifyHandle, "notify_is_initted"); + notify_init = (notify_init_t)dlsym(libNotifyHandle, "notify_init"); + notify_get_server_caps = (notify_get_server_caps_t)dlsym( + libNotifyHandle, "notify_get_server_caps"); + notify_notification_new = (notify_notification_new_t)dlsym( + libNotifyHandle, "notify_notification_new"); + notify_notification_show = (notify_notification_show_t)dlsym( + libNotifyHandle, "notify_notification_show"); + notify_notification_set_icon_from_pixbuf = + (notify_notification_set_icon_from_pixbuf_t)dlsym( + libNotifyHandle, "notify_notification_set_icon_from_pixbuf"); + notify_notification_add_action = (notify_notification_add_action_t)dlsym( + libNotifyHandle, "notify_notification_add_action"); + notify_notification_close = (notify_notification_close_t)dlsym( + libNotifyHandle, "notify_notification_close"); + notify_notification_set_hint = (notify_notification_set_hint_t)dlsym( + libNotifyHandle, "notify_notification_set_hint"); + if (!notify_is_initted || !notify_init || !notify_get_server_caps || + !notify_notification_new || !notify_notification_show || + !notify_notification_set_icon_from_pixbuf || + !notify_notification_add_action || !notify_notification_close) { + dlclose(libNotifyHandle); + libNotifyHandle = nullptr; + } + } +} + +nsAlertsIconListener::~nsAlertsIconListener() { + mBackend->RemoveListener(mAlertName, this); + // Don't dlclose libnotify as it uses atexit(). +} + +NS_IMETHODIMP +nsAlertsIconListener::OnImageMissing(nsISupports*) { + // This notification doesn't have an image, or there was an error getting + // the image. Show the notification without an icon. + return ShowAlert(nullptr); +} + +NS_IMETHODIMP +nsAlertsIconListener::OnImageReady(nsISupports*, imgIRequest* aRequest) { + GdkPixbuf* imagePixbuf = GetPixbufFromImgRequest(aRequest); + if (!imagePixbuf) { + ShowAlert(nullptr); + } else { + ShowAlert(imagePixbuf); + g_object_unref(imagePixbuf); + } + + return NS_OK; +} + +nsresult nsAlertsIconListener::ShowAlert(GdkPixbuf* aPixbuf) { + if (!mBackend->IsActiveListener(mAlertName, this)) return NS_OK; + + mNotification = notify_notification_new(mAlertTitle.get(), mAlertText.get(), + nullptr, nullptr); + + if (!mNotification) return NS_ERROR_OUT_OF_MEMORY; + + nsCOMPtr<nsIObserverService> obsServ = + do_GetService("@mozilla.org/observer-service;1"); + if (obsServ) obsServ->AddObserver(this, "quit-application", true); + + if (aPixbuf) notify_notification_set_icon_from_pixbuf(mNotification, aPixbuf); + + NS_ADDREF(this); + if (mAlertHasAction) { + // What we put as the label doesn't matter here, if the action + // string is "default" then that makes the entire bubble clickable + // rather than creating a button. + notify_notification_add_action(mNotification, "default", "Activate", + notify_action_cb, this, nullptr); + } + + if (notify_notification_set_hint) { + // If MOZ_DESKTOP_FILE_NAME variable is set, use it as the application id, + // otherwise use gAppData->name + if (getenv("MOZ_DESKTOP_FILE_NAME")) { + // Send the desktop name to identify the application + // The desktop-entry is the part before the .desktop + notify_notification_set_hint( + mNotification, "desktop-entry", + g_variant_new("s", getenv("MOZ_DESKTOP_FILE_NAME"))); + } else { + notify_notification_set_hint(mNotification, "desktop-entry", + g_variant_new("s", gAppData->remotingName)); + } + } + + // Fedora 10 calls NotifyNotification "closed" signal handlers with a + // different signature, so a marshaller is used instead of a C callback to + // get the user_data (this) in a parseable format. |closure| is created + // with a floating reference, which gets sunk by g_signal_connect_closure(). + GClosure* closure = g_closure_new_simple(sizeof(GClosure), this); + g_closure_set_marshal(closure, notify_closed_marshal); + mClosureHandler = + g_signal_connect_closure(mNotification, "closed", closure, FALSE); + GError* error = nullptr; + if (!notify_notification_show(mNotification, &error)) { + NS_WARNING(error->message); + g_error_free(error); + return NS_ERROR_FAILURE; + } + + if (mAlertListener) + mAlertListener->Observe(nullptr, "alertshow", mAlertCookie.get()); + + return NS_OK; +} + +void nsAlertsIconListener::SendCallback() { + if (mAlertListener) + mAlertListener->Observe(nullptr, "alertclickcallback", mAlertCookie.get()); +} + +void nsAlertsIconListener::SendClosed() { + if (mNotification) { + g_object_unref(mNotification); + mNotification = nullptr; + } + NotifyFinished(); +} + +NS_IMETHODIMP +nsAlertsIconListener::Observe(nsISupports* aSubject, const char* aTopic, + const char16_t* aData) { + // We need to close any open notifications upon application exit, otherwise + // we will leak since libnotify holds a ref for us. + if (!nsCRT::strcmp(aTopic, "quit-application") && mNotification) { + g_signal_handler_disconnect(mNotification, mClosureHandler); + g_object_unref(mNotification); + mNotification = nullptr; + Release(); // equivalent to NS_RELEASE(this) + } + return NS_OK; +} + +nsresult nsAlertsIconListener::Close() { + if (mIconRequest) { + mIconRequest->Cancel(NS_BINDING_ABORTED); + mIconRequest = nullptr; + } + + if (!mNotification) { + NotifyFinished(); + return NS_OK; + } + + GError* error = nullptr; + if (!notify_notification_close(mNotification, &error)) { + NS_WARNING(error->message); + g_error_free(error); + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +nsresult nsAlertsIconListener::InitAlertAsync(nsIAlertNotification* aAlert, + nsIObserver* aAlertListener) { + if (!libNotifyHandle) return NS_ERROR_FAILURE; + + if (!notify_is_initted()) { + // Give the name of this application to libnotify + nsCOMPtr<nsIStringBundleService> bundleService = + do_GetService(NS_STRINGBUNDLE_CONTRACTID); + + nsAutoCString appShortName; + if (bundleService) { + nsCOMPtr<nsIStringBundle> bundle; + bundleService->CreateBundle("chrome://branding/locale/brand.properties", + getter_AddRefs(bundle)); + nsAutoString appName; + + if (bundle) { + bundle->GetStringFromName("brandShortName", appName); + CopyUTF16toUTF8(appName, appShortName); + } else { + NS_WARNING( + "brand.properties not present, using default application name"); + appShortName.AssignLiteral("Mozilla"); + } + } else { + appShortName.AssignLiteral("Mozilla"); + } + + if (!notify_init(appShortName.get())) return NS_ERROR_FAILURE; + + GList* server_caps = notify_get_server_caps(); + if (server_caps) { + gHasCaps = true; + for (GList* cap = server_caps; cap != nullptr; cap = cap->next) { + if (!strcmp((char*)cap->data, "actions")) { + gHasActions = true; + break; + } + } + g_list_foreach(server_caps, (GFunc)g_free, nullptr); + g_list_free(server_caps); + } + } + + if (!gHasCaps) { + // if notify_get_server_caps() failed above we need to assume + // there is no notification-server to display anything + return NS_ERROR_FAILURE; + } + + nsresult rv = aAlert->GetTextClickable(&mAlertHasAction); + NS_ENSURE_SUCCESS(rv, rv); + if (!gHasActions && mAlertHasAction) + return NS_ERROR_FAILURE; // No good, fallback to XUL + + nsAutoString title; + rv = aAlert->GetTitle(title); + NS_ENSURE_SUCCESS(rv, rv); + // Workaround for a libnotify bug - blank titles aren't dealt with + // properly so we use a space + if (title.IsEmpty()) { + mAlertTitle = " "_ns; + } else { + CopyUTF16toUTF8(title, mAlertTitle); + } + + nsAutoString text; + rv = aAlert->GetText(text); + NS_ENSURE_SUCCESS(rv, rv); + CopyUTF16toUTF8(text, mAlertText); + + mAlertListener = aAlertListener; + + rv = aAlert->GetCookie(mAlertCookie); + NS_ENSURE_SUCCESS(rv, rv); + + return aAlert->LoadImage(/* aTimeout = */ 0, this, /* aUserData = */ nullptr, + getter_AddRefs(mIconRequest)); +} + +void nsAlertsIconListener::NotifyFinished() { + if (mAlertListener) + mAlertListener->Observe(nullptr, "alertfinished", mAlertCookie.get()); +} diff --git a/toolkit/system/gnome/nsAlertsIconListener.h b/toolkit/system/gnome/nsAlertsIconListener.h new file mode 100644 index 0000000000..fc98e0ff1f --- /dev/null +++ b/toolkit/system/gnome/nsAlertsIconListener.h @@ -0,0 +1,100 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsAlertsIconListener_h__ +#define nsAlertsIconListener_h__ + +#include "nsCOMPtr.h" +#include "nsIAlertsService.h" +#include "nsString.h" +#include "nsIObserver.h" +#include "nsWeakReference.h" + +#include <gdk-pixbuf/gdk-pixbuf.h> + +class nsIAlertNotification; +class nsICancelable; +class nsSystemAlertsService; + +struct NotifyNotification; + +class nsAlertsIconListener : public nsIAlertNotificationImageListener, + public nsIObserver, + public nsSupportsWeakReference { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIALERTNOTIFICATIONIMAGELISTENER + NS_DECL_NSIOBSERVER + + nsAlertsIconListener(nsSystemAlertsService* aBackend, + const nsAString& aAlertName); + + nsresult InitAlertAsync(nsIAlertNotification* aAlert, + nsIObserver* aAlertListener); + nsresult Close(); + + void SendCallback(); + void SendClosed(); + + protected: + virtual ~nsAlertsIconListener(); + + /** + * The only difference between libnotify.so.4 and libnotify.so.1 for these + * symbols is that notify_notification_new takes three arguments in + * libnotify.so.4 and four in libnotify.so.1. Passing the fourth argument as + * NULL is binary compatible. + */ + typedef void (*NotifyActionCallback)(NotifyNotification*, char*, gpointer); + typedef bool (*notify_is_initted_t)(void); + typedef bool (*notify_init_t)(const char*); + typedef GList* (*notify_get_server_caps_t)(void); + typedef NotifyNotification* (*notify_notification_new_t)(const char*, + const char*, + const char*, + const char*); + typedef bool (*notify_notification_show_t)(void*, GError**); + typedef void (*notify_notification_set_icon_from_pixbuf_t)(void*, GdkPixbuf*); + typedef void (*notify_notification_add_action_t)(void*, const char*, + const char*, + NotifyActionCallback, + gpointer, GFreeFunc); + typedef bool (*notify_notification_close_t)(void*, GError**); + typedef void (*notify_notification_set_hint_t)(NotifyNotification*, + const char*, GVariant*); + + nsCOMPtr<nsICancelable> mIconRequest; + nsCString mAlertTitle; + nsCString mAlertText; + + nsCOMPtr<nsIObserver> mAlertListener; + nsString mAlertCookie; + nsString mAlertName; + + RefPtr<nsSystemAlertsService> mBackend; + + bool mAlertHasAction; + + static void* libNotifyHandle; + static bool libNotifyNotAvail; + static notify_is_initted_t notify_is_initted; + static notify_init_t notify_init; + static notify_get_server_caps_t notify_get_server_caps; + static notify_notification_new_t notify_notification_new; + static notify_notification_show_t notify_notification_show; + static notify_notification_set_icon_from_pixbuf_t + notify_notification_set_icon_from_pixbuf; + static notify_notification_add_action_t notify_notification_add_action; + static notify_notification_close_t notify_notification_close; + static notify_notification_set_hint_t notify_notification_set_hint; + NotifyNotification* mNotification; + gulong mClosureHandler; + + nsresult ShowAlert(GdkPixbuf* aPixbuf); + + void NotifyFinished(); +}; + +#endif diff --git a/toolkit/system/gnome/nsGIOService.cpp b/toolkit/system/gnome/nsGIOService.cpp new file mode 100644 index 0000000000..867e6c0ee0 --- /dev/null +++ b/toolkit/system/gnome/nsGIOService.cpp @@ -0,0 +1,734 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsGIOService.h" +#include "nsString.h" +#include "nsIURI.h" +#include "nsTArray.h" +#include "nsStringEnumerator.h" +#include "nsIMIMEInfo.h" +#include "nsComponentManagerUtils.h" +#include "nsArray.h" +#include "nsPrintfCString.h" +#include "mozilla/Preferences.h" + +#include <gio/gio.h> +#include <gtk/gtk.h> +#ifdef MOZ_ENABLE_DBUS +# include <dbus/dbus-glib.h> +# include <dbus/dbus-glib-lowlevel.h> +#endif + +using namespace mozilla; + +// s. a. the code gtk_should_use_portal() uses to detect if in flatpak env +// https://gitlab.gnome.org/GNOME/gtk/-/blob/4300a5c609306ce77cbc8a3580c19201dccd8d13/gdk/gdk.c#L472 +static bool GetFlatpakPortalEnv() { + bool shouldUsePortal; + if (g_file_test("/.flatpak-info", G_FILE_TEST_EXISTS)) { + shouldUsePortal = true; + } else { + const char* portalEnvString = g_getenv("GTK_USE_PORTAL"); + shouldUsePortal = portalEnvString != nullptr && atoi(portalEnvString) != 0; + } + return shouldUsePortal; +} + +static bool GetShouldUseFlatpakPortal() { + static bool sFlatpakPortalEnv = GetFlatpakPortalEnv(); + return Preferences::HasUserValue("widget.use-xdg-desktop-portal") + ? Preferences::GetBool("widget.use-xdg-desktop-portal", false) + : sFlatpakPortalEnv; +} + +class nsFlatpakHandlerApp : public nsIHandlerApp { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIHANDLERAPP + nsFlatpakHandlerApp() = default; + + private: + virtual ~nsFlatpakHandlerApp() = default; +}; + +NS_IMPL_ISUPPORTS(nsFlatpakHandlerApp, nsIHandlerApp) + +NS_IMETHODIMP +nsFlatpakHandlerApp::GetName(nsAString& aName) { + aName.AssignLiteral("System Handler"); + return NS_OK; +} + +NS_IMETHODIMP +nsFlatpakHandlerApp::SetName(const nsAString& aName) { + // We don't implement SetName because flatpak system handler name is fixed + return NS_OK; +} + +NS_IMETHODIMP +nsFlatpakHandlerApp::GetDetailedDescription(nsAString& aDetailedDescription) { + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +nsFlatpakHandlerApp::SetDetailedDescription( + const nsAString& aDetailedDescription) { + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +nsFlatpakHandlerApp::Equals(nsIHandlerApp* aHandlerApp, bool* _retval) { + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +nsFlatpakHandlerApp::LaunchWithURI( + nsIURI* aUri, mozilla::dom::BrowsingContext* aBrowsingContext) { + nsCString spec; + aUri->GetSpec(spec); + GError* error = nullptr; + + // The TMPDIR where files are downloaded when user choose to open them + // needs to be accessible from sandbox and host. The default settings + // TMPDIR=/tmp is accessible only to the sandbox. That can be the reason + // why the gtk_show_uri fails there. + // The workaround is to set TMPDIR environment variable in sandbox to + // $XDG_CACHE_HOME/tmp before executing Firefox. + gtk_show_uri(nullptr, spec.get(), GDK_CURRENT_TIME, &error); + if (error) { + NS_WARNING( + nsPrintfCString("Cannot launch flatpak handler: %s", error->message) + .get()); + g_error_free(error); + return NS_ERROR_FAILURE; + } + return NS_OK; +} + +/** + * Get command without any additional arguments + * @param aCommandWithArguments full commandline input string + * @param aCommand string for storing command without arguments + * @return NS_ERROR_FAILURE when unable to parse commandline + */ +static nsresult GetCommandFromCommandline( + nsACString const& aCommandWithArguments, nsACString& aCommand) { + GError* error = nullptr; + gchar** argv = nullptr; + if (!g_shell_parse_argv(aCommandWithArguments.BeginReading(), nullptr, &argv, + &error) || + !argv[0]) { + g_warning("Cannot parse command with arguments: %s", error->message); + g_error_free(error); + g_strfreev(argv); + return NS_ERROR_FAILURE; + } + aCommand.Assign(argv[0]); + g_strfreev(argv); + return NS_OK; +} + +class nsGIOMimeApp final : public nsIGIOMimeApp { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIHANDLERAPP + NS_DECL_NSIGIOMIMEAPP + + explicit nsGIOMimeApp(GAppInfo* aApp) : mApp(aApp) {} + + private: + ~nsGIOMimeApp() { g_object_unref(mApp); } + + GAppInfo* mApp; +}; + +NS_IMPL_ISUPPORTS(nsGIOMimeApp, nsIGIOMimeApp, nsIHandlerApp) + +NS_IMETHODIMP +nsGIOMimeApp::GetId(nsACString& aId) { + aId.Assign(g_app_info_get_id(mApp)); + return NS_OK; +} + +NS_IMETHODIMP +nsGIOMimeApp::GetName(nsAString& aName) { + aName.Assign(NS_ConvertUTF8toUTF16(g_app_info_get_name(mApp))); + return NS_OK; +} + +NS_IMETHODIMP +nsGIOMimeApp::SetName(const nsAString& aName) { + // We don't implement SetName because we're using mGIOMimeApp instance for + // obtaining application name + return NS_OK; +} + +NS_IMETHODIMP +nsGIOMimeApp::GetCommand(nsACString& aCommand) { + const char* cmd = g_app_info_get_commandline(mApp); + if (!cmd) return NS_ERROR_FAILURE; + aCommand.Assign(cmd); + return NS_OK; +} + +NS_IMETHODIMP +nsGIOMimeApp::GetExpectsURIs(int32_t* aExpects) { + *aExpects = g_app_info_supports_uris(mApp); + return NS_OK; +} + +NS_IMETHODIMP +nsGIOMimeApp::GetDetailedDescription(nsAString& aDetailedDescription) { + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +nsGIOMimeApp::SetDetailedDescription(const nsAString& aDetailedDescription) { + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +nsGIOMimeApp::Equals(nsIHandlerApp* aHandlerApp, bool* _retval) { + if (!aHandlerApp) return NS_ERROR_FAILURE; + + // Compare with nsILocalHandlerApp instance by name + nsCOMPtr<nsILocalHandlerApp> localHandlerApp = do_QueryInterface(aHandlerApp); + if (localHandlerApp) { + nsAutoString theirName; + nsAutoString thisName; + GetName(thisName); + localHandlerApp->GetName(theirName); + *_retval = thisName.Equals(theirName); + return NS_OK; + } + + // Compare with nsIGIOMimeApp instance by command with stripped arguments + nsCOMPtr<nsIGIOMimeApp> gioMimeApp = do_QueryInterface(aHandlerApp); + if (gioMimeApp) { + nsAutoCString thisCommandline, thisCommand; + nsresult rv = GetCommand(thisCommandline); + NS_ENSURE_SUCCESS(rv, rv); + + rv = GetCommandFromCommandline(thisCommandline, thisCommand); + NS_ENSURE_SUCCESS(rv, rv); + + nsAutoCString theirCommandline, theirCommand; + gioMimeApp->GetCommand(theirCommandline); + NS_ENSURE_SUCCESS(rv, rv); + + rv = GetCommandFromCommandline(theirCommandline, theirCommand); + NS_ENSURE_SUCCESS(rv, rv); + + *_retval = thisCommand.Equals(theirCommand); + return NS_OK; + } + + // We can only compare with nsILocalHandlerApp and nsGIOMimeApp + *_retval = false; + return NS_OK; +} + +NS_IMETHODIMP +nsGIOMimeApp::LaunchWithURI(nsIURI* aUri, + mozilla::dom::BrowsingContext* aBrowsingContext) { + GList uris = {0}; + nsCString spec; + aUri->GetSpec(spec); + // nsPromiseFlatCString flatUri(aUri); + uris.data = const_cast<char*>(spec.get()); + + GError* error = nullptr; + gboolean result = g_app_info_launch_uris(mApp, &uris, nullptr, &error); + + if (!result) { + g_warning("Cannot launch application: %s", error->message); + g_error_free(error); + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +class GIOUTF8StringEnumerator final : public nsStringEnumeratorBase { + ~GIOUTF8StringEnumerator() = default; + + public: + GIOUTF8StringEnumerator() : mIndex(0) {} + + NS_DECL_ISUPPORTS + NS_DECL_NSIUTF8STRINGENUMERATOR + + using nsStringEnumeratorBase::GetNext; + + nsTArray<nsCString> mStrings; + uint32_t mIndex; +}; + +NS_IMPL_ISUPPORTS(GIOUTF8StringEnumerator, nsIUTF8StringEnumerator, + nsIStringEnumerator) + +NS_IMETHODIMP +GIOUTF8StringEnumerator::HasMore(bool* aResult) { + *aResult = mIndex < mStrings.Length(); + return NS_OK; +} + +NS_IMETHODIMP +GIOUTF8StringEnumerator::GetNext(nsACString& aResult) { + if (mIndex >= mStrings.Length()) return NS_ERROR_UNEXPECTED; + + aResult.Assign(mStrings[mIndex]); + ++mIndex; + return NS_OK; +} + +NS_IMETHODIMP +nsGIOMimeApp::GetSupportedURISchemes(nsIUTF8StringEnumerator** aSchemes) { + *aSchemes = nullptr; + + RefPtr<GIOUTF8StringEnumerator> array = new GIOUTF8StringEnumerator(); + NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY); + + GVfs* gvfs = g_vfs_get_default(); + + if (!gvfs) { + g_warning("Cannot get GVfs object."); + return NS_ERROR_OUT_OF_MEMORY; + } + + const gchar* const* uri_schemes = g_vfs_get_supported_uri_schemes(gvfs); + + while (*uri_schemes != nullptr) { + // XXX(Bug 1631371) Check if this should use a fallible operation as it + // pretended earlier. + array->mStrings.AppendElement(*uri_schemes); + uri_schemes++; + } + + array.forget(aSchemes); + return NS_OK; +} + +NS_IMETHODIMP +nsGIOMimeApp::SetAsDefaultForMimeType(nsACString const& aMimeType) { + char* content_type = + g_content_type_from_mime_type(PromiseFlatCString(aMimeType).get()); + if (!content_type) return NS_ERROR_FAILURE; + GError* error = nullptr; + g_app_info_set_as_default_for_type(mApp, content_type, &error); + if (error) { + g_warning("Cannot set application as default for MIME type (%s): %s", + PromiseFlatCString(aMimeType).get(), error->message); + g_error_free(error); + g_free(content_type); + return NS_ERROR_FAILURE; + } + + g_free(content_type); + return NS_OK; +} +/** + * Set default application for files with given extensions + * @param fileExts string of space separated extensions + * @return NS_OK when application was set as default for given extensions, + * NS_ERROR_FAILURE otherwise + */ +NS_IMETHODIMP +nsGIOMimeApp::SetAsDefaultForFileExtensions(nsACString const& fileExts) { + GError* error = nullptr; + char* extensions = g_strdup(PromiseFlatCString(fileExts).get()); + char* ext_pos = extensions; + char* space_pos; + + while ((space_pos = strchr(ext_pos, ' ')) || (*ext_pos != '\0')) { + if (space_pos) { + *space_pos = '\0'; + } + g_app_info_set_as_default_for_extension(mApp, ext_pos, &error); + if (error) { + g_warning("Cannot set application as default for extension (%s): %s", + ext_pos, error->message); + g_error_free(error); + g_free(extensions); + return NS_ERROR_FAILURE; + } + if (space_pos) { + ext_pos = space_pos + 1; + } else { + *ext_pos = '\0'; + } + } + g_free(extensions); + return NS_OK; +} + +/** + * Set default application for URI's of a particular scheme + * @param aURIScheme string containing the URI scheme + * @return NS_OK when application was set as default for URI scheme, + * NS_ERROR_FAILURE otherwise + */ +NS_IMETHODIMP +nsGIOMimeApp::SetAsDefaultForURIScheme(nsACString const& aURIScheme) { + GError* error = nullptr; + nsAutoCString contentType("x-scheme-handler/"); + contentType.Append(aURIScheme); + + g_app_info_set_as_default_for_type(mApp, contentType.get(), &error); + if (error) { + g_warning("Cannot set application as default for URI scheme (%s): %s", + PromiseFlatCString(aURIScheme).get(), error->message); + g_error_free(error); + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +NS_IMPL_ISUPPORTS(nsGIOService, nsIGIOService) + +NS_IMETHODIMP +nsGIOService::GetMimeTypeFromExtension(const nsACString& aExtension, + nsACString& aMimeType) { + nsAutoCString fileExtToUse("file."); + fileExtToUse.Append(aExtension); + + gboolean result_uncertain; + char* content_type = + g_content_type_guess(fileExtToUse.get(), nullptr, 0, &result_uncertain); + if (!content_type) return NS_ERROR_FAILURE; + + char* mime_type = g_content_type_get_mime_type(content_type); + if (!mime_type) { + g_free(content_type); + return NS_ERROR_FAILURE; + } + + aMimeType.Assign(mime_type); + + g_free(mime_type); + g_free(content_type); + + return NS_OK; +} +// used in nsGNOMERegistry +// ----------------------------------------------------------------------------- +NS_IMETHODIMP +nsGIOService::GetAppForURIScheme(const nsACString& aURIScheme, + nsIHandlerApp** aApp) { + *aApp = nullptr; + + // Application in flatpak sandbox does not have access to the list + // of installed applications on the system. We use generic + // nsFlatpakHandlerApp which forwards launch call to the system. + if (GetShouldUseFlatpakPortal()) { + nsFlatpakHandlerApp* mozApp = new nsFlatpakHandlerApp(); + NS_ADDREF(*aApp = mozApp); + return NS_OK; + } + + GAppInfo* app_info = g_app_info_get_default_for_uri_scheme( + PromiseFlatCString(aURIScheme).get()); + if (app_info) { + nsGIOMimeApp* mozApp = new nsGIOMimeApp(app_info); + NS_ADDREF(*aApp = mozApp); + } else { + return NS_ERROR_FAILURE; + } + return NS_OK; +} + +NS_IMETHODIMP +nsGIOService::GetAppsForURIScheme(const nsACString& aURIScheme, + nsIMutableArray** aResult) { + // We don't need to return the nsFlatpakHandlerApp here because + // it would be skipped by the callers anyway. + // The preferred handler is provided by GetAppForURIScheme. + // This method returns all possible application handlers + // including preferred one. The callers skips the preferred + // handler in this list to avoid duplicate records in the list + // they create. + nsCOMPtr<nsIMutableArray> handlersArray = + do_CreateInstance(NS_ARRAY_CONTRACTID); + + nsAutoCString contentType("x-scheme-handler/"); + contentType.Append(aURIScheme); + + GList* appInfoList = g_app_info_get_all_for_type(contentType.get()); + // g_app_info_get_all_for_type returns NULL when no appinfo is found + // or error occurs (contentType is NULL). We are fine with empty app list + // and we're sure that contentType is not NULL, so we won't return failure. + if (appInfoList) { + GList* appInfo = appInfoList; + while (appInfo) { + nsCOMPtr<nsIGIOMimeApp> mimeApp = + new nsGIOMimeApp(G_APP_INFO(appInfo->data)); + handlersArray->AppendElement(mimeApp); + appInfo = appInfo->next; + } + g_list_free(appInfoList); + } + NS_ADDREF(*aResult = handlersArray); + return NS_OK; +} + +NS_IMETHODIMP +nsGIOService::GetAppForMimeType(const nsACString& aMimeType, + nsIHandlerApp** aApp) { + *aApp = nullptr; + + // Flatpak does not reveal installed application to the sandbox, + // we need to create generic system handler. + if (GetShouldUseFlatpakPortal()) { + nsFlatpakHandlerApp* mozApp = new nsFlatpakHandlerApp(); + NS_ADDREF(*aApp = mozApp); + return NS_OK; + } + + char* content_type = + g_content_type_from_mime_type(PromiseFlatCString(aMimeType).get()); + if (!content_type) return NS_ERROR_FAILURE; + + // GIO returns "unknown" appinfo for the application/octet-stream, which is + // useless. It's better to fallback to create appinfo from file extension + // later. + if (g_content_type_is_unknown(content_type)) { + return NS_ERROR_NOT_AVAILABLE; + } + +#if defined(__OpenBSD__) && defined(MOZ_SANDBOX) + // g_app_info_get_default_for_type will fail on OpenBSD's veiled filesystem + // since we most likely don't have direct access to the binaries that are + // registered as defaults for this type. Fake it up by just executing + // xdg-open via gio-launch-desktop (which we do have access to) and letting + // it figure out which program to execute for this MIME type + GAppInfo* app_info = g_app_info_create_from_commandline( + "/usr/local/bin/xdg-open", + nsPrintfCString("System default for %s", content_type).get(), + G_APP_INFO_CREATE_NONE, NULL); +#else + GAppInfo* app_info = g_app_info_get_default_for_type(content_type, false); +#endif + if (app_info) { + nsGIOMimeApp* mozApp = new nsGIOMimeApp(app_info); + NS_ENSURE_TRUE(mozApp, NS_ERROR_OUT_OF_MEMORY); + NS_ADDREF(*aApp = mozApp); + } else { + g_free(content_type); + return NS_ERROR_FAILURE; + } + g_free(content_type); + return NS_OK; +} + +NS_IMETHODIMP +nsGIOService::GetDescriptionForMimeType(const nsACString& aMimeType, + nsACString& aDescription) { + char* content_type = + g_content_type_from_mime_type(PromiseFlatCString(aMimeType).get()); + if (!content_type) return NS_ERROR_FAILURE; + + char* desc = g_content_type_get_description(content_type); + if (!desc) { + g_free(content_type); + return NS_ERROR_FAILURE; + } + + aDescription.Assign(desc); + g_free(content_type); + g_free(desc); + return NS_OK; +} + +NS_IMETHODIMP +nsGIOService::ShowURI(nsIURI* aURI) { + nsAutoCString spec; + nsresult rv = aURI->GetSpec(spec); + NS_ENSURE_SUCCESS(rv, rv); + GError* error = nullptr; + if (!g_app_info_launch_default_for_uri(spec.get(), nullptr, &error)) { + g_warning("Could not launch default application for URI: %s", + error->message); + g_error_free(error); + return NS_ERROR_FAILURE; + } + return NS_OK; +} + +NS_IMETHODIMP +nsGIOService::ShowURIForInput(const nsACString& aUri) { + GFile* file = g_file_new_for_commandline_arg(PromiseFlatCString(aUri).get()); + char* spec = g_file_get_uri(file); + nsresult rv = NS_ERROR_FAILURE; + GError* error = nullptr; + + g_app_info_launch_default_for_uri(spec, nullptr, &error); + if (error) { + g_warning("Cannot launch default application: %s", error->message); + g_error_free(error); + } else { + rv = NS_OK; + } + g_object_unref(file); + g_free(spec); + + return rv; +} + +NS_IMETHODIMP +nsGIOService::OrgFreedesktopFileManager1ShowItems(const nsACString& aPath) { +#ifndef MOZ_ENABLE_DBUS + return NS_ERROR_FAILURE; +#else + GError* error = nullptr; + static bool org_freedesktop_FileManager1_exists = true; + + if (!org_freedesktop_FileManager1_exists) { + return NS_ERROR_NOT_AVAILABLE; + } + + DBusGConnection* dbusGConnection = dbus_g_bus_get(DBUS_BUS_SESSION, &error); + + if (!dbusGConnection) { + if (error) { + g_printerr("Failed to open connection to session bus: %s\n", + error->message); + g_error_free(error); + } + return NS_ERROR_FAILURE; + } + + char* uri = + g_filename_to_uri(PromiseFlatCString(aPath).get(), nullptr, nullptr); + if (uri == nullptr) { + return NS_ERROR_FAILURE; + } + + DBusConnection* dbusConnection = + dbus_g_connection_get_connection(dbusGConnection); + // Make sure we do not exit the entire program if DBus connection get lost. + dbus_connection_set_exit_on_disconnect(dbusConnection, false); + + DBusGProxy* dbusGProxy = dbus_g_proxy_new_for_name( + dbusGConnection, "org.freedesktop.FileManager1", + "/org/freedesktop/FileManager1", "org.freedesktop.FileManager1"); + + const char* uris[2] = {uri, nullptr}; + gboolean rv_dbus_call = + dbus_g_proxy_call(dbusGProxy, "ShowItems", nullptr, G_TYPE_STRV, uris, + G_TYPE_STRING, "", G_TYPE_INVALID, G_TYPE_INVALID); + + g_object_unref(dbusGProxy); + dbus_g_connection_unref(dbusGConnection); + g_free(uri); + + if (!rv_dbus_call) { + org_freedesktop_FileManager1_exists = false; + return NS_ERROR_NOT_AVAILABLE; + } + + return NS_OK; +#endif +} + +/** + * Find GIO Mime App from given commandline. + * This is different from CreateAppFromCommand because instead of creating the + * GIO Mime App in case it's not found in the GIO application list, the method + * returns error. + * @param aCmd command with parameters used to start the application + * @return NS_OK when application is found, NS_ERROR_NOT_AVAILABLE otherwise + */ +NS_IMETHODIMP +nsGIOService::FindAppFromCommand(nsACString const& aCmd, + nsIGIOMimeApp** aAppInfo) { + GAppInfo *app_info = nullptr, *app_info_from_list = nullptr; + GList* apps = g_app_info_get_all(); + GList* apps_p = apps; + + // Try to find relevant and existing GAppInfo in all installed application + // We do this by comparing each GAppInfo's executable with out own + while (apps_p) { + app_info_from_list = (GAppInfo*)apps_p->data; + if (!app_info) { + // If the executable is not absolute, get it's full path + char* executable = + g_find_program_in_path(g_app_info_get_executable(app_info_from_list)); + + if (executable && + strcmp(executable, PromiseFlatCString(aCmd).get()) == 0) { + g_object_ref(app_info_from_list); + app_info = app_info_from_list; + } + g_free(executable); + } + + g_object_unref(app_info_from_list); + apps_p = apps_p->next; + } + g_list_free(apps); + if (app_info) { + nsGIOMimeApp* app = new nsGIOMimeApp(app_info); + NS_ENSURE_TRUE(app, NS_ERROR_OUT_OF_MEMORY); + NS_ADDREF(*aAppInfo = app); + return NS_OK; + } + + *aAppInfo = nullptr; + return NS_ERROR_NOT_AVAILABLE; +} + +/** + * Create application info for specified command and application name. + * Command arguments are ignored and the "%u" is always added. + * @param cmd command to execute + * @param appName application name + * @param appInfo location where created GAppInfo is stored + * @return NS_OK when object is created, NS_ERROR_FILE_NOT_FOUND when executable + * is not found in the system path or NS_ERROR_FAILURE otherwise. + */ +NS_IMETHODIMP +nsGIOService::CreateAppFromCommand(nsACString const& cmd, + nsACString const& appName, + nsIGIOMimeApp** appInfo) { + GError* error = nullptr; + *appInfo = nullptr; + + // Using G_APP_INFO_CREATE_SUPPORTS_URIS calling + // g_app_info_create_from_commandline appends %u to the cmd even when cmd + // already contains this parameter. To avoid that we're going to remove + // arguments before passing to it. + nsAutoCString commandWithoutArgs; + nsresult rv = GetCommandFromCommandline(cmd, commandWithoutArgs); + NS_ENSURE_SUCCESS(rv, rv); + GAppInfo* app_info = g_app_info_create_from_commandline( + commandWithoutArgs.BeginReading(), PromiseFlatCString(appName).get(), + G_APP_INFO_CREATE_SUPPORTS_URIS, &error); + if (!app_info) { + g_warning("Cannot create application info from command: %s", + error->message); + g_error_free(error); + return NS_ERROR_FAILURE; + } + + // Check if executable exist in path + gchar* executableWithFullPath = + g_find_program_in_path(commandWithoutArgs.BeginReading()); + if (!executableWithFullPath) { + return NS_ERROR_FILE_NOT_FOUND; + } + g_free(executableWithFullPath); + + nsGIOMimeApp* mozApp = new nsGIOMimeApp(app_info); + NS_ENSURE_TRUE(mozApp, NS_ERROR_OUT_OF_MEMORY); + NS_ADDREF(*appInfo = mozApp); + return NS_OK; +} + +NS_IMETHODIMP +nsGIOService::ShouldUseFlatpakPortal(bool* aRes) { + *aRes = GetShouldUseFlatpakPortal(); + return NS_OK; +} diff --git a/toolkit/system/gnome/nsGIOService.h b/toolkit/system/gnome/nsGIOService.h new file mode 100644 index 0000000000..e83c724de2 --- /dev/null +++ b/toolkit/system/gnome/nsGIOService.h @@ -0,0 +1,26 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsGIOService_h_ +#define nsGIOService_h_ + +#include "nsIGIOService.h" + +#define NS_GIOSERVICE_CID \ + { \ + 0xe3a1f3c9, 0x3ae1, 0x4b40, { \ + 0xa5, 0xe0, 0x7b, 0x45, 0x7f, 0xc9, 0xa9, 0xad \ + } \ + } + +class nsGIOService final : public nsIGIOService { + ~nsGIOService() = default; + + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIGIOSERVICE +}; + +#endif diff --git a/toolkit/system/gnome/nsGSettingsService.cpp b/toolkit/system/gnome/nsGSettingsService.cpp new file mode 100644 index 0000000000..36962c6554 --- /dev/null +++ b/toolkit/system/gnome/nsGSettingsService.cpp @@ -0,0 +1,319 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/ArrayUtils.h" + +#include "nsGSettingsService.h" +#include "nsString.h" +#include "nsCOMPtr.h" +#include "nsMemory.h" +#include "prlink.h" +#include "nsComponentManagerUtils.h" +#include "nsIMutableArray.h" +#include "nsISupportsPrimitives.h" + +#include <glib.h> +#include <glib-object.h> + +using namespace mozilla; + +typedef struct _GSettings GSettings; +typedef struct _GVariantType GVariantType; +typedef struct _GVariant GVariant; + +#ifndef G_VARIANT_TYPE_INT32 +# define G_VARIANT_TYPE_INT32 ((const GVariantType*)"i") +# define G_VARIANT_TYPE_BOOLEAN ((const GVariantType*)"b") +# define G_VARIANT_TYPE_STRING ((const GVariantType*)"s") +# define G_VARIANT_TYPE_OBJECT_PATH ((const GVariantType*)"o") +# define G_VARIANT_TYPE_SIGNATURE ((const GVariantType*)"g") +#endif +#ifndef G_VARIANT_TYPE_STRING_ARRAY +# define G_VARIANT_TYPE_STRING_ARRAY ((const GVariantType*)"as") +#endif + +#define GSETTINGS_FUNCTIONS \ + FUNC(g_settings_new, GSettings*, (const char* schema)) \ + FUNC(g_settings_list_schemas, const char* const*, (void)) \ + FUNC(g_settings_list_keys, char**, (GSettings * settings)) \ + FUNC(g_settings_get_value, GVariant*, \ + (GSettings * settings, const char* key)) \ + FUNC(g_settings_set_value, gboolean, \ + (GSettings * settings, const char* key, GVariant* value)) \ + FUNC(g_settings_range_check, gboolean, \ + (GSettings * settings, const char* key, GVariant* value)) \ + FUNC(g_variant_get_int32, gint32, (GVariant * variant)) \ + FUNC(g_variant_get_boolean, gboolean, (GVariant * variant)) \ + FUNC(g_variant_get_string, const char*, (GVariant * value, gsize * length)) \ + FUNC(g_variant_get_strv, const char**, (GVariant * value, gsize * length)) \ + FUNC(g_variant_is_of_type, gboolean, \ + (GVariant * value, const GVariantType* type)) \ + FUNC(g_variant_new_int32, GVariant*, (gint32 value)) \ + FUNC(g_variant_new_boolean, GVariant*, (gboolean value)) \ + FUNC(g_variant_new_string, GVariant*, (const char* string)) \ + FUNC(g_variant_unref, void, (GVariant * value)) + +#define FUNC(name, type, params) \ + typedef type(*_##name##_fn) params; \ + static _##name##_fn _##name; + +GSETTINGS_FUNCTIONS + +#undef FUNC + +#define g_settings_new _g_settings_new +#define g_settings_list_schemas _g_settings_list_schemas +#define g_settings_list_keys _g_settings_list_keys +#define g_settings_get_value _g_settings_get_value +#define g_settings_set_value _g_settings_set_value +#define g_settings_range_check _g_settings_range_check +#define g_variant_get_int32 _g_variant_get_int32 +#define g_variant_get_boolean _g_variant_get_boolean +#define g_variant_get_string _g_variant_get_string +#define g_variant_get_strv _g_variant_get_strv +#define g_variant_is_of_type _g_variant_is_of_type +#define g_variant_new_int32 _g_variant_new_int32 +#define g_variant_new_boolean _g_variant_new_boolean +#define g_variant_new_string _g_variant_new_string +#define g_variant_unref _g_variant_unref + +static PRLibrary* gioLib = nullptr; + +class nsGSettingsCollection final : public nsIGSettingsCollection { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIGSETTINGSCOLLECTION + + explicit nsGSettingsCollection(GSettings* aSettings) + : mSettings(aSettings), mKeys(nullptr) {} + + private: + ~nsGSettingsCollection(); + + bool KeyExists(const nsACString& aKey); + bool SetValue(const nsACString& aKey, GVariant* aValue); + + GSettings* mSettings; + char** mKeys; +}; + +nsGSettingsCollection::~nsGSettingsCollection() { + g_strfreev(mKeys); + g_object_unref(mSettings); +} + +bool nsGSettingsCollection::KeyExists(const nsACString& aKey) { + if (!mKeys) mKeys = g_settings_list_keys(mSettings); + + for (uint32_t i = 0; mKeys[i] != nullptr; i++) { + if (aKey.Equals(mKeys[i])) return true; + } + + return false; +} + +bool nsGSettingsCollection::SetValue(const nsACString& aKey, GVariant* aValue) { + if (!KeyExists(aKey) || + !g_settings_range_check(mSettings, PromiseFlatCString(aKey).get(), + aValue)) { + g_variant_unref(aValue); + return false; + } + + return g_settings_set_value(mSettings, PromiseFlatCString(aKey).get(), + aValue); +} + +NS_IMPL_ISUPPORTS(nsGSettingsCollection, nsIGSettingsCollection) + +NS_IMETHODIMP +nsGSettingsCollection::SetString(const nsACString& aKey, + const nsACString& aValue) { + GVariant* value = g_variant_new_string(PromiseFlatCString(aValue).get()); + if (!value) return NS_ERROR_OUT_OF_MEMORY; + + bool res = SetValue(aKey, value); + + return res ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsGSettingsCollection::SetBoolean(const nsACString& aKey, bool aValue) { + GVariant* value = g_variant_new_boolean(aValue); + if (!value) return NS_ERROR_OUT_OF_MEMORY; + + bool res = SetValue(aKey, value); + + return res ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsGSettingsCollection::SetInt(const nsACString& aKey, int32_t aValue) { + GVariant* value = g_variant_new_int32(aValue); + if (!value) return NS_ERROR_OUT_OF_MEMORY; + + bool res = SetValue(aKey, value); + + return res ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +nsGSettingsCollection::GetString(const nsACString& aKey, nsACString& aResult) { + if (!KeyExists(aKey)) return NS_ERROR_INVALID_ARG; + + GVariant* value = + g_settings_get_value(mSettings, PromiseFlatCString(aKey).get()); + if (!g_variant_is_of_type(value, G_VARIANT_TYPE_STRING) && + !g_variant_is_of_type(value, G_VARIANT_TYPE_OBJECT_PATH) && + !g_variant_is_of_type(value, G_VARIANT_TYPE_SIGNATURE)) { + g_variant_unref(value); + return NS_ERROR_FAILURE; + } + + aResult.Assign(g_variant_get_string(value, nullptr)); + g_variant_unref(value); + + return NS_OK; +} + +NS_IMETHODIMP +nsGSettingsCollection::GetBoolean(const nsACString& aKey, bool* aResult) { + NS_ENSURE_ARG_POINTER(aResult); + + if (!KeyExists(aKey)) return NS_ERROR_INVALID_ARG; + + GVariant* value = + g_settings_get_value(mSettings, PromiseFlatCString(aKey).get()); + if (!g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) { + g_variant_unref(value); + return NS_ERROR_FAILURE; + } + + gboolean res = g_variant_get_boolean(value); + *aResult = res ? true : false; + g_variant_unref(value); + + return NS_OK; +} + +NS_IMETHODIMP +nsGSettingsCollection::GetInt(const nsACString& aKey, int32_t* aResult) { + NS_ENSURE_ARG_POINTER(aResult); + + if (!KeyExists(aKey)) return NS_ERROR_INVALID_ARG; + + GVariant* value = + g_settings_get_value(mSettings, PromiseFlatCString(aKey).get()); + if (!g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) { + g_variant_unref(value); + return NS_ERROR_FAILURE; + } + + *aResult = g_variant_get_int32(value); + g_variant_unref(value); + + return NS_OK; +} + +// These types are local to nsGSettingsService::Init, but ISO C++98 doesn't +// allow a template (ArrayLength) to be instantiated based on a local type. +// Boo-urns! +typedef void (*nsGSettingsFunc)(); +struct nsGSettingsDynamicFunction { + const char* functionName; + nsGSettingsFunc* function; +}; + +NS_IMETHODIMP +nsGSettingsCollection::GetStringList(const nsACString& aKey, + nsIArray** aResult) { + if (!KeyExists(aKey)) return NS_ERROR_INVALID_ARG; + + nsCOMPtr<nsIMutableArray> items(do_CreateInstance(NS_ARRAY_CONTRACTID)); + if (!items) { + return NS_ERROR_OUT_OF_MEMORY; + } + + GVariant* value = + g_settings_get_value(mSettings, PromiseFlatCString(aKey).get()); + + if (!g_variant_is_of_type(value, G_VARIANT_TYPE_STRING_ARRAY)) { + g_variant_unref(value); + return NS_ERROR_FAILURE; + } + + const gchar** gs_strings = g_variant_get_strv(value, nullptr); + if (!gs_strings) { + // empty array + items.forget(aResult); + g_variant_unref(value); + return NS_OK; + } + + const gchar** p_gs_strings = gs_strings; + while (*p_gs_strings != nullptr) { + nsCOMPtr<nsISupportsCString> obj( + do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID)); + if (obj) { + obj->SetData(nsDependentCString(*p_gs_strings)); + items->AppendElement(obj); + } + p_gs_strings++; + } + g_free(gs_strings); + items.forget(aResult); + g_variant_unref(value); + return NS_OK; +} + +nsresult nsGSettingsService::Init() { +#define FUNC(name, type, params) {#name, (nsGSettingsFunc*)&_##name}, + static const nsGSettingsDynamicFunction kGSettingsSymbols[] = { + GSETTINGS_FUNCTIONS}; +#undef FUNC + + if (!gioLib) { + gioLib = PR_LoadLibrary("libgio-2.0.so.0"); + if (!gioLib) return NS_ERROR_FAILURE; + } + + for (auto GSettingsSymbol : kGSettingsSymbols) { + *GSettingsSymbol.function = + PR_FindFunctionSymbol(gioLib, GSettingsSymbol.functionName); + if (!*GSettingsSymbol.function) { + return NS_ERROR_FAILURE; + } + } + + return NS_OK; +} + +NS_IMPL_ISUPPORTS(nsGSettingsService, nsIGSettingsService) + +nsGSettingsService::~nsGSettingsService() { + if (gioLib) { + PR_UnloadLibrary(gioLib); + gioLib = nullptr; + } +} + +NS_IMETHODIMP +nsGSettingsService::GetCollectionForSchema( + const nsACString& schema, nsIGSettingsCollection** collection) { + NS_ENSURE_ARG_POINTER(collection); + + const char* const* schemas = g_settings_list_schemas(); + + for (uint32_t i = 0; schemas[i] != nullptr; i++) { + if (schema.Equals(schemas[i])) { + GSettings* settings = g_settings_new(PromiseFlatCString(schema).get()); + nsGSettingsCollection* mozGSettings = new nsGSettingsCollection(settings); + NS_ADDREF(*collection = mozGSettings); + return NS_OK; + } + } + + return NS_ERROR_FAILURE; +} diff --git a/toolkit/system/gnome/nsGSettingsService.h b/toolkit/system/gnome/nsGSettingsService.h new file mode 100644 index 0000000000..1eb592e4aa --- /dev/null +++ b/toolkit/system/gnome/nsGSettingsService.h @@ -0,0 +1,29 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsGSettingsService_h_ +#define nsGSettingsService_h_ + +#include "nsIGSettingsService.h" + +#define NS_GSETTINGSSERVICE_CID \ + { \ + 0xbfd4a9d8, 0xd886, 0x4161, { \ + 0x81, 0xef, 0x88, 0x68, 0xda, 0x11, 0x41, 0x70 \ + } \ + } + +class nsGSettingsService final : public nsIGSettingsService { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIGSETTINGSSERVICE + + nsresult Init(); + + private: + ~nsGSettingsService(); +}; + +#endif diff --git a/toolkit/system/gnome/nsSystemAlertsService.cpp b/toolkit/system/gnome/nsSystemAlertsService.cpp new file mode 100644 index 0000000000..3f442cc58f --- /dev/null +++ b/toolkit/system/gnome/nsSystemAlertsService.cpp @@ -0,0 +1,123 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsComponentManagerUtils.h" +#include "nsXULAppAPI.h" +#include "nsSystemAlertsService.h" +#include "nsAlertsIconListener.h" + +NS_IMPL_ADDREF(nsSystemAlertsService) +NS_IMPL_RELEASE(nsSystemAlertsService) + +NS_INTERFACE_MAP_BEGIN(nsSystemAlertsService) + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAlertsService) + NS_INTERFACE_MAP_ENTRY(nsIAlertsService) + NS_INTERFACE_MAP_ENTRY(nsIAlertsDoNotDisturb) +NS_INTERFACE_MAP_END + +nsSystemAlertsService::nsSystemAlertsService() = default; + +nsSystemAlertsService::~nsSystemAlertsService() = default; + +nsresult nsSystemAlertsService::Init() { return NS_OK; } + +NS_IMETHODIMP nsSystemAlertsService::ShowAlertNotification( + const nsAString& aImageUrl, const nsAString& aAlertTitle, + const nsAString& aAlertText, bool aAlertTextClickable, + const nsAString& aAlertCookie, nsIObserver* aAlertListener, + const nsAString& aAlertName, const nsAString& aBidi, const nsAString& aLang, + const nsAString& aData, nsIPrincipal* aPrincipal, bool aInPrivateBrowsing, + bool aRequireInteraction) { + nsCOMPtr<nsIAlertNotification> alert = + do_CreateInstance(ALERT_NOTIFICATION_CONTRACTID); + NS_ENSURE_TRUE(alert, NS_ERROR_FAILURE); + nsresult rv = + alert->Init(aAlertName, aImageUrl, aAlertTitle, aAlertText, + aAlertTextClickable, aAlertCookie, aBidi, aLang, aData, + aPrincipal, aInPrivateBrowsing, aRequireInteraction); + NS_ENSURE_SUCCESS(rv, rv); + return ShowAlert(alert, aAlertListener); +} + +NS_IMETHODIMP nsSystemAlertsService::ShowPersistentNotification( + const nsAString& aPersistentData, nsIAlertNotification* aAlert, + nsIObserver* aAlertListener) { + return ShowAlert(aAlert, aAlertListener); +} + +NS_IMETHODIMP nsSystemAlertsService::ShowAlert(nsIAlertNotification* aAlert, + nsIObserver* aAlertListener) { + NS_ENSURE_ARG(aAlert); + + nsAutoString alertName; + nsresult rv = aAlert->GetName(alertName); + NS_ENSURE_SUCCESS(rv, rv); + + RefPtr<nsAlertsIconListener> alertListener = + new nsAlertsIconListener(this, alertName); + if (!alertListener) return NS_ERROR_OUT_OF_MEMORY; + + if (mSuppressForScreenSharing) { + alertListener->SendClosed(); + return NS_OK; + } + + AddListener(alertName, alertListener); + return alertListener->InitAlertAsync(aAlert, aAlertListener); +} + +NS_IMETHODIMP nsSystemAlertsService::CloseAlert(const nsAString& aAlertName) { + RefPtr<nsAlertsIconListener> listener = mActiveListeners.Get(aAlertName); + if (!listener) { + return NS_OK; + } + mActiveListeners.Remove(aAlertName); + return listener->Close(); +} + +NS_IMETHODIMP nsSystemAlertsService::GetManualDoNotDisturb(bool* aRetVal) { + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP nsSystemAlertsService::SetManualDoNotDisturb(bool aDoNotDisturb) { + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP nsSystemAlertsService::GetSuppressForScreenSharing( + bool* aRetVal) { + NS_ENSURE_ARG(aRetVal); + *aRetVal = mSuppressForScreenSharing; + return NS_OK; +} + +NS_IMETHODIMP nsSystemAlertsService::SetSuppressForScreenSharing( + bool aSuppress) { + mSuppressForScreenSharing = aSuppress; + return NS_OK; +} + +bool nsSystemAlertsService::IsActiveListener(const nsAString& aAlertName, + nsAlertsIconListener* aListener) { + return mActiveListeners.Get(aAlertName) == aListener; +} + +void nsSystemAlertsService::AddListener(const nsAString& aAlertName, + nsAlertsIconListener* aListener) { + RefPtr<nsAlertsIconListener> oldListener = mActiveListeners.Get(aAlertName); + mActiveListeners.Put(aAlertName, aListener); + if (oldListener) { + // If an alert with this name already exists, close it. + oldListener->Close(); + } +} + +void nsSystemAlertsService::RemoveListener(const nsAString& aAlertName, + nsAlertsIconListener* aListener) { + if (IsActiveListener(aAlertName, aListener)) { + // The alert may have been replaced; only remove it from the active + // listeners map if it's the same. + mActiveListeners.Remove(aAlertName); + } +} diff --git a/toolkit/system/gnome/nsSystemAlertsService.h b/toolkit/system/gnome/nsSystemAlertsService.h new file mode 100644 index 0000000000..c737822a47 --- /dev/null +++ b/toolkit/system/gnome/nsSystemAlertsService.h @@ -0,0 +1,43 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsSystemAlertsService_h__ +#define nsSystemAlertsService_h__ + +#include "nsIAlertsService.h" +#include "nsDataHashtable.h" +#include "nsCOMPtr.h" + +class nsAlertsIconListener; + +class nsSystemAlertsService : public nsIAlertsService, + public nsIAlertsDoNotDisturb { + public: + NS_DECL_NSIALERTSSERVICE + NS_DECL_NSIALERTSDONOTDISTURB + NS_DECL_ISUPPORTS + + nsSystemAlertsService(); + + nsresult Init(); + + bool IsActiveListener(const nsAString& aAlertName, + nsAlertsIconListener* aListener); + void RemoveListener(const nsAString& aAlertName, + nsAlertsIconListener* aListener); + + protected: + virtual ~nsSystemAlertsService(); + + void AddListener(const nsAString& aAlertName, + nsAlertsIconListener* aListener); + + nsDataHashtable<nsStringHashKey, nsAlertsIconListener*> mActiveListeners; + + private: + bool mSuppressForScreenSharing = false; +}; + +#endif /* nsSystemAlertsService_h__ */ diff --git a/toolkit/system/osxproxy/ProxyUtils.h b/toolkit/system/osxproxy/ProxyUtils.h new file mode 100644 index 0000000000..8a449f10a8 --- /dev/null +++ b/toolkit/system/osxproxy/ProxyUtils.h @@ -0,0 +1,21 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_toolkit_system_osxproxy_ProxyUtils_h +#define mozilla_toolkit_system_osxproxy_ProxyUtils_h + +#include "nsString.h" + +namespace mozilla { +namespace toolkit { +namespace system { + +bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride); + +} // namespace system +} // namespace toolkit +} // namespace mozilla + +#endif // mozilla_toolkit_system_osxproxy_ProxyUtils_h diff --git a/toolkit/system/osxproxy/ProxyUtils.mm b/toolkit/system/osxproxy/ProxyUtils.mm new file mode 100644 index 0000000000..cade1c5fae --- /dev/null +++ b/toolkit/system/osxproxy/ProxyUtils.mm @@ -0,0 +1,150 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "ProxyUtils.h" +#include "nsTArray.h" +#include "prnetdb.h" +#include "prtypes.h" + +namespace mozilla { +namespace toolkit { +namespace system { + +/** + * Normalize the short IP form into the complete form. + * For example, it converts "192.168" into "192.168.0.0" + */ +static void NormalizeAddr(const nsACString& aAddr, nsCString& aNormalized) { + nsTArray<nsCString> addr; + ParseString(aAddr, '.', addr); + aNormalized = ""; + for (uint32_t i = 0; i < 4; ++i) { + if (i != 0) { + aNormalized.AppendLiteral("."); + } + if (i < addr.Length()) { + aNormalized.Append(addr[i]); + } else { + aNormalized.AppendLiteral("0"); + } + } +} + +static PRUint32 MaskIPv4Addr(PRUint32 aAddr, uint16_t aMaskLen) { + if (aMaskLen == 32) { + return aAddr; + } + return PR_htonl(PR_ntohl(aAddr) & (~0L << (32 - aMaskLen))); +} + +static void MaskIPv6Addr(PRIPv6Addr& aAddr, uint16_t aMaskLen) { + if (aMaskLen == 128) { + return; + } + + if (aMaskLen > 96) { + aAddr.pr_s6_addr32[3] = PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[3]) & (~0L << (128 - aMaskLen))); + } else if (aMaskLen > 64) { + aAddr.pr_s6_addr32[3] = 0; + aAddr.pr_s6_addr32[2] = PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[2]) & (~0L << (96 - aMaskLen))); + } else if (aMaskLen > 32) { + aAddr.pr_s6_addr32[3] = 0; + aAddr.pr_s6_addr32[2] = 0; + aAddr.pr_s6_addr32[1] = PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[1]) & (~0L << (64 - aMaskLen))); + } else { + aAddr.pr_s6_addr32[3] = 0; + aAddr.pr_s6_addr32[2] = 0; + aAddr.pr_s6_addr32[1] = 0; + aAddr.pr_s6_addr32[0] = PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[0]) & (~0L << (32 - aMaskLen))); + } + + return; +} + +static bool IsMatchMask(const nsACString& aHost, const nsACString& aOverride) { + nsresult rv; + + auto tokenEnd = aOverride.FindChar('/'); + if (tokenEnd == -1) { + return false; + } + + nsAutoCString prefixStr(Substring(aOverride, tokenEnd + 1, aOverride.Length() - tokenEnd - 1)); + auto maskLen = prefixStr.ToInteger(&rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return false; + } + + nsAutoCString override(aOverride); + NormalizeAddr(Substring(aOverride, 0, tokenEnd), override); + + PRNetAddr prAddrHost; + PRNetAddr prAddrOverride; + if (PR_SUCCESS != PR_StringToNetAddr(PromiseFlatCString(aHost).get(), &prAddrHost) || + PR_SUCCESS != PR_StringToNetAddr(override.get(), &prAddrOverride)) { + return false; + } + + if (prAddrHost.raw.family == PR_AF_INET && prAddrOverride.raw.family == PR_AF_INET) { + return MaskIPv4Addr(prAddrHost.inet.ip, maskLen) == + MaskIPv4Addr(prAddrOverride.inet.ip, maskLen); + } else if (prAddrHost.raw.family == PR_AF_INET6 && prAddrOverride.raw.family == PR_AF_INET6) { + MaskIPv6Addr(prAddrHost.ipv6.ip, maskLen); + MaskIPv6Addr(prAddrOverride.ipv6.ip, maskLen); + + return memcmp(&prAddrHost.ipv6.ip, &prAddrOverride.ipv6.ip, sizeof(PRIPv6Addr)) == 0; + } + + return false; +} + +static bool IsMatchWildcard(const nsACString& aHost, const nsACString& aOverride) { + nsAutoCString host(aHost); + nsAutoCString override(aOverride); + + int32_t overrideLength = override.Length(); + int32_t tokenStart = 0; + int32_t offset = 0; + bool star = false; + + while (tokenStart < overrideLength) { + int32_t tokenEnd = override.FindChar('*', tokenStart); + if (tokenEnd == tokenStart) { + // Star is the first character in the token. + star = true; + tokenStart++; + // If the character following the '*' is a '.' character then skip + // it so that "*.foo.com" allows "foo.com". + if (override.FindChar('.', tokenStart) == tokenStart) { + nsAutoCString token(Substring(override, tokenStart + 1, overrideLength - tokenStart - 1)); + if (host.Equals(token)) { + return true; + } + } + } else { + if (tokenEnd == -1) { + tokenEnd = overrideLength; // no '*' char, match rest of string + } + nsAutoCString token(Substring(override, tokenStart, tokenEnd - tokenStart)); + offset = host.Find(token, /* aIgnoreCase = */ false, offset); + if (offset == -1 || (!star && offset)) { + return false; + } + star = false; + tokenStart = tokenEnd; + offset += token.Length(); + } + } + + return (star || (offset == static_cast<int32_t>(host.Length()))); +} + +bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride) { + return IsMatchMask(aHost, aOverride) || IsMatchWildcard(aHost, aOverride); +} + +} // namespace system +} // namespace toolkit +} // namespace mozilla diff --git a/toolkit/system/osxproxy/components.conf b/toolkit/system/osxproxy/components.conf new file mode 100644 index 0000000000..b3e3c464b6 --- /dev/null +++ b/toolkit/system/osxproxy/components.conf @@ -0,0 +1,13 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +Classes = [ + { + 'cid': '{9afcd4b8-2e0f-41f4-8f1f-3bf0d3cf67de}', + 'contract_ids': ['@mozilla.org/system-proxy-settings;1'], + 'type': 'nsOSXSystemProxySettings', + }, +] diff --git a/toolkit/system/osxproxy/moz.build b/toolkit/system/osxproxy/moz.build new file mode 100644 index 0000000000..387e636d28 --- /dev/null +++ b/toolkit/system/osxproxy/moz.build @@ -0,0 +1,21 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Core", "Networking") + +TEST_DIRS += ["tests/gtest"] + +SOURCES += [ + "nsOSXSystemProxySettings.mm", + "ProxyUtils.mm", +] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +FINAL_LIBRARY = "xul" diff --git a/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm b/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm new file mode 100644 index 0000000000..e22eedd866 --- /dev/null +++ b/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm @@ -0,0 +1,280 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#import <Cocoa/Cocoa.h> +#import <SystemConfiguration/SystemConfiguration.h> + +#include "nsISystemProxySettings.h" +#include "mozilla/Components.h" +#include "nsPrintfCString.h" +#include "nsNetCID.h" +#include "nsObjCExceptions.h" +#include "mozilla/Attributes.h" +#include "ProxyUtils.h" + +class nsOSXSystemProxySettings final : public nsISystemProxySettings { + public: + NS_DECL_THREADSAFE_ISUPPORTS + NS_DECL_NSISYSTEMPROXYSETTINGS + + nsOSXSystemProxySettings(); + nsresult Init(); + + // called by OSX when the proxy settings have changed + void ProxyHasChanged(); + + // is there a PAC url specified in the system configuration + bool IsAutoconfigEnabled() const; + // retrieve the pac url + nsresult GetAutoconfigURL(nsAutoCString& aResult) const; + + // Find the SystemConfiguration proxy & port for a given URI + nsresult FindSCProxyPort(const nsACString& aScheme, nsACString& aResultHost, int32_t& aResultPort, + bool& aResultSocksProxy); + + // is host:port on the proxy exception list? + bool IsInExceptionList(const nsACString& aHost) const; + + private: + ~nsOSXSystemProxySettings(); + + SCDynamicStoreContext mContext; + SCDynamicStoreRef mSystemDynamicStore; + NSDictionary* mProxyDict; + + // Mapping of URI schemes to SystemConfiguration keys + struct SchemeMapping { + const char* mScheme; + CFStringRef mEnabled; + CFStringRef mHost; + CFStringRef mPort; + bool mIsSocksProxy; + }; + static const SchemeMapping gSchemeMappingList[]; +}; + +NS_IMPL_ISUPPORTS(nsOSXSystemProxySettings, nsISystemProxySettings) + +NS_IMETHODIMP +nsOSXSystemProxySettings::GetMainThreadOnly(bool* aMainThreadOnly) { + *aMainThreadOnly = true; + return NS_OK; +} + +// Mapping of URI schemes to SystemConfiguration keys +const nsOSXSystemProxySettings::SchemeMapping nsOSXSystemProxySettings::gSchemeMappingList[] = { + {"http", kSCPropNetProxiesHTTPEnable, kSCPropNetProxiesHTTPProxy, kSCPropNetProxiesHTTPPort, + false}, + {"https", kSCPropNetProxiesHTTPSEnable, kSCPropNetProxiesHTTPSProxy, kSCPropNetProxiesHTTPSPort, + false}, + {"ftp", kSCPropNetProxiesFTPEnable, kSCPropNetProxiesFTPProxy, kSCPropNetProxiesFTPPort, false}, + {"socks", kSCPropNetProxiesSOCKSEnable, kSCPropNetProxiesSOCKSProxy, kSCPropNetProxiesSOCKSPort, + true}, + {NULL, NULL, NULL, NULL, false}, +}; + +static void ProxyHasChangedWrapper(SCDynamicStoreRef aStore, CFArrayRef aChangedKeys, void* aInfo) { + static_cast<nsOSXSystemProxySettings*>(aInfo)->ProxyHasChanged(); +} + +nsOSXSystemProxySettings::nsOSXSystemProxySettings() : mSystemDynamicStore(NULL), mProxyDict(NULL) { + mContext = (SCDynamicStoreContext){0, this, NULL, NULL, NULL}; +} + +nsresult nsOSXSystemProxySettings::Init() { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; + + // Register for notification of proxy setting changes + // See: + // http://developer.apple.com/documentation/Networking/Conceptual/CFNetwork/CFStreamTasks/chapter_4_section_5.html + mSystemDynamicStore = + SCDynamicStoreCreate(NULL, CFSTR("Mozilla"), ProxyHasChangedWrapper, &mContext); + if (!mSystemDynamicStore) return NS_ERROR_FAILURE; + + // Set up the store to monitor any changes to the proxies + CFStringRef proxiesKey = SCDynamicStoreKeyCreateProxies(NULL); + if (!proxiesKey) return NS_ERROR_FAILURE; + + CFArrayRef keyArray = CFArrayCreate(NULL, (const void**)(&proxiesKey), 1, &kCFTypeArrayCallBacks); + CFRelease(proxiesKey); + if (!keyArray) return NS_ERROR_FAILURE; + + SCDynamicStoreSetNotificationKeys(mSystemDynamicStore, keyArray, NULL); + CFRelease(keyArray); + + // Add the dynamic store to the run loop + CFRunLoopSourceRef storeRLSource = + SCDynamicStoreCreateRunLoopSource(NULL, mSystemDynamicStore, 0); + if (!storeRLSource) return NS_ERROR_FAILURE; + CFRunLoopAddSource(CFRunLoopGetCurrent(), storeRLSource, kCFRunLoopCommonModes); + CFRelease(storeRLSource); + + // Load the initial copy of proxy info + mProxyDict = (NSDictionary*)SCDynamicStoreCopyProxies(mSystemDynamicStore); + if (!mProxyDict) return NS_ERROR_FAILURE; + + return NS_OK; + + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; +} + +nsOSXSystemProxySettings::~nsOSXSystemProxySettings() { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK; + + [mProxyDict release]; + + if (mSystemDynamicStore) { + // Invalidate the dynamic store's run loop source + // to get the store out of the run loop + CFRunLoopSourceRef rls = SCDynamicStoreCreateRunLoopSource(NULL, mSystemDynamicStore, 0); + if (rls) { + CFRunLoopSourceInvalidate(rls); + CFRelease(rls); + } + CFRelease(mSystemDynamicStore); + } + + NS_OBJC_END_TRY_ABORT_BLOCK; +} + +void nsOSXSystemProxySettings::ProxyHasChanged() { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK; + + [mProxyDict release]; + mProxyDict = (NSDictionary*)SCDynamicStoreCopyProxies(mSystemDynamicStore); + + NS_OBJC_END_TRY_ABORT_BLOCK; +} + +nsresult nsOSXSystemProxySettings::FindSCProxyPort(const nsACString& aScheme, + nsACString& aResultHost, int32_t& aResultPort, + bool& aResultSocksProxy) { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; + + NS_ENSURE_TRUE(mProxyDict != NULL, NS_ERROR_FAILURE); + + for (const SchemeMapping* keys = gSchemeMappingList; keys->mScheme != NULL; ++keys) { + // Check for matching scheme (when appropriate) + if (strcasecmp(keys->mScheme, PromiseFlatCString(aScheme).get()) && !keys->mIsSocksProxy) + continue; + + // Check the proxy is enabled + NSNumber* enabled = [mProxyDict objectForKey:(NSString*)keys->mEnabled]; + NS_ENSURE_TRUE(enabled == NULL || [enabled isKindOfClass:[NSNumber class]], NS_ERROR_FAILURE); + if ([enabled intValue] == 0) continue; + + // Get the proxy host + NSString* host = [mProxyDict objectForKey:(NSString*)keys->mHost]; + if (host == NULL) break; + NS_ENSURE_TRUE([host isKindOfClass:[NSString class]], NS_ERROR_FAILURE); + aResultHost.Assign([host UTF8String]); + + // Get the proxy port + NSNumber* port = [mProxyDict objectForKey:(NSString*)keys->mPort]; + NS_ENSURE_TRUE([port isKindOfClass:[NSNumber class]], NS_ERROR_FAILURE); + aResultPort = [port intValue]; + + aResultSocksProxy = keys->mIsSocksProxy; + + return NS_OK; + } + + return NS_ERROR_FAILURE; + + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; +} + +bool nsOSXSystemProxySettings::IsAutoconfigEnabled() const { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; + + NSNumber* value = [mProxyDict objectForKey:(NSString*)kSCPropNetProxiesProxyAutoConfigEnable]; + NS_ENSURE_TRUE(value == NULL || [value isKindOfClass:[NSNumber class]], false); + return ([value intValue] != 0); + + NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false); +} + +nsresult nsOSXSystemProxySettings::GetAutoconfigURL(nsAutoCString& aResult) const { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; + + NSString* value = [mProxyDict objectForKey:(NSString*)kSCPropNetProxiesProxyAutoConfigURLString]; + if (value != NULL) { + NS_ENSURE_TRUE([value isKindOfClass:[NSString class]], NS_ERROR_FAILURE); + aResult.Assign([value UTF8String]); + return NS_OK; + } + + return NS_ERROR_FAILURE; + + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; +} + +bool nsOSXSystemProxySettings::IsInExceptionList(const nsACString& aHost) const { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; + + NS_ENSURE_TRUE(mProxyDict != NULL, false); + + NSArray* exceptionList = [mProxyDict objectForKey:(NSString*)kSCPropNetProxiesExceptionsList]; + NS_ENSURE_TRUE(exceptionList == NULL || [exceptionList isKindOfClass:[NSArray class]], false); + + NSEnumerator* exceptionEnumerator = [exceptionList objectEnumerator]; + NSString* currentValue = NULL; + while ((currentValue = [exceptionEnumerator nextObject])) { + NS_ENSURE_TRUE([currentValue isKindOfClass:[NSString class]], false); + nsAutoCString overrideStr([currentValue UTF8String]); + if (mozilla::toolkit::system::IsHostProxyEntry(aHost, overrideStr)) return true; + } + + NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false); +} + +nsresult nsOSXSystemProxySettings::GetPACURI(nsACString& aResult) { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; + + NS_ENSURE_TRUE(mProxyDict != NULL, NS_ERROR_FAILURE); + + nsAutoCString pacUrl; + if (IsAutoconfigEnabled() && NS_SUCCEEDED(GetAutoconfigURL(pacUrl))) { + aResult.Assign(pacUrl); + return NS_OK; + } + + return NS_ERROR_FAILURE; + + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; +} + +nsresult nsOSXSystemProxySettings::GetProxyForURI(const nsACString& aSpec, + const nsACString& aScheme, + const nsACString& aHost, const int32_t aPort, + nsACString& aResult) { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; + + int32_t proxyPort; + nsAutoCString proxyHost; + bool proxySocks; + nsresult rv = FindSCProxyPort(aScheme, proxyHost, proxyPort, proxySocks); + + if (NS_FAILED(rv) || IsInExceptionList(aHost)) { + aResult.AssignLiteral("DIRECT"); + } else if (proxySocks) { + aResult.Assign("SOCKS "_ns + proxyHost + nsPrintfCString(":%d", proxyPort)); + } else { + aResult.Assign("PROXY "_ns + proxyHost + nsPrintfCString(":%d", proxyPort)); + } + + return NS_OK; + + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; +} + +NS_IMPL_COMPONENT_FACTORY(nsOSXSystemProxySettings) { + auto settings = mozilla::MakeRefPtr<nsOSXSystemProxySettings>(); + if (NS_SUCCEEDED(settings->Init())) { + return settings.forget().downcast<nsISupports>(); + } + return nullptr; +} diff --git a/toolkit/system/osxproxy/tests/gtest/TestProxyBypassRules.cpp b/toolkit/system/osxproxy/tests/gtest/TestProxyBypassRules.cpp new file mode 100644 index 0000000000..826011f13e --- /dev/null +++ b/toolkit/system/osxproxy/tests/gtest/TestProxyBypassRules.cpp @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "gtest/gtest.h" +#include "ProxyUtils.h" + +using namespace mozilla::toolkit::system; + +TEST(OSXProxy, TestProxyBypassRules) +{ + EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "*mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "*.mozilla.org"_ns)); + EXPECT_FALSE(IsHostProxyEntry("notmozilla.org"_ns, "*.mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("www.mozilla.org"_ns, "*mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("www.mozilla.org"_ns, "*.mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("www.mozilla.com"_ns, "*.mozilla.*"_ns)); +} + +TEST(OSXProxy, TestProxyBypassRulesIPv4) +{ + EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.*"_ns)); + EXPECT_FALSE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.2.*"_ns)); + + EXPECT_TRUE(IsHostProxyEntry("10.1.2.3"_ns, "10.0.0.0/8"_ns)); + EXPECT_TRUE(IsHostProxyEntry("192.168.192.1"_ns, "192.168/16"_ns)); + EXPECT_FALSE(IsHostProxyEntry("192.168.192.1"_ns, "192.168/17"_ns)); + EXPECT_TRUE(IsHostProxyEntry("192.168.192.1"_ns, "192.168.128/17"_ns)); + EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.1/32"_ns)); +} + +TEST(OSXProxy, TestProxyBypassRulesIPv6) +{ + EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/64"_ns)); + EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0000:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/80"_ns)); + EXPECT_FALSE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/80"_ns)); + EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0000:0000:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/96"_ns)); + EXPECT_FALSE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/96"_ns)); +} diff --git a/toolkit/system/osxproxy/tests/gtest/moz.build b/toolkit/system/osxproxy/tests/gtest/moz.build new file mode 100644 index 0000000000..976c83a07e --- /dev/null +++ b/toolkit/system/osxproxy/tests/gtest/moz.build @@ -0,0 +1,18 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, you can obtain one at http://mozilla.org/MPL/2.0/. + +UNIFIED_SOURCES += [ + "TestProxyBypassRules.cpp", +] + +LOCAL_INCLUDES += [ + "/toolkit/system/osxproxy", +] + +FINAL_LIBRARY = "xul-gtest" + +if CONFIG["CC_TYPE"] in ("clang", "gcc"): + CXXFLAGS += ["-Wshadow"] diff --git a/toolkit/system/unixproxy/components.conf b/toolkit/system/unixproxy/components.conf new file mode 100644 index 0000000000..4f8f11c82a --- /dev/null +++ b/toolkit/system/unixproxy/components.conf @@ -0,0 +1,13 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +Classes = [ + { + 'cid': '{0fa3158c-d5a7-43de-9181-a285e74cf1d4}', + 'contract_ids': ['@mozilla.org/system-proxy-settings;1'], + 'type': 'nsUnixSystemProxySettings', + }, +] diff --git a/toolkit/system/unixproxy/moz.build b/toolkit/system/unixproxy/moz.build new file mode 100644 index 0000000000..e5d9eb046e --- /dev/null +++ b/toolkit/system/unixproxy/moz.build @@ -0,0 +1,24 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Core", "Networking") + +if CONFIG["MOZ_ENABLE_LIBPROXY"]: + CXXFLAGS += CONFIG["MOZ_LIBPROXY_CFLAGS"] + SOURCES += [ + "nsLibProxySettings.cpp", + ] +else: + SOURCES += [ + "nsUnixSystemProxySettings.cpp", + ] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +FINAL_LIBRARY = "xul" diff --git a/toolkit/system/unixproxy/nsLibProxySettings.cpp b/toolkit/system/unixproxy/nsLibProxySettings.cpp new file mode 100644 index 0000000000..4f6f43fb76 --- /dev/null +++ b/toolkit/system/unixproxy/nsLibProxySettings.cpp @@ -0,0 +1,104 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsISystemProxySettings.h" +#include "mozilla/Components.h" +#include "nsString.h" +#include "nsCOMPtr.h" +#include "nsNetCID.h" +#include "nspr.h" + +extern "C" { +#include <proxy.h> +} + +class nsUnixSystemProxySettings : public nsISystemProxySettings { + public: + NS_DECL_THREADSAFE_ISUPPORTS + NS_DECL_NSISYSTEMPROXYSETTINGS + + nsUnixSystemProxySettings() { mProxyFactory = nullptr; } + + private: + ~nsUnixSystemProxySettings() { + if (mProxyFactory) px_proxy_factory_free(mProxyFactory); + } + + pxProxyFactory* mProxyFactory; +}; + +NS_IMPL_ISUPPORTS(nsUnixSystemProxySettings, nsISystemProxySettings) + +NS_IMETHODIMP +nsUnixSystemProxySettings::GetMainThreadOnly(bool* aMainThreadOnly) { + *aMainThreadOnly = false; + return NS_OK; +} + +nsresult nsUnixSystemProxySettings::GetPACURI(nsACString& aResult) { + // Make sure we return an empty result. + aResult.Truncate(); + return NS_OK; +} + +nsresult nsUnixSystemProxySettings::GetProxyForURI(const nsACString& aSpec, + const nsACString& aScheme, + const nsACString& aHost, + const int32_t aPort, + nsACString& aResult) { + nsresult rv; + + if (!mProxyFactory) { + mProxyFactory = px_proxy_factory_new(); + } + NS_ENSURE_TRUE(mProxyFactory, NS_ERROR_NOT_AVAILABLE); + + char** proxyArray = nullptr; + proxyArray = px_proxy_factory_get_proxies(mProxyFactory, + PromiseFlatCString(aSpec).get()); + NS_ENSURE_TRUE(proxyArray, NS_ERROR_NOT_AVAILABLE); + + // Translate libproxy's output to PAC string as expected + // libproxy returns an array of proxies in the format: + // <procotol>://[username:password@]proxy:port + // or + // direct:// + // + // PAC format: "PROXY proxy1.foo.com:8080; PROXY proxy2.foo.com:8080; DIRECT" + // but nsISystemProxySettings allows "PROXY http://proxy.foo.com:8080" as + // well. + + int c = 0; + while (proxyArray[c] != nullptr) { + if (!aResult.IsEmpty()) { + aResult.AppendLiteral("; "); + } + + // figure out the scheme, and we can't use nsIIOService::NewURI because + // this is not the main thread. + char* colon = strchr(proxyArray[c], ':'); + uint32_t schemelen = colon ? colon - proxyArray[c] : 0; + if (schemelen < 1) { + c++; + continue; + } + + if (schemelen == 6 && !strncasecmp(proxyArray[c], "direct", 6)) { + aResult.AppendLiteral("DIRECT"); + } else { + aResult.AppendLiteral("PROXY "); + aResult.Append(proxyArray[c]); + } + + c++; + } + + free(proxyArray); + return NS_OK; +} + +NS_IMPL_COMPONENT_FACTORY(nsUnixSystemProxySettings) { + return do_AddRef(new nsUnixSystemProxySettings()).downcast<nsISupports>(); +} diff --git a/toolkit/system/unixproxy/nsUnixSystemProxySettings.cpp b/toolkit/system/unixproxy/nsUnixSystemProxySettings.cpp new file mode 100644 index 0000000000..56a53d8579 --- /dev/null +++ b/toolkit/system/unixproxy/nsUnixSystemProxySettings.cpp @@ -0,0 +1,397 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsISystemProxySettings.h" +#include "mozilla/Components.h" +#include "nsIURI.h" +#include "nsReadableUtils.h" +#include "nsArrayUtils.h" +#include "prnetdb.h" +#include "prenv.h" +#include "nsPrintfCString.h" +#include "nsNetCID.h" +#include "nsNetUtil.h" +#include "nsISupportsPrimitives.h" +#include "nsIGSettingsService.h" +#include "nsInterfaceHashtable.h" +#include "mozilla/Attributes.h" +#include "nsIURI.h" + +using namespace mozilla; + +class nsUnixSystemProxySettings final : public nsISystemProxySettings { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSISYSTEMPROXYSETTINGS + + nsUnixSystemProxySettings() : mSchemeProxySettings(4) {} + void Init(); + + private: + ~nsUnixSystemProxySettings() = default; + + nsCOMPtr<nsIGSettingsService> mGSettings; + nsCOMPtr<nsIGSettingsCollection> mProxySettings; + nsInterfaceHashtable<nsCStringHashKey, nsIGSettingsCollection> + mSchemeProxySettings; + nsresult GetProxyFromGSettings(const nsACString& aScheme, + const nsACString& aHost, int32_t aPort, + nsACString& aResult); + nsresult SetProxyResultFromGSettings(const char* aKeyBase, const char* aType, + nsACString& aResult); +}; + +NS_IMPL_ISUPPORTS(nsUnixSystemProxySettings, nsISystemProxySettings) + +NS_IMETHODIMP +nsUnixSystemProxySettings::GetMainThreadOnly(bool* aMainThreadOnly) { + // dbus prevents us from being threadsafe, but this routine should not block + // anyhow + *aMainThreadOnly = true; + return NS_OK; +} + +void nsUnixSystemProxySettings::Init() { + mGSettings = do_GetService(NS_GSETTINGSSERVICE_CONTRACTID); + if (mGSettings) { + mGSettings->GetCollectionForSchema("org.gnome.system.proxy"_ns, + getter_AddRefs(mProxySettings)); + } +} + +nsresult nsUnixSystemProxySettings::GetPACURI(nsACString& aResult) { + if (mProxySettings) { + nsCString proxyMode; + // Check if mode is auto + nsresult rv = mProxySettings->GetString("mode"_ns, proxyMode); + if (rv == NS_OK && proxyMode.EqualsLiteral("auto")) { + return mProxySettings->GetString("autoconfig-url"_ns, aResult); + } + } + + // Return an empty string when auto mode is not set. + aResult.Truncate(); + return NS_OK; +} + +static bool IsInNoProxyList(const nsACString& aHost, int32_t aPort, + const char* noProxyVal) { + NS_ASSERTION(aPort >= 0, "Negative port?"); + + nsAutoCString noProxy(noProxyVal); + if (noProxy.EqualsLiteral("*")) return true; + + noProxy.StripWhitespace(); + + nsReadingIterator<char> pos; + nsReadingIterator<char> end; + noProxy.BeginReading(pos); + noProxy.EndReading(end); + while (pos != end) { + nsReadingIterator<char> last = pos; + nsReadingIterator<char> nextPos; + if (FindCharInReadable(',', last, end)) { + nextPos = last; + ++nextPos; + } else { + last = end; + nextPos = end; + } + + nsReadingIterator<char> colon = pos; + int32_t port = -1; + if (FindCharInReadable(':', colon, last)) { + ++colon; + nsDependentCSubstring portStr(colon, last); + nsAutoCString portStr2( + portStr); // We need this for ToInteger. String API's suck. + nsresult err; + port = portStr2.ToInteger(&err); + if (NS_FAILED(err)) { + port = -2; // don't match any port, so we ignore this pattern + } + --colon; + } else { + colon = last; + } + + if (port == -1 || port == aPort) { + nsDependentCSubstring hostStr(pos, colon); + // By using StringEndsWith instead of an equality comparator, we can + // include sub-domains + if (StringEndsWith(aHost, hostStr, nsCaseInsensitiveCStringComparator)) + return true; + } + + pos = nextPos; + } + + return false; +} + +static void SetProxyResult(const char* aType, const nsACString& aHost, + int32_t aPort, nsACString& aResult) { + aResult.AssignASCII(aType); + aResult.Append(' '); + aResult.Append(aHost); + if (aPort > 0) { + aResult.Append(':'); + aResult.AppendInt(aPort); + } +} + +static void SetProxyResultDirect(nsACString& aResult) { + aResult.AssignLiteral("DIRECT"); +} + +static nsresult GetProxyFromEnvironment(const nsACString& aScheme, + const nsACString& aHost, int32_t aPort, + nsACString& aResult) { + nsAutoCString envVar; + envVar.Append(aScheme); + envVar.AppendLiteral("_proxy"); + const char* proxyVal = PR_GetEnv(envVar.get()); + if (!proxyVal) { + proxyVal = PR_GetEnv("all_proxy"); + if (!proxyVal) { + // Return failure so that the caller can detect the failure and + // fall back to other proxy detection (e.g., WPAD) + return NS_ERROR_FAILURE; + } + } + + const char* noProxyVal = PR_GetEnv("no_proxy"); + if (noProxyVal && IsInNoProxyList(aHost, aPort, noProxyVal)) { + SetProxyResultDirect(aResult); + return NS_OK; + } + + // Use our URI parser to crack the proxy URI + nsCOMPtr<nsIURI> proxyURI; + nsresult rv = NS_NewURI(getter_AddRefs(proxyURI), proxyVal); + NS_ENSURE_SUCCESS(rv, rv); + + // Is there a way to specify "socks://" or something in these environment + // variables? I can't find any documentation. + if (!proxyURI->SchemeIs("http")) { + return NS_ERROR_UNKNOWN_PROTOCOL; + } + + nsAutoCString proxyHost; + rv = proxyURI->GetHost(proxyHost); + NS_ENSURE_SUCCESS(rv, rv); + + int32_t proxyPort; + rv = proxyURI->GetPort(&proxyPort); + NS_ENSURE_SUCCESS(rv, rv); + + SetProxyResult("PROXY", proxyHost, proxyPort, aResult); + return NS_OK; +} + +nsresult nsUnixSystemProxySettings::SetProxyResultFromGSettings( + const char* aKeyBase, const char* aType, nsACString& aResult) { + nsDependentCString key(aKeyBase); + + nsCOMPtr<nsIGSettingsCollection> proxy_settings = + mSchemeProxySettings.Get(key); + nsresult rv; + if (!proxy_settings) { + rv = + mGSettings->GetCollectionForSchema(key, getter_AddRefs(proxy_settings)); + NS_ENSURE_SUCCESS(rv, rv); + + mSchemeProxySettings.Put(key, proxy_settings); + } + + nsAutoCString host; + rv = proxy_settings->GetString("host"_ns, host); + NS_ENSURE_SUCCESS(rv, rv); + if (host.IsEmpty()) return NS_ERROR_FAILURE; + + int32_t port; + rv = proxy_settings->GetInt("port"_ns, &port); + NS_ENSURE_SUCCESS(rv, rv); + + /* When port is 0, proxy is not considered as enabled even if host is set. */ + if (port == 0) return NS_ERROR_FAILURE; + + SetProxyResult(aType, host, port, aResult); + return NS_OK; +} + +/* copied from nsProtocolProxyService.cpp --- we should share this! */ +static void proxy_MaskIPv6Addr(PRIPv6Addr& addr, uint16_t mask_len) { + if (mask_len == 128) return; + + if (mask_len > 96) { + addr.pr_s6_addr32[3] = + PR_htonl(PR_ntohl(addr.pr_s6_addr32[3]) & (~0L << (128 - mask_len))); + } else if (mask_len > 64) { + addr.pr_s6_addr32[3] = 0; + addr.pr_s6_addr32[2] = + PR_htonl(PR_ntohl(addr.pr_s6_addr32[2]) & (~0L << (96 - mask_len))); + } else if (mask_len > 32) { + addr.pr_s6_addr32[3] = 0; + addr.pr_s6_addr32[2] = 0; + addr.pr_s6_addr32[1] = + PR_htonl(PR_ntohl(addr.pr_s6_addr32[1]) & (~0L << (64 - mask_len))); + } else { + addr.pr_s6_addr32[3] = 0; + addr.pr_s6_addr32[2] = 0; + addr.pr_s6_addr32[1] = 0; + addr.pr_s6_addr32[0] = + PR_htonl(PR_ntohl(addr.pr_s6_addr32[0]) & (~0L << (32 - mask_len))); + } +} + +static bool ConvertToIPV6Addr(const nsACString& aName, PRIPv6Addr* aAddr, + int32_t* aMask) { + PRNetAddr addr; + // try to convert hostname to IP + if (PR_StringToNetAddr(PromiseFlatCString(aName).get(), &addr) != PR_SUCCESS) + return false; + + // convert parsed address to IPv6 + if (addr.raw.family == PR_AF_INET) { + // convert to IPv4-mapped address + PR_ConvertIPv4AddrToIPv6(addr.inet.ip, aAddr); + if (aMask) { + if (*aMask <= 32) + *aMask += 96; + else + return false; + } + } else if (addr.raw.family == PR_AF_INET6) { + // copy the address + memcpy(aAddr, &addr.ipv6.ip, sizeof(PRIPv6Addr)); + } else { + return false; + } + + return true; +} + +static bool HostIgnoredByProxy(const nsACString& aIgnore, + const nsACString& aHost) { + if (aIgnore.Equals(aHost, nsCaseInsensitiveCStringComparator)) return true; + + if (aIgnore.First() == '*' && + StringEndsWith(aHost, nsDependentCSubstring(aIgnore, 1), + nsCaseInsensitiveCStringComparator)) + return true; + + int32_t mask = 128; + nsReadingIterator<char> start; + nsReadingIterator<char> slash; + nsReadingIterator<char> end; + aIgnore.BeginReading(start); + aIgnore.BeginReading(slash); + aIgnore.EndReading(end); + if (FindCharInReadable('/', slash, end)) { + ++slash; + nsDependentCSubstring maskStr(slash, end); + nsAutoCString maskStr2(maskStr); + nsresult err; + mask = maskStr2.ToInteger(&err); + if (NS_FAILED(err)) { + mask = 128; + } + --slash; + } else { + slash = end; + } + + nsDependentCSubstring ignoreStripped(start, slash); + PRIPv6Addr ignoreAddr, hostAddr; + if (!ConvertToIPV6Addr(ignoreStripped, &ignoreAddr, &mask) || + !ConvertToIPV6Addr(aHost, &hostAddr, nullptr)) + return false; + + proxy_MaskIPv6Addr(ignoreAddr, mask); + proxy_MaskIPv6Addr(hostAddr, mask); + + return memcmp(&ignoreAddr, &hostAddr, sizeof(PRIPv6Addr)) == 0; +} + +nsresult nsUnixSystemProxySettings::GetProxyFromGSettings( + const nsACString& aScheme, const nsACString& aHost, int32_t aPort, + nsACString& aResult) { + nsCString proxyMode; + nsresult rv = mProxySettings->GetString("mode"_ns, proxyMode); + NS_ENSURE_SUCCESS(rv, rv); + + // return NS_ERROR_FAILURE when no proxy is set + if (!proxyMode.EqualsLiteral("manual")) { + return NS_ERROR_FAILURE; + } + + nsCOMPtr<nsIArray> ignoreList; + if (NS_SUCCEEDED(mProxySettings->GetStringList("ignore-hosts"_ns, + getter_AddRefs(ignoreList))) && + ignoreList) { + uint32_t len = 0; + ignoreList->GetLength(&len); + for (uint32_t i = 0; i < len; ++i) { + nsCOMPtr<nsISupportsCString> str = do_QueryElementAt(ignoreList, i); + if (str) { + nsCString s; + if (NS_SUCCEEDED(str->GetData(s)) && !s.IsEmpty()) { + if (HostIgnoredByProxy(s, aHost)) { + SetProxyResultDirect(aResult); + return NS_OK; + } + } + } + } + } + + if (aScheme.LowerCaseEqualsLiteral("http")) { + rv = SetProxyResultFromGSettings("org.gnome.system.proxy.http", "PROXY", + aResult); + } else if (aScheme.LowerCaseEqualsLiteral("https")) { + rv = SetProxyResultFromGSettings("org.gnome.system.proxy.https", "PROXY", + aResult); + /* Try to use HTTP proxy when HTTPS proxy is not explicitly defined */ + if (rv != NS_OK) + rv = SetProxyResultFromGSettings("org.gnome.system.proxy.http", "PROXY", + aResult); + } else if (aScheme.LowerCaseEqualsLiteral("ftp")) { + rv = SetProxyResultFromGSettings("org.gnome.system.proxy.ftp", "PROXY", + aResult); + } else { + rv = NS_ERROR_FAILURE; + } + if (rv != NS_OK) { + /* If proxy for scheme is not specified, use SOCKS proxy for all schemes */ + rv = SetProxyResultFromGSettings("org.gnome.system.proxy.socks", "SOCKS", + aResult); + } + + if (NS_FAILED(rv)) { + SetProxyResultDirect(aResult); + } + + return NS_OK; +} + +nsresult nsUnixSystemProxySettings::GetProxyForURI(const nsACString& aSpec, + const nsACString& aScheme, + const nsACString& aHost, + const int32_t aPort, + nsACString& aResult) { + if (mProxySettings) { + nsresult rv = GetProxyFromGSettings(aScheme, aHost, aPort, aResult); + if (NS_SUCCEEDED(rv)) return rv; + } + + return GetProxyFromEnvironment(aScheme, aHost, aPort, aResult); +} + +NS_IMPL_COMPONENT_FACTORY(nsUnixSystemProxySettings) { + auto result = MakeRefPtr<nsUnixSystemProxySettings>(); + result->Init(); + return result.forget().downcast<nsISupports>(); +} diff --git a/toolkit/system/windowsDHCPClient/DHCPUtils.cpp b/toolkit/system/windowsDHCPClient/DHCPUtils.cpp new file mode 100644 index 0000000000..5229e0205a --- /dev/null +++ b/toolkit/system/windowsDHCPClient/DHCPUtils.cpp @@ -0,0 +1,247 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "DHCPUtils.h" +#include <vector> +#include "mozilla/Logging.h" +#include "nsString.h" + +#define MOZ_WORKING_BUFFER_SIZE_NETWORK_ADAPTERS 15000 +#define MOZ_WORKING_BUFFER_SIZE_DHCP_PARAMS 1000 +#define MOZ_MAX_TRIES 3 +namespace mozilla { +namespace toolkit { +namespace system { +namespace windowsDHCPClient { + +// +// The comments on this page reference the following Microsoft documentation +// pages (both retrieved 2017-06-27) +// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365915(v=vs.85).aspx +// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363298(v=vs.85).aspx +mozilla::LazyLogModule gDhcpUtilsLog("dhcpUtils"); + +#undef LOG +#define LOG(args) MOZ_LOG(gDhcpUtilsLog, LogLevel::Debug, args) + +bool IsCurrentAndHasDHCP(PIP_ADAPTER_ADDRESSES aAddresses) { + return aAddresses->OperStatus == 1 && + (aAddresses->Dhcpv4Server.iSockaddrLength || + aAddresses->Dhcpv6Server.iSockaddrLength); +} + +nsresult GetActiveDHCPNetworkAdapterName( + nsACString& aNetworkAdapterName, + WindowsNetworkFunctionsWrapper* aWindowsNetworkFunctionsWrapper) { + /* Declare and initialize variables */ + + uint32_t dwRetVal = 0; + nsresult rv = NS_ERROR_FAILURE; + + // Set the flags to pass to GetAdaptersAddresses + uint32_t flags = GAA_FLAG_INCLUDE_PREFIX; + + // default to unspecified address family (both) + uint32_t family = AF_UNSPEC; + + // Allocate a 15 KB buffer to start with. + uint32_t outBufLen = MOZ_WORKING_BUFFER_SIZE_NETWORK_ADAPTERS; + uint32_t iterations = 0; + + aNetworkAdapterName.Truncate(); + + // Now we try calling the GetAdaptersAddresses method until the return value + // is not ERROR_BUFFER_OVERFLOW. According to [1] + // + // + // > When the return value is ERROR_BUFFER_OVERFLOW, the SizePointer parameter + // returned > points to the required size of the buffer to hold the adapter + // information. > Note that it is possible for the buffer size required for + // the IP_ADAPTER_ADDRESSES > structures pointed to by the AdapterAddresses + // parameter to change between > subsequent calls to the GetAdaptersAddresses + // function if an adapter address > is added or removed. However, this method + // of using the GetAdaptersAddresses > function is strongly discouraged. This + // method requires calling the > GetAdaptersAddresses function multiple times. + // > + // > The recommended method of calling the GetAdaptersAddresses function is + // > to pre-allocate a 15KB working buffer pointed to by the AdapterAddresses + // parameter. > On typical computers, this dramatically reduces the chances + // that the > GetAdaptersAddresses function returns ERROR_BUFFER_OVERFLOW, + // which would require > calling GetAdaptersAddresses function multiple times. + // + // + // The possibility of the buffer size changing between calls to + // GetAdaptersAddresses is why we allow the following code to be called + // several times, rather than just the two that would be neccessary if we + // could rely on the value returned in outBufLen being the true size needed. + + std::vector<IP_ADAPTER_ADDRESSES> pAddresses; + do { + // resize outBufLen up to the next multiple of sizeof(IP_ADAPTER_ADDRESSES) + outBufLen = ((outBufLen + sizeof(IP_ADAPTER_ADDRESSES) - 1) / + sizeof(IP_ADAPTER_ADDRESSES)) * + sizeof(IP_ADAPTER_ADDRESSES); + pAddresses.resize(outBufLen / sizeof(IP_ADAPTER_ADDRESSES)); + LOG( + ("Trying GetAdaptersAddresses with pAddresses sized to %d and buffer " + "length of %d", + pAddresses.size(), outBufLen)); + + dwRetVal = aWindowsNetworkFunctionsWrapper->GetAdaptersAddressesWrapped( + family, flags, nullptr, pAddresses.data(), (PULONG)&outBufLen); + + if (dwRetVal == ERROR_BUFFER_OVERFLOW) { + iterations++; + } + } while (dwRetVal == ERROR_BUFFER_OVERFLOW && iterations < MOZ_MAX_TRIES); + + switch (dwRetVal) { + case NO_ERROR: { + // set default return value if we don't find a suitable network adapter + rv = NS_ERROR_NOT_AVAILABLE; + PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses.data(); + while (pCurrAddresses) { + if (IsCurrentAndHasDHCP(pCurrAddresses)) { + rv = NS_OK; + aNetworkAdapterName.Assign(pCurrAddresses->AdapterName); + break; + } + pCurrAddresses = pCurrAddresses->Next; + } + } break; + case ERROR_NO_DATA: + rv = NS_ERROR_NOT_AVAILABLE; + break; + default: + MOZ_LOG(gDhcpUtilsLog, mozilla::LogLevel::Warning, + ("GetAdaptersAddresses returned %d", dwRetVal)); + rv = NS_ERROR_FAILURE; + break; + } + return rv; +} + +DWORD +IterateDHCPInformRequestsUntilBufferLargeEnough( + DHCPCAPI_PARAMS& aDhcpRequestedOptionParams, + wchar_t* aWideNetworkAdapterName, std::vector<char>& aBuffer, + WindowsNetworkFunctionsWrapper* aWindowsNetworkFunctionsWrapper) { + uint32_t iterations = 0; + uint32_t outBufLen = MOZ_WORKING_BUFFER_SIZE_DHCP_PARAMS; + + DHCPCAPI_PARAMS_ARRAY RequestParams = {1, // only one option to request + &aDhcpRequestedOptionParams}; + + // According to [2], + // the following is for 'Optional data to be requested, + // in addition to the data requested in the RecdParams array.' + // We are not requesting anything in addition, so this is empty. + DHCPCAPI_PARAMS_ARRAY SendParams = {0, nullptr}; + + DWORD winAPIResponse; + // Now we try calling the DHCPRequestParams method until the return value + // is not ERROR_MORE_DATA. According to [2]: + // + // + // > Note that the required size of Buffer may increase during the time that + // elapses > between the initial function call's return and a subsequent call; + // > therefore, the required size of Buffer (indicated in pSize) + // > provides an indication of the approximate size required of Buffer, + // > rather than guaranteeing that subsequent calls will return successfully + // > if Buffer is set to the size indicated in pSize. + // + // + // This is why we allow this DHCPRequestParams to be called several times, + // rather than just the two that would be neccessary if we could rely on the + // value returned in outBufLen being the true size needed. + do { + aBuffer.resize(outBufLen); + + winAPIResponse = aWindowsNetworkFunctionsWrapper->DhcpRequestParamsWrapped( + DHCPCAPI_REQUEST_SYNCHRONOUS, // Flags + nullptr, // Reserved + aWideNetworkAdapterName, // Adapter Name + nullptr, // not using class id + SendParams, // sent parameters + RequestParams, // requesting params + (PBYTE)aBuffer.data(), // buffer for the output of RequestParams + (PULONG)&outBufLen, // buffer size + nullptr // Request ID for persistent requests - not needed here + ); + + if (winAPIResponse == ERROR_MORE_DATA) { + iterations++; + } + } while (winAPIResponse == ERROR_MORE_DATA && iterations < MOZ_MAX_TRIES); + return winAPIResponse; +} + +nsresult RetrieveOption( + const nsACString& aAdapterName, uint8_t aOption, + std::vector<char>& aOptionValueBuf, uint32_t* aOptionSize, + WindowsNetworkFunctionsWrapper* aWindowsNetworkFunctionsWrapper) { + nsresult rv; + nsAutoString wideNetworkAdapterName = NS_ConvertUTF8toUTF16(aAdapterName); + + DHCPCAPI_PARAMS DhcpRequestedOptionParams = { + 0, // Flags - Reserved, must be set to zero [2] + aOption, // OptionId + false, // whether this is vendor specific - let's assume not + nullptr, // data filled in on return + 0 // nBytes used by return data + }; + + std::vector<char> tmpBuffer( + MOZ_WORKING_BUFFER_SIZE_DHCP_PARAMS); // a buffer for the DHCP response + // object + DWORD winAPIResponse = IterateDHCPInformRequestsUntilBufferLargeEnough( + DhcpRequestedOptionParams, wideNetworkAdapterName.get(), tmpBuffer, + aWindowsNetworkFunctionsWrapper); + + switch (winAPIResponse) { + case NO_ERROR: { + if (DhcpRequestedOptionParams.nBytesData == 0) { + *aOptionSize = 0; + rv = NS_ERROR_NOT_AVAILABLE; + break; + } + + if (*aOptionSize >= DhcpRequestedOptionParams.nBytesData) { + rv = NS_OK; + } else { + rv = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA; + } + + uint32_t actualSizeReturned = + *aOptionSize > DhcpRequestedOptionParams.nBytesData + ? DhcpRequestedOptionParams.nBytesData + : *aOptionSize; + + memcpy(aOptionValueBuf.data(), DhcpRequestedOptionParams.Data, + actualSizeReturned); + *aOptionSize = DhcpRequestedOptionParams.nBytesData; + break; + } + case ERROR_INVALID_PARAMETER: + MOZ_LOG(gDhcpUtilsLog, mozilla::LogLevel::Warning, + ("RetrieveOption returned %d (ERROR_INVALID_PARAMETER) when " + "option %d requested", + winAPIResponse, aOption)); + rv = NS_ERROR_INVALID_ARG; + break; + default: + MOZ_LOG(gDhcpUtilsLog, mozilla::LogLevel::Warning, + ("RetrieveOption returned %d when option %d requested", + winAPIResponse, aOption)); + rv = NS_ERROR_FAILURE; + } + return rv; +} + +} // namespace windowsDHCPClient +} // namespace system +} // namespace toolkit +} // namespace mozilla diff --git a/toolkit/system/windowsDHCPClient/DHCPUtils.h b/toolkit/system/windowsDHCPClient/DHCPUtils.h new file mode 100644 index 0000000000..20f2996acb --- /dev/null +++ b/toolkit/system/windowsDHCPClient/DHCPUtils.h @@ -0,0 +1,31 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_toolkit_system_windowsDHCPClient_DHCPUtils_h +#define mozilla_toolkit_system_windowsDHCPClient_DHCPUtils_h + +#include "WindowsNetworkFunctionsWrapper.h" +#include <vector> + +namespace mozilla { +namespace toolkit { +namespace system { +namespace windowsDHCPClient { + +nsresult GetActiveDHCPNetworkAdapterName( + nsACString& aNetworkAdapterName, + WindowsNetworkFunctionsWrapper* aWindowsNetworkFunctionsWrapper); + +nsresult RetrieveOption( + const nsACString& aAdapterName, uint8_t aOption, + std::vector<char>& aOptionValueBuf, uint32_t* aOptionSize, + WindowsNetworkFunctionsWrapper* aWindowsNetworkFunctionsWrapper); + +} // namespace windowsDHCPClient +} // namespace system +} // namespace toolkit +} // namespace mozilla +#endif // mozilla_toolkit_system_windowsDHCPClient_DHCPUtils_h diff --git a/toolkit/system/windowsDHCPClient/WindowsNetworkFunctionsWrapper.cpp b/toolkit/system/windowsDHCPClient/WindowsNetworkFunctionsWrapper.cpp new file mode 100644 index 0000000000..0dac4bcbb9 --- /dev/null +++ b/toolkit/system/windowsDHCPClient/WindowsNetworkFunctionsWrapper.cpp @@ -0,0 +1,41 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "WindowsNetworkFunctionsWrapper.h" + +#ifndef __MINGW32__ +# pragma comment(lib, "IPHLPAPI.lib") +# pragma comment(lib, "dhcpcsvc.lib") +#endif + +namespace mozilla { +namespace toolkit { +namespace system { +namespace windowsDHCPClient { + +NS_IMPL_ISUPPORTS(WindowsNetworkFunctionsWrapper, nsISupports) + +ULONG WindowsNetworkFunctionsWrapper::GetAdaptersAddressesWrapped( + _In_ ULONG aFamily, _In_ ULONG aFlags, _In_ PVOID aReserved, + _Inout_ PIP_ADAPTER_ADDRESSES aAdapterAddresses, + _Inout_ PULONG aSizePointer) { + return GetAdaptersAddresses(aFamily, aFlags, aReserved, aAdapterAddresses, + aSizePointer); +} + +DWORD WindowsNetworkFunctionsWrapper::DhcpRequestParamsWrapped( + _In_ DWORD aFlags, _In_ LPVOID aReserved, _In_ LPWSTR aAdapterName, + _In_ LPDHCPCAPI_CLASSID aClassId, _In_ DHCPCAPI_PARAMS_ARRAY aSendParams, + _Inout_ DHCPCAPI_PARAMS_ARRAY aRecdParams, _In_ LPBYTE aBuffer, + _Inout_ LPDWORD apSize, _In_ LPWSTR aRequestIdStr) { + return DhcpRequestParams(aFlags, aReserved, aAdapterName, aClassId, + aSendParams, aRecdParams, aBuffer, apSize, + aRequestIdStr); +} +} // namespace windowsDHCPClient +} // namespace system +} // namespace toolkit +} // namespace mozilla diff --git a/toolkit/system/windowsDHCPClient/WindowsNetworkFunctionsWrapper.h b/toolkit/system/windowsDHCPClient/WindowsNetworkFunctionsWrapper.h new file mode 100644 index 0000000000..2da6a9ff33 --- /dev/null +++ b/toolkit/system/windowsDHCPClient/WindowsNetworkFunctionsWrapper.h @@ -0,0 +1,48 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_toolkit_system_windowsDHCPClient_windowsNetworkFunctionsWrapper_h +#define mozilla_toolkit_system_windowsDHCPClient_windowsNetworkFunctionsWrapper_h + +#include <winsock2.h> // there is a compilation error if Winsock.h is not + // declared before dhcpcsdk.h +#include <dhcpcsdk.h> +#include <iphlpapi.h> + +#include "nsISupports.h" + +// Thin wrapper around low-level network functions needed for DHCP querying for +// web proxy +namespace mozilla { +namespace toolkit { +namespace system { +namespace windowsDHCPClient { + +class WindowsNetworkFunctionsWrapper : nsISupports { + public: + NS_DECL_THREADSAFE_ISUPPORTS + WindowsNetworkFunctionsWrapper(){}; + + virtual ULONG GetAdaptersAddressesWrapped( + _In_ ULONG aFamily, _In_ ULONG aFlags, _In_ PVOID aReserved, + _Inout_ PIP_ADAPTER_ADDRESSES aAdapterAddresses, + _Inout_ PULONG aSizePointer); + + virtual DWORD DhcpRequestParamsWrapped( + _In_ DWORD aFlags, _In_ LPVOID aReserved, _In_ LPWSTR aAdapterName, + _In_ LPDHCPCAPI_CLASSID aClassId, _In_ DHCPCAPI_PARAMS_ARRAY aSendParams, + _Inout_ DHCPCAPI_PARAMS_ARRAY aRecdParams, _In_ LPBYTE aBuffer, + _Inout_ LPDWORD apSize, _In_ LPWSTR aRequestIdStr); + + protected: + virtual ~WindowsNetworkFunctionsWrapper(){}; +}; + +} // namespace windowsDHCPClient +} // namespace system +} // namespace toolkit +} // namespace mozilla +#endif // mozilla_toolkit_system_windowsDHCPClient_windowsNetworkFunctionsWrapper_h diff --git a/toolkit/system/windowsDHCPClient/components.conf b/toolkit/system/windowsDHCPClient/components.conf new file mode 100644 index 0000000000..f2085bce3e --- /dev/null +++ b/toolkit/system/windowsDHCPClient/components.conf @@ -0,0 +1,14 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +Classes = [ + { + 'cid': '{febf1d69-4d7d-4891-9524-045ad18b5592}', + 'contract_ids': ['@mozilla.org/dhcp-client;1'], + 'type': 'mozilla::toolkit::system::windowsDHCPClient::nsWindowsDHCPClient', + 'headers': ['/toolkit/system/windowsDHCPClient/nsWindowsDHCPClient.h'] + }, +] diff --git a/toolkit/system/windowsDHCPClient/moz.build b/toolkit/system/windowsDHCPClient/moz.build new file mode 100644 index 0000000000..308ac4dd4d --- /dev/null +++ b/toolkit/system/windowsDHCPClient/moz.build @@ -0,0 +1,22 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Core", "Networking: HTTP") + +TEST_DIRS += ["tests/gtest"] + +SOURCES += [ + "DHCPUtils.cpp", + "nsWindowsDHCPClient.cpp", + "WindowsNetworkFunctionsWrapper.cpp", +] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +FINAL_LIBRARY = "xul" diff --git a/toolkit/system/windowsDHCPClient/nsWindowsDHCPClient.cpp b/toolkit/system/windowsDHCPClient/nsWindowsDHCPClient.cpp new file mode 100644 index 0000000000..9ddb3bb994 --- /dev/null +++ b/toolkit/system/windowsDHCPClient/nsWindowsDHCPClient.cpp @@ -0,0 +1,77 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsWindowsDHCPClient.h" + +#include <vector> + +#include "DHCPUtils.h" +#include "nsNetCID.h" +#include "nsString.h" +#include "mozilla/Logging.h" +#include "mozilla/Components.h" + +namespace mozilla { +namespace toolkit { +namespace system { +namespace windowsDHCPClient { + +LazyLogModule gDhcpLog("windowsDHCPClient"); + +#undef LOG +#define LOG(args) MOZ_LOG(gDhcpLog, LogLevel::Debug, args) + +#define MOZ_MAX_DHCP_OPTION_LENGTH \ + 255 // this is the maximum option length in DHCP V4 and 6 + +NS_IMPL_ISUPPORTS(nsWindowsDHCPClient, nsIDHCPClient) + +NS_IMETHODIMP +nsWindowsDHCPClient::GetOption(uint8_t aOption, nsACString& aRetVal) { + nsCString networkAdapterName; + nsresult rv; + rv = GetActiveDHCPNetworkAdapterName(networkAdapterName, mNetworkFunctions); + if (rv != NS_OK) { + LOG( + ("Failed to get network adapter name in nsWindowsDHCPClient::GetOption " + "due to error %d", + rv)); + return rv; + } + + uint32_t sizeOptionValue = MOZ_MAX_DHCP_OPTION_LENGTH; + std::vector<char> optionValue; + + bool retryingAfterLossOfSignificantData = false; + do { + optionValue.resize(sizeOptionValue); + rv = RetrieveOption(networkAdapterName, aOption, optionValue, + &sizeOptionValue, mNetworkFunctions); + if (rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA) { + LOG(( + "In nsWindowsDHCPClient::GetOption, DHCP Option %d required %d bytes", + aOption, sizeOptionValue)); + if (retryingAfterLossOfSignificantData) { + break; + } + retryingAfterLossOfSignificantData = true; + } + } while (rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA); + if (rv != NS_OK) { + LOG( + ("Failed to get DHCP Option %d nsWindowsDHCPClient::GetOption due to " + "error %d", + aOption, rv)); + return rv; + } + aRetVal.Assign(optionValue.data(), sizeOptionValue); + return NS_OK; +} + +} // namespace windowsDHCPClient +} // namespace system +} // namespace toolkit +} // namespace mozilla diff --git a/toolkit/system/windowsDHCPClient/nsWindowsDHCPClient.h b/toolkit/system/windowsDHCPClient/nsWindowsDHCPClient.h new file mode 100644 index 0000000000..d1d3eac0f0 --- /dev/null +++ b/toolkit/system/windowsDHCPClient/nsWindowsDHCPClient.h @@ -0,0 +1,34 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsIDHCPClient.h" +#include "nsNetCID.h" +#include "WindowsNetworkFunctionsWrapper.h" + +namespace mozilla { +namespace toolkit { +namespace system { +namespace windowsDHCPClient { + +class nsWindowsDHCPClient final : public nsIDHCPClient { + public: + NS_DECL_THREADSAFE_ISUPPORTS + NS_DECL_NSIDHCPCLIENT + + explicit nsWindowsDHCPClient( + WindowsNetworkFunctionsWrapper* aNetworkFunctions = + new WindowsNetworkFunctionsWrapper()) + : mNetworkFunctions(aNetworkFunctions){}; + + private: + ~nsWindowsDHCPClient(){}; + WindowsNetworkFunctionsWrapper* mNetworkFunctions; +}; + +} // namespace windowsDHCPClient +} // namespace system +} // namespace toolkit +} // namespace mozilla diff --git a/toolkit/system/windowsDHCPClient/tests/gtest/TestDHCPUtils.cpp b/toolkit/system/windowsDHCPClient/tests/gtest/TestDHCPUtils.cpp new file mode 100644 index 0000000000..d701a31104 --- /dev/null +++ b/toolkit/system/windowsDHCPClient/tests/gtest/TestDHCPUtils.cpp @@ -0,0 +1,306 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "DHCPUtils.h" +#include "gtest/gtest.h" +#include "nsCOMPtr.h" +#include "nsString.h" +#include "nsWindowsDHCPClient.h" + +using namespace mozilla::toolkit::system::windowsDHCPClient; + +class WindowsNetworkFunctionsMock : public WindowsNetworkFunctionsWrapper { + public: + WindowsNetworkFunctionsMock() : mAddressesToReturn(nullptr) { + memset(mOptions, 0, sizeof(char*) * 256); + } + + ULONG GetAdaptersAddressesWrapped( + _In_ ULONG Family, _In_ ULONG Flags, _In_ PVOID Reserved, + _Inout_ PIP_ADAPTER_ADDRESSES AdapterAddresses, + _Inout_ PULONG SizePointer) { + if (*SizePointer < sizeof(*mAddressesToReturn)) { + *SizePointer = sizeof(*mAddressesToReturn); + return ERROR_BUFFER_OVERFLOW; + } + + *SizePointer = sizeof(*mAddressesToReturn); + memcpy(AdapterAddresses, mAddressesToReturn, *SizePointer); + return 0; + } + + DWORD DhcpRequestParamsWrapped(_In_ DWORD Flags, _In_ LPVOID Reserved, + _In_ LPWSTR AdapterName, + _In_ LPDHCPCAPI_CLASSID ClassId, + _In_ DHCPCAPI_PARAMS_ARRAY SendParams, + _Inout_ DHCPCAPI_PARAMS_ARRAY RecdParams, + _In_ LPBYTE Buffer, _Inout_ LPDWORD pSize, + _In_ LPWSTR RequestIdStr) { + mLastRequestedNetworkAdapterName.Assign(AdapterName); + + if (mOptions[RecdParams.Params[0].OptionId] == nullptr) { + RecdParams.Params[0].nBytesData = 0; + } else { + RecdParams.Params[0].Data = Buffer; + size_t lengthOfValue = strlen(mOptions[RecdParams.Params[0].OptionId]); + if (*pSize > lengthOfValue) { + memcpy(Buffer, mOptions[RecdParams.Params[0].OptionId], lengthOfValue); + RecdParams.Params[0].nBytesData = lengthOfValue; + } else { + *pSize = lengthOfValue; + return ERROR_MORE_DATA; + } + } + return 0; + } + + void AddAdapterAddresses(IP_ADAPTER_ADDRESSES& aAddressesToAdd) { + if (mAddressesToReturn == nullptr) { + mAddressesToReturn = &aAddressesToAdd; + return; + } + IP_ADAPTER_ADDRESSES* tail = mAddressesToReturn; + + while (tail->Next != nullptr) { + tail = tail->Next; + } + tail->Next = &aAddressesToAdd; + } + + void SetDHCPOption(uint8_t option, const char* value) { + mOptions[option] = value; + } + + nsString GetLastRequestedNetworkAdapterName() { + return mLastRequestedNetworkAdapterName; + } + + private: + IP_ADAPTER_ADDRESSES* mAddressesToReturn = nullptr; + const char* mOptions[256]; + nsString mLastRequestedNetworkAdapterName; +}; + +class TestDHCPUtils : public ::testing::Test { + protected: + RefPtr<WindowsNetworkFunctionsMock> mMockWindowsFunctions; + nsCString mDefaultAdapterName; + + virtual void SetUp() { + mMockWindowsFunctions = new WindowsNetworkFunctionsMock(); + mDefaultAdapterName.AssignLiteral("my favourite network adapter"); + } + + void Given_DHCP_Option_Is(uint8_t option, const char* value) { + mMockWindowsFunctions.get()->SetDHCPOption(option, value); + } + + void Given_Network_Adapter_Called(IP_ADAPTER_ADDRESSES& adapterAddresses, + const char* adapterName) { + adapterAddresses.AdapterName = const_cast<char*>(adapterName); + adapterAddresses.Next = nullptr; + adapterAddresses.Dhcpv4Server.iSockaddrLength = 0; + adapterAddresses.Dhcpv6Server.iSockaddrLength = 0; + AddAdapterAddresses(adapterAddresses); + } + + void Given_Network_Adapter_Supports_DHCP_V4( + IP_ADAPTER_ADDRESSES& adapterAddresses) { + adapterAddresses.Dhcpv4Server.iSockaddrLength = 4; + } + + void Given_Network_Adapter_Supports_DHCP_V6( + IP_ADAPTER_ADDRESSES& adapterAddresses) { + adapterAddresses.Dhcpv6Server.iSockaddrLength = 12; + } + + void Given_Network_Adapter_Has_Operational_Status( + IP_ADAPTER_ADDRESSES& adapterAddresses, IF_OPER_STATUS operStatus) { + adapterAddresses.OperStatus = operStatus; + } + + private: + void AddAdapterAddresses(IP_ADAPTER_ADDRESSES& aAddressToAdd) { + mMockWindowsFunctions.get()->AddAdapterAddresses(aAddressToAdd); + } +}; + +// following class currently just distinguishes tests of nsWindowsDHCPClient +// from tests of DHCPUtils. +class TestNsWindowsDHCPClient : public TestDHCPUtils {}; + +TEST_F(TestDHCPUtils, TestGetAdaptersAddresses) { + IP_ADAPTER_ADDRESSES adapterAddresses = {}; + Given_Network_Adapter_Called(adapterAddresses, + "my favourite network adapter"); + Given_Network_Adapter_Supports_DHCP_V4(adapterAddresses); + Given_Network_Adapter_Has_Operational_Status(adapterAddresses, + IfOperStatusUp); + + nsCString networkAdapterName; + + ASSERT_EQ(NS_OK, GetActiveDHCPNetworkAdapterName(networkAdapterName, + mMockWindowsFunctions)); + + ASSERT_STREQ(networkAdapterName.Data(), "my favourite network adapter"); +} + +TEST_F(TestDHCPUtils, TestGetAdaptersAddressesNoAvailableNetworks) { + IP_ADAPTER_ADDRESSES adapterAddresses = {}; + Given_Network_Adapter_Called(adapterAddresses, + "my favourite network adapter"); + Given_Network_Adapter_Supports_DHCP_V4(adapterAddresses); + Given_Network_Adapter_Has_Operational_Status(adapterAddresses, + IfOperStatusDown); + + nsCString networkAdapterName; + ASSERT_EQ(NS_ERROR_NOT_AVAILABLE, + GetActiveDHCPNetworkAdapterName(networkAdapterName, + mMockWindowsFunctions)); + + ASSERT_STREQ(networkAdapterName.Data(), ""); +} + +TEST_F(TestDHCPUtils, TestGetAdaptersAddressesNoNetworksWithDHCP) { + IP_ADAPTER_ADDRESSES adapterAddresses = {}; + Given_Network_Adapter_Called(adapterAddresses, + "my favourite network adapter"); + Given_Network_Adapter_Has_Operational_Status(adapterAddresses, + IfOperStatusUp); + + nsCString networkAdapterName; + ASSERT_EQ(NS_ERROR_NOT_AVAILABLE, + GetActiveDHCPNetworkAdapterName(networkAdapterName, + mMockWindowsFunctions)); + + ASSERT_STREQ(networkAdapterName.Data(), ""); +} + +TEST_F(TestDHCPUtils, TestGetAdaptersAddressesSecondNetworkIsAvailable) { + IP_ADAPTER_ADDRESSES adapterAddresses = {}; + Given_Network_Adapter_Called(adapterAddresses, + "my favourite network adapter"); + Given_Network_Adapter_Supports_DHCP_V4(adapterAddresses); + Given_Network_Adapter_Has_Operational_Status(adapterAddresses, + IfOperStatusDown); + + IP_ADAPTER_ADDRESSES secondAdapterAddresses = {}; + Given_Network_Adapter_Called(secondAdapterAddresses, + "my second favourite network adapter"); + Given_Network_Adapter_Supports_DHCP_V6(secondAdapterAddresses); + Given_Network_Adapter_Has_Operational_Status(secondAdapterAddresses, + IfOperStatusUp); + + nsCString networkAdapterName; + ASSERT_EQ(NS_OK, GetActiveDHCPNetworkAdapterName(networkAdapterName, + mMockWindowsFunctions)); + + ASSERT_STREQ(networkAdapterName.Data(), + "my second favourite network adapter"); +} + +TEST_F(TestDHCPUtils, TestGetOption) { + const char* pacURL = "http://pac.com"; + Given_DHCP_Option_Is(1, "My network option"); + Given_DHCP_Option_Is(252, pacURL); + + std::vector<char> optionValue(255, *"originalValue originalValue"); + memcpy(optionValue.data(), "originalValue originalValue", + strlen("originalValue originalValue") + 1); + + uint32_t size = 255; + + nsresult retVal = RetrieveOption(mDefaultAdapterName, 252, optionValue, &size, + mMockWindowsFunctions); + + ASSERT_EQ(strlen(pacURL), size); + ASSERT_STREQ("http://pac.comoriginalValue", optionValue.data()); + ASSERT_EQ(NS_OK, retVal); +} + +TEST_F(TestDHCPUtils, TestGetAbsentOption) { + std::vector<char> optionValue(255); + uint32_t size = 256; + memcpy(optionValue.data(), "originalValue", strlen("originalValue") + 1); + + nsresult retVal = RetrieveOption(mDefaultAdapterName, 252, optionValue, &size, + mMockWindowsFunctions); + + ASSERT_EQ(0, size); + ASSERT_EQ(NS_ERROR_NOT_AVAILABLE, retVal); +} + +TEST_F(TestDHCPUtils, TestGetTooLongOption) { + Given_DHCP_Option_Is(252, "http://pac.com"); + + std::vector<char> optionValue(255); + memcpy(optionValue.data(), "originalValue", strlen("originalValue") + 1); + uint32_t size = 4; + nsresult retVal = RetrieveOption(mDefaultAdapterName, 252, optionValue, &size, + mMockWindowsFunctions); + + ASSERT_STREQ("httpinalValue", optionValue.data()); + ASSERT_EQ(NS_ERROR_LOSS_OF_SIGNIFICANT_DATA, retVal); + ASSERT_EQ(strlen("http://pac.com"), size); +} + +TEST_F(TestNsWindowsDHCPClient, TestGettingOptionThroughNSWindowsDHCPClient) { + IP_ADAPTER_ADDRESSES adapterAddresses = {}; + Given_Network_Adapter_Called(adapterAddresses, + "my favourite network adapter"); + Given_Network_Adapter_Supports_DHCP_V4(adapterAddresses); + Given_Network_Adapter_Has_Operational_Status(adapterAddresses, + IfOperStatusUp); + Given_DHCP_Option_Is(252, "http://pac.com"); + + nsCString optionValue; + nsCOMPtr<nsIDHCPClient> dhcpClient = + new nsWindowsDHCPClient(mMockWindowsFunctions); + nsresult retVal = dhcpClient->GetOption(252, optionValue); + + ASSERT_STREQ("http://pac.com", optionValue.Data()); + ASSERT_STREQ( + L"my favourite network adapter", + mMockWindowsFunctions->GetLastRequestedNetworkAdapterName().Data()); + ASSERT_EQ(NS_OK, retVal); +} + +TEST_F( + TestNsWindowsDHCPClient, + TestGettingOptionThroughNSWindowsDHCPClientWhenNoAvailableNetworkAdapter) { + IP_ADAPTER_ADDRESSES adapterAddresses = {}; + Given_Network_Adapter_Called(adapterAddresses, + "my favourite network adapter"); + Given_Network_Adapter_Has_Operational_Status(adapterAddresses, + IfOperStatusDown); + Given_DHCP_Option_Is(252, "http://pac.com"); + + nsCString optionValue; + nsCOMPtr<nsIDHCPClient> dhcpClient = + new nsWindowsDHCPClient(mMockWindowsFunctions); + nsresult retVal = dhcpClient->GetOption(252, optionValue); + + ASSERT_STREQ("", optionValue.Data()); + ASSERT_EQ(NS_ERROR_NOT_AVAILABLE, retVal); +} + +TEST_F(TestNsWindowsDHCPClient, + TestGettingAbsentOptionThroughNSWindowsDHCPClient) { + IP_ADAPTER_ADDRESSES adapterAddresses = {}; + Given_Network_Adapter_Called(adapterAddresses, + "my favourite network adapter"); + Given_Network_Adapter_Supports_DHCP_V6(adapterAddresses); + Given_Network_Adapter_Has_Operational_Status(adapterAddresses, + IfOperStatusUp); + + nsCString optionValue; + nsCOMPtr<nsIDHCPClient> dhcpClient = + new nsWindowsDHCPClient(mMockWindowsFunctions); + nsresult retVal = dhcpClient->GetOption(252, optionValue); + + ASSERT_STREQ("", optionValue.Data()); + ASSERT_EQ(NS_ERROR_NOT_AVAILABLE, retVal); +} diff --git a/toolkit/system/windowsDHCPClient/tests/gtest/moz.build b/toolkit/system/windowsDHCPClient/tests/gtest/moz.build new file mode 100644 index 0000000000..b87007a353 --- /dev/null +++ b/toolkit/system/windowsDHCPClient/tests/gtest/moz.build @@ -0,0 +1,21 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, you can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Core", "Networking: HTTP") + +UNIFIED_SOURCES += [ + "TestDHCPUtils.cpp", +] + +LOCAL_INCLUDES += [ + "/toolkit/system/windowsDHCPClient", +] + +FINAL_LIBRARY = "xul-gtest" + +if CONFIG["GNU_CXX"]: + CXXFLAGS += ["-Wshadow"] diff --git a/toolkit/system/windowsproxy/ProxyUtils.cpp b/toolkit/system/windowsproxy/ProxyUtils.cpp new file mode 100644 index 0000000000..37af2f8f4a --- /dev/null +++ b/toolkit/system/windowsproxy/ProxyUtils.cpp @@ -0,0 +1,162 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "ProxyUtils.h" + +#include "mozilla/IntegerRange.h" +#include "nsReadableUtils.h" +#include "nsTArray.h" +#include "prnetdb.h" +#include "prtypes.h" + +namespace mozilla { +namespace toolkit { +namespace system { + +/** + * Normalize the short IP form into the complete form. + * For example, it converts "192.168" into "192.168.0.0" + */ +static void NormalizeAddr(const nsACString& aAddr, nsCString& aNormalized) { + nsTArray<nsCString> addr; + ParseString(aAddr, '.', addr); + aNormalized = StringJoin("."_ns, IntegerRange(4), + [&addr](nsACString& dst, const int i) { + if (i < addr.Length()) { + dst.Append(addr[i]); + } else { + dst.Append('0'); + } + }); +} + +static PRUint32 MaskIPv4Addr(PRUint32 aAddr, uint16_t aMaskLen) { + if (aMaskLen == 32) { + return aAddr; + } + return PR_htonl(PR_ntohl(aAddr) & (~0L << (32 - aMaskLen))); +} + +static void MaskIPv6Addr(PRIPv6Addr& aAddr, uint16_t aMaskLen) { + if (aMaskLen == 128) { + return; + } + + if (aMaskLen > 96) { + aAddr.pr_s6_addr32[3] = + PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[3]) & (~0L << (128 - aMaskLen))); + } else if (aMaskLen > 64) { + aAddr.pr_s6_addr32[3] = 0; + aAddr.pr_s6_addr32[2] = + PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[2]) & (~0L << (96 - aMaskLen))); + } else if (aMaskLen > 32) { + aAddr.pr_s6_addr32[3] = 0; + aAddr.pr_s6_addr32[2] = 0; + aAddr.pr_s6_addr32[1] = + PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[1]) & (~0L << (64 - aMaskLen))); + } else { + aAddr.pr_s6_addr32[3] = 0; + aAddr.pr_s6_addr32[2] = 0; + aAddr.pr_s6_addr32[1] = 0; + aAddr.pr_s6_addr32[0] = + PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[0]) & (~0L << (32 - aMaskLen))); + } + + return; +} + +static bool IsMatchMask(const nsACString& aHost, const nsACString& aOverride) { + nsresult rv; + + auto tokenEnd = aOverride.FindChar('/'); + if (tokenEnd == -1) { + return false; + } + + nsAutoCString prefixStr( + Substring(aOverride, tokenEnd + 1, aOverride.Length() - tokenEnd - 1)); + auto maskLen = prefixStr.ToInteger(&rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return false; + } + + nsAutoCString override(aOverride); + NormalizeAddr(Substring(aOverride, 0, tokenEnd), override); + + PRNetAddr prAddrHost; + PRNetAddr prAddrOverride; + if (PR_SUCCESS != + PR_StringToNetAddr(PromiseFlatCString(aHost).get(), &prAddrHost) || + PR_SUCCESS != PR_StringToNetAddr(override.get(), &prAddrOverride)) { + return false; + } + + if (prAddrHost.raw.family == PR_AF_INET && + prAddrOverride.raw.family == PR_AF_INET) { + return MaskIPv4Addr(prAddrHost.inet.ip, maskLen) == + MaskIPv4Addr(prAddrOverride.inet.ip, maskLen); + } else if (prAddrHost.raw.family == PR_AF_INET6 && + prAddrOverride.raw.family == PR_AF_INET6) { + MaskIPv6Addr(prAddrHost.ipv6.ip, maskLen); + MaskIPv6Addr(prAddrOverride.ipv6.ip, maskLen); + + return memcmp(&prAddrHost.ipv6.ip, &prAddrOverride.ipv6.ip, + sizeof(PRIPv6Addr)) == 0; + } + + return false; +} + +static bool IsMatchWildcard(const nsACString& aHost, + const nsACString& aOverride) { + nsAutoCString host(aHost); + nsAutoCString override(aOverride); + + int32_t overrideLength = override.Length(); + int32_t tokenStart = 0; + int32_t offset = 0; + bool star = false; + + while (tokenStart < overrideLength) { + int32_t tokenEnd = override.FindChar('*', tokenStart); + if (tokenEnd == tokenStart) { + // Star is the first character in the token. + star = true; + tokenStart++; + // If the character following the '*' is a '.' character then skip + // it so that "*.foo.com" allows "foo.com". + if (override.FindChar('.', tokenStart) == tokenStart) { + nsAutoCString token(Substring(override, tokenStart + 1, + overrideLength - tokenStart - 1)); + if (host.Equals(token)) { + return true; + } + } + } else { + if (tokenEnd == -1) { + tokenEnd = overrideLength; // no '*' char, match rest of string + } + nsAutoCString token( + Substring(override, tokenStart, tokenEnd - tokenStart)); + offset = host.Find(token, /* aIgnoreCase = */ false, offset); + if (offset == -1 || (!star && offset)) { + return false; + } + star = false; + tokenStart = tokenEnd; + offset += token.Length(); + } + } + + return (star || (offset == static_cast<int32_t>(host.Length()))); +} + +bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride) { + return IsMatchMask(aHost, aOverride) || IsMatchWildcard(aHost, aOverride); +} + +} // namespace system +} // namespace toolkit +} // namespace mozilla diff --git a/toolkit/system/windowsproxy/ProxyUtils.h b/toolkit/system/windowsproxy/ProxyUtils.h new file mode 100644 index 0000000000..547ef643b5 --- /dev/null +++ b/toolkit/system/windowsproxy/ProxyUtils.h @@ -0,0 +1,21 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_toolkit_system_windowsproxy_ProxyUtils_h +#define mozilla_toolkit_system_windowsproxy_ProxyUtils_h + +#include "nsString.h" + +namespace mozilla { +namespace toolkit { +namespace system { + +bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride); + +} // namespace system +} // namespace toolkit +} // namespace mozilla + +#endif // mozilla_toolkit_system_windowsproxy_ProxyUtils_h diff --git a/toolkit/system/windowsproxy/components.conf b/toolkit/system/windowsproxy/components.conf new file mode 100644 index 0000000000..4c8c62fc43 --- /dev/null +++ b/toolkit/system/windowsproxy/components.conf @@ -0,0 +1,13 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +Classes = [ + { + 'cid': '{4e22d3ea-aaa2-436e-ada4-9247de57d367}', + 'contract_ids': ['@mozilla.org/system-proxy-settings;1'], + 'type': 'nsWindowsSystemProxySettings', + }, +] diff --git a/toolkit/system/windowsproxy/moz.build b/toolkit/system/windowsproxy/moz.build new file mode 100644 index 0000000000..8cc1f085f7 --- /dev/null +++ b/toolkit/system/windowsproxy/moz.build @@ -0,0 +1,18 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Core", "Networking") + +TEST_DIRS += ["tests/gtest"] + +SOURCES += ["nsWindowsSystemProxySettings.cpp", "ProxyUtils.cpp"] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +FINAL_LIBRARY = "xul" diff --git a/toolkit/system/windowsproxy/nsWindowsSystemProxySettings.cpp b/toolkit/system/windowsproxy/nsWindowsSystemProxySettings.cpp new file mode 100644 index 0000000000..72ff740788 --- /dev/null +++ b/toolkit/system/windowsproxy/nsWindowsSystemProxySettings.cpp @@ -0,0 +1,247 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include <windows.h> +#include <ras.h> +#include <wininet.h> + +#include "mozilla/ArrayUtils.h" +#include "mozilla/Attributes.h" +#include "nsISystemProxySettings.h" +#include "mozilla/Components.h" +#include "nsPrintfCString.h" +#include "nsNetCID.h" +#include "nsThreadUtils.h" +#include "GeckoProfiler.h" +#include "prnetdb.h" +#include "ProxyUtils.h" + +class nsWindowsSystemProxySettings final : public nsISystemProxySettings { + public: + NS_DECL_THREADSAFE_ISUPPORTS + NS_DECL_NSISYSTEMPROXYSETTINGS + + nsWindowsSystemProxySettings(){}; + + private: + ~nsWindowsSystemProxySettings(){}; + + bool MatchOverride(const nsACString& aHost); + bool PatternMatch(const nsACString& aHost, const nsACString& aOverride); +}; + +NS_IMPL_ISUPPORTS(nsWindowsSystemProxySettings, nsISystemProxySettings) + +NS_IMETHODIMP +nsWindowsSystemProxySettings::GetMainThreadOnly(bool* aMainThreadOnly) { + // bug 1366133: if you change this to main thread only, please handle + // nsProtocolProxyService::Resolve_Internal carefully to avoid hang on main + // thread. + *aMainThreadOnly = false; + return NS_OK; +} + +static void SetProxyResult(const char* aType, const nsACString& aHostPort, + nsACString& aResult) { + aResult.AssignASCII(aType); + aResult.Append(' '); + aResult.Append(aHostPort); +} + +static void SetProxyResultDirect(nsACString& aResult) { + // For whatever reason, a proxy is not to be used. + aResult.AssignLiteral("DIRECT"); +} + +static nsresult ReadInternetOption(uint32_t aOption, uint32_t& aFlags, + nsAString& aValue) { + // Bug 1366133: InternetGetConnectedStateExW() may cause hangs + MOZ_ASSERT(!NS_IsMainThread()); + + DWORD connFlags = 0; + WCHAR connName[RAS_MaxEntryName + 1]; + MOZ_SEH_TRY { + InternetGetConnectedStateExW(&connFlags, connName, + mozilla::ArrayLength(connName), 0); + } + MOZ_SEH_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { return NS_ERROR_FAILURE; } + + INTERNET_PER_CONN_OPTIONW options[2]; + options[0].dwOption = INTERNET_PER_CONN_FLAGS_UI; + options[1].dwOption = aOption; + + INTERNET_PER_CONN_OPTION_LISTW list; + list.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW); + list.pszConnection = + connFlags & INTERNET_CONNECTION_MODEM ? connName : nullptr; + list.dwOptionCount = mozilla::ArrayLength(options); + list.dwOptionError = 0; + list.pOptions = options; + + unsigned long size = sizeof(INTERNET_PER_CONN_OPTION_LISTW); + if (!InternetQueryOptionW(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, + &list, &size)) { + return NS_ERROR_FAILURE; + } + + aFlags = options[0].Value.dwValue; + aValue.Assign(options[1].Value.pszValue); + GlobalFree(options[1].Value.pszValue); + + return NS_OK; +} + +bool nsWindowsSystemProxySettings::MatchOverride(const nsACString& aHost) { + nsresult rv; + uint32_t flags = 0; + nsAutoString buf; + + rv = ReadInternetOption(INTERNET_PER_CONN_PROXY_BYPASS, flags, buf); + if (NS_FAILED(rv)) return false; + + NS_ConvertUTF16toUTF8 cbuf(buf); + + nsAutoCString host(aHost); + int32_t start = 0; + int32_t end = cbuf.Length(); + + // Windows formats its proxy override list in the form: + // server;server;server where 'server' is a server name pattern or IP + // address, or "<local>". "<local>" must be translated to + // "localhost;127.0.0.1". + // In a server name pattern, a '*' character matches any substring and + // all other characters must match themselves; the whole pattern must match + // the whole hostname. + while (true) { + int32_t delimiter = cbuf.FindCharInSet(" ;", start); + if (delimiter == -1) delimiter = end; + + if (delimiter != start) { + const nsAutoCString override(Substring(cbuf, start, delimiter - start)); + if (override.EqualsLiteral("<local>")) { + PRNetAddr addr; + bool isIpAddr = (PR_StringToNetAddr(host.get(), &addr) == PR_SUCCESS); + + // Don't use proxy for local hosts (plain hostname, no dots) + if (!isIpAddr && !host.Contains('.')) { + return true; + } + + if (host.EqualsLiteral("127.0.0.1") || host.EqualsLiteral("::1")) { + return true; + } + } else if (PatternMatch(host, override)) { + return true; + } + } + + if (delimiter == end) break; + start = ++delimiter; + } + + return false; +} + +bool nsWindowsSystemProxySettings::PatternMatch(const nsACString& aHost, + const nsACString& aOverride) { + return mozilla::toolkit::system::IsHostProxyEntry(aHost, aOverride); +} + +nsresult nsWindowsSystemProxySettings::GetPACURI(nsACString& aResult) { + AUTO_PROFILER_LABEL("nsWindowsSystemProxySettings::GetPACURI", OTHER); + nsresult rv; + uint32_t flags = 0; + nsAutoString buf; + + rv = ReadInternetOption(INTERNET_PER_CONN_AUTOCONFIG_URL, flags, buf); + if (!(flags & PROXY_TYPE_AUTO_PROXY_URL)) { + aResult.Truncate(); + return rv; + } + + if (NS_SUCCEEDED(rv)) aResult = NS_ConvertUTF16toUTF8(buf); + return rv; +} + +nsresult nsWindowsSystemProxySettings::GetProxyForURI(const nsACString& aSpec, + const nsACString& aScheme, + const nsACString& aHost, + const int32_t aPort, + nsACString& aResult) { + nsresult rv; + uint32_t flags = 0; + nsAutoString buf; + + rv = ReadInternetOption(INTERNET_PER_CONN_PROXY_SERVER, flags, buf); + if (NS_FAILED(rv) || !(flags & PROXY_TYPE_PROXY)) { + SetProxyResultDirect(aResult); + return NS_OK; + } + + if (MatchOverride(aHost)) { + SetProxyResultDirect(aResult); + return NS_OK; + } + + NS_ConvertUTF16toUTF8 cbuf(buf); + + constexpr auto kSocksPrefix = "socks="_ns; + nsAutoCString prefix; + ToLowerCase(aScheme, prefix); + + prefix.Append('='); + + nsAutoCString specificProxy; + nsAutoCString defaultProxy; + nsAutoCString socksProxy; + int32_t start = 0; + int32_t end = cbuf.Length(); + + while (true) { + int32_t delimiter = cbuf.FindCharInSet(" ;", start); + if (delimiter == -1) delimiter = end; + + if (delimiter != start) { + const nsAutoCString proxy(Substring(cbuf, start, delimiter - start)); + if (proxy.FindChar('=') == -1) { + // If a proxy name is listed by itself, it is used as the + // default proxy for any protocols that do not have a specific + // proxy specified. + // (http://msdn.microsoft.com/en-us/library/aa383996%28VS.85%29.aspx) + defaultProxy = proxy; + } else if (proxy.Find(prefix) == 0) { + // To list a proxy for a specific protocol, the string must + // follow the format "<protocol>=<protocol>://<proxy_name>". + // (http://msdn.microsoft.com/en-us/library/aa383996%28VS.85%29.aspx) + specificProxy = Substring(proxy, prefix.Length()); + break; + } else if (proxy.Find(kSocksPrefix) == 0) { + // SOCKS proxy. + socksProxy = + Substring(proxy, kSocksPrefix.Length()); // "socks=" length. + } + } + + if (delimiter == end) break; + start = ++delimiter; + } + + if (!specificProxy.IsEmpty()) + SetProxyResult("PROXY", specificProxy, + aResult); // Protocol-specific proxy. + else if (!defaultProxy.IsEmpty()) + SetProxyResult("PROXY", defaultProxy, aResult); // Default proxy. + else if (!socksProxy.IsEmpty()) + SetProxyResult("SOCKS", socksProxy, aResult); // SOCKS proxy. + else + SetProxyResultDirect(aResult); // Direct connection. + + return NS_OK; +} + +NS_IMPL_COMPONENT_FACTORY(nsWindowsSystemProxySettings) { + return mozilla::MakeAndAddRef<nsWindowsSystemProxySettings>() + .downcast<nsISupports>(); +} diff --git a/toolkit/system/windowsproxy/tests/gtest/TestProxyBypassRules.cpp b/toolkit/system/windowsproxy/tests/gtest/TestProxyBypassRules.cpp new file mode 100644 index 0000000000..9ccb216388 --- /dev/null +++ b/toolkit/system/windowsproxy/tests/gtest/TestProxyBypassRules.cpp @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "gtest/gtest.h" +#include "ProxyUtils.h" + +using namespace mozilla::toolkit::system; + +TEST(WindowsProxy, TestProxyBypassRules) +{ + EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "*mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "*.mozilla.org"_ns)); + EXPECT_FALSE(IsHostProxyEntry("notmozilla.org"_ns, "*.mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("www.mozilla.org"_ns, "*mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("www.mozilla.org"_ns, "*.mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("www.mozilla.com"_ns, "*.mozilla.*"_ns)); +} + +TEST(WindowsProxy, TestProxyBypassRulesIPv4) +{ + EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.*"_ns)); + EXPECT_FALSE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.2.*"_ns)); + + EXPECT_TRUE(IsHostProxyEntry("10.1.2.3"_ns, "10.0.0.0/8"_ns)); + EXPECT_TRUE(IsHostProxyEntry("192.168.192.1"_ns, "192.168/16"_ns)); + EXPECT_FALSE(IsHostProxyEntry("192.168.192.1"_ns, "192.168/17"_ns)); + EXPECT_TRUE(IsHostProxyEntry("192.168.192.1"_ns, "192.168.128/17"_ns)); + EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.1/32"_ns)); +} + +TEST(WindowsProxy, TestProxyBypassRulesIPv6) +{ + EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/64"_ns)); + EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0000:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/80"_ns)); + EXPECT_FALSE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/80"_ns)); + EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0000:0000:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/96"_ns)); + EXPECT_FALSE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/96"_ns)); +} diff --git a/toolkit/system/windowsproxy/tests/gtest/moz.build b/toolkit/system/windowsproxy/tests/gtest/moz.build new file mode 100644 index 0000000000..46501cdf86 --- /dev/null +++ b/toolkit/system/windowsproxy/tests/gtest/moz.build @@ -0,0 +1,18 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, you can obtain one at http://mozilla.org/MPL/2.0/. + +UNIFIED_SOURCES += [ + "TestProxyBypassRules.cpp", +] + +LOCAL_INCLUDES += [ + "/toolkit/system/windowsproxy", +] + +FINAL_LIBRARY = "xul-gtest" + +if CONFIG["CC_TYPE"] in ("clang", "gcc"): + CXXFLAGS += ["-Wshadow"] |