Develop on Firefox OS

Firefox OS Hackaton

Rome 2013/12/07

Created by Luca Greco @ ALCA Società Cooperativa

## **Who am I?**
### **Software Developer** ### @ ### ALCA Società Cooperativa ![ALCA Società Cooperativa](images/alcacoop-logo.png) ## http://alcacoop.it
### **Mozilla Developers Community**, ### **Code Contributor** ### on ## *Firefox OS Simulator* ## *Firefox DevTools*

http://github.com/rpl

http://twitter.com/lucagreco

luca.greco @ alcacoop.it

# Mobile, Apps & Web
## Mobile is REAL
## Apps are TRENDY ![](images/grandma-there-is-an-app.jpg)
## Web is a really ## OPEN platform
## What IF the Web...
![Mozilla](images/mozilla-logo.png)
## develop the openweb ## for the users ![Mozilla](images/firefox-logo.png)
## respect the users privacy ![Lightbeam for Firefox](images/lightbeam-mozorg.jpg)
## Mozilla projects lives in open code repositories planning and brainstorming in open wikis, mailinglists, irc channels... ![Doing Good](images/doing-good.jpg)
# **Firefox** OS
# Mobile OS ### Powered by Web Technologies

Platform Layers

Security Levels

App Types

Hosted or Packaged

## Always bet on ## **"the Web Platform"**
### Firefox OS Apps are ## **OpenWebApps**
### OpenWebApps are # **WebApps**
### we love WebApps...
## a WebApp... ### borns as a white page... like a painting ![a White Page](images/white-page.jpg)
### where we can sketch down ideas... ![Demo App - Fase 01](images/demoapp01.png) e.g. put together app structure mockups using plain HTML, CSS and JS
### define our style... ![Demo App - Fase 02](images/demoapp02.png) e.g. introducing your own styles and/or CSS Frameworks (like Bootstrap, Foundation, TopCoat etc.)
### and then bring it to life! ![web has already a lot of libs and frameworks](images/webapp-libs-and-frameworks.png) e.g. refactoring into better designed app, optionally using a Javascript Framework (like Backbone, Ember, Argular etc.)
## **Firefox OS Apps** are... ## **installable** WebApps
## "Let's make Peter happy" ### **add a manifest.webapp**

manifest.webapp


{
  "name": "MyAppName",
  "version": "1.7",
  "launch_path": "/index.html",
  "description": "...",
  "developer": {
    "name": "...",
    "url": "..."
  },
  "locales": {
    "en-US": {
      "name": "...",
      "description": "...",
    },
  },
  "default_locale": "en-US",
  "icons": {
    "128": "/style/icons/Blank.png"
  }
}
            

{
  "name": "MyAppName",
  "version": "1.7",
  "launch_path": "/index.html",
  "icons": {
    "128": "/style/icons/Blank.png"
  },
  ...,
}
            
*launch_path*, *appcache_path*, *icons urls* are ## **ALWAYS RELATIVE** ## to the App **Origin**
## **Hosted App** ### e.g. relative to ### **https://myapp/** ### or ### **http://localhost:3000/**
## **Packaged App** ### relative to the root of the zip file

App Origin:

protocol, domain, port


https://myapp.com
http://localhost:9000
app://550e8400-e29b-41d4-a716-446655440000/
            
## OK... WHY? - classic install/uninstall/update app lifecycle - app metadata - app permissions - apps marketplace and apps discovery - new experimental feature (e.g. WebActivities, Simple Push)
## **Packaged App** ## vs ## **Hosted App**
## **Packaged App:** - no internet origin - use a special **app://** protocol - generated (at install time) uuid as app domain - packaged apps MAY be **privileged** - all resources packaged into a *zip archive*

myapp.zip:


manifest.webapp
index.html
js/
   app.js
css/
   app.css
res/
   ...
vendor/
   ...
            
## **Hosted App** - regular webapp security restrictions - served from an HTTP Server - **1 hosted app x origin**
### **different urls, same origin** ## http://name.domain/folder1 ## http://name.domain/folder2

manifest.webapp

Content Type:

application/x-web-app-manifest+json

DOH!

Simple Python Static HTTP Server


import SimpleHTTPServer
import SocketServer

SimpleHTTPServer.SimpleHTTPRequestHandler. \
  extensions_map['.webapp'] = \
  'application/x-web-app-manifest+json'

httpd = SocketServer.TCPServer(("", 3000), \
  SimpleHTTPServer.SimpleHTTPRequestHandler)

httpd.serve_forever()
            
## **Hosted App** ### works offline using AppCache, ### IndexedDB, localStorage, ### and online/offline events
## MANIFEST.APPCACHE

                CACHE MANIFEST
                # v1 2011-08-14

                CACHE:
                index.html
                cache.html
                style.css
                image1.png

                # Use from network if available
                NETWORK:
                network.html

                # Fallback content
                FALLBACK:
                / fallback.html
            

In the Simple Python Static HTTP Server


  SimpleHTTPServer.SimpleHTTPRequestHandler. \
    extensions_map['.appcache] = \
      'text/cache-manifest'
            

In the manifest.webapp


{
  "appcache_path": "/manifest.appcache",
  ...
}
            

DO NOT CACHE THE MANIFESTS!!!

e.g. on Apache:

ExpiresByType text/cache-manifest "access plus 0 seconds"
ExpiresByType application/x-web-app-manifest+json "access plus 0"
            
## **App Manager** ## Install and Debug Apps ### on **Firefox OS 1.2** ### Simulator and Device
## Once upon a time... ![Firefox OS 1.1 Simulator Overview](images/simulator-overview-4.0.png) ### Firefox OS 1.1 Simulator #### (codename *r2d2b2g*)
### starting from Firefox for Desktop 26... ![App Manager DevTool](images/appmanager-apps-panel.png) main *Firefox OS Simulator 1.1* Dashboard Features are now included into the integrated Firefox DevTools
![App Manager addons](images/appmanager-addons.png) *integrated ADB* and *Simulator Window* components from *Firefox OS Simulator 1.1* are now splitted into separated addons
### Remote DevTools ![App Manager Remote DevTools](images/appmanager-debug.png) Starting from Firefox 26 and Firefox OS 1.2, all integrated DevTool are now fully supported on a connected device or simulator.
### Installed App and Permissions Table: ![App Manager Permissions Table](images/appmanager-permissions.png)
## App Development Workflow - develop most of the code on your Desktop WebBrowser - refine Firefox OS-specific features on Simulator and Devices using the AppManager
## Using the app manager - Open the App Manager - Install ADB Helper and Firefox OS Simulator 1.2 addons - Add an OpenWebApp to the Apps Panel - add Packaged Apps by dir (no need to zip it manually) - add Hosted Apps by url (needs a reachable HTTP Server) - Install/Run on the Simulator/Device - Inspect and Debug using integrated Remote DevTools
## **Firefox OS Apps** are... ## enabled to use new ## **experimental** ## webapis and ## permissions
**Hosted App**: experimental WebAPIs available - webactivities - network information - vibration / webfm - proximity / ambient light - screen orientation - alarm - simple push - persona / webpayments
**Hosted App**: standard WebAPIs - location - device orientation - battery - fullscreen - multitouch - websockets - webgl (*with care on Firefox OS*) - webrtc (*under construction*)
**Hosted App:** ### Security Restrictions - no privileged APIs - no cross domain requests (but you can use CORS) - same "webapp in a browser tab" constraints (mostly) - ... but more relaxed storage limits (appcache, indexeddb, localstorage)
**Packaged App**: all standard and experimental WebAPIs + extended **priviliged** WebAPIs available - cross domain requests (systemXHR) - tcp sockets - contacts - device storage - file handle - browser
**Priviliged and Certified Packaged App**: enforce a CSP (content security policy) by default: - no remote scripts injection - no inline scripts in html tags/attributes - no javascript URIs - eval is not allowed
## ask for... # permissions ## (if necessary)

manifest.webapp permissions


...
"type": "privileged",
"permissions": {
  "contacts": {
    "description": "Required for ...",
    "access": "readcreate"
    },
  "alarms": {
    "description": "Required to ..."
  }
}
...
            
## check and use... # experimental webapis

if (typeof MozActivity == "function") {
  ...
} else {
  console.log("FALLBACK: call WebActivity" +
              " 'pick image'");
  return;
}
            

WebActivities API


var pick = new MozActivity({
  name: "pick",
  data: {
    type: ["image/png",
           "image/jpeg"]
  }
});
            

WebActivities are mostly like Android Intents

WebActivities API


pick.onsuccess = function () {
  var img = document.createElement("img");
  img.src = window.URL.
    createObjectURL(this.result.blob);
  var viewer = document.
    querySelector("#image-viewer");
  viewer.appendChild(img);
};

pick.onerror = function () { ... };
            

Declaring new WebActivities


  ...
  "activities": {
    "pick": {
      "filters": {
        "type": ["image/*", "image/jpeg"]
      },
      "disposition": "inline",
      "returnValue": true,
      "href": "/index.html#pick"
    },
    ...
            
# More Docs & Examples
## Examples - [Firefox OS Shopping List App](https://github.com/alcacoop/firefoxos-shopping-list) - [Firefox OS Boilerplate App](https://github.com/robnyman/Firefox-OS-Boilerplate-App) - [firefoxos-simulator-walkthrough example app](https://github.com/rpl/firefoxos-simulator-walkthrough) - [FxOS Stub App](https://github.com/Jaxo/fxosstub) - [buildingfirefoxos.com - gaia building blocks and style guide](http://buildingfirefoxos.com/)
## Articles - [Firefox OS articles on hacks.mozilla.org](https://hacks.mozilla.org/category/firefox-os/) - [FxOS Stub App walkthrough](http://jaxo.github.io/fxosstub/docs/index.html)
### MDN App Developers Doc Pages - [Apps start page on developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/Apps) - [Apps Development API Reference](https://developer.mozilla.org/en-US/Apps/Reference) - [WebAPI start page on developer.mozilla.org](https://developer.mozilla.org/en-US/docs/WebAPI) - [Using the application cache on developer.mozilla.org](https://developer.mozilla.org/en/docs/HTML/Using_the_application_cache) - [Apps Content Security Policy on developer.mozilla.org](https://developer.mozilla.org/en-US/Apps/CSP)
### Firefox Marketplace Docs & Tools - [Developers start page on marketplace.firefox.com](https://marketplace.firefox.com/developers) - [Marketplace App Validator](https://marketplace.firefox.com/developers/validator) - [Marketplace Test Payments Generator](https://marketplace.firefox.com/developers/in-app-keys/)
## MDN Firefox OS Doc Pages - [Firefox OS start page on developer.mozilla.org](https://developer.mozilla.org/en/docs/Mozilla/Firefox_OS) - [Firefox OS Platform Architecture](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Platform/Architecture) - [Firefox OS Security Model on developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model)

Questions?

http://learn.alcacoop.it/2013/FirefoxOSHackaton