WebAPI/PresentationDeviceInfoAPI

From MozillaWiki
Jump to: navigation, search

Introduction

Presentation Device Info API is a certified-app-only API for managing device list. It should be only used in Gaia core apps and browser chrome UI.

Interface

See latest WebIDL definition in mozilla-central.

 dictionary PresentationDeviceInfo {
   DOMString id;
   DOMString name;
   DOMString type;
 };
 
 interface PresentationDeviceInfoManager : EventTarget {
   // notify if any device updated.
   attribute EventHandler ondevicechange;
 
   // retrieve all available device infos
   Promise<sequence<PresentationDeviceInfo>> getAll();
 
   // Force all registered device provider to update device information.
   void forceDiscovery();
 };

Example Usage

In manifest.webapp we need to declare presentation-device-manage permission

 "permissions": {
   "presentation-device-manage": {}
 }

You'll be able to get the API entry via navigator.mozPresentationDeviceInfo

 navigator.mozPresentationDeviceInfo.forceDiscovery();
 
 navigator.mozPresentationDeviceInfo.getAll().then(function(devices) {
   for (let device of devices) {
     console.log(device.type + "[" + device.id + "]: " + device.name); 
   }
 });
 
 navigator.mozPresentationDeviceInfo.addEventListener("devicechange", function(e) {
   let changeType = e.detail.type;
   let device = e.detail.deviceInfo;
   
   switch (changeType) {
     case "add":
       console.log("device added: " + device);
       break;
     case "update":
       console.log("device updated: " + device);
       break;
     case "remove":
       console.log("device removed: " + device);
       break;
   }
 });

Architecture

Architecture overview of Presentation Device Info API

PresentationDeviceInfoManager.js

  • Provide the WebAPI interface

PresentationDeviceInfoManager.jsm

  • Gather device information and dispatch to requested web content
  • Live in chrome process

Device Manager

  • Load registered device provider at start-up automatically
  • Provide device list and firing availability event
  • Live in chrome process

Device Provider

  • Implement device discovery mechanism
  • Handle control channel setup procedure
  • Live in chrome process