From d1bf3e4bf2f1e469b4b6423f16b7adeff3bb2c69 Mon Sep 17 00:00:00 2001
From: Dante Ruiz <dante@highfidelity.io>
Date: Sat, 1 Apr 2017 00:58:09 +0100
Subject: [PATCH 1/2] fixed webview mobile view

---
 interface/resources/qml/Browser.qml           |   4 ++
 interface/resources/qml/TabletBrowser.qml     |  10 ++---
 .../qml/controls/.#TabletWebView.qml          | Bin 0 -> 76 bytes
 .../resources/qml/controls/TabletWebView.qml  |  23 +++++-----
 interface/src/Application.cpp                 |   2 +
 .../networking/HFTabletWebEngineProfile.cpp   |  26 +++++++++++
 .../src/networking/HFTabletWebEngineProfile.h |  23 ++++++++++
 .../HFTabletWebEngineRequestInterceptor.cpp   |  42 ++++++++++++++++++
 .../HFTabletWebEngineRequestInterceptor.h     |  24 ++++++++++
 9 files changed, 139 insertions(+), 15 deletions(-)
 create mode 100644 interface/resources/qml/controls/.#TabletWebView.qml
 create mode 100644 interface/src/networking/HFTabletWebEngineProfile.cpp
 create mode 100644 interface/src/networking/HFTabletWebEngineProfile.h
 create mode 100644 interface/src/networking/HFTabletWebEngineRequestInterceptor.cpp
 create mode 100644 interface/src/networking/HFTabletWebEngineRequestInterceptor.h

diff --git a/interface/resources/qml/Browser.qml b/interface/resources/qml/Browser.qml
index bd98e42709..c4e0c85642 100644
--- a/interface/resources/qml/Browser.qml
+++ b/interface/resources/qml/Browser.qml
@@ -33,6 +33,10 @@ ScrollingWindow {
         addressBar.text = webview.url
     }
 
+    function setProfile(profile) {
+        webview.profile = profile;
+    }
+
     function showPermissionsBar(){
         permissionsContainer.visible=true;
     }
diff --git a/interface/resources/qml/TabletBrowser.qml b/interface/resources/qml/TabletBrowser.qml
index 1d49d46c43..312b811928 100644
--- a/interface/resources/qml/TabletBrowser.qml
+++ b/interface/resources/qml/TabletBrowser.qml
@@ -70,10 +70,10 @@ Item {
             width: parent.width
             height: keyboardEnabled && keyboardRaised ? parent.height - keyboard.height : parent.height
             
-            //profile: HFWebEngineProfile {
-                //id: webviewProfile
-                //storageName: "qmlWebEngine"
-            //}
+            profile: HFWebEngineProfile {
+                id: webviewProfile
+                storageName: "qmlWebEngine"
+            }
 
             property string userScriptUrl: ""
             
@@ -159,7 +159,7 @@ Item {
                         return;
                     }
                     var newWindow = component.createObject();
-                    //newWindow.setProfile(webview.profile);
+                    newWindow.setProfile(webview.profile);
                     request.openIn(newWindow.webView);
                     newWindow.eventBridge = web.eventBridge;
                     stackRoot.push(newWindow);
diff --git a/interface/resources/qml/controls/.#TabletWebView.qml b/interface/resources/qml/controls/.#TabletWebView.qml
new file mode 100644
index 0000000000000000000000000000000000000000..ff599d4a294cea1cc4c391e587b48bf1cc302068
GIT binary patch
literal 76
zcmY$iDXz@T$;`{P`~S~{A(0`Ep@booL4hHNp_Cz$p^CwQ!G*z<A(+9NA%wx7A%H=b
b!GggZj6)d=85|k(7|a>W7)%+=7#J7;)@cr?

literal 0
HcmV?d00001

diff --git a/interface/resources/qml/controls/TabletWebView.qml b/interface/resources/qml/controls/TabletWebView.qml
index 70d2539744..890215a714 100644
--- a/interface/resources/qml/controls/TabletWebView.qml
+++ b/interface/resources/qml/controls/TabletWebView.qml
@@ -6,6 +6,7 @@ import "../controls-uit" as HiFiControls
 import "../styles" as HifiStyles
 import "../styles-uit"
 import HFWebEngineProfile 1.0
+import HFTabletWebEngineProfile 1.0
 import "../"
 Item {
     id: web
@@ -23,7 +24,7 @@ Item {
     property bool isDesktop: false
     property WebEngineView view: root
 
-
+    
     Row {
         id: buttons
         HifiConstants { id: hifi }
@@ -136,10 +137,11 @@ Item {
             y: 0
             width: parent.width
             height: keyboardEnabled && keyboardRaised ? (parent.height - keyboard.height) : parent.height
-            //profile: HFWebEngineProfile {
-                //id: webviewProfile
-                //storageName: "qmlWebEngine"
-            //}
+            profile: HFTabletWebEngineProfile {
+                id: webviewTabletProfile
+                storageName: "qmlTabletWebEngine"
+            }
+            
             property WebEngineView webView: root
             function reloadPage() {
                 root.reload();
@@ -188,7 +190,7 @@ Item {
                     console.log("Web Entity JS message: " + sourceID + " " + lineNumber + " " +  message);
                 });
 
-                root.profile.httpUserAgent = "Mozilla/5.0 Chrome (HighFidelityInterface)"
+                root.profile.httpUserAgent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36"   
 
             }
 
@@ -200,7 +202,7 @@ Item {
                 keyboardRaised = false;
                 punctuationMode = false;
                 keyboard.resetShiftMode(false);
-
+                console.log("[DR] -> printing user string " + root.profile.httpUserAgent);
                 // Required to support clicking on "hifi://" links
                 if (WebEngineView.LoadStartedStatus == loadRequest.status) {
                     var url = loadRequest.url.toString();
@@ -214,9 +216,10 @@ Item {
 
             onNewViewRequested:{
                 // desktop is not defined for web-entities
-                if (isDesktop) {
+                if (web.isDesktop) {
                     var component = Qt.createComponent("../Browser.qml");
                     var newWindow = component.createObject(desktop);
+                    newWindow.setProfile(root.profile);
                     request.openIn(newWindow.webView);
                 } else {
                     var component = Qt.createComponent("../TabletBrowser.qml");
@@ -228,7 +231,7 @@ Item {
                         return;
                     }
                     var newWindow = component.createObject();
-                    //newWindow.setProfile(root.profile);
+                    newWindow.setProfile(root.profile);
                     request.openIn(newWindow.webView);
                     newWindow.eventBridge = web.eventBridge;
                     stackRoot.push(newWindow);
@@ -250,7 +253,7 @@ Item {
     }
 
     Component.onCompleted: {
-        stackRoot.isDesktop = (typeof desktop !== "undefined");
+        web.isDesktop = (typeof desktop !== "undefined");
         address = url;
     }
 
diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp
index 25a585ea34..ed6068ece7 100644
--- a/interface/src/Application.cpp
+++ b/interface/src/Application.cpp
@@ -141,6 +141,7 @@
 #include "LODManager.h"
 #include "ModelPackager.h"
 #include "networking/HFWebEngineProfile.h"
+#include "networking/HFTabletWebEngineProfile.h"
 #include "scripting/TestScriptingInterface.h"
 #include "scripting/AccountScriptingInterface.h"
 #include "scripting/AssetMappingsScriptingInterface.h"
@@ -1931,6 +1932,7 @@ void Application::initializeUi() {
     qmlRegisterType<Preference>("Hifi", 1, 0, "Preference");
 
     qmlRegisterType<HFWebEngineProfile>("HFWebEngineProfile", 1, 0, "HFWebEngineProfile");
+    qmlRegisterType<HFTabletWebEngineProfile>("HFTabletWebEngineProfile", 1, 0, "HFTabletWebEngineProfile");
 
     auto offscreenUi = DependencyManager::get<OffscreenUi>();
     offscreenUi->create(_glWidget->qglContext());
diff --git a/interface/src/networking/HFTabletWebEngineProfile.cpp b/interface/src/networking/HFTabletWebEngineProfile.cpp
new file mode 100644
index 0000000000..46634299bb
--- /dev/null
+++ b/interface/src/networking/HFTabletWebEngineProfile.cpp
@@ -0,0 +1,26 @@
+//
+//  HFTabletWebEngineProfile.h
+//  interface/src/networking
+//
+//  Created by Dante Ruiz on 2017-03-31.
+//  Copyright 2017 High Fidelity, Inc.
+//
+//  Distributed under the Apache License, Version 2.0.
+//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
+//
+
+#include "HFTabletWebEngineProfile.h"
+#include "HFTabletWebEngineRequestInterceptor.h"
+
+static const QString QML_WEB_ENGINE_NAME = "qmlTabletWebEngine";
+
+HFTabletWebEngineProfile::HFTabletWebEngineProfile(QObject* parent) : QQuickWebEngineProfile(parent) {
+
+    static const QString WEB_ENGINE_USER_AGENT = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36";
+
+    setHttpUserAgent(WEB_ENGINE_USER_AGENT);
+
+    auto requestInterceptor = new HFTabletWebEngineRequestInterceptor(this);
+    setRequestInterceptor(requestInterceptor);
+}
+
diff --git a/interface/src/networking/HFTabletWebEngineProfile.h b/interface/src/networking/HFTabletWebEngineProfile.h
new file mode 100644
index 0000000000..406cb1a19a
--- /dev/null
+++ b/interface/src/networking/HFTabletWebEngineProfile.h
@@ -0,0 +1,23 @@
+//
+//  HFTabletWebEngineProfile.h
+//  interface/src/networking
+//
+//  Created by Dante Ruiz on 2017-03-31.
+//  Copyright 2017 High Fidelity, Inc.
+//
+//  Distributed under the Apache License, Version 2.0.
+//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
+//
+
+
+#ifndef hifi_HFTabletWebEngineProfile_h
+#define hifi_HFTabletWebEngineProfile_h
+
+#include <QtWebEngine/QQuickWebEngineProfile>
+
+class HFTabletWebEngineProfile : public QQuickWebEngineProfile {
+public:
+    HFTabletWebEngineProfile(QObject* parent = Q_NULLPTR);
+};
+
+#endif // hifi_HFTabletWebEngineProfile_h
diff --git a/interface/src/networking/HFTabletWebEngineRequestInterceptor.cpp b/interface/src/networking/HFTabletWebEngineRequestInterceptor.cpp
new file mode 100644
index 0000000000..7282fb5e3d
--- /dev/null
+++ b/interface/src/networking/HFTabletWebEngineRequestInterceptor.cpp
@@ -0,0 +1,42 @@
+//
+//  HFTabletWebEngineRequestInterceptor.cpp
+//  interface/src/networking
+//
+//  Created by Dante Ruiz on 2017-3-31.
+//  Copyright 2017 High Fidelity, Inc.
+//
+//  Distributed under the Apache License, Version 2.0.
+//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
+//
+
+#include "HFTabletWebEngineRequestInterceptor.h"
+#include <QtCore/QDebug>
+#include <AccountManager.h>
+
+bool isTabletAuthableHighFidelityURL(const QUrl& url) {
+    static const QStringList HF_HOSTS = {
+        "highfidelity.com", "highfidelity.io",
+        "metaverse.highfidelity.com", "metaverse.highfidelity.io"
+    };
+
+    return url.scheme() == "https" && HF_HOSTS.contains(url.host());
+}
+
+void HFTabletWebEngineRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info) {
+    // check if this is a request to a highfidelity URL
+    if (isTabletAuthableHighFidelityURL(info.requestUrl())) {
+        // if we have an access token, add it to the right HTTP header for authorization
+        auto accountManager = DependencyManager::get<AccountManager>();
+
+        if (accountManager->hasValidAccessToken()) {
+            static const QString OAUTH_AUTHORIZATION_HEADER = "Authorization";
+
+            QString bearerTokenString = "Bearer " + accountManager->getAccountInfo().getAccessToken().token;
+            info.setHttpHeader(OAUTH_AUTHORIZATION_HEADER.toLocal8Bit(), bearerTokenString.toLocal8Bit());
+        }
+    }
+
+    static const QString USER_AGENT = "User-Agent";
+    QString tokenString = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36";
+    info.setHttpHeader(USER_AGENT.toLocal8Bit(), tokenString.toLocal8Bit());
+}
diff --git a/interface/src/networking/HFTabletWebEngineRequestInterceptor.h b/interface/src/networking/HFTabletWebEngineRequestInterceptor.h
new file mode 100644
index 0000000000..e38549937e
--- /dev/null
+++ b/interface/src/networking/HFTabletWebEngineRequestInterceptor.h
@@ -0,0 +1,24 @@
+//
+//  HFTabletWebEngineRequestInterceptor.h
+//  interface/src/networking
+//
+//  Created by Dante Ruiz on 2017-3-31.
+//  Copyright 2017 High Fidelity, Inc.
+//
+//  Distributed under the Apache License, Version 2.0.
+//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
+//
+
+#ifndef hifi_HFTabletWebEngineRequestInterceptor_h
+#define hifi_HFTabletWebEngineRequestInterceptor_h
+
+#include <QWebEngineUrlRequestInterceptor>
+
+class HFTabletWebEngineRequestInterceptor : public QWebEngineUrlRequestInterceptor {
+public:
+    HFTabletWebEngineRequestInterceptor(QObject* parent) : QWebEngineUrlRequestInterceptor(parent) {};
+
+    virtual void interceptRequest(QWebEngineUrlRequestInfo& info) override;
+};
+
+#endif // hifi_HFWebEngineRequestInterceptor_h

From 7ca7ad5e0b97b797a9435f490673c7cfc74f649c Mon Sep 17 00:00:00 2001
From: Dante Ruiz <dante@highfidelity.io>
Date: Sat, 1 Apr 2017 01:53:00 +0100
Subject: [PATCH 2/2] removed junk file

---
 .../resources/qml/controls/.#TabletWebView.qml     | Bin 76 -> 0 bytes
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 interface/resources/qml/controls/.#TabletWebView.qml

diff --git a/interface/resources/qml/controls/.#TabletWebView.qml b/interface/resources/qml/controls/.#TabletWebView.qml
deleted file mode 100644
index ff599d4a294cea1cc4c391e587b48bf1cc302068..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 76
zcmY$iDXz@T$;`{P`~S~{A(0`Ep@booL4hHNp_Cz$p^CwQ!G*z<A(+9NA%wx7A%H=b
b!GggZj6)d=85|k(7|a>W7)%+=7#J7;)@cr?