From 51879d8e2f2a9b9209022774ada265164abdc620 Mon Sep 17 00:00:00 2001
From: Piotr Zalewa <piotr@zalewa.info>
Date: Fri, 4 Apr 2014 12:16:01 +0200
Subject: [PATCH] CB-6396 [Firefox OS] Adding basic support

---
 doc/index.md                       |  11 +++
 plugin.xml                         |   9 +++
 src/firefoxos/InAppBrowserProxy.js | 108 +++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 src/firefoxos/InAppBrowserProxy.js

diff --git a/doc/index.md b/doc/index.md
index 5201927..ebc4734 100644
--- a/doc/index.md
+++ b/doc/index.md
@@ -30,6 +30,17 @@ and can't access Cordova APIs.
 
     cordova plugin add org.apache.cordova.inappbrowser
 
+### Firefox OS 
+
+Create __www/manifest.webapp__ as described in 
+[Manifest Docs](https://developer.mozilla.org/en-US/Apps/Developing/Manifest).
+Add relevant permisions.
+
+	"permissions": {
+		"browser": {}
+    }
+    
+
 ## window.open
 
 Opens a URL in a new `InAppBrowser` instance, the current browser
diff --git a/plugin.xml b/plugin.xml
index 13adc8d..0d52960 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -137,5 +137,14 @@
         </js-module>
     </platform>
         
+    <!-- firefoxos -->
+    <platform name="firefoxos">
+        <js-module src="www/inappbrowser.js" name="inappbrowser">
+            <clobbers target="window.open" />
+        </js-module>
+        <js-module src="src/firefoxos/InAppBrowserProxy.js" name="InAppBrowserProxy">
+            <merges target="" />
+        </js-module>
+    </platform>    
 
 </plugin>
diff --git a/src/firefoxos/InAppBrowserProxy.js b/src/firefoxos/InAppBrowserProxy.js
new file mode 100644
index 0000000..fc2388b
--- /dev/null
+++ b/src/firefoxos/InAppBrowserProxy.js
@@ -0,0 +1,108 @@
+/*
+ *
+ * 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.
+ *
+*/
+
+// https://developer.mozilla.org/en-US/docs/WebAPI/Browser
+
+var cordova = require('cordova'),
+    channel = require('cordova/channel'),
+    modulemapper = require('cordova/modulemapper');
+
+var origOpenFunc = modulemapper.getOriginalSymbol(window, 'window.open');
+var browserWrap;
+
+var IABExecs = {
+
+    close: function (win, lose) {
+        if (browserWrap) {
+            browserWrap.parentNode.removeChild(browserWrap);
+            browserWrap = null;
+        }
+    },
+
+    /*
+     * Reveal browser if opened hidden
+     */
+    show: function (win, lose) {
+        console.error('[FirefoxOS] show not implemented');
+    },
+
+    open: function (win, lose, args) {
+        var strUrl = args[0],
+            target = args[1],
+            features = args[2],
+            url,
+            elem;
+
+        if (target === '_system') {
+            origOpenFunc.apply(window, [strUrl, '_blank']);
+        } else if (target === '_blank') {
+            var browserElem = document.createElement('iframe');
+            browserElem.setAttribute('mozbrowser', true);
+            // make this loaded in its own child process
+            browserElem.setAttribute('remote', true);
+            browserElem.setAttribute('src', strUrl);
+            if (browserWrap) {
+                document.body.removeChild(browserWrap);
+            }
+            browserWrap = document.createElement('div');
+            browserWrap.style.position = 'absolute';
+            browserWrap.style.backgroundColor = 'rgba(0,0,0,0.75)';
+            browserWrap.style.color = 'rgba(235,235,235,1.0)';
+            browserWrap.style.width = window.innerWidth + 'px';
+            browserWrap.style.height = window.innerHeight + 'px';
+            browserWrap.style.padding = '10px,0,0,0';
+            browserElem.style.position = 'absolute';
+            browserElem.style.top = '60px';
+            browserElem.style.left = '0px';
+            browserElem.style.height = (window.innerHeight - 60) + 'px';
+            browserElem.style.width = browserWrap.style.width;
+
+            browserWrap.addEventListener('click', function () {
+                setTimeout(function () {
+                    IAB.close();
+                }, 0);
+            }, false);
+            var p = document.createElement('p');
+            p.appendChild(document.createTextNode('close'));
+            // TODO: make all buttons - ← → ×
+            p.style.paddingTop = '10px';
+            p.style.textAlign = 'center';
+            browserWrap.appendChild(p);
+            browserWrap.appendChild(browserElem);
+            document.body.appendChild(browserWrap);
+            // assign browser element to browserWrap for future
+            // reference
+            browserWrap.browser = browserElem;
+        } else {
+            window.location = strUrl;
+        }
+    },
+    injectScriptCode: function (code, bCB) {
+        console.error('[FirefoxOS] injectScriptCode not implemented');
+    },
+    injectScriptFile: function (file, bCB) {
+        console.error('[FirefoxOS] injectScriptFile not implemented');
+    }
+};
+
+module.exports = IABExecs;
+
+require('cordova/firefoxos/commandProxy').add('InAppBrowser', module.exports);