discovery-api/Overview.src.html
author Rich Tibbett <richt@opera.com>
Thu, 17 Jan 2013 15:08:37 +0100
changeset 332 e19dc529aacc
parent 331 43b139afeba9
child 371 8b03e17da083
permissions -rw-r--r--
Minor editorial changes to DIAL protocol support section
<!DOCTYPE html>
<!--

  THIS IS THE WORKING VERSION OF THE CURRENT SPECIFICATION!

  This specification is built using ReSpec.js <http://dev.w3.org/2009/dap/ReSpec.js/documentation.html>

  From time to time it's necessary to HTML5 Tidy this document using the tool @ <http://w3c.github.com/tidy-html5/>.

  The command used to format this document (Overview.src.html) is as follows (replacing all = signs with - signs!):

  @> tidy ==new-blocklevel-tags section ==char-encoding utf8 ==tidy-mark no ==indent yes ==indent-spaces 2 ==indent-attributes yes ==wrap 120 ==fix-bad-comments yes -m Overview.src.html

  To publish a new compiled version (Overview.html), we need to open this file (Overview.src.html) in any web browser.
  Once it has loaded we press 'Ctrl + Shift + S' (or 'Cmd' + 'Shift' + 'S' on Mac) and then select
  'Save as HTML (Source)' from the resulting menu.

  We then replace Overview.html with the produced HTML source of this process.

  Next we run HTML5 Tidy over our new Overview.html file with the following command (replacing all = signs with - signs!):

  @> tidy ==new-blocklevel-tags section ==char-encoding utf8 ==tidy-mark no ==indent yes ==indent-spaces 2 ==indent-attributes yes ==wrap 120 ==hide-comments yes -m Overview.html

  Now the specification is ready to be published :)

-->
<html>
  <head>
    <title>
      Network Service Discovery
    </title>
    <meta http-equiv='Content-Type'
          content='text/html; charset=utf-8'>
    <script type="text/javascript"
          class='remove'>
var respecConfig = {
          specStatus:   "ED",
          //publishDate:  "2012-10-04",
          shortName:    "discovery-api",
          edDraftURI:   "http://dvcs.w3.org/hg/dap/raw-file/tip/discovery-api/Overview.html",
          previousMaturity: "FPWD",
          previousPublishDate: "2012-08-07",
          editors: [
            {
              name:       "Rich Tibbett",
              //url:        "http://richt.me/",
              company:    "Opera Software ASA",
              companyURL: "http://opera.com/"
            },
            {
              name:       "Clarke Stevens",
              //url:      "",
              company:    "CableLabs",
              companyURL: "http://cablelabs.com/"
            }
          ],
          noIDLIn:      true,
          wg:           "Device APIs Working Group",
          wgURI:        "http://www.w3.org/2009/dap/",
          wgPublicList: "public-device-apis",
          wgPatentURI:  "http://www.w3.org/2004/01/pp-impl/43696/status"
      };
    </script>
    <script src='http://www.w3.org/Tools/respec/respec-w3c-common'
          type="text/javascript"
          class='remove'
          async="">
</script>
    <style type="text/css">
/* Custom ReSpec CSS (by Rich Tibbett) */

      /* Add better spacing to sections */
      section, .section { margin-bottom: 2em; }

      /* Reduce note & issue render size */
      .note, .issue { font-size:0.8em; }

      /* Add addition spacing to <ol> and <ul> for rule definition */
      ol.rule li, ul.rule li { padding:0.6em; }

      pre.widl { border: solid thin; background: #EEEEEE; color: black; padding: 0.5em 1em; position: relative; }
      pre.widl :link, pre.widl :visited { color: #000; background: transparent; }
      pre.widl:before { content: "IDL"; font: bold small sans-serif; padding: 0.5em; background: white; position: absolute; top: 0; margin: -1px 0 0 -4em; width: 1.5em; border: thin solid; border-radius: 0 0 0 0.5em }

      div.example { border: solid thin red; background: #F7DFE5; color: black; padding: 0.5em 1em; position: relative; margin: 1em 0 1em 4.6em; width: auto; }
      div.example:before { content: "EXAMPLE"; font: bold small sans-serif; padding: 0.5em; background: red; color: white; position: absolute; top: 0; margin: -1px 0 0 -7.6em; width: 5em; border: thin solid red; border-radius: 0 0 0 0.5em }

      dl.domintro { color: green; margin: 2em 0 2em 2em; padding: 0.5em 1em; border: none; background: #DDFFDD; }
      hr + dl.domintro, div.impl + dl.domintro { margin-top: 2.5em; margin-bottom: 1.5em; }
      dl.domintro dt, dl.domintro dt * { color: black; text-decoration: none; }
      dl.domintro dd { margin: 0.5em 0 1em 2em; padding: 0; }
      dl.domintro dd p { margin: 0.5em 0; }
      dl.domintro code {font-size: inherit; font-style: italic; }
      dl.domintro:before { display: table; margin: -1em -0.5em 0.5em auto; width: auto; content: 'This box is non-normative. Implementation requirements are given below this box.'; color: red; border: solid 2px; background: white; padding: 0 0.25em; }

      table { border-collapse:collapse; border-style:hidden hidden none hidden }
      table thead { border-bottom:solid }
      table tbody th:first-child { border-left:solid }
      table td, table th { border-left:solid; border-right:solid; border-bottom:solid thin; vertical-align:top; padding:0.2em }
    </style>
  </head>
  <body>
    <section id='abstract'>
      <p>
        This specification defines a mechanism for an HTML document to discover and subsequently communicate with
        <abbr title="Hypertext Transfer Protocol">HTTP</abbr>-based services advertised via common discovery protocols
        within the current network.
      </p>
    </section>
    <section id='sotd'>
      <p>
        This document represents the early consensus of the group on the scope and features of the proposed API.
      </p>
    </section>
    <section class="informative">
      <h3>
        Introduction
      </h3>
      <p>
        To enable Web pages to connect and communicate with Local-networked Services provided over HTTP, this
        specification introduces the <a href="#navigatornetworkservice"><code>NavigatorNetworkService</code></a>
        interface.
      </p>
      <p>
        Using this <abbr title="Application Programming Interface">API</abbr> consists of requesting a well-known
        service type, known by developers and advertised by Local-networked Devices. User authorization, where the user
        connects the web page to discovered services, is expected before the web page is able to interact
        with any Local-networked Services.
      </p>
      <p>
        A web page creates a request to obtain connectivity to services running in the network by specifying a
        well-known discovery service type that it wishes to interact with.
      </p>
      <p>
        The user agent, having captured all advertised services on the network from the <a>service discovery
        mechanisms</a> included in this recommendation, attempts to match the requested service type to a discovered
        service according to the processing described herein.
      </p>
      <p>
        If a service connectivity request is successful then the Web page is provided with the necessary information to
        communicate with the authorized Local-networked Service. If the request fails then the Web page will receive an
        error callback containing an error code describing the cause of Local-networked Service connectivity failure.
      </p>
      <p>
        Once connected to a Local-networked Service the Web page can send requests and receive responses to the
        Local-networked Service via the messaging format and appropriate channel inferred from the service type
        authorized via the provided API. The Web page, once connected, can also receive service-pushed events, in the
        messaging format supported by the Local-networked Device, if such event subscription functionality is provided
        by the connected Local-networked Service.
      </p>
      <div class="example">
        <p>
          Example of requesting a DNS-SD advertised service:
        </p>
        <hr>
        <pre class="highlight">
function showServices( services ) {
  // Show a list of all the services provided to the web page
  for(var i = 0, l = services.length; i &lt; l; i++) console.log( services[i].name );
}

navigator.getNetworkServices('zeroconf:_boxee-jsonrpc._tcp', showServices);
</pre>
      </div>
      <div class="example">
        <p>
          Example of requesting a UPnP advertised service, also handling error conditions:
        </p>
        <hr>
        <pre class="highlight">
function showServices( services ) {
  // Show a list of all the services provided to the web page
  for(var i = 0, l = services.length; i &lt; l; i++) console.log( services[i].name );
}

function error( e ) {
  console.log( "Error occurred: " + e.code );
}

navigator.getNetworkServices('upnp:urn:schemas-upnp-org:service:ContentDirectory:1', showServices, error);
</pre>
      </div>
      <div class="example">
        <p>
          Example of requesting either a DNS-SD or UPnP advertised service:
        </p>
        <hr>
        <pre class="highlight">
function showServices( services ) {
  // Show a list of all the services provided to the web page (+ service type)
  for(var i = 0, l = services.length; i &lt; l; i++)
     console.log( services[i].name + '(' + services[i].type + ')' );
}

navigator.getNetworkServices([
  'zeroconf:_boxee-jsonrpc._tcp',
  'upnp:urn:schemas-upnp-org:service:ContentDirectory:1'
], showServices);
</pre>
      </div>
      <p>
        For more detailed examples see the <a href="#examples">Examples</a> section.
      </p>
    </section>
    <section id='conformance'>
      <p>
        Requirements phrased in the imperative as part of algorithms (such as "strip any leading space characters" or
        "return false and abort these steps") are to be interpreted with the meaning of the key word ("must", "should",
        "may", etc) used in introducing the algorithm.
      </p>
      <p>
        Some conformance requirements are phrased as requirements on attributes, methods or objects. Such requirements
        are to be interpreted as requirements on user agents.
      </p>
      <p>
        Conformance requirements phrased as algorithms or specific steps MAY be implemented in any manner, so long as
        the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be
        easy to follow, and not intended to be performant.)
      </p>
      <p>
        The only conformance class defined by this specification is a <dfn>user agent</dfn>.
      </p>
      <p>
        User agents MAY impose implementation-specific limits on otherwise unconstrained inputs, e.g. to prevent denial
        of service attacks, to guard against running out of memory, or to work around platform-specific limitations.
      </p>
      <p>
        When support for a feature is disabled (e.g. as an emergency measure to mitigate a security problem, or to aid
        in development, or for performance reasons), user agents MUST act as if they had no support for the feature
        whatsoever, and as if the feature was not mentioned in this specification. For example, if a particular feature
        is accessed via an attribute in a Web IDL interface, the attribute itself would be omitted from the objects
        that implement that interface - leaving the attribute on the object but making it return null or throw an
        exception is insufficient.
      </p>
      <section>
        <h3>
          Dependencies
        </h3>This specification relies on several other underlying specifications.
        <dl>
          <dt>
            HTML
          </dt>
          <dd>
            Many fundamental concepts from HTML are used by this specification. [[!HTML5]]
          </dd>
          <dt>
            WebIDL
          </dt>
          <dd>
            The IDL blocks in this specification use the semantics of the WebIDL specification. [[!WEBIDL]]
          </dd>
        </dl>
      </section>
    </section>
    <section>
      <h3>
        Terminology
      </h3>
      <p>
        The construction "a <code>Foo</code> object", where <code>Foo</code> is actually an interface, is sometimes
        used instead of the more accurate "an object implementing the interface <code>Foo</code>".
      </p>
      <p>
        The term DOM is used to refer to the API set made available to scripts in Web applications, and does not
        necessarily imply the existence of an actual <code>Document</code> object or of any other <code>Node</code>
        objects as defined in the DOM Core specifications. [[!DOM4]]
      </p>
      <p>
        An IDL attribute is said to be <em>getting</em> when its value is being retrieved (e.g. by author script), and
        is said to be <em>setting</em> when a new value is assigned to it.
      </p>
      <p>
        A <dfn>valid service type</dfn> is any of the following:
        <ul>
           <li>
              a string that begins with <code>upnp:</code> or <code>zeroconf:</code>
              followed by one or more characters in the ranges U+0021, U+0023 to U+0027, U+002A to U+002B, U+002D to U+002E,
              U+0030 to U+0039, U+0041 to U+005A, U+005E to U+007E.
           </li>
           <li>
              a string that begins with <code>dial:</code> followed by an integer version.
           </li>
        </ul>
      </p>
      <p>
        A <a>valid service type</a> provided in the <code>type</code> attribute of the <a href=
        "#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a> method will be matched against the
        services currently contained in the <a>list of available service records</a> according to the algorithms
        defined in this specification.
      </p>
      <p>
        A <dfn>user-agent generated callback url</dfn> is a Local-network accessible URL endpoint that a <a>user
        agent</a> generates and maintains for receiving HTTP NOTIFY requests from UPnP Event sources. It is only
        required when the user agent implements UPnP Service Discovery as defined in this specification.
      </p>
    </section>
    <section>
      <h2>
        Requesting networked services
      </h2>
      <pre class="widl">
[Supplemental, NoInterfaceObject]
interface <dfn id="navigatornetworkservice">NavigatorNetworkService</dfn> {
  // Obtain a Local-networked Service
  void <a href="#dom-navigator-getnetworkservices">getNetworkServices</a>( in any type,
                           in <a href=
"#navigatornetworkservicesuccesscallback">NavigatorNetworkServiceSuccessCallback</a> successCallback,
                           in optional <a href=
"#navigatornetworkserviceerrorcallback">NavigatorNetworkServiceErrorCallback</a> errorCallback );
};
<a class="externalDFN"
     href=
     "http://www.whatwg.org/specs/web-apps/current-work/complete/timers.html#navigator">Navigator</a> implements <a href=
     "#navigatornetworkservice">NavigatorNetworkService</a>;

[Callback=FunctionOnly, NoInterfaceObject]
interface <dfn id="navigatornetworkservicesuccesscallback">NavigatorNetworkServiceSuccessCallback</dfn> {
  void handleEvent( in <a href="#networkservices">NetworkServices</a> services );
};

[NoInterfaceObject]
interface <dfn id="navigatornetworkserviceerror">NavigatorNetworkServiceError</dfn> {
  const unsigned short <a href="#dom-navigatornetworkserviceerror-permission_denied">PERMISSION_DENIED_ERR</a> = 1;
  const unsigned short <a href="#dom-navigatornetworkserviceerror-unknown_type_prefix">UNKNOWN_TYPE_PREFIX_ERR</a> = 2;
  readonly attribute unsigned short <a href="#dom-navigatornetworkserviceerror-code">code</a>;
};

[Callback=FunctionOnly, NoInterfaceObject]
interface <dfn id="navigatornetworkserviceerrorcallback">NavigatorNetworkServiceErrorCallback</dfn> {
  void handleEvent( in <a href="#navigatornetworkserviceerror">NavigatorNetworkServiceError</a> error );
};
</pre>
      <section>
        <h2>
          Methods
        </h2>
        <dl class="domintro">
          <dt>
            <var title="">window</var> . <code title="dom-navigator"><a href=
            "http://www.whatwg.org/specs/web-apps/current-work/complete/timers.html#navigator">navigator</a></code> .
            <code title="dom-navigator-getNetworkServices"><a href=
            "#dom-navigator-getnetworkservices">getNetworkServices</a></code> ( <var title="">type</var> , <var title=
            "">successCallback</var> [, <var title="">errorCallback</var> ] )
          </dt>
          <dd>
            <p>
              Prompts the user to select discovered network services that have advertised support for the
              requested service type.
            </p>
            <p>
              The <var title="">type</var> argument contains one or more <a>valid service type</a> tokens that the web
              page would like to interact with.
            </p>
            <p>
              If the user accepts, the <var title="">successCallback</var> is invoked, with zero or more <a href=
              "#networkservice"><code>NetworkService</code></a> objects as its argument.
            </p>
            <p>
              If the user declines, the <var title="">errorCallback</var> (if any) is invoked.
            </p>
          </dd>
        </dl>
        <div>
          <p>
            When the <dfn id="dom-navigator-getnetworkservices"
               title="dom-navigator-getnetworkservices"><code>getNetworkServices(type, successCallback[,
               errorCallback])</code></dfn> method is called, the <a>user agent</a> MUST run the following steps:
          </p>
          <ol class="rule">
            <li>Let <var>requested control types</var> be initially set to an empty array.
            </li>
            <li>If <var>type</var> is an array consisting of one or more <a>valid service type</a> tokens, then let
            <var>requested control types</var> by the value of <var>type</var>, removing any non-<a>valid service
            type</a> tokens from the resulting array.
            </li>
            <li>If <var>type</var> is a string consisting of one <a>valid service type</a> token, then let
            <var>requested control types</var> be an array containing one item with a value of <var>type</var>.
            </li>
            <li>If <var>requested control types</var> is an array that contains at least one or more <a title=
            "valid service type">valid service type</a> tokens then continue to the step labeled <em>process</em>
            below. Otherwise, the <a>user agent</a> MUST <a href=
            "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
                  class="externalDFN">queue a task</a> to invoke <var>errorCallback</var>, if it is provided and is an
                  object of type <code>Function</code>, with a new <a href=
                  "#navigatornetworkserviceerror"><code>NavigatorNetworkServiceError</code></a> object whose <a href=
                  "#dom-navigatornetworkserviceerror-code"><code>code</code></a> attribute has the numeric value 2
                  (<a href=
                  "#dom-navigatornetworkserviceerror-unknown_type_prefix"><code>UNKNOWN_TYPE_PREFIX_ERR</code></a>) as
                  its argument, abort any remaining steps and return.
            </li>
            <li>
              <em>Process</em>: Let <var>services found</var> be an empty array.
            </li>
            <li>For each <var>available service</var> in the <a>list of available service records</a> run the following
            steps:
              <ol class="rule">
                <li>For each <var>requested control type</var> in <var>requested control types</var>: If <var>available
                service</var>'s <code>type</code> attribute equals the <var>requested control type</var> then let <var>
                  matched service</var> equal the value of <var>available service</var> and continue at the step
                  labeled <var>attach</var> below.
                </li>
                <li>Continue at the next <var>available service</var>.
                </li>
                <li>
                  <em>Attach</em>: If <var>matched service</var> is not empty then run the following steps:
                  <ol class="rule">
                    <li>Let <var>new service object</var> be a new <a href=
                    "#networkservice"><code>NetworkService</code></a> object, mapping the parameters of <var>matched
                    service</var> to this new object where possible.
                    </li>
                    <li>Append <var>new service object</var> to the <var>services found</var> array.
                    </li>
                  </ol>
                </li>
              </ol>
            </li>
            <li>Return, and run the remaining steps asynchronously.
            </li>
            <li>Optionally, e.g. based on a previously-established user preference, for security reasons, or due to
            platform limitations, the <a>user agent</a> MAY <a href=
            "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
                  class="externalDFN">queue a task</a> to invoke <var>errorCallback</var>, if it is provided and is an
                  object of type <code>Function</code>, with a new <a href=
                  "#navigatornetworkserviceerror"><code>NavigatorNetworkServiceError</code></a> object whose <a href=
                  "#dom-navigatornetworkserviceerror-code"><code>code</code></a> attribute has the numeric value 1
                  (<a href=
                  "#dom-navigatornetworkserviceerror-permission_denied"><code>PERMISSION_DENIED_ERR</code></a>) as its
                  argument, abort any remaining steps and return.
            </li>
            <li>If <var>services found</var> is not an empty array then the <a>user agent</a> MUST prompt the user in a
              user-agent-specific manner for permission to provide
            the <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/browsers.html#entry-script"
                  class="externalDFN">entry script</a>'s <a href=
                  "http://www.whatwg.org/specs/web-apps/current-work/complete/origin-0.html#origin"
                  class="externalDFN">origin</a> with an array of <a href=
                  "#networkservice"><code>NetworkService</code></a> objects representing the <a>user-authorized</a> subset
                  of <var>services found</var>.
              <p>
                If the user grants permission to access one or more networked services then the <a>user agent</a>
                SHOULD include an "ongoing local-network communication" indicator.
              </p>
              <p>
                If the user denies permission, then the <a>user agent</a> MUST <a href=
                "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
                   class="externalDFN">queue a task</a> to invoke <var>errorCallback</var>, if it is provided and is an
                   object of type <code>Function</code>, with a new <a href=
                   "#navigatornetworkserviceerror"><code>NavigatorNetworkServiceError</code></a> object whose <a href=
                   "#dom-navigatornetworkserviceerror-code"><code>code</code></a> attribute has the numeric value 1
                   (<a href=
                   "#dom-navigatornetworkserviceerror-permission_denied"><code>PERMISSION_DENIED_ERR</code></a>) as its
                   argument, abort any remaining steps and return.
              </p>
              <p>
                If the user never responds, this algorithm stalls on this step.
              </p>
            </li>
            <li>Let <var>services</var> be an empty array.
            </li>
            <li>
               If <var>services found</var> is not an empty array then set <var>services</var> to be an array of one or more <a href=
            "#networkservice"><code>NetworkService</code></a> objects for which the user granted permission above - known as the
            current objects <dfn>user-authorized</dfn> services.
            </li>
            <li>For each Object <var>service</var> in <var>services</var>, if any, run the following sub-steps:
              <ol class="rule">
                <li>Add the <var>service</var>'s <code>url</code> parameter to the <a>entry script origin's
                  <abbr title="Uniform Resource Locator">URL</abbr> whitelist</a>.
                </li>
                <li>If <var>service</var>'s <code>type</code> parameter begins with the DOMString "<code>upnp:</code>"
                and the <var>service</var>'s <code>eventsUrl</code> parameter is not empty then <a>setup a UPnP Events
                Subscription</a> for <var>service</var>.
                </li>
              </ol>
            </li>
            <li>Let <var>services manager</var> be a new <a href="#networkservices"><code>NetworkServices</code></a>
            object.
            </li>
            <li>Set <var>services manager</var>'s <a href=
            "#dom-networkservices-servicesavailable"><code>servicesAvailable</code></a> attribute to the number of
            items currently found in the <a>list of available service records</a> whose <code>type</code> property
            matches any of the tokens requested in <var>requested control types</var>.
            </li>
            <li>Add <var>services</var>, if any, to the <var>services manager</var> object as its collection of <a>indexed
            properties</a>. If <var>services</var> is an empty array then the <var>services manager</var> does not have any
            <var>indexed properties</var>.
            </li>
            <li>Set <var>services manager</var>'s <a href="#dom-networkservices-length"><code>length</code></a>
            attribute to the number of items in <var>services</var>.
            </li>
            <li>Add <var>services manager</var> to the <a>list of active service managers</a>.
            </li>
            <li>The <a>user agent</a> MUST <a href=
            "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
                  class="externalDFN">queue a task</a> to invoke <var>successCallback</var> with <var>services
                  manager</var> as its argument.
            </li>
          </ol>
          <p>
            The <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#task-source"
               class="externalDFN">task source</a> for these <a href=
               "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#concept-task"
               class="externalDFN">tasks</a> is the <a href=
               "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#user-interaction-task-source"
               class="externalDFN">user interaction task source</a>.
          </p>
          <p>
            When a <a href="#networkservice"><code>NetworkService</code></a> object is provided to a Web page, the
            <a>user agent</a> MUST add the <code>url</code> property to the <dfn>entry script origin's URL
            whitelist</dfn>. This list enables the Web page to override and initiate cross-site resource requests
            towards these URLs, and any sub-resources of these URLs, within the current <a href=
            "http://www.whatwg.org/specs/web-apps/current-work/complete/browsers.html#entry-script"
               class="externalDFN">entry script</a>'s <a href=
               "http://www.whatwg.org/specs/web-apps/current-work/complete/origin-0.html#origin"
               class="externalDFN">origin</a> via various existing mechanisms (e.g. Web Sockets, Server-Sent Events,
               Web Messaging, XMLHttpRequest).
          </p>
          <p>
            If the user navigates away from the
            <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/browsers.html#entry-script"
                  class="externalDFN">entry script</a>'s <a href=
                  "http://www.whatwg.org/specs/web-apps/current-work/complete/origin-0.html#origin"
                  class="externalDFN">origin</a> then the <a>user agent</a> <em class=
            "ct">MUST</em> remove all previously whitelisted urls from the <a>entry script origin's URL whitelist</a>.
            There is no persistence to network service selections provided to a web page. It is not possible to access
            a previously white-listed networked service without the necessary user authorization in all of the
            following cases:
          </p>
          <ul>
            <li>If the current script is reloaded at any point in the same or different window.
            </li>
            <li>if the current script reinvokes the <a href=
            "#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a> method at any point in its
            execution.
            </li>
            <li>If the user navigates forward or back in their history to reload the current page.
            </li>
            <li>If a script is running in a different origin.
            </li>
          </ul>
        </div>
      </section>
      <section>
        <h3>
          Error Handling
        </h3>
        <dl class="domintro">
          <dt>
            <var title="">error</var> . <code title="dom-NavigatorNetworkServiceError-code"><a href=
            "#dom-navigatornetworkserviceerror-code">code</a></code>
          </dt>
          <dd>
            <p>
              Returns the current error's error code. At the current time, this will be <code>1</code> or
              <code>2</code>, for which the corresponding error constants <a href=
              "#dom-navigatornetworkserviceerror-permission_denied"><code>PERMISSION_DENIED_ERR</code></a> and <a href=
              "#dom-navigatornetworkserviceerror-unknown_type_prefix"><code>UNKNOWN_TYPE_PREFIX_ERR</code></a> are
              defined.
            </p>
          </dd>
        </dl>
        <p>
          The <dfn id="dom-navigatornetworkserviceerror-code"
             title="dom-navigatornetworkserviceerror-code"><code>code</code></dfn> attribute of a <a href=
             "#navigatornetworkserviceerror"><code>NavigatorNetworkServiceError</code></a> object <em class=
             "ct">MUST</em> return the code for the error, which will be one of the following:
        </p>
        <dl>
          <dt>
            <dfn id="dom-navigatornetworkserviceerror-permission_denied"
                title="dom-navigatornetworkserviceerror-permission_denied"><code>PERMISSION_DENIED_ERR</code></dfn>
                (numeric value 1)
          </dt>
          <dd>
            The user or user agent denied the page permission to access any services.
          </dd>
          <dt>
            <dfn id="dom-navigatornetworkserviceerror-unknown_type_prefix"
                title="dom-navigatornetworkserviceerror-unknown_type_prefix"><code>UNKNOWN_TYPE_PREFIX_ERR</code></dfn>
                (numeric value 2)
          </dt>
          <dd>
            No <a>valid service type</a> tokens were provided in the method invocation.
          </dd>
        </dl>
      </section>
    </section>
    <section>
      <h2>
        Obtaining networked services
      </h2>
      <p>
        The <a href="#networkservices"><code>NetworkServices</code></a> interface represents a collection of zero
        or more <dfn>indexed properties</dfn> that are
        each a <a>user-authorized</a> <a href="#networkservice"><code>NetworkService</code></a> object.
      </p>
      <p>
        A <a href="#networkservices"><code>NetworkServices</code></a> object is the top level success callback
        parameter from a call to
        <a href="#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a>.
      </p>
      <pre class="widl">
[NoInterfaceObject]
interface <dfn id="networkservices">NetworkServices</dfn> {
  readonly attribute unsigned long    <a href="#dom-networkservices-length">length</a>;
  getter <a href="#networkservice">NetworkService</a> (unsigned long index);
  <a href="#networkservice">NetworkService</a>? <a href=
"#dom-networkservices-getservicebyid">getServiceById</a>(DOMString id);

  readonly attribute unsigned long    <a href="#dom-networkservices-servicesavailable">servicesAvailable</a>;

  // event handler attributes
           attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
     class="externalDFN">EventHandler</a>     <a href="#dom-networkservices-onserviceavailable">onserviceavailable</a>;
           attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
     class="externalDFN">EventHandler</a>     <a href=
     "#dom-networkservices-onserviceunavailable">onserviceunavailable</a>;

};

<a href="#networkservices">NetworkServices</a> implements <a href=
"http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-eventtarget"
     class="externalDFN">EventTarget</a>;
</pre>
      <section>
        <h2>
          Attributes
        </h2>
        <dl class="domintro">
          <dt>
            <code title="dom-networkservices-length"><a href="#dom-networkservices-length">length</a></code>
          </dt>
          <dd>
            <p>
              Returns the current number of items in the current object's collection of <a href=
              "#networkservice"><code>NetworkService</code></a> objects.
            </p>
          </dd>
          <dt>
            <code title="dom-networkservices-servicesavailable"><a href=
            "#dom-networkservices-servicesavailable">servicesAvailable</a></code>
          </dt>
          <dd>
            <p>
              Returns the current number of items matching one of the app-requested <a>valid service type</a> tokens in
              the <a>list of available service records</a>.
            </p>
          </dd>
        </dl>
        <div>
          <p>
            The <dfn id="dom-networkservices-length"><code>length</code></dfn> attribute MUST return the number of
            <a href="#networkservice"><code>NetworkService</code></a> objects represented by the collection.
          </p>
          <p>
            The <dfn id="dom-networkservices-servicesavailable"><code>servicesAvailable</code></dfn> attribute MUST
            return the number of services in the <a>list of available service records</a> whose <code>type</code>
            attribute matches any of the <a>valid service type</a> tokens that was initially used to create the current
            <a href="#networkservices"><code>NetworkServices</code></a> object.
          </p>
        </div>
      </section>
      <section>
        <h2>
          Methods
        </h2>
        <dl class="domintro">
          <dt>
            <code title="networkservices-getter"><a href="#networkservices">services</a></code> [ <var title=
            "">index</var> ]
          </dt>
          <dd>
            <p>
              Returns the specified <a href="#networkservice"><code>NetworkService</code></a> object.
            </p>
          </dd>
          <dt>
            <code title="networkservices-getter"><a href="#networkservices">services</a></code> . <code title=
            "dom-networkservices-getservicebyid"><a href=
            "#dom-networkservices-getservicebyid">getServiceById</a></code> ( <var title="">id</var> )
          </dt>
          <dd>
            <p>
              Returns the <a href="#networkservice"><code>NetworkService</code></a> object with the given identifier,
              or null if no service has that identifier.
            </p>
          </dd>
        </dl>
        <p>
          A <a href="#networkservices"><code>NetworkServices</code></a> object represents the current collection of zero
          or more <a href="#networkservice"><code>NetworkService</code></a> objects - its <a>indexed properties</a>. A <a href=
          "#networkservices"><code>NetworkServices</code></a> object is <span>immutable</span> meaning that
          <a>indexed properties</a> cannot be added and <a>indexed properties</a> cannot be removed for the lifetime of
          a <a href="#networkservices"><code>NetworkServices</code></a> object.
        </p>
        <p class="note">
          Each service in a <a href="#networkservices"><code>NetworkServices</code></a> object thus has an index; the
          first has the index 0, and each subsequent service is numbered one higher than the previous one.
        </p>
        <p>
          The <a href=
          "http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#supported-property-indices"
             class="externalDFN">supported property indices</a> of <a href=
             "#networkservices"><code>NetworkServices</code></a> objects at any instant are the numbers from zero to
             the number of the <a href="#networkservice"><code>NetworkService</code></a> objects in the collection
             minus one.
        </p>
        <p>
          To <a href=
          "http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#determine-the-value-of-an-indexed-property"
             class="externalDFN">determine the value of an indexed property</a> for a given index <var>index</var> in a
             <a href="#networkservices"><code>NetworkServices</code></a> object the user agent MUST return the <a href=
             "#networkservice"><code>NetworkService</code></a> object that represents the <var>index</var>th item in
             the collection.
        </p>
        <p>
          The <dfn id="dom-networkservices-getservicebyid"><code>getServiceById(id)</code></dfn> method <em class=
          "ct">MUST</em> return the first <a href="#networkservice"><code>NetworkService</code></a> object in the
          collection whose <a href="#dom-networkservice-id"><code>id</code></a> attribute is equal to the value of the
          <var>id</var> argument provided. When no <a href="#networkservice"><code>NetworkService</code></a> objects
          match the given argument, the method MUST return null.
        </p>
        <p>
          Services available within the local network can connect and disconnect at different times during the
          execution of a web page. A <a>user agent</a> can inform a web page when the state of networked services
          matching the requested <a>valid service type</a> change. Web pages can use this information to enable in-page
          experiences for communicating the state of networked services with the ability to change the particular
          service or set of services the page is connected to by re-invoking the <a href=
          "#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a> method.
        </p>
      </section>
      <section>
        <h2>
          Events
        </h2>
        <p>
          The following are the event handlers (and their corresponding event handler event types) that <em class=
          "ct">MUST</em> be supported, as IDL attributes, by all objects implementing the <a href=
          "#networkservices"><code>NetworkServices</code></a> interface:
        </p>
        <table border="1">
          <thead>
            <tr>
              <th>
                <span title="event handlers">Event handler</span>
              </th>
              <th>
                <span>Event handler event type</span>
              </th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>
                <dfn id="dom-networkservices-onserviceavailable"
                    title="dom-NetworkServices-onserviceavailable"><code>onserviceavailable</code></dfn>
              </td>
              <td>
                <a href="#event-serviceavailable"><code>serviceavailable</code></a>
              </td>
            </tr>
            <tr>
              <td>
                <dfn id="dom-networkservices-onserviceunavailable"
                    title="dom-NetworkServices-onserviceunavailable"><code>onserviceunavailable</code></dfn>
              </td>
              <td>
                <a href="#event-serviceunavailable"><code>serviceunavailable</code></a>
              </td>
            </tr>
          </tbody>
        </table>
      </section>
    </section>
    <section>
      <h2>
        Communicating with a networked service
      </h2>
      <p>
        The <a href="#networkservice"><code>NetworkService</code></a> interface is used to provide a set of connection
        information for an HTTP service endpoint and if available, service events, running on a networked device.
      </p>
      <pre class="widl">
[NoInterfaceObject]
interface <dfn id="networkservice">NetworkService</dfn> {
  readonly attribute DOMString        <a href="#dom-networkservice-id">id</a>;
  readonly attribute DOMString        <a href="#dom-networkservice-name">name</a>;
  readonly attribute DOMString        <a href="#dom-networkservice-type">type</a>;
  readonly attribute DOMString        <a href="#dom-networkservice-url">url</a>;
  readonly attribute DOMString        <a href="#dom-networkservice-config">config</a>;

  readonly attribute boolean          <a href="#dom-networkservice-online">online</a>;

  // event handler attributes
           attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
     class="externalDFN">EventHandler</a>     <a href="#dom-networkservice-onserviceonline">onserviceonline</a>;
           attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
     class="externalDFN">EventHandler</a>     <a href="#dom-networkservice-onserviceoffline">onserviceoffline</a>;

           attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
     class="externalDFN">EventHandler</a>     <a href="#dom-networkservice-onnotify">onnotify</a>;
};

<a href="#networkservice">NetworkService</a> implements <a href=
"http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-eventtarget"
     class="externalDFN">EventTarget</a>;
</pre>
      <section>
        <h2>
          Attributes
        </h2>
        <dl class="domintro">
          <dt>
            <var title="">service</var> . <code title="dom-networkservice-id"><a href=
            "#dom-networkservice-id">id</a></code>
          </dt>
          <dd>
            <p>
              A unique identifier for the given user-selected service instance.
            </p>
          </dd>
          <dt>
            <var title="">service</var> . <code title="dom-networkservice-name"><a href=
            "#dom-networkservice-name">name</a></code>
          </dt>
          <dd>
            <p>
              The name of the user-selected service.
            </p>
          </dd>
          <dt>
            <var title="">service</var> . <code title="dom-networkservice-type"><a href=
            "#dom-networkservice-type">type</a></code>
          </dt>
          <dd>
            <p>
              The <a>valid service type</a> token value of the user-selected service.
            </p>
          </dd>
          <dt>
            <var title="">service</var> . <code title="dom-networkservice-url"><a href=
            "#dom-networkservice-url">url</a></code>
          </dt>
          <dd>
            <p>
              The control URL endpoint (including any required port information) of the user-selected control service
              that has been added to the <a>entry script origin's URL whitelist</a>.
            </p>
          </dd>
          <dt>
            <var title="">service</var> . <code title="dom-networkservice-config"><a href=
            "#dom-networkservice-config">config</a></code>
          </dt>
          <dd>
            <p>
              The configuration information associated with the service depending on the requested service type.
            </p>
          </dd>
        </dl>
        <p>
          The <dfn id="dom-networkservice-id"><code>id</code></dfn> attribute is a unique identifier for the service.
          The same service provided at different times or on different objects MUST have the same <a href=
          "#dom-networkservice-id"><code>id</code></a> value.
        </p>
        <p>
          The <dfn id="dom-networkservice-name"><code>name</code></dfn> attribute represents a human-readable title for
          the service.
        </p>
        <p>
          The <dfn id="dom-networkservice-type"><code>type</code></dfn> attribute reflects the value of the <a>valid
          service type</a> of the service.
        </p>
        <p>
          The <dfn id="dom-networkservice-url"><code>url</code></dfn> attribute is an <a href=
          "http://www.w3.org/TR/html5/urls.html#absolute-url"
             class="externalDFN">absolute URL</a> pointing to the root HTTP endpoint for the service that has been
             added to the <a>entry script origin's URL whitelist</a>. Web pages can subsequently use this value for
             implicit cross-document messaging via various existing mechanisms (e.g. Web Sockets, Server-Sent Events,
             Web Messaging, XMLHttpRequest).
        </p>
        <p>
          The <dfn id="dom-networkservice-config"><code>config</code></dfn> attribute provides the raw configuration
          information extracted from the given network service.
        </p>
      </section>
      <section>
        <h3>
          States
        </h3>
        <dl class="domintro">
          <dt>
            <var title="">service</var> . <code title="dom-networkservice-online"><a href=
            "#dom-networkservice-online">online</a></code>
          </dt>
          <dd>
            <p>
              Returns <code>true</code> if the service is reporting that it is accessible on the local network or
              <code>false</code> if the service is reporting that it is no longer accessible (temporarily or
              permanently) on the local network.
            </p>
          </dd>
        </dl>
        <p>
          The <dfn id="dom-networkservice-online"><code>online</code></dfn> attribute indicates whether the service is
          reporting itself as being either <var>online</var>, and therefore accessible on the local network, in which
          case this attribute will return <code>true</code> or, <var>offline</var>, and therefore not accessible on the
          local network, either temporarily or permanently, in which case this attribute will return
          <code>false</code>. This attribute MUST default to <code>true</code>.
        </p>
      </section>
      <section>
        <h3>
          Events
        </h3>
        <p>
          The following are the event handlers (and their corresponding event handler event types) that <em class=
          "ct">MUST</em> be supported, as IDL attributes, by all objects implementing the <a href=
          "#networkservice"><code>NetworkService</code></a> interface:
        </p>
        <table border="1">
          <thead>
            <tr>
              <th>
                <span title="event handlers">Event handler</span>
              </th>
              <th>
                <span>Event handler event type</span>
              </th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>
                <dfn id="dom-networkservice-onnotify"
                    title="dom-NetworkService-onnotify"><code>onnotify</code></dfn>
              </td>
              <td>
                <a href="#event-notify"><code>notify</code></a>
              </td>
            </tr>
            <tr>
              <td>
                <dfn id="dom-networkservice-onserviceonline"
                    title="dom-NetworkService-onserviceonline"><code>onserviceonline</code></dfn>
              </td>
              <td>
                <a href="#event-serviceonline"><code>serviceonline</code></a>
              </td>
            </tr>
            <tr>
              <td>
                <dfn id="dom-networkservice-onserviceoffline"
                    title="dom-NetworkService-onserviceoffline"><code>onserviceoffline</code></dfn>
              </td>
              <td>
                <a href="#event-serviceoffline"><code>serviceoffline</code></a>
              </td>
            </tr>
          </tbody>
        </table>
      </section>
    </section>
    <section>
      <h2>
        Service Discovery
      </h2>
      <p>
        A <a>user agent</a> conforming to this specification MAY implement <abbr title=
        "Simple Service Discovery Protocol">SSDP</abbr> [[!UPNP-DEVICEARCH11]], Zeroconf [[!DNS-SD]] + [[!MDNS]] or
        <abbr title="Discovery and Launch Protocol">DIAL</abbr> [<a href = "https://sites.google.com/a/dial-multiscreen.org/dial/dial-protocol-specification">DIAL</a>]
        <dfn>service discovery mechanisms</dfn> - the requirements detailed in this section of the specification - to
        enable Web pages to request and connect with HTTP services running on networked devices, discovered via either
        mechanism, through this API. When a <a>user agent</a> implements either of these <a>service discovery
        mechanisms</a>, then it MUST conform to the corresponding algorithms provided in this section of the
        specification.
      </p>
      <p>
        This section presents how the results of these two <a>service discovery mechanisms</a> will be matched to
        requested service types, how the user agent stores available and active services and how their properties are
        applied to any resulting <a href="#networkservice"><code>NetworkService</code></a> objects.
      </p>
      <p>
        It is expected that user agents will perform these <a>service discovery mechanisms</a> asynchronously and
        periodically update the <a>list of available service records</a> as required. The timing of any <a>service
        discovery mechanisms</a> is an implementation detail left to the discretion of the implementer (e.g. by
        continuously monitoring the network as a background process or on invocation of this API from a Web page).
      </p>
      <p>
        The <dfn>list of available service records</dfn> is a single dynamic internal lookup table within user agents
        that is used to track all the services that have been discovered and are available in the current network at
        any given time. At any point during the running of either of the two <a>service discovery mechanisms</a> then
        existing entries within this table can be updated, entries can be added and entries can be removed as the
        status of networked services changes according to the rules defined in this specification.
      </p>
      <p>
        The <dfn>list of active service managers</dfn> is an internal list within user agents that is used to track all
        <a href="#networkservices"><code>NetworkServices</code></a> objects currently being shared with any web pages
        at the current time within the user agent. Each <a href="#networkservices"><code>NetworkServices</code></a> object
        in the <a>list of
        active service managers</a> represents a collection of zero or more <a href=
        "#networkservice"><code>NetworkService</code></a> objects - known as its <dfn>indexed properties</dfn>.
        <a href="#networkservice"><code>NetworkService</code></a> objects are attached as the <a>indexed properties</a>
        of a <a href="#networkservices"><code>NetworkServices</code></a> object as part of the <a href=
        "#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a> algorithm.
      </p>
      <p>
        The rule for <dfn>adding an available service</dfn> is the process of adding a new service or updating an
        existing service in the <a>list of available service records</a> that is generally available on the user's
        current network. This rule takes one argument, <var>network service record</var>, and consists of running the
        following steps:
      </p>
      <ol class="rule">
        <li>Let <var>new service registration flag</var> be <code>true</code>.
        </li>
        <li>For each <var>existing service record</var> in the current <a>list of available service records</a>, run
        the following sub-steps:
          <ol class="rule">
            <li>If the <var>existing service record</var>'s <code>id</code> property does not equal <var>network
            service record</var>'s <code>id</code> property then abort any remaining sub-steps and continue at the next
            available <var>existing service record</var>.
            </li>
            <li>Set <var>new service registration flag</var> to <code>false</code>.
            </li>
            <li>Replace the value of <var>existing service record</var> in the current <a>list of available service
            records</a> with the value of <var>network service record</var>.
            </li>
          </ol>
        </li>
        <li>If <var>new service registration flag</var> is set to <code>true</code> then add <var>network service
        record</var> to the <a>list of available service records</a> as a new item.
        </li>
        <li>For each <var>service manager</var> in the <a>list of active service managers</a> run the following steps:
          <ol class="rule">
            <li>Let <var>service type in current service manager flag</var> be <code>false</code>.
            </li>
            <li>For each <var>active service</var> in <var>service manager</var> run the following steps:
              <ol class="rule">
                <li>If <var>network service record</var>'s <code>type</code> property does not equal <var>active
                service</var>'s <code>type</code> attribute then abort any remaining sub-steps for this <var>active
                service</var> and continue at the next available <var>active service</var>.
                </li>
                <li>Set the <var>service type in current service manager flag</var> to <code>true</code>.
                </li>
                <li>If the <var>new service registration flag</var> is set to <code>false</code>, the <var>network
                service record</var>'s <code>id</code> property equals the <var>active service</var>'s <code>id</code>
                attribute and <var>active service</var>'s <a href="#dom-networkservice-online"><code>online</code></a>
                attribute is currently set to <code>false</code> then set <var>active service</var>'s <a href=
                "#dom-networkservice-online"><code>online</code></a> attribute to <code>true</code> and then <a href=
                "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
                      class="externalDFN">queue a task</a> to dispatch a newly created event with the name <a href=
                      "#event-serviceonline"><code>serviceonline</code></a> that uses the <a href=
                      "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                      class="externalDFN"><code>Event</code></a> interface, which does not bubble, is not cancellable,
                      and has no default action, at the current <var>active service</var> object.
                </li>
              </ol>
            </li>
            <li>If the <var>new service registration flag</var> is set to <code>true</code> and the <var>service type
            in current service manager flag</var> is also set to <code>true</code> then increment <var>service
            manager</var>'s <a href="#dom-networkservices-servicesavailable"><code>servicesAvailable</code></a>
            attribute by <code>1</code> and then <a href=
            "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
                  class="externalDFN">queue a task</a> to dispatch a newly created event with the name <a href=
                  "#event-serviceavailable"><code>serviceavailable</code></a> that uses the <a href=
                  "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                  class="externalDFN"><code>Event</code></a> interface, which does not bubble, is not cancellable, and
                  has no default action, at the current <var>service manager</var> object.
            </li>
          </ol>
        </li>
      </ol>
      <p>
        The rule for <dfn>removing an available service</dfn> is the general process of removing a service from the
        <a>list of available service records</a> that has left the user's current network or has otherwise expired.
        This rule takes one argument, <var>service identifier</var>, and consists of running the following steps:
      </p>
      <ol class="rule">
        <li>For each <var>existing service record</var> in the current <a>list of available service records</a>, run
        the following sub-steps:
          <ol class="rule">
            <li>If the <var>existing service record</var>'s <code>id</code> property does not match <var>service
            identifier</var> then skip any remaining sub-steps for the current <var>existing service record</var> and
            continue at the next available <var>existing service record</var>.
            </li>
            <li>If the <var>existing service record</var>'s <code>type</code> property begins with the DOMString
            "<code>upnp:</code>" and <var>existing service record</var>'s <code>eventsURL</code> property is set then
            run the rule to <a>terminate an existing UPnP Events Subscription</a>, if one is currently active (as a
            result of having previously called <a>setup a UPnP Events Subscription</a> against the current
            <var>existing service record</var>).
            </li>
            <li>For each <var>service manager</var> in the <a>list of active service managers</a> run the following
            steps:
              <ol class="rule">
                <li>Let <var>service type in current service manager flag</var> be <code>false</code>.
                </li>
                <li>For each <var>active service</var> in <var>service manager</var> run the following steps:
                  <ol class="rule">
                    <li>If <var>existing service record</var>'s <code>type</code> property does not equal the
                    <var>active service</var>'s <code>type</code> attribute then abort any remaining sub-steps for this
                    <var>active service</var> and continue at the next available <var>active service</var>.
                    </li>
                    <li>Set the <var>service type in current service manager flag</var> to <code>true</code>.
                    </li>
                    <li>If <var>existing service record</var>'s <code>id</code> property equals the <var>active
                    service</var>'s <code>id</code> attribute and <var>active service</var>'s <a href=
                    "#dom-networkservice-online"><code>online</code></a> attribute is currently set to
                    <code>true</code> then set <var>active service</var>'s <a href="#dom-networkservice-online"><code>
                      online</code></a> attribute to <code>false</code> and then <a href=
                      "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
                          class="externalDFN">queue a task</a> to dispatch a newly created event with the name <a href=
                          "#event-serviceoffline"><code>serviceoffline</code></a> that uses the <a href=
                          "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                          class="externalDFN"><code>Event</code></a> interface, which does not bubble, is not
                          cancellable, and has no default action, at the current <var>active service</var>.
                    </li>
                  </ol>
                </li>
                <li>If the <var>service type in current service manager flag</var> is set to <code>true</code> then
                decrement <var>service manager</var>'s <a href=
                "#dom-networkservices-servicesavailable"><code>servicesAvailable</code></a> attribute by <code>1</code>
                and then <a href=
                "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
                      class="externalDFN">queue a task</a> to dispatch a newly created event with the name <a href=
                      "#event-serviceunavailable"><code>serviceunavailable</code></a> that uses the <a href=
                      "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                      class="externalDFN"><code>Event</code></a> interface, which does not bubble, is not cancellable,
                      and has no default action, at the current <var>service manager</var> object.
                </li>
              </ol>
            </li>
            <li>Remove <var>existing service record</var> from the current <a>list of available service records</a>.
            </li>
          </ol>
        </li>
      </ol>
      <p>
        User agents SHOULD expire a service record from the <a>list of available service records</a> when its
        <code>expiryTimestamp</code> attribute exceeds the current UTC timestamp. When this condition is met the
        <a>user agent</a> SHOULD run the rule for <a>removing an available service</a>, passing in the expired service
        record's <code>id</code> attribute as the only argument.
      </p>
      <section>
        <h4>
          Zeroconf (<abbr title="Multicast DNS">mDNS</abbr> + <abbr title="Domain Name System">DNS</abbr>-<abbr title=
          "Service Discovery">SD</abbr>)
        </h4>
        <p>
          For each DNS response received from a user-agent-initiated Multicast DNS Browse for <abbr title=
          "DNS Pointer Record">PTR</abbr> records with the name <code>_services._dns-sd._udp</code> on the resolved
          recommended automatic browsing domain [[!MDNS]], the <a>user agent</a> MUST run the following steps:
        </p>
        <ol class="rule">
          <li>Let <var>service mDNS responses</var> be an array of PTR records received by issuing a Multicast DNS
          Browse for PTR records with the name of the current discovered service type.
          </li>
          <li>For each Object <var>service mDNS response</var> in <var>service mDNS responses</var>, run the following
          steps:
            <ol>
              <li>Let <var>network service record</var> be an Object consisting of the following empty properties:
              <code>id</code>, <code>name</code>, <code>type</code>, <code>url</code>, <code>config</code>,
              <code>expiryTimestamp</code>.
              </li>
              <li>Set <var>network service record</var>'s <code>id</code> property to the value of the full PTR Service
              Instance Name [[!MDNS]].
              </li>
              <li>Set <var>network service record</var>'s <code>name</code> property to the value of the PTR Service
              Instance Name's <var>Instance</var> component [[!MDNS]].
              </li>
              <li>Set <var>network service record</var>'s <code>type</code> property to the concatenation of the string
              <code>zeroconf:</code> followed be the value of the PTR Service Instance Name's <var>Service</var>
              component [[!MDNS]].
              </li>
              <li>Set <var>network service record</var>'s <code>url</code> property to the resolvable Service URL
              obtained from performing an DNS-SD Lookup [[!DNS-SD]] of the current service from the PTR record provided
              [[!MDNS]].
              </li>
              <li>Set <var>network service record</var>'s <code>config</code> property to the string value of the
              contents of the first DNS-SD TXT record associated with the <var>service mDNS response</var> as defined
              in [[!DNS-SD]].
              </li>
              <li>Set <var>network service record</var>'s <code>expiryTimestamp</code> property to the value of the
              current date, in UTC timestamp format, plus a value of <code>120</code> seconds.
              </li>
              <li>Run the general rule for <a>adding an available service</a>, passing in the current <var>network
              service record</var> as the only argument.
              </li>
            </ol>
          </li>
        </ol>
      </section>
      <section>
        <h5>
          Simple Service Discovery Protocol (<abbr title="Simple Service Discovery Protocol">SSDP</abbr>)
        </h5>
        <p>
          A user agent that implements UPnP service discovery MUST issue a <dfn>search request for UPnP root
          devices</dfn> against the user's current local network according to the full normative text and timing
          provided in 'Section 1.3.2: Search request with M-SEARCH' detailed in [[!UPNP-DEVICEARCH11]].
        </p>
        <p>
          The user agent MUST issue all <a title="search request for UPnP root devices">search requests for UPnP root
          devices</a> with a HTTP request line equal to <code>M-SEARCH * HTTP/1.1</code>, with a HOST header equal to
          the reserved multicast address and port of <code>239.255.255.250:1900</code>, a MAN header equal to
          <code>ssdp:discover</code>, an ST header equal to <code>upnp:rootdevice</code> and a user-agent defined MX
          header equal to a <dfn>maximum UPnP advertisement response wait time</dfn> value between <code>1</code> and
          <code>5</code> seconds.
        </p>
        <p>
          The user agent MUST listen for any incoming responses to any <a>search request for UPnP root devices</a>.
        </p>
        <p>
          For each <dfn>HTTP Response</dfn> following an initial <a>search request for UPnP root devices</a> sent on a
          <a>standard UPnP address and port</a> the user agent MUST run the following steps:
        </p>
        <ol class="rule">
          <li>If the <a>HTTP Response</a> is not a HTTP 200 OK response then this response is invalid and the user
          agent MUST discard this response, abort any remaining steps and return. The user agent MAY issue a new
          <a>search request for UPnP root devices</a> as a result of this error occurring.
          </li>
          <li>If the <a>maximum UPnP advertisement response wait time</a> has been exceeded since the initial <a>search
          request for UPnP root devices</a> was sent then the <a>HTTP Response</a> is invalid and the user agent MUST
          discard this response, abort any remaining steps and return. The user agent MAY stop listening for responses
          from the current <a>search request for UPnP root devices</a> as a result of this error occurring. Equally,
          the user agent MAY issue a new <a>search request for UPnP root devices</a> as a result of this error
          occurring.
          </li>
          <li>Let <var>ssdp device</var> be an Object with a property for each HTTP header received in the <a>HTTP
          Response</a>, with each key being the name of a HTTP response header and each value being that HTTP response
          header's value.
          </li>
          <li>If <var>ssdp device</var> does not contain at least one <var>CACHE-CONTROL</var> entry, at least one
          <var>USN</var> entry, at least one <var>ST</var> entry and at least one <var>LOCATION</var> entry or the
          value of its <var>ST</var> entry is not <code>upnp:rootdevice</code>, then the <a>HTTP Response</a> is
          invalid and the <a>user agent</a> MUST discard this response, abort any remaining steps and return.
          </li>
          <li>The user agent MUST run the rule for <a>obtaining a UPnP Device Description File</a> passing in the first
          occurrence of <var>LOCATION</var> from <var>ssdp device</var> as the <var>device descriptor URL</var>
          argument and the first occurrence of <var>USN</var> from <var>ssdp device</var> as the <var>device
          identifier</var> argument and the first occurrence of <var>CACHE-CONTROL</var> from <var>ssdp device</var>
          (minus the leading string of <code>max-age=</code>) as the <var>device expiry</var> argument.
          </li>
        </ol>
        <p>
          The user agent MUST listen for incoming requests on the <dfn>standard UPnP address and port</dfn> on all
          current local network interface addresses with the port <code>1900</code>.
        </p>
        <p>
          For each <dfn>HTTP Request</dfn> received on a <a>standard UPnP address and port</a> the user agent MUST run
          the following steps:
        </p>
        <ol class="rule">
          <li>If the <a>HTTP Request</a> is not a HTTP NOTIFY request then it is not a valid UPnP Request and the user
          agent MUST discard this request, abort any remaining steps and return.
          </li>
          <li>Let <var>ssdp device</var> be an Object with a property for each HTTP header received in the <a>HTTP
          Request</a>, with each key being the name of a HTTP header and each value being that HTTP header's value.
          </li>
          <li>If <var>ssdp device</var> does not contain at least one <var>CACHE-CONTROL</var> entry, at least one
          <var>USN</var> entry, at least one <var>NT</var> entry, at least one <var>NTS</var> entry and at least one
          <var>LOCATION</var> entry or the value of its <var>NT</var> entry is not <code>upnp:rootdevice</code>, then
          the <a>HTTP Request</a> is a malformed UPnP Request and the <a>user agent</a> MUST discard this request,
          abort any remaining steps and return.
          </li>
          <li>If <var>ssdp device</var>'s <var>NTS</var> entry is equal to <code>ssdp:alive</code> then the user agent
          MUST run the rule for <a>obtaining a UPnP Device Description File</a> passing in the first occurrence of
          <var>LOCATION</var> from <var>ssdp device</var> as the <var>device descriptor URL</var> argument and the
          first occurrence of <var>USN</var> from <var>ssdp device</var> as the <var>device identifier</var> argument
          and the first occurrence of <var>CACHE-CONTROL</var> from <var>ssdp device</var> (minus the leading string of
          <code>max-age=</code>) as the <var>device expiry</var>.<br>
            <br>
            Otherwise, if <var>ssdp device</var>'s <var>NTS</var> entry is equal to <code>ssdp:byebye</code> then the
            user agent MUST run the rule for <a>removing all services from a registered UPnP Device</a> passing in the
            first occurrence of <var>USN</var> from <var>ssdp device</var> as the <var>device identifier</var>
            argument.
          </li>
        </ol>
        <p>
          The rule for <dfn>obtaining a UPnP Device Description File</dfn> is the process of obtaining the contents of
          a standard UPnP Device Description [[!UPNP-DEVICEARCH11]] from a URL-based resource. This rule takes three
          arguments - <var>device descriptor URL</var>, <var>device identifier</var> and <var>device expiry</var> - and
          when called the user agent MUST run the following steps:
        </p>
        <ol class="rule">
          <li>Let <var>device descriptor file</var> contain the contents of the file located at the URL provided in
          <var>device descriptor URL</var> obtained according to the rules defined in 'Section 2.11: Retrieving a
          description using HTTP' in [[!UPNP-DEVICEARCH11]].
          </li>
          <li>If the value provided in <var>device descriptor URL</var> cannot be resolved as a reachable URL on the
          current network or the <var>device descriptor file</var> remains empty then it is invalid and the <a>user
          agent</a> MUST abort any remaining steps and return.
          </li>
          <li>Run the rule for <a>processing a UPnP Device Description File</a>, passing in the current <var>device
          descriptor file</var>, <var>device identifier</var> and <var>device expiry</var> arguments.
          </li>
        </ol>
        <p>
          The rule for <dfn>processing a UPnP Device Description File</dfn> is the process of parsing the contents of a
          standard UPnP Device Description [[!UPNP-DEVICEARCH11]] and registering the UPnP services contained therein
          within the <a>list of available service records</a>.
        </p>
        <p>
          The rule for <a>processing a UPnP Device Description File</a> takes three arguments - <var>device descriptor
          file</var>, <var>device identifier</var> and <var>device expiry</var> - and when called the user agent MUST
          run the following steps:
        </p>
        <ol class="rule">
          <li>Let <var>advertised services</var> be a list of all advertised services obtained from the <var>device
          descriptor file</var> containing the value of the first occurrence of the <code>&lt;serviceList&gt;</code>
          element as it is defined in 'Section 2.3: Device Description' in [[!UPNP-DEVICEARCH11]].
          </li>
          <li>For each <code>&lt;service&gt;</code> element - known as an <var>advertised service</var> - in
          <var>advertised services</var> run the following steps:
            <ol class="rule">
              <li>Let <var>network service record</var> be a new Object consisting of the following empty properties:
              <code>id</code>, <code>deviceId</code>, <code>name</code>, <code>type</code>, <code>url</code>,
              <code>eventsUrl</code>, <code>config</code>, <code>expiryTimestamp</code>.
              </li>
              <li>Set <var>network service record</var>'s <code>id</code> property to the concatenated string value of
              the first occurrence of the <code>&lt;UDN&gt;</code> element in the <var>device descriptor file</var>
              with the <var>advertised service</var>'s <code>&lt;serviceId&gt;</code> sub-element.
              </li>
              <li>Set <var>network service record</var>'s <code>deviceId</code> property to the value of <var>device
              identifier</var>.
              </li>
              <li>Set <var>network service record</var>'s <code>name</code> property to the string value of the first
              occurrence of the <var>advertised service</var>'s <code>&lt;serviceId&gt;</code> sub-element.
              </li>
              <li>Set <var>network service record</var>'s <code>type</code> property to the concatenation of the string
              <code>upnp:</code> followed by the string value of the first occurrence of the <var>advertised
              service</var>'s <code>&lt;serviceType&gt;</code> sub-element.
              </li>
              <li>Set <var>network service record</var>'s <code>url</code> property to the string value of the first
              occurrence of the <var>advertised service</var>'s <code>&lt;controlURL&gt;</code> sub-element.
              </li>
              <li>Set <var>network service record</var>'s <code>config</code> property to the string value of the
              contents of the first occurrence of the <code>&lt;device&gt;</code> element in the <var>device descriptor
              file</var>.
              </li>
              <li>If <var>advertised service</var>'s <code>&lt;eventSubURL&gt;</code> sub-element is not empty, then
              set <var>network service record</var>'s <code>eventsUrl</code> property to the string value of the first
              occurrence of the <var>advertised service</var>'s <code>&lt;eventSubURL&gt;</code> sub-element.
              Otherwise, do not set <var>network service record</var>'s <code>eventsUrl</code> property.
              </li>
              <li>Set <var>network service record</var>'s <code>expiryTimestamp</code> property to the value of the
              current date, in UTC timestamp format, plus the value of <var>device expiry</var>.
              </li>
              <li>Run the general rule for <a>adding an available service</a>, passing in the current <var>network
              service record</var> as the only argument.
              </li>
            </ol>
          </li>
          <li>If <var>device descriptor file</var> contains a <code>&lt;deviceList&gt;</code> element then for each
          <code>&lt;device&gt;</code> element within <code>&lt;deviceList&gt;</code> - herein known as an <var>embedded
          device descriptor file</var> - the user agent MUST run the rule for <a>processing a UPnP Device Description
          File</a>, passing in the current <var>embedded device descriptor file</var> as the <var>device descriptor
          file</var> argument, along with the current <var>device identifier</var> and <var>device expiry</var>
          arguments.
          </li>
        </ol>
        <p>
          The rule for <dfn>removing all services from a registered UPnP Device</dfn> is the process of removing all
          services associated with a device from the <a>list of available service records</a> that has left the user's
          current network or has otherwise timed out or expired. This rule takes one argument, <var>device
          identifier</var>, and consists of running the following steps:
        </p>
        <ol class="rule">
          <li>For each <var>existing service record</var> in the current <a>list of available service records</a>, run
          the following sub-steps:
            <ol class="rule">
              <li>If the <var>existing service record</var>'s <code>deviceId</code> property does not match <var>device
              identifier</var> then skip any remaining sub-steps for the current <var>existing service record</var> and
              continue at the next available <var>existing service record</var>.
              </li>
              <li>Run the general rule for <a>removing an available service</a> passing in <var>existing service
              record</var>'s <code>id</code> property as the only argument.
              </li>
            </ol>
          </li>
        </ol>
        <p>
          When the <a>user agent</a> is to <dfn>setup a UPnP Events Subscription</dfn>, it is to run the following
          steps with the current <var>network service record</var> object as defined in 'Section 4.1.2: SUBSCRIBE with
          NT and CALLBACK' in [[!UPNP-DEVICEARCH11]]:
        </p>
        <ol class="rule">
          <li>If <var>network service record</var>'s <code>eventsUrl</code> property is empty then the <a>user
          agent</a> MUST abort these steps.
          </li>
          <li>Let <var>callback URL</var> be the value of creating a new <a>user-agent generated callback url</a>.
          </li>
          <li>Send a HTTP SUBSCRIBE request with a <em>NT</em> header with a string value of <code>upnp:event</code>, a
          <em>TIMEOUT</em> header with a user-agent defined timeout value (in the form <code>Second-XX</code> where
          <code>XX</code> is the user-agent defined timeout value in seconds) and a <em>CALLBACK</em> header with a
          string value of <var>callback URL</var> towards the <var>network service record</var>'s
          <code>eventsUrl</code> property.
          </li>
          <li>If a non-200 OK response is received from the HTTP SUBSCRIBE request then the <a>user agent</a> MUST
          abort these steps.
          </li>
          <li>On receiving a valid 200 OK response, run the following steps:
            <ol class="rule">
              <li>Let <var>callback ID</var> equal the string value of the first included <em>SID</em> header, if it
              exists.
              </li>
              <li>Let <var>timeout date</var> equal the sum of the current UTC date value plus the integer value of the
              first included <em>TIMEOUT</em> header (minus the leading string of <code>Second-</code>), if it exists.
              </li>
              <li>Run the following steps asynchronously and continue to the step labeled <em>listen</em> below.
              </li>
              <li>
                <em>Refresh Subscription</em>: Run the following steps at a set interval (X) within the <a>user
                agent</a>:
                <ol class="rule">
                  <li>Let <var>current date</var> equal the current UTC date.
                  </li>
                  <li>If <var>current date</var> is less than the <var>timeout date</var> then continue to the step
                  labeled <em>refresh subscription</em> above.
                  </li>
                  <li>Send a HTTP SUBSCRIBE request with a <em>SID</em> header with the string value of <var>callback
                  ID</var> and a user-agent defined <em>TIMEOUT</em> header (in the form <code>Second-XX</code> where
                  <code>XX</code> is the user-agent defined timeout value in seconds) towards the <var>network service
                  record</var>'s <code>eventsUrl</code> property.
                  </li>
                  <li>On receiving a valid 200 OK, update <var>callback ID</var> with the string value of the first
                  included <em>SID</em> header and set <var>timeout date</var> to the sum of the current UTC date value
                  plus the integer value of the first included <em>TIMEOUT</em> header (minus the leading string of
                  <code>Second-</code>), if it exists. If the current date is greater than or equal to <var>timeout
                  date</var> then the <a>user agent</a> SHOULD continue from the step labeled <em>refresh
                  subscription</em> above. For all non 200 OK responses the <a>user agent</a> SHOULD continue from the
                  step labeled <em>refresh subscription</em> above.
                  </li>
                </ol>
              </li>
              <li>
                <em>Listen</em>: For each HTTP NOTIFY request received at the <var>callback URL</var> the <a>user
                agent</a> is to run the following steps:
                <ol class="rule">
                  <li>Let <var>content clone</var> be the result of obtaining the message body of the HTTP NOTIFY
                  request. If <var>content clone</var> is empty, then the <a>user agent</a> MUST abort these steps.
                  </li>
                  <li>Let <var>notification event</var> be a new simple event that uses the <a href=
                  "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                        class="externalDFN"><code>Event</code></a> interface with the name <a href=
                        "#event-notify"><code>notify</code></a>, which does not bubble, is not cancellable, and has no
                        default action.
                  </li>
                  <li>Let the <code>data</code> attribute of <var>notification event</var> have the DOMString value of
                  <var>content clone</var>.
                  </li>
                  <li>
                    <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
                        class="externalDFN">Queue a task</a> to dispatch <var>notification event</var> at the current
                        <a><code>NetworkService</code></a> object.
                  </li>
                  <li>Return a HTTP 200 OK response to the sender of the HTTP NOTIFY request.
                  </li>
                </ol>
              </li>
            </ol>
          </li>
        </ol>
        <p>
          A <a>user agent</a> can <dfn>terminate an existing UPnP Events Subscription</dfn> at any time for a
          <var>network service record</var> by sending an HTTP UNSUBSCRIBE request - as defined in 'Section 4.1.4:
          Cancelling a subscription with UNSUBSCRIBE' in [[!UPNP-DEVICEARCH11]] - with a HOST header set to that
          <var>active service</var>'s <code>eventsUrl</code> property and a SID header set to the <var>callback
          ID</var> obtained when the initial <a>setup a UPnP Events Subscription</a> action occurred.
        </p>
      </section>
      <section>
        <h5>
          Discovery and Launch Protocol (<abbr title="Discovery and Launch Protocol">DIAL</abbr>)
        </h5>
         <p>
          A user agent that implements DIAL service discovery MUST issue a <dfn>search request for DIAL-enabled
          devices</dfn> against the user's current local network according to the full normative text and timing
          provided in 'Section 1.3.2: Search request with M-SEARCH' detailed in [[!UPNP-DEVICEARCH11]].
        </p>
        <p>
          Let <var>dial version</var> be the version number specified in the <a>valid service type</a> token.
          Let <var>dial search target</var> be the concatentation of the
          <code>urn:dial-multiscreen-org:service:dial:</code> string constant with the <var>dial version</var>
          (currently, <var>dial version</var> can only be <code>1</code>)
        </p>
        <p>
          The user agent MUST issue all <a title="search request for DIAL devices">search requests for DIAL
          devices</a> with a HTTP request line equal to <code>M-SEARCH * HTTP/1.1</code>, with a HOST header equal to
          the reserved multicast address and port of <code>239.255.255.250:1900</code>, a MAN header equal to
          <code>ssdp:discover</code>, an ST header equal to <var>dial search target</var>
          and a user-agent defined MX header equal to a <dfn>maximum DIAL advertisement response wait time</dfn>
          value between <code>1</code> and <code>5</code> seconds.
        </p>
        <p>
          The user agent MUST listen for any incoming responses to a <a>search request for DIAL devices</a>.
        </p>
        <p>
          For each HTTP Response following an initial <a>search request for DIAL devices</a> sent on a
          <a>standard UPnP address and port</a> the user agent MUST run the following steps:
        </p>
        <ol class="rule">
          <li>If the HTTP Response is not a HTTP 200 OK response then this response is invalid and the user
          agent MUST discard this response, abort any remaining steps and return. The user agent MAY issue a new
          <a>search request for DIAL devices</a> as a result of this error occurring.
          </li>
          <li>If the <a>maximum DIAL advertisement response wait time</a> has been exceeded since the initial <a>search
          request for DIAL devices</a> was sent then the <a>HTTP Response</a> is invalid and the user agent MUST
          discard this response, abort any remaining steps and return. The user agent MAY stop listening for responses
          from the current <a>search request for DIAL devices</a> as a result of this error occurring. Equally,
          the user agent MAY issue a new <a>search request for DIAL devices</a> as a result of this error
          occurring.
          </li>
          <li>Let <var>dial device</var> be an Object with a property for each HTTP header received in the <a>HTTP
          Response</a>, with each key being the name of a HTTP response header and each value being that HTTP response
          header's value.
          </li>
          <li>If <var>dial device</var> does not contain at least one <var>CACHE-CONTROL</var> entry, at least one
          <var>USN</var> entry, at least one <var>ST</var> entry and at least one <var>LOCATION</var> entry or the
          value of its <var>ST</var> entry is not <var>dial search target</var>, then the
          <a>HTTP Response</a> is invalid and the <a>user agent</a> MUST discard this response, abort any remaining
          steps and return.
          </li>
          <li>The user agent MUST run the rule for <a>obtaining a UPnP Device Description File</a> passing in the first
          occurrence of <var>LOCATION</var> from <var>dial device</var> as the <var>device descriptor URL</var>
          argument and the first occurrence of <var>USN</var> from <var>dial device</var> as the <var>device
          identifier</var> argument and the first occurrence of <var>CACHE-CONTROL</var> from <var>dial device</var>
          (minus the leading string of <code>max-age=</code>) as the <var>device expiry</var> argument.
          </li>
        </ol>
        <p>
          The rule for <dfn>obtaining a DIAL Device Description File</dfn> is the process of obtaining the contents of
          a standard UPnP Device Description [[!UPNP-DEVICEARCH11]] from a URL-based resource. This rule takes three
          arguments - <var>device descriptor URL</var>, <var>device identifier</var> and <var>device expiry</var> - and
          when called the user agent MUST run the following steps:
        </p>
        <ol class="rule">
          <li>Let <var>device descriptor file</var> contain the contents of the file located at the URL provided in
          <var>device descriptor URL</var> obtained according to the rules defined in 'Section 2.11: Retrieving a
          description using HTTP' in [[!UPNP-DEVICEARCH11]].
          </li>
          <li>Let <var>application url</var> be the value of the first occurrence of the <code>Application-URL</code>
          response header field obtained according to the rules defined in 'Section 5.4: Device Description Response' in
          [<a href = "https://sites.google.com/a/dial-multiscreen.org/dial/dial-protocol-specification">DIAL</a>]
          </li>
          <li>If the value provided in <var>device descriptor URL</var> cannot be resolved as a reachable URL on the
          current network or the <var>device descriptor file</var> remains empty or <var>application url</var> is undefined
          then it is invalid and the <a>user agent</a> MUST abort any remaining steps and return.
          </li>
          <li>Run the following steps to add the newly discovered DIAL service:
            <ol class="rule">
              <li>Let <var>network service record</var> be a new Object consisting of the following empty properties:
              <code>id</code>, <code>deviceId</code>, <code>name</code>, <code>type</code>, <code>url</code>,
              <code>expiryTimestamp</code>.
              </li>
              <li>Set <var>network service record</var>'s <code>id</code> property to the first occurrence of the
              <code>&lt;UDN&gt;</code> element in the <var>descriptor file</var> prefixed with the <code>dial:</code>
              string constant
              </li>
              <li>Set <var>network service record</var>'s <code>deviceId</code> property to the value of <var>device
              identifier</var>.
              </li>
              <li>Set <var>network service record</var>'s <code>name</code> property to the string value of the first
              occurrence of the <code>&lt;friendlyName&gt;</code> element in the <var>descriptor file</var>.
              </li>
              <li>Set <var>network service record</var>'s <code>type</code> property to <var>dial search target</var>.
              </li>
              <li>Set <var>network service record</var>'s <code>url</code> property to the string value of the <var>
              application url</var>.
              </li>
              <li>Set <var>network service record</var>'s <code>expiryTimestamp</code> property to the value of the
              current date, in UTC timestamp format, plus the value of <var>device expiry</var>.
              </li>
              <li>Run the general rule for <a>adding an available service</a>, passing in the current <var>network
              service record</var> as the only argument.
              </li>
            </ol>
          </li>
        </ol>
      </section>
      <section>
        <h3>
          Network Topology Monitoring
        </h3>
        <div>
          <p>
            When the <a>user agent</a> detects that the user has dropped from their connected network then, for each
            <var>existing service record</var> in the <a>list of available service records</a>, the user agent MUST run
            the general rule for <a>removing an available service</a> passing in each <var>existing service
            record</var>'s <code>id</code> property as the only argument for each call.
          </p>
          <p>
            When the <a>user agent</a> detects that the user has connected to a new network or reconnected to an
            existing network, then it SHOULD restart its discovery mechanisms as defined in the <a href=
            "#service-discovery">Service Discovery</a> section of this specification, maintaining the existing <a>list
            of active service managers</a> currently in use.
          </p>
        </div>
      </section>
    </section>
    <section>
      <h3>
        Events Summary
      </h3>
      <p>
        The following events are dispatched on the <a href="#networkservices"><code>NetworkServices</code></a> and/or
        <a href="#networkservice"><code>NetworkService</code></a> objects:
      </p>
      <table border="1">
        <thead>
          <tr>
            <th>
              <span>Event name</span>
            </th>
            <th>
              <span>Interface</span>
            </th>
            <th>
              <span>Dispatched when...</span>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              <dfn id="event-serviceavailable"><code>serviceavailable</code></dfn>
            </td>
            <td>
              <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                  class="externalDFN"><code>Event</code></a>
            </td>
            <td>
              When a new service that matches one of the <a>requested type tokens</a> is found in the current network.
            </td>
          </tr>
          <tr>
            <td>
              <dfn id="event-serviceunavailable"><code>serviceunavailable</code></dfn>
            </td>
            <td>
              <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                  class="externalDFN"><code>Event</code></a>
            </td>
            <td>
              When an existing service that matches one of the <a>requested type tokens</a> gracefully leaves or
              expires from the current network.
            </td>
          </tr>
          <tr>
            <td>
              <dfn id="event-serviceonline"><code>serviceonline</code></dfn>
            </td>
            <td>
              <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                  class="externalDFN"><code>Event</code></a>
            </td>
            <td>
              When a current service renews its service registration within the current network.
            </td>
          </tr>
          <tr>
            <td>
              <dfn id="event-serviceoffline"><code>serviceoffline</code></dfn>
            </td>
            <td>
              <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                  class="externalDFN"><code>Event</code></a>
            </td>
            <td>
              When a current service gracefully leaves or otherwise expires from the current network.
            </td>
          </tr>
          <tr>
            <td>
              <dfn id="event-notify"><code>notify</code></dfn>
            </td>
            <td>
              <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
                  class="externalDFN"><code>Event</code></a>
            </td>
            <td>
              When a valid UPnP Events Subscription Message is received on a <a>user-agent generated callback url</a>
              for a current service. This event never fires for Zeroconf-based services.
            </td>
          </tr>
        </tbody>
      </table>
    </section>
    <section>
      <h3>
        Garbage collection
      </h3>
      <p>
        A user agent MUST only garbage collect a <a><code>NetworkServices</code></a> object and remove its entry from
        the <a>list of active service managers</a> when the user has navigated away from the
        <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/browsers.html#entry-script"
              class="externalDFN">entry script</a>'s <a href=
              "http://www.whatwg.org/specs/web-apps/current-work/complete/origin-0.html#origin"
              class="externalDFN">origin</a> in which
        the current <a><code>NetworkServices</code></a> object was provided.
      </p>
      <p>
        A user agent MUST NOT garbage collect individual <a><code>NetworkService</code></a> objects until their parent
        <a><code>NetworkServices</code></a> object has been garbage collected.
      </p>
      <p>
        A user agent MUST garbage collect the <a><code>NetworkService</code></a> <a>indexed properties</a> of a
        <a><code>NetworkServices</code></a> object when that <a><code>NetworkServices</code></a> object itself has been
        garbage-collected.
      </p>
    </section>
    <section>
      <h3>
        Use Cases and Requirements
      </h3>
      <p>
        This section covers what the requirements are for this API, as well as illustrates some use cases.
      </p>
      <ul class="rule">
        <li>Once a user has given permission, user agents should provide the ability for Web pages to communicate
        directly with a Local-networked Service.
          <ul class="rule">
            <li>Example: A web-based TV remote control. A Web page wants to control the current user's TV, changing the
            programming shown or increasing/decreasing/muting the volume of the Local-networked Device. The Web page
            requests a service type that is known to be implemented by television sets to which it has the application
            logic to communicate. Local devices providing the request service types are discovered and presented to the
            user for authorization. The user selects one or more of the discovered television sets to be accessible to
            the current Web page and then clicks 'Share'. The Web page can now communicate directly with the
            user-authorized Local-networked services.
            </li>
          </ul>
        </li>
        <li>Web pages should be able to communicate with Local-networked Services using the messaging channel supported
        by those Devices.
          <ul class="rule">
            <li>Example: A Web page advertises that it is capable of controlling multiple Home Media Servers. The user
            can select their Home Media Server type from a drop-down list, at which point the Web page sends a request
            to the user agent to connect with the associated service type of the Home Media Server. The Media Server
            selected implements a Web Socket channel for bi-directional service communication and so the Web page opens
            a web socket to the requested Media Server and can communicate as required via that appropriate channel.
            </li>
          </ul>
        </li>
        <li>Web pages should be able to communicate with Local-networked Services using the messaging format supported
        by those Devices.
          <ul class="rule">
            <li>Example: A Web page advertises that it is capable of interacting with and controlling multiple types of
            Home Media Server. The user can select their Home Media Server type from a drop-down list or known Media
            Servers, at which point the Web page sends a request to the user agent to connect with the associated
            service type (and, optionally, the associated event type) of the Home Media Server. The communication
            protocols supported by Home Media Servers typically vary between UPnP, JSON-RPC, Protocol Buffers or other
            messaging formats depending on the Home Media Server requested. The Web page is able to communicate with
            the user-selected Home Media Server in the messaging format supported by that Device, which, in this
            example is a simple key/value pair text format.
            </li>
          </ul>
        </li>
        <li>Web pages should not be able to communicate with Local-networked Services that have not been authorized by
        the user thereby maintaining the user's privacy.
          <ul class="rule">
            <li>Example: A Web page requests access to one type of Local-networked service. The user authorizes access
            to that particular service. Other services running on that same device, and on other devices within the
            network, should not be accessible to the current Web page.
            </li>
          </ul>
        </li>
        <li>A user should be able to share one or more Local-networked Services based on a particular service type
        request from a Web page.
          <ul class="rule">
            <li>Example: A Web page is capable of interacting with a specific profile of Local-networked Service. As
            such, it makes a request to the user agent to access those services, of which multiple matches are found.
            The user is capable of selecting one or more of the discovered services to share with the Web page. The Web
            page can then implement a drag-and-drop interface for the user to drag specific actions on to one or more
            of the authorized Local-networked Services.
            </li>
          </ul>
        </li>
        <li>User agents should provide an API exposed to script that exposes the features above. The user is notified
        by UI anytime interaction with Local-networked Services is requested, giving the user full ability to cancel or
        abort the transaction. The user selects the Local-networked Services to be connected to the current Web page,
        and can cancel these at any time. No invocations to these APIs occur silently without user intervention.
        </li>
      </ul>
    </section>
    <section class="informative appendix">
      <h3>
        Examples
      </h3>
      <div class="example">
        <p>
          This sample code exposes a button. When clicked, this button is disabled and the user is prompted to offer a
          network service. The user may also select multiple network services. When the user has authorized a network
          service to be connected to the web page then the web page issues a simple command to get a list of all the
          albums stored on the connected media player service.
        </p>
        <p>
          The button is re-enabled only when the connected network service disconnects for whatever reason (the service
          becomes unavailable on the network, the user disconnects from their current network or the user revokes
          access to the service from the current web page). At this point the user can re-click the button to select a
          new network service to connect to the web page and the above steps are repeated.
        </p>
        <p>
          The provided service type identifier and service interaction used in this example is based on the
          well-defined service type and messaging format supported by the <a href="http://xbmc.org/about/">XBMC Media
          Server</a>.
        </p>
        <hr>
        <pre class="highlight">
&lt;input type="button" value="Start" onclick="start()" id="startBtn"/&gt;
&lt;div id="debugconsole"&gt;&lt;/div&gt;

&lt;script&gt;
 var startBtn = document.getElementById('startBtn'),
     debug = document.getElementById('debugconsole');

 function start() {
   if(navigator.getNetworkServices) {
      navigator.getNetworkServices('zeroconf:_xbmc-jsonrpc._tcp', gotXBMCService, error);
      startBtn.disabled = true;
   } else {
      debug.innerHTML += "&lt;br&gt;Service Discovery not supported!";
   }
 }

 function gotXBMCService(services) {

// Listen for service disconnect messages

   services[0].addEventListener('serviceoffline', function ( e ) {
       debug.innerHTML += "&lt;br&gt;" + services[0].name + " disconnected.";
       startBtn.disabled = false;
   }, false);

// Send a service message to get albums list (and process the service response)

   var svcXhr = new XMLHttpRequest();
   svcXhr.open("POST", services[0].url + "/getAlbums"); // services[0].url and its sub-resources have been
                                                        // whitelisted for cross-site XHR use in this
                                                        // current browsing context.

   svcXhr.setRequestHeader('Content-Type', 'application/json-rpc');

   svcXhr.addEventListener('readystatechange', function ( response ) {
     if( response.readyState != 4 || response.status != 200 )
        return;
     debug.innerHTML += "&lt;br&gt;" + services[0].name + " response received: ";
     debug.textContent += JSON.parse(response.responseText);
   }, false);

   var svcMsg = [
     { "jsonrpc": "2.0", "method": "AudioLibrary.GetAlbums", "params": { "genreid": -1,
         "artistid": -1, "start": -1, "end": -1 }, "id": "1" }
   ];

   svcXhr.send(JSON.stringify(svcMsg));
   debug.innerHTML += "&lt;br&gt;" + services[0].name + " request sent: ";
   debug.textContent += JSON.stringify(svcMsg);

 }

 function error( err ) {
   debug.innerHTML += "&lt;br&gt;An error occurred obtaining a local network service.";
   startBtn.disabled = false;
 }
&lt;/script&gt;
</pre>
      </div>
      <div class="example">
        <p>
          This sample exposes a drop-down list containing a number of common Home-based audio devices. When the user
          selects an audio device from the list provided, they are prompted to authorize a network service based on the
          service type requested. The user may also select multiple network services matching the selected service
          type. In this example, the user selects their make as being <var>Sony</var> and their model as being
          <var>Bravia S1000</var> from which the Web page can derive a service type
          (<var>urn:schemas-upnp-org:service:RenderingControl:1</var>).<br>
          <br>
          Once the user has authorized the device, the web page sends a simple mute command according to the messaging
          format supported by the device.
        </p>
        <hr>
        <pre class="highlight">
&lt;select name="make" id="make"&gt;
  &lt;option selected="selected" disabled="disabled"&gt;Select make&lt;/option&gt;
  &lt;option&gt;Sony&lt;/option&gt;
  &lt;option&gt;Philips&lt;/option&gt;
  &lt;option&gt;Alba&lt;/option&gt;
&lt;/select&gt;
&lt;select name="model" id="model"&gt;&lt;/select&gt;
&lt;div id="debugconsole"&gt;&lt;/div&gt;

&lt;script&gt;
  var debug = document.getElementById('debugconsole');

  var models = {
    "Sony": [
      {"name": "Bravia TV S1000", "type": "upnp", "service": "urn:schemas-upnp-org:service:RenderingControl:1" },
      {"name": "Bravia TV S2000", "type": "zeroconf", "service": "_mediarenderer._http._tcp" },
      {"name": "HiFi WD10", "type": "upnp", "service": "urn:schemas-upnp-org:service:RenderingControl:1" }
    ],
    "Philips": [ /* ... */ ],
    "Alba": [ /* ... */ ]
  };

  var makeEl = document.getElementById("make"),
      modelEl = document.getElementById("model");

  makeEl.addEventListener('change', function() {
    modelEl.innerHTML = ""; // reset
    var defaultOption = document.createElement("option");
    defaultOption.textContent = "Select model";
    defaultOption.setAttribute("disabled", "disabled");
    defaultOption.setAttribute("selected", "selected");
    modelEl.appendChild(defaultOption);
    for(var i = 0, l = models[makeEl.value].length; i &lt; l; i++) {
      var option = document.createElement("option");
      option.textContent = models[makeEl.value][i]["name"];
      option.setAttribute("value", models[makeEl.value][i]["type"] + ":" + models[makeEl.value][i]["service"]);
      modelEl.appendChild(option);
    }
  }, false);

  modelEl.addEventListener('change', function() {
    if(navigator.getNetworkServices &amp;&amp;
         modelEl.value == "upnp:urn:schemas-upnp-org:service:RenderingControl:1") {
      navigator.getNetworkServices(modelEl.value, successCallback, errorCallback);
    } else if (modelEl.value == "zeroconf:_mediarenderer._http._tcp") {
      debug.innerHTML += "&lt;br&gt;Service type is not implemented by this application.";
    } else {
      debug.innerHTML += "&lt;br&gt;Service Discovery is not supported!";
    }
  }, false);
&lt;/script&gt;

&lt;script&gt;
  function successCallback( services ) {

  // Listen for service push notification messages

    services[0].addEventListener('notify', function ( msg ) {
         debug.innerHTML += "&lt;br&gt;" + services[0].name + " event received: ";
         debug.textContent += msg.data;
    }, false);

 // Send a control signal to mute the service audio

    var svcXhr = new XMLHttpRequest();
    svcXhr.open("POST", services[0].url); // services[0].url and its
                                          // sub-resources have been whitelisted for
                                          // cross-site XHR use in this current
                                          // browsing context.

    svcXhr.setRequestHeader('SOAPAction', 'urn:schemas-upnp-org:service:RenderingControl:1#SetMute');
    svcXhr.setRequestHeader('Content-Type', 'text/xml; charset="utf-8";');

    svcXhr.onreadystatechange = function ( response ) {
      if( response.readyState != 4 || response.status != 200 )
        return;
      debug.innerHTML += "&lt;br&gt;" + services[0].name + " response received: ";
      debug.textContent += response.responseXML;
    }

    // Service messaging to mute the provided service
    var svcMsg = '&lt;?xml version="1.0" encoding="utf-8"?&gt;' +
                 '&lt;s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" ' +
                   'xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;' +
                   '&lt;s:Body&gt;' +
                     '&lt;u:SetMute xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"&gt;' +
                       '&lt;InstanceID&gt;0&lt;/InstanceID&gt;' +
                       '&lt;Channel&gt;Master&lt;/Channel&gt;' +
                       '&lt;DesiredMute&gt;true&lt;/DesiredMute&gt;' +
                     '&lt;/u:SetMute&gt;' +
                   '&lt;/s:Body&gt;' +
                 '&lt;/s:Envelope&gt;';

    svcXhr.send(svcMsg);
    debug.innerHTML += "&lt;br&gt;" + services[0].name + " request sent: ";
    debug.textContent += svcMsg;
  }

  function errorCallback( error ) {
    debug.innerHTML += "&lt;br&gt;An error occurred: " + error.code;
  }
&lt;/script&gt;
</pre>
      </div>
    </section>
    <section>
      <h3>
        Acknowledgements
      </h3>
      <p>
        Thanks are expressed by the editor to the following individuals for their feedback on this specification to
        date (in alphabetical order):<br>
        <br>
        Gar Bergstedt, Lars-Erik Bolstad, Cathy Chan, Hari G Kumar, Bob Lund, Giuseppe Pascale, Marcin Simonides,
        Clarke Stevens, Christian Söderström, Mark Vickers.
      </p>
      <p>
        Thanks are also expressed by the editor to the following organizations and groups for their support in
        producing this specification to date (in alphabetical order):<br>
        <br>
        CableLabs, Opera Software ASA, W3C Device APIs Working Group, W3C Web and TV Interest Group.
      </p>
    </section>
  </body>
</html>