W3C home > Mailing lists > Public > public-secondscreen@w3.org > July 2015

Re: [presentation-api] Allow page to designate a default presentation URL

From: Anton Vayvod via GitHub <sysbot+gh@w3.org>
Date: Thu, 02 Jul 2015 13:11:01 +0000
To: public-secondscreen@w3.org
Message-ID: <issue_comment.created-118027625-1435842661-sysbot+gh@w3.org>
Hey there, we've come up with the following after discussing with 
Mozilla and getting @schien's approval:

```webidl
partial interface NavigatorPresentation {
    attribute PresentationRequest defaultRequest = null;
};

[Constructor(DOMString presentationUrl)]
interface PresentationRequest {
    readonly attribute DOMString url;
    attribute EventHandler onpresentationstart;
    Promise<PresentationSession> joinPresentation(DOMString 
presentationId);
    Promise<PresentationSession> startPresentation();
    Promise<Availability> getAvailability();
};
```

So the sender methods have moved to a separate interface, 
```PresentationRequest```. The page can create one or more instances 
of the interface for different presentation URLs and also use each 
request more than once. If the request is assigned to the 
defaultRequest, the user agent is able to initiate a presentation 
session on behalf of the page and fire ```onpresentationstart``` event
 on the appropriate request instance. The event is also fired when 
```start/joinPresentation()``` succeed along with the promise being 
resolved to ease receiving the new ```PresentationSession``` object by
 the page.

The shortest way to start a presentation would be:

```javascript
presBtn.onclick = function(e) {
    new PresentationRequest(“example.com/presentation.html”)
        .startPresentation().then(handleSession);
}
```

The full example would look like:

```javascript
var presUrl = “www.example.com/presentation.html”;
var mySession;

// One PresentationRequest(presentationUrl) is constructed per each 
// PresentationSession the page wants to receive.
var request = PresentationRequest(presUrl);

// event will be fired if the presentation defined by this request is
// started somehow, by the browser or as a result of 
startPresentation().
// that way if the web author doesn’t care which way the session was 
// started, it can be handled in one place.
request.onpresentationstart = function(evt) {
    handleSession(evt.session);
};

// the browser will use this request to start browser initiated 
presentation
// (unless user disables it)
navigator.presentation.defaultrequest = request;

// try to join an existing presentation known to the user agent
// and the page via local storage
request.joinPresentation(localStorage["presId"])
    .catch(closeSession);

request.getAvailability().then(function(availability) {
    ... 
}).catch (function() {
    ... 
});

document.getElementById("presBtn").onclick = function(e) {
    request.startPresentation()
        .catch(closeSession);
};

var handleSession = function(session) {
  // in case we handle start/joinPresentation promise resolution:
  // if (session === mySession) return;

  // close existing session, if any
  closeSession();
  if (!session)
    return;

  // save presId in localStorage
  localStorage && (localStorage["presId"] = session.presentationId);

  // monitor channel's state
  session.onstatechange = function () {
    if (channel.state == "disconnected")
      closeChannel();
  };

  // register message handler
  session.onmessage = function(evt) {
    console.log("receive message", evt.data);
  };

  // send message to presentation page
  session.send("say hello");
  mySession = session;
};

var closeSession = function() {
  // close old session if exists
  mySession && mySession.close() && mySession = null;
  // remove old presId from localStorage if exists
  localStorage && delete localStorage["presId"];
};
```

-- 
GitHub Notif of comment by avayvod
See 
https://github.com/w3c/presentation-api/issues/26#issuecomment-118027625
Received on Thursday, 2 July 2015 13:11:08 UTC

This archive was generated by hypermail 2.3.1 : Thursday, 2 July 2015 13:11:09 UTC