[epiphany/pgriffis/web-extension/bitwarden: 1/7] WebExtensions: Implement runtime.getPlatformInfo()




commit f83af58c11ff0583917a6ad9bc47f41f585a1d86
Author: Patrick Griffis <pgriffis igalia com>
Date:   Tue Jun 14 13:10:41 2022 -0500

    WebExtensions: Implement runtime.getPlatformInfo()
    
    Part-of: <https://gitlab.gnome.org/GNOME/epiphany/-/merge_requests/1145>

 .../resources/js/webextensions.js                  |  1 +
 src/webextension/api/runtime.c                     | 59 ++++++++++++++++++++++
 2 files changed, 60 insertions(+)
---
diff --git a/embed/web-process-extension/resources/js/webextensions.js 
b/embed/web-process-extension/resources/js/webextensions.js
index df2790151..cbd0f3c55 100644
--- a/embed/web-process-extension/resources/js/webextensions.js
+++ b/embed/web-process-extension/resources/js/webextensions.js
@@ -80,6 +80,7 @@ window.browser.extension.getBackgroundPage = function () {
 
 // browser.runtime is defined in webextensions-common.js
 window.browser.runtime.getBrowserInfo = function (...args) { return ephy_message ('runtime.getBrowserInfo', 
args); };
+window.browser.runtime.getPlatformInfo = function (...args) { return ephy_message 
('runtime.getPlatformInfo', args); };
 window.browser.runtime.connectNative = function (...args) { return ephy_message ('runtime.connectNative', 
args); };
 window.browser.runtime.openOptionsPage = function (...args) { return ephy_message 
('runtime.openOptionsPage', args); };
 window.browser.runtime.setUninstallURL = function (...args) { return ephy_message 
('runtime.setUninstallURL', args); };
diff --git a/src/webextension/api/runtime.c b/src/webextension/api/runtime.c
index 3d1760cf8..26b99aee0 100644
--- a/src/webextension/api/runtime.c
+++ b/src/webextension/api/runtime.c
@@ -51,6 +51,64 @@ runtime_handler_get_browser_info (EphyWebExtension  *self,
   return json_to_string (root, FALSE);
 }
 
+static const char *
+get_os (void)
+{
+  /* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/PlatformOs */
+#if defined(__linux__)
+  return "linux";
+#elif defined(_WIN32)
+  return "win";
+#elif defined(__OpenBSD__) || defined(__FreeBSD__)
+  return "openbsd"; /* MDN documents same for both. */
+#elif defined(__APPLE__)
+  return "mac";
+#else
+  #warning "Unknown OS"
+  return "unknown";
+#endif
+}
+
+static const char *
+get_arch (void)
+{
+  /* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/PlatformArch */
+#if defined (__x86_64__) || defined(_M_X64)
+  return "x86-64";
+#elif defined(__i386__) || defined(_M_IX86)
+  return "x86-32";
+#elif defined(__aarch64__) || defined(__arm__) || defined(_M_ARM64) || defined(_M_ARM)
+  return "arm";
+#else
+  #warning "Unknown architecture"
+  return "unknown";
+#endif
+}
+
+static char *
+runtime_handler_get_platform_info (EphyWebExtension  *self,
+                                   char              *name,
+                                   JSCValue          *args,
+                                   WebKitWebView     *web_view,
+                                   GError           **error)
+{
+  g_autoptr (JsonBuilder) builder = json_builder_new ();
+  g_autoptr (JsonNode) root = NULL;
+
+  json_builder_begin_object (builder);
+  json_builder_set_member_name (builder, "os");
+  json_builder_add_string_value (builder, get_os ());
+  json_builder_set_member_name (builder, "arch");
+  json_builder_add_string_value (builder, get_arch ());
+  json_builder_set_member_name (builder, "nacl_arch");
+  json_builder_add_string_value (builder, get_arch ());
+  json_builder_end_object (builder);
+
+  root = json_builder_get_root (builder);
+
+  return json_to_string (root, FALSE);
+}
+
 static gboolean
 is_empty_object (JSCValue *value)
 {
@@ -144,6 +202,7 @@ runtime_handler_open_options_page (EphyWebExtension  *self,
 
 static EphyWebExtensionSyncApiHandler runtime_sync_handlers[] = {
   {"getBrowserInfo", runtime_handler_get_browser_info},
+  {"getPlatformInfo", runtime_handler_get_platform_info},
   {"openOptionsPage", runtime_handler_open_options_page},
 };
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]