WebBrowser/WebBrowserPage.py

changeset 6140
c20e2d414d0d
parent 6139
d24997c47244
child 6142
304a469455c8
diff -r d24997c47244 -r c20e2d414d0d WebBrowser/WebBrowserPage.py
--- a/WebBrowser/WebBrowserPage.py	Mon Feb 12 18:22:39 2018 +0100
+++ b/WebBrowser/WebBrowserPage.py	Mon Feb 12 19:04:07 2018 +0100
@@ -43,10 +43,11 @@
         malicious web site as determined by safe browsing
     """
     if qVersionTuple() >= (5, 7, 0):
-        # SafeJsWorld = QWebEngineScript.ApplicationWorld
-        SafeJsWorld = QWebEngineScript.MainWorld
+        SafeJsWorld = QWebEngineScript.ApplicationWorld
+        # SafeJsWorld = QWebEngineScript.MainWorld
     else:
         SafeJsWorld = QWebEngineScript.MainWorld
+    UnsafeJsWorld = QWebEngineScript.MainWorld
     
     safeBrowsingAbort = pyqtSignal()
     safeBrowsingBad = pyqtSignal(str, str)
@@ -60,8 +61,6 @@
         super(WebBrowserPage, self).__init__(
             WebBrowserWindow.webProfile(), parent)
         
-        self.__setupWebChannel()
-        
         self.featurePermissionRequested.connect(
             self.__featurePermissionRequested)
         
@@ -81,6 +80,22 @@
         
         if qVersionTuple == (5, 10, 0):
             self.loadProgress.connect(self.__loadProgressSlot)
+        
+        # Workaround for changing webchannel world inside
+        # acceptNavigationRequest not working
+        self.__channelUrl = QUrl()
+        self.__channelWorldId = -1
+        self.__setupChannelTimer = QTimer(self)
+        self.__setupChannelTimer.setSingleShot(True)
+        self.__setupChannelTimer.setInterval(100)
+        self.__setupChannelTimer.timeout.connect(self.__setupChannelTimeout)
+    
+    @pyqtSlot()
+    def __setupChannelTimeout(self):
+        """
+        Private slot to initiate the setup of the web channel.
+        """
+        self.__setupWebChannelForUrl(self.__channelUrl)
     
     @pyqtSlot(int)
     def __loadProgressSlot(self, progress):
@@ -167,6 +182,9 @@
                 enable = True
             self.settings().setAttribute(
                 QWebEngineSettings.JavascriptEnabled, enable)
+            
+            self.__channelUrl = url
+            self.__setupChannelTimer.start()
         
         return result
     
@@ -347,18 +365,30 @@
         """
         return WebHitTestResult(self, pos)
     
-    def __setupWebChannel(self):
+    def __setupWebChannelForUrl(self, url):
         """
         Private method to setup a web channel to our external object.
+        
+        @param url URL for which to setup the web channel
+        @type QUrl
         """
-        oldChannel = self.webChannel()
-        newChannel = QWebChannel(self)
-        ExternalJsObject.setupWebChannel(newChannel, self)
-        self.setWebChannel(newChannel)
+        channel = self.webChannel()
+        if channel is None:
+            channel = QWebChannel(self)
+            ExternalJsObject.setupWebChannel(channel, self)
         
-        if oldChannel:
-            del oldChannel.registeredObjects["eric_object"]
-            del oldChannel
+        worldId = -1
+        if url.scheme() in ("eric", "qthelp"):
+            worldId = self.UnsafeJsWorld
+        else:
+            worldId = self.SafeJsWorld
+        if worldId != self.__channelWorldId:
+            self.__channelWorldId = worldId
+            try:
+                self.setWebChannel(channel, self.__channelWorldId)
+            except TypeError:
+                # pre Qt 5.7.0
+                self.setWebChannel(channel)
     
     def certificateError(self, error):
         """

eric ide

mercurial