Przeglądaj źródła

HUE-7743 [editor] Enable webworkers in embedded mode

Johan Ahlen 8 lat temu
rodzic
commit
cb7b7e8

+ 11 - 6
desktop/core/src/desktop/templates/ace_sql_location_worker.mako

@@ -18,14 +18,19 @@
   from desktop import conf
 %>
 
+var scriptPrefix = '';
+% if conf.IS_EMBEDDED.get():
+  scriptPrefix = location.href.substring(0, location.href.indexOf('/desktop/workers/'));
+% endif
+
 % if conf.DEV.get():
-importScripts('${ static('desktop/js/autocomplete/sqlParseSupport.js') }' + '?' + Math.random());
-importScripts('${ static('desktop/js/autocomplete/sqlAutocompleteParser.js') }' + '?' + Math.random());
-importScripts('${ static('desktop/js/sqlFunctions.js') }' + '?' + Math.random());
+importScripts(scriptPrefix + '${ static('desktop/js/autocomplete/sqlParseSupport.js') }' + '?' + Math.random());
+importScripts(scriptPrefix + '${ static('desktop/js/autocomplete/sqlAutocompleteParser.js') }' + '?' + Math.random());
+importScripts(scriptPrefix + '${ static('desktop/js/sqlFunctions.js') }' + '?' + Math.random());
 % else:
-importScripts('${ static('desktop/js/autocomplete/sqlParseSupport.js') }');
-importScripts('${ static('desktop/js/autocomplete/sqlAutocompleteParser.js') }');
-importScripts('${ static('desktop/js/sqlFunctions.js') }');
+importScripts(scriptPrefix + '${ static('desktop/js/autocomplete/sqlParseSupport.js') }');
+importScripts(scriptPrefix + '${ static('desktop/js/autocomplete/sqlAutocompleteParser.js') }');
+importScripts(scriptPrefix + '${ static('desktop/js/sqlFunctions.js') }');
 % endif
 
 (function () {

+ 9 - 4
desktop/core/src/desktop/templates/ace_sql_syntax_worker.mako

@@ -18,12 +18,17 @@
   from desktop import conf
 %>
 
+var scriptPrefix = '';
+% if conf.IS_EMBEDDED.get():
+  scriptPrefix = location.href.substring(0, location.href.indexOf('/desktop/workers/'));
+% endif
+
 % if conf.DEV.get():
-importScripts('${ static('desktop/js/autocomplete/sqlParseSupport.js') }' + '?' + Math.random());
-importScripts('${ static('desktop/js/autocomplete/sqlSyntaxParser.js') }' + '?' + Math.random());
+importScripts(scriptPrefix + '${ static('desktop/js/autocomplete/sqlParseSupport.js') }' + '?' + Math.random());
+importScripts(scriptPrefix + '${ static('desktop/js/autocomplete/sqlSyntaxParser.js') }' + '?' + Math.random());
 % else:
-importScripts('${ static('desktop/js/autocomplete/sqlParseSupport.js') }');
-importScripts('${ static('desktop/js/autocomplete/sqlSyntaxParser.js') }');
+importScripts(scriptPrefix + '${ static('desktop/js/autocomplete/sqlParseSupport.js') }');
+importScripts(scriptPrefix + '${ static('desktop/js/autocomplete/sqlSyntaxParser.js') }');
 % endif
 
 (function () {

+ 24 - 0
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -3187,6 +3187,30 @@ ${ sqlSyntaxDropdown.sqlSyntaxDropdown() }
           whenWorkerIsReady(aceSqlLocationWorker, message);
         });
       }
+  % else:
+    var iframe = document.createElement("iframe");
+    iframe.src = typeof adaptHueEmbeddedUrls !== 'undefined' ? adaptHueEmbeddedUrls('/notebook/workers_embedded') : '/notebook/workers_embedded';
+    iframe.name = "workerFrame";
+    iframe.setAttribute('style', 'display: none;');
+    document.body.appendChild(iframe);
+
+    window.addEventListener("message", function (event) {
+      if (event.data.locationWorkerResponse) {
+        huePubSub.publish('ace.sql.location.worker.message', { data: event.data.locationWorkerResponse });
+      }
+      if (event.data.syntaxWorkerResponse) {
+        huePubSub.publish('ace.sql.syntax.worker.message', { data: event.data.syntaxWorkerResponse });
+      }
+    }, false);
+
+    huePubSub.subscribe('ace.sql.location.worker.post', function (message) {
+      iframe.contentWindow.postMessage({ locationWorkerRequest: message }, '*')
+    });
+
+    huePubSub.subscribe('ace.sql.syntax.worker.post', function (message) {
+      iframe.contentWindow.postMessage({ syntaxWorkerRequest: message }, '*')
+    });
+
   % endif
 
 

+ 67 - 0
desktop/libs/notebook/src/notebook/templates/workers_embedded.mako

@@ -0,0 +1,67 @@
+## Licensed to Cloudera, Inc. under one
+## or more contributor license agreements.  See the NOTICE file
+## distributed with this work for additional information
+## regarding copyright ownership.  Cloudera, Inc. licenses this file
+## to you under the Apache License, Version 2.0 (the
+## "License"); you may not use this file except in compliance
+## with the License.  You may obtain a copy of the License at
+##
+##     http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+
+<html>
+<body></body>
+<script type="text/javascript">
+  (function () {
+    if (window.Worker) {
+      var baseUrl = window.location.href.substring(0, window.location.href.indexOf('/notebook/workers_embedded'));
+      // It can take a while before the worker is active
+      var whenWorkerIsReady = function (worker, message) {
+        if (!worker.isReady) {
+          window.clearTimeout(worker.pingTimeout);
+          worker.postMessage({ ping: true });
+          worker.pingTimeout = window.setTimeout(function () {
+            whenWorkerIsReady(worker, message);
+          }, 500);
+        } else {
+          worker.postMessage(message);
+        }
+      };
+
+      // For syntax checking
+      var aceSqlSyntaxWorker = new Worker(baseUrl + '/desktop/workers/aceSqlSyntaxWorker.js?bust=' + Math.random());
+      aceSqlSyntaxWorker.onmessage = function (e) {
+        if (e.data.ping) {
+          aceSqlSyntaxWorker.isReady = true;
+        } else {
+          window.top.postMessage({ syntaxWorkerResponse: e.data }, '*');
+        }
+      };
+
+      // For location marking
+      var aceSqlLocationWorker = new Worker(baseUrl + '/desktop/workers/aceSqlLocationWorker.js?bust=' + Math.random());
+      aceSqlLocationWorker.onmessage = function (e) {
+        if (e.data.ping) {
+          aceSqlLocationWorker.isReady = true;
+        } else {
+          window.top.postMessage({ locationWorkerResponse: e.data }, '*');
+        }
+      };
+
+      window.addEventListener("message", function (event) {
+        if (event.data.locationWorkerRequest) {
+          whenWorkerIsReady(aceSqlLocationWorker, event.data.locationWorkerRequest);
+        }
+        if (event.data.syntaxWorkerRequest) {
+          whenWorkerIsReady(aceSqlSyntaxWorker, event.data.syntaxWorkerRequest);
+        }
+      }, false);
+    }
+  })();
+</script>
+</html>

+ 1 - 0
desktop/libs/notebook/src/notebook/urls.py

@@ -45,6 +45,7 @@ urlpatterns = patterns('notebook.views',
   url(r'^editor_m/?$', 'editor_m', name='editor_m'),
   url(r'^browse/(?P<database>\w+)/(?P<table>\w+)/(?P<partition_spec>.+?)?$', 'browse', name='browse'),
   url(r'^execute_and_watch/?$', 'execute_and_watch', name='execute_and_watch'),
+  url(r'^workers_embedded$', 'workers_embedded', name='workers_embedded'),
 )
 
 # APIs

+ 4 - 0
desktop/libs/notebook/src/notebook/views.py

@@ -22,6 +22,7 @@ from django.core.urlresolvers import reverse
 from django.db.models import Q
 from django.shortcuts import redirect
 from django.utils.translation import ugettext as _
+from django.views.decorators.clickjacking import xframe_options_exempt
 
 from desktop.conf import ENABLE_DOWNLOAD, USE_NEW_EDITOR
 from desktop.lib.django_util import render, JsonResponse
@@ -91,6 +92,9 @@ def notebook(request, is_embeddable=False):
       'is_yarn_mode': is_yarn_mode,
   })
 
+@xframe_options_exempt
+def workers_embedded(request):
+  return render('workers_embedded.mako', request, {})
 
 @check_document_access_permission()
 def notebook_embeddable(request):