[chrome-gnome-shell] api: added onShellSettingChanged event handler.



commit 521faec4a5f99216643d6e684aed4c21de04047a
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Mon Jan 7 17:34:07 2019 +0400

    api: added onShellSettingChanged event handler.
    
    This event handler will react on "disable-user-extensions" and
    "disable-extension-version-validation" settings only for now.

 connector/chrome-gnome-shell.py     | 72 +++++++++++++++++++++++++++----------
 extension/content-script-start.js   |  5 +--
 extension/extension.js              |  9 +++--
 extension/include/constants.js      |  3 +-
 extension/include/sweettooth-api.js |  4 +++
 5 files changed, 69 insertions(+), 24 deletions(-)
---
diff --git a/connector/chrome-gnome-shell.py b/connector/chrome-gnome-shell.py
index 2609536..003bfc9 100755
--- a/connector/chrome-gnome-shell.py
+++ b/connector/chrome-gnome-shell.py
@@ -64,8 +64,11 @@ class ChromeGNOMEShell(Gio.Application):
             else Gio.ApplicationFlags.IS_LAUNCHER | Gio.ApplicationFlags.HANDLES_OPEN
         )
 
+        self.gio_settings = None
         self.shellAppearedId = None
         self.shellSignalId = None
+        self.disableUserExtensionsSignalId = None
+        self.disableVersionCheckSignalId = None
 
         # Set custom exception hook
         # noinspection SpellCheckingInspection
@@ -125,6 +128,14 @@ class ChromeGNOMEShell(Gio.Application):
             if dbus_connection is not None:
                 dbus_connection.signal_unsubscribe(self.shellSignalId)
 
+        if self.disableUserExtensionsSignalId:
+            if self.gio_settings is not None:
+                self.gio_settings.disconnect(self.disableUserExtensionsSignalId)
+
+        if self.disableVersionCheckSignalId:
+            if self.gio_settings is not None:
+                self.gio_settings.disconnect(self.disableVersionCheckSignalId)
+
         self.release()
 
     def default_exception_hook(self, exception_type, value, tb):
@@ -257,6 +268,17 @@ class ChromeGNOMEShell(Gio.Application):
         self.send_message({'signal': name})
         debug('Signal: from %s' % name)
 
+    def on_setting_changed(self, settings, key):
+        if not key in (DISABLE_USER_EXTENSIONS_KEY, EXTENSION_DISABLE_VERSION_CHECK_KEY):
+            return
+
+        debug('on_setting_changed: %s=%s' % (key, settings.get_value(key).unpack()))
+        self.send_message({
+            'signal': 'ShellSettingsChanged',
+            'key': key,
+            'value': settings.get_value(key).unpack()
+        })
+
     # General events
     # noinspection PyUnusedLocal
     def on_hup(self, source, condition, data):
@@ -357,14 +379,17 @@ class ChromeGNOMEShell(Gio.Application):
         else:
             raise Exception("Unknown data type: %s" % type(data))
 
-    def set_shell_boolean(self, key, value):
-        source = Gio.SettingsSchemaSource.get_default()
+    def obtain_gio_settings(self):
+        if not self.gio_settings:
+            source = Gio.SettingsSchemaSource.get_default()
 
-        if source is not None and source.lookup(SHELL_SCHEMA, True) is not None:
-            settings = Gio.Settings.new(SHELL_SCHEMA)
+            if source is not None and source.lookup(SHELL_SCHEMA, True) is not None:
+                self.gio_settings = Gio.Settings.new(SHELL_SCHEMA)
 
-            if key in settings.keys():
-                return settings.set_boolean(key, True if value else False)
+    def set_shell_boolean(self, key, value):
+        self.obtain_gio_settings()
+        if key in self.gio_settings.keys():
+            return self.gio_settings.set_boolean(key, True if value else False)
 
         return False
 
@@ -372,19 +397,18 @@ class ChromeGNOMEShell(Gio.Application):
         debug('Execute: to %s' % request['execute'])
 
         if request['execute'] == 'initialize':
-            source = Gio.SettingsSchemaSource.get_default()
             shell_version = self.shell_proxy.get_cached_property("ShellVersion")
 
-            if source is not None and source.lookup(SHELL_SCHEMA, True) is not None and shell_version is not 
None:
-                settings = Gio.Settings.new(SHELL_SCHEMA)
+            if shell_version is not None:
+                self.obtain_gio_settings()
 
-                if EXTENSION_DISABLE_VERSION_CHECK_KEY in settings.keys():
-                    disable_version_check = settings.get_boolean(EXTENSION_DISABLE_VERSION_CHECK_KEY)
+                if EXTENSION_DISABLE_VERSION_CHECK_KEY in self.gio_settings.keys():
+                    disable_version_check = 
self.gio_settings.get_boolean(EXTENSION_DISABLE_VERSION_CHECK_KEY)
                 else:
                     disable_version_check = False
 
-                if DISABLE_USER_EXTENSIONS_KEY in settings.keys():
-                    disable_user_extensions = settings.get_boolean(DISABLE_USER_EXTENSIONS_KEY)
+                if DISABLE_USER_EXTENSIONS_KEY in self.gio_settings.keys():
+                    disable_user_extensions = self.gio_settings.get_boolean(DISABLE_USER_EXTENSIONS_KEY)
                 else:
                     disable_user_extensions = False
 
@@ -434,6 +458,18 @@ class ChromeGNOMEShell(Gio.Application):
                     None
                 )
 
+            if not self.disableUserExtensionsSignalId:
+                self.obtain_gio_settings()
+                self.disableUserExtensionsSignalId = self.gio_settings.connect(
+                    "changed::%s" % DISABLE_USER_EXTENSIONS_KEY,
+                    self.on_setting_changed)
+
+            if not self.disableVersionCheckSignalId:
+                self.obtain_gio_settings()
+                self.disableVersionCheckSignalId = self.gio_settings.connect(
+                    "changed::%s" % EXTENSION_DISABLE_VERSION_CHECK_KEY,
+                    self.on_setting_changed)
+
         elif request['execute'] == 'installExtension':
             self.dbus_call_response(
                 "InstallRemoteExtension",
@@ -445,8 +481,8 @@ class ChromeGNOMEShell(Gio.Application):
             self.dbus_call_response("ListExtensions", None, "extensions")
 
         elif request['execute'] == 'enableExtension':
-            settings = Gio.Settings.new(SHELL_SCHEMA)
-            uuids = settings.get_strv(ENABLED_EXTENSIONS_KEY)
+            self.obtain_gio_settings()
+            uuids = self.gio_settings.get_strv(ENABLED_EXTENSIONS_KEY)
 
             extensions = []
             if 'extensions' in request:
@@ -464,7 +500,7 @@ class ChromeGNOMEShell(Gio.Application):
                 elif extension['uuid'] in uuids:
                     uuids = [value for value in uuids if value != extension['uuid']]
 
-            settings.set_strv(ENABLED_EXTENSIONS_KEY, uuids)
+            self.gio_settings.set_strv(ENABLED_EXTENSIONS_KEY, uuids)
 
             self.send_message({'success': True})
 
@@ -545,9 +581,9 @@ class ChromeGNOMEShell(Gio.Application):
             None
         )
 
+        self.obtain_gio_settings()
         extensions = result.unpack()[0]
-        settings = Gio.Settings.new(SHELL_SCHEMA)
-        enabled_extensions = settings.get_strv(ENABLED_EXTENSIONS_KEY)
+        enabled_extensions = self.gio_settings.get_strv(ENABLED_EXTENSIONS_KEY)
 
         if extensions:
             http_request = {
diff --git a/extension/content-script-start.js b/extension/content-script-start.js
index 63e5afd..a1ec599 100644
--- a/extension/content-script-start.js
+++ b/extension/content-script-start.js
@@ -1,6 +1,6 @@
 /*
     GNOME Shell integration for Chrome
-    Copyright (C) 2016  Yuri Konotopov <ykonotopov gnome org>
+    Copyright (C) 2016-2019  Yuri Konotopov <ykonotopov gnome org>
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -42,7 +42,8 @@ chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {
                if(
                        sender.id && sender.id === GS_CHROME_ID &&
-                       request && request.signal && [SIGNAL_EXTENSION_CHANGED, 
SIGNAL_SHELL_APPEARED].indexOf(request.signal) !== -1)
+                       request && request.signal &&
+                       [SIGNAL_EXTENSION_CHANGED, SIGNAL_SHELL_APPEARED, 
SIGNAL_SHELL_SETTING_CHANGED].indexOf(request.signal) !== -1)
                {
                        window.postMessage(
                                {
diff --git a/extension/extension.js b/extension/extension.js
index 5994ae0..57cc50d 100644
--- a/extension/extension.js
+++ b/extension/extension.js
@@ -1,6 +1,6 @@
 /*
     GNOME Shell integration for Chrome
-    Copyright (C) 2016-2018  Yuri Konotopov <ykonotopov gnome org>
+    Copyright (C) 2016-2019  Yuri Konotopov <ykonotopov gnome org>
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -83,12 +83,15 @@ var port = chrome.runtime.connectNative(NATIVE_HOST);
 port.onMessage.addListener(function (message) {
        if (message && message.signal)
        {
-               if([SIGNAL_EXTENSION_CHANGED, SIGNAL_SHELL_APPEARED].indexOf(message.signal) !== -1)
+               if([SIGNAL_EXTENSION_CHANGED, SIGNAL_SHELL_APPEARED, 
SIGNAL_SHELL_SETTING_CHANGED].indexOf(message.signal) !== -1)
                {
                        /*
                         * Skip duplicate events. This is happens eg when extension is installed.
                         */
-                       if ((new Date().getTime()) - lastPortMessage.date < 1000 && 
GSC.isSignalsEqual(message, lastPortMessage.message))
+                       if (
+                                       message.signal != SIGNAL_SHELL_SETTING_CHANGED &&
+                                       (new Date().getTime()) - lastPortMessage.date < 1000 && 
GSC.isSignalsEqual(message, lastPortMessage.message)
+                       )
                        {
                                lastPortMessage.date = new Date().getTime();
                                return;
diff --git a/extension/include/constants.js b/extension/include/constants.js
index 5e33d1d..211ebf1 100644
--- a/extension/include/constants.js
+++ b/extension/include/constants.js
@@ -1,6 +1,6 @@
 /*
     GNOME Shell integration for Chrome
-    Copyright (C) 2016-2018  Yuri Konotopov <ykonotopov gnome org>
+    Copyright (C) 2016-2019  Yuri Konotopov <ykonotopov gnome org>
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -28,6 +28,7 @@ MESSAGE_IDLE_PERMISSION_REMOVED       = 'gs-idle-removed';
 SIGNAL_EXTENSION_CHANGED               = 'ExtensionStatusChanged';
 SIGNAL_NOTIFICATION_ACTION             = 'NotificationAction';
 SIGNAL_NOTIFICATION_CLICKED            = 'NotificationClicked';
+SIGNAL_SHELL_SETTING_CHANGED   = 'ShellSettingsChanged';
 SIGNAL_SHELL_APPEARED                  = 'org.gnome.Shell';
 
 EXTENSION_CHANGED_UUID                 = 0;
diff --git a/extension/include/sweettooth-api.js b/extension/include/sweettooth-api.js
index b512a0c..dc32306 100644
--- a/extension/include/sweettooth-api.js
+++ b/extension/include/sweettooth-api.js
@@ -171,6 +171,10 @@ window.SweetTooth = function () {
                                {
                                        apiObject.onshellrestart();
                                }
+                               else if (event.data.request.signal == 'ShellSettingsChanged' && 
apiObject.onShellSettingChanged)
+                               {
+                                       apiObject.onShellSettingChanged(event.data.request.key, 
event.data.request.value);
+                               }
                        }
                }
        }, false);


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