From 73968ba1b3c3b5efeb92f70969e40d143eebf3d8 Mon Sep 17 00:00:00 2001 From: ARC Date: Wed, 13 May 2015 14:58:25 -0400 Subject: Added plugin directory as well to make sure you have all you need to compile (hopefully) --- .../src/ubuntu/InAppBrowser.qml | 92 ++++++++++++++++++ .../src/ubuntu/InAppBrowser_escapeScript.js | 29 ++++++ .../src/ubuntu/close.png | Bin 0 -> 461 bytes .../src/ubuntu/inappbrowser.cpp | 105 +++++++++++++++++++++ .../src/ubuntu/inappbrowser.h | 61 ++++++++++++ 5 files changed, 287 insertions(+) create mode 100644 plugins/cordova-plugin-inappbrowser/src/ubuntu/InAppBrowser.qml create mode 100644 plugins/cordova-plugin-inappbrowser/src/ubuntu/InAppBrowser_escapeScript.js create mode 100644 plugins/cordova-plugin-inappbrowser/src/ubuntu/close.png create mode 100644 plugins/cordova-plugin-inappbrowser/src/ubuntu/inappbrowser.cpp create mode 100644 plugins/cordova-plugin-inappbrowser/src/ubuntu/inappbrowser.h (limited to 'plugins/cordova-plugin-inappbrowser/src/ubuntu') diff --git a/plugins/cordova-plugin-inappbrowser/src/ubuntu/InAppBrowser.qml b/plugins/cordova-plugin-inappbrowser/src/ubuntu/InAppBrowser.qml new file mode 100644 index 00000000..781e8a6e --- /dev/null +++ b/plugins/cordova-plugin-inappbrowser/src/ubuntu/InAppBrowser.qml @@ -0,0 +1,92 @@ +/* + * + * Copyright 2013 Canonical Ltd. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + * +*/ +import QtQuick 2.0 +import Ubuntu.Components.Popups 0.1 +import Ubuntu.Components 0.1 +import com.canonical.Oxide 1.0 + +Rectangle { + anchors.fill: parent + id: inappbrowser + property string url1 + Rectangle { + border.color: "black" + width: parent.width + height: urlEntry.height + color: "gray" + TextInput { + id: urlEntry + width: parent.width - closeButton.width + text: url1 + activeFocusOnPress: false + } + Image { + id: closeButton + width: height + x: parent.width - width + height: parent.height + source: "close.png" + MouseArea { + anchors.fill: parent + onClicked: { + root.exec("InAppBrowser", "close", [0, 0]) + } + } + } + } + + property string usContext: "oxide://main-world/2" + + function executeJS(scId, code) { + var req = _view.rootFrame.sendMessage(usContext, "EXECUTE", {code: code}); + + req.onreply = function(response) { + var code = 'cordova.callback(' + scId + ', JSON.parse(\'' + JSON.stringify(response.result) + '\'))'; + console.warn(code); + cordova.javaScriptExecNeeded(code); + console.warn("RESP:" + JSON.stringify(response)); + }; + } + + WebView { + width: parent.width + y: urlEntry.height + height: parent.height - y + url: url1 + id: _view + onLoadingStateChanged: { + root.exec("InAppBrowser", "loadFinished", [_view.loading]) + } + context: WebContext { + id: webcontext + + userScripts: [ + UserScript { + context: usContext + emulateGreasemonkey: true + url: "InAppBrowser_escapeScript.js" + } + ] + } + } +} diff --git a/plugins/cordova-plugin-inappbrowser/src/ubuntu/InAppBrowser_escapeScript.js b/plugins/cordova-plugin-inappbrowser/src/ubuntu/InAppBrowser_escapeScript.js new file mode 100644 index 00000000..07661bb6 --- /dev/null +++ b/plugins/cordova-plugin-inappbrowser/src/ubuntu/InAppBrowser_escapeScript.js @@ -0,0 +1,29 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + * +*/ + +oxide.addMessageHandler("EXECUTE", function(msg) { + var code = msg.args.code; + try { + msg.reply({result: eval(code)}); + } catch(e) { + msg.error("Code threw exception: \"" + e + "\""); + } +}); diff --git a/plugins/cordova-plugin-inappbrowser/src/ubuntu/close.png b/plugins/cordova-plugin-inappbrowser/src/ubuntu/close.png new file mode 100644 index 00000000..56373d1f Binary files /dev/null and b/plugins/cordova-plugin-inappbrowser/src/ubuntu/close.png differ diff --git a/plugins/cordova-plugin-inappbrowser/src/ubuntu/inappbrowser.cpp b/plugins/cordova-plugin-inappbrowser/src/ubuntu/inappbrowser.cpp new file mode 100644 index 00000000..c5a9e64a --- /dev/null +++ b/plugins/cordova-plugin-inappbrowser/src/ubuntu/inappbrowser.cpp @@ -0,0 +1,105 @@ +/* + * + * Copyright 2013 Canonical Ltd. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + * +*/ + +#include +#include + +#include "inappbrowser.h" +#include + +Inappbrowser::Inappbrowser(Cordova *cordova): CPlugin(cordova), _eventCb(0) { +} + +const char code[] = "\ +var component; \ +function createObject() { \ + component = Qt.createComponent(%1); \ + if (component.status == Component.Ready) \ + finishCreation(); \ + else \ + component.statusChanged.connect(finishCreation); \ +} \ +function finishCreation() { \ + CordovaWrapper.global.inappbrowser = component.createObject(root, \ + {root: root, cordova: cordova, url1: %2}); \ +} \ +createObject()"; + +const char EXIT_EVENT[] = "{type: 'exit'}"; +const char LOADSTART_EVENT[] = "{type: 'loadstart'}"; +const char LOADSTOP_EVENT[] = "{type: 'loadstop'}"; +const char LOADERROR_EVENT[] = "{type: 'loaderror'}"; + +void Inappbrowser::open(int cb, int, const QString &url, const QString &, const QString &) { + assert(_eventCb == 0); + + _eventCb = cb; + + QString path = m_cordova->get_app_dir() + "/../qml/InAppBrowser.qml"; + QString qml = QString(code) + .arg(CordovaInternal::format(path)).arg(CordovaInternal::format(url)); + m_cordova->execQML(qml); +} + +void Inappbrowser::show(int, int) { + m_cordova->execQML("CordovaWrapper.global.inappbrowser.visible = true"); +} + +void Inappbrowser::close(int, int) { + m_cordova->execQML("CordovaWrapper.global.inappbrowser.destroy()"); + this->callbackWithoutRemove(_eventCb, EXIT_EVENT); + _eventCb = 0; +} + +void Inappbrowser::injectStyleFile(int scId, int ecId, const QString& src, bool b) { + QString code("(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %1; d.head.appendChild(c);})(document)"); + code = code.arg(CordovaInternal::format(src)); + + injectScriptCode(scId, ecId, code, b); +} + +void Inappbrowser::injectStyleCode(int scId, int ecId, const QString& src, bool b) { + QString code("(function(d) { var c = d.createElement('style'); c.innerHTML = %1; d.body.appendChild(c); })(document)"); + code = code.arg(CordovaInternal::format(src)); + + injectScriptCode(scId, ecId, code, b); +} + +void Inappbrowser::injectScriptFile(int scId, int ecId, const QString& src, bool b) { + QString code("(function(d) { var c = d.createElement('script'); c.src = %1; d.body.appendChild(c);})(document)"); + code = code.arg(CordovaInternal::format(src)); + + injectScriptCode(scId, ecId, code, b); +} + +void Inappbrowser::injectScriptCode(int scId, int, const QString& code, bool) { + m_cordova->execQML(QString("CordovaWrapper.global.inappbrowser.executeJS(%2, %1)").arg(CordovaInternal::format(code)).arg(scId)); +} + +void Inappbrowser::loadFinished(bool status) { + if (!status) { + this->callbackWithoutRemove(_eventCb, LOADSTOP_EVENT); + } else { + this->callbackWithoutRemove(_eventCb, LOADSTART_EVENT); + } +} diff --git a/plugins/cordova-plugin-inappbrowser/src/ubuntu/inappbrowser.h b/plugins/cordova-plugin-inappbrowser/src/ubuntu/inappbrowser.h new file mode 100644 index 00000000..1da4e033 --- /dev/null +++ b/plugins/cordova-plugin-inappbrowser/src/ubuntu/inappbrowser.h @@ -0,0 +1,61 @@ +/* + * + * Copyright 2013 Canonical Ltd. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + * +*/ +#ifndef INAPPBROWSER_H +#define INAPPBROWSER_H + +#include +#include + +class Inappbrowser: public CPlugin { + Q_OBJECT +public: + Inappbrowser(Cordova *cordova); + + virtual const QString fullName() override { + return Inappbrowser::fullID(); + } + + virtual const QString shortName() override { + return "InAppBrowser"; + } + + static const QString fullID() { + return "InAppBrowser"; + } + +public slots: + void open(int cb, int, const QString &url, const QString &windowName, const QString &windowFeatures); + void show(int, int); + void close(int, int); + void injectStyleFile(int cb, int, const QString&, bool); + void injectStyleCode(int cb, int, const QString&, bool); + void injectScriptFile(int cb, int, const QString&, bool); + void injectScriptCode(int cb, int, const QString&, bool); + + void loadFinished(bool status); + +private: + int _eventCb; +}; + +#endif -- cgit v1.2.3