discovery-api/Overview.src.html
author Rich Tibbett <richt@opera.com>
Mon, 11 Mar 2013 05:25:35 +0100
changeset 398 19eb703af1c9
parent 397 7d43f1efc56f
child 399 8b1d2cd4858d
permissions -rw-r--r--
Minor editorial changes to NSD spec
     1 <!DOCTYPE html>
     2 <!--
     3 
     4   THIS IS THE WORKING VERSION OF THE CURRENT SPECIFICATION!
     5 
     6   This specification is built using ReSpec.js <http://dev.w3.org/2009/dap/ReSpec.js/documentation.html>
     7 
     8   From time to time it's necessary to HTML5 Tidy this document using the tool @ <http://w3c.github.com/tidy-html5/>.
     9 
    10   The command used to format this document (Overview.src.html) is as follows (replacing all = signs with - signs!):
    11 
    12   @> 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
    13 
    14   To publish a new compiled version (Overview.html), we need to open this file (Overview.src.html) in any web browser.
    15   Once it has loaded we press 'Ctrl + Shift + S' (or 'Cmd' + 'Shift' + 'S' on Mac) and then select
    16   'Save as HTML (Source)' from the resulting menu.
    17 
    18   We then replace Overview.html with the produced HTML source of this process.
    19 
    20   Next we run HTML5 Tidy over our new Overview.html file with the following command (replacing all = signs with - signs!):
    21 
    22   @> 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
    23 
    24   Now the specification is ready to be published :)
    25 
    26 -->
    27 <html>
    28   <head>
    29     <title>
    30       Network Service Discovery
    31     </title>
    32     <meta http-equiv='Content-Type'
    33           content='text/html; charset=utf-8'>
    34     <script type="text/javascript"
    35           class='remove'>
    36 var respecConfig = {
    37           specStatus:   "ED",
    38           //publishDate:  "2012-10-04",
    39           shortName:    "discovery-api",
    40           edDraftURI:   "http://dvcs.w3.org/hg/dap/raw-file/tip/discovery-api/Overview.html",
    41           previousMaturity: "FPWD",
    42           previousPublishDate: "2012-08-07",
    43           editors: [
    44             {
    45               name:       "Rich Tibbett",
    46               //url:        "http://richt.me/",
    47               company:    "Opera Software ASA",
    48               companyURL: "http://opera.com/"
    49             },
    50             {
    51               name:       "Clarke Stevens",
    52               //url:      "",
    53               company:    "CableLabs",
    54               companyURL: "http://cablelabs.com/"
    55             }
    56           ],
    57 
    58           wg:           "Device APIs Working Group",
    59           wgURI:        "http://www.w3.org/2009/dap/",
    60           wgPublicList: "public-device-apis",
    61           wgPatentURI:  "http://www.w3.org/2004/01/pp-impl/43696/status",
    62 
    63           extraCSS:     ["http://dvcs.w3.org/hg/dap/raw-file/tip/discovery-api/css/respec.nsd.css"],
    64 //          inlineCSS:    true,
    65           noIDLIn:      true
    66 
    67       };
    68     </script>
    69     <script src='http://www.w3.org/Tools/respec/respec-w3c-common'
    70           type="text/javascript"
    71           class='remove'
    72           async=""></script>
    73   </head>
    74   <body>
    75     <section id='abstract'>
    76       <p>
    77         This specification defines a mechanism for an HTML document to discover and subsequently communicate with
    78         <abbr title="Hypertext Transfer Protocol">HTTP</abbr>-based services advertised via common discovery protocols
    79         within the current network.
    80       </p>
    81     </section>
    82     <section id='sotd'>
    83       <p>
    84         This document represents the early consensus of the group on the scope and features of the proposed API.
    85       </p>
    86     </section>
    87     <section class="informative">
    88       <h3>
    89         Introduction
    90       </h3>
    91       <p>
    92         To enable Web pages to connect and communicate with Local-networked Services provided over HTTP, this
    93         specification introduces the <a href="#navigatornetworkservice"><code>NavigatorNetworkService</code></a>
    94         interface.
    95       </p>
    96       <p>
    97         Using this <abbr title="Application Programming Interface">API</abbr> consists of requesting a well-known
    98         service type, known by developers and advertised by Local-networked Devices. User authorization, where the user
    99         connects the web page to discovered services, is expected before the web page is able to interact with any
   100         Local-networked Services.
   101       </p>
   102       <p>
   103         A web page creates a request to obtain connectivity to services running in the network by specifying a
   104         well-known discovery service type that it wishes to interact with.
   105       </p>
   106       <p>
   107         The user agent, having captured all advertised services on the network from the <a>service discovery
   108         mechanisms</a> included in this recommendation, attempts to match the requested service type to a discovered
   109         service according to the processing described herein.
   110       </p>
   111       <p>
   112         If a service connectivity request is successful then the Web page is provided with the necessary information to
   113         communicate with the authorized Local-networked Service. If the request fails then the Web page will receive an
   114         error callback containing an error code describing the cause of Local-networked Service connectivity failure.
   115       </p>
   116       <p>
   117         Once connected to a Local-networked Service the Web page can send requests and receive responses to the
   118         Local-networked Service via the messaging format and appropriate channel inferred from the service type
   119         authorized via the provided API. The Web page, once connected, can also receive service-pushed events, in the
   120         messaging format supported by the Local-networked Device, if such event subscription functionality is provided
   121         by the connected Local-networked Service.
   122       </p>
   123       <p>
   124         Services available within the local network can connect and disconnect at different times during the execution of a web page. The user agent can inform a web page when the state of networked services matching any of the requested valid service types 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=
   125         "#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a> method defined herein).
   126       </p>
   127       <div class="example">
   128         <p>
   129           Example of requesting a DNS-SD advertised service:
   130         </p>
   131         <hr>
   132         <pre class="highlight">
   133 function showServices( services ) {
   134   // Show a list of all the services provided to the web page
   135   for(var i = 0, l = services.length; i &lt; l; i++) console.log( services[i].name );
   136 }
   137 
   138 navigator.getNetworkServices('zeroconf:_boxee-jsonrpc._tcp', showServices);
   139 </pre>
   140       </div>
   141       <div class="example">
   142         <p>
   143           Example of requesting a UPnP advertised service, also handling error conditions:
   144         </p>
   145         <hr>
   146         <pre class="highlight">
   147 function showServices( services ) {
   148   // Show a list of all the services provided to the web page
   149   for(var i = 0, l = services.length; i &lt; l; i++) console.log( services[i].name );
   150 }
   151 
   152 function error( e ) {
   153   console.log( "Error occurred: " + e.code );
   154 }
   155 
   156 navigator.getNetworkServices('upnp:urn:schemas-upnp-org:service:ContentDirectory:1', showServices, error);
   157 </pre>
   158       </div>
   159       <div class="example">
   160         <p>
   161           Example of requesting either a DNS-SD or UPnP advertised service:
   162         </p>
   163         <hr>
   164         <pre class="highlight">
   165 function showServices( services ) {
   166   // Show a list of all the services provided to the web page (+ service type)
   167   for(var i = 0, l = services.length; i &lt; l; i++)
   168      console.log( services[i].name + '(' + services[i].type + ')' );
   169 }
   170 
   171 navigator.getNetworkServices([
   172   'zeroconf:_boxee-jsonrpc._tcp',
   173   'upnp:urn:schemas-upnp-org:service:ContentDirectory:1'
   174 ], showServices);
   175 </pre>
   176       </div>
   177       <p>
   178         For more detailed examples see the <a href="#examples">Examples</a> section.
   179       </p>
   180     </section>
   181     <section id='conformance'>
   182       <p>
   183         Requirements phrased in the imperative as part of algorithms (such as "strip any leading space characters" or
   184         "return false and abort these steps") are to be interpreted with the meaning of the key word ("must", "should",
   185         "may", etc) used in introducing the algorithm.
   186       </p>
   187       <p>
   188         Some conformance requirements are phrased as requirements on attributes, methods or objects. Such requirements
   189         are to be interpreted as requirements on user agents.
   190       </p>
   191       <p>
   192         Conformance requirements phrased as algorithms or specific steps MAY be implemented in any manner, so long as
   193         the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be
   194         easy to follow, and not intended to be performant.)
   195       </p>
   196       <p>
   197         The only conformance class defined by this specification is a <dfn>user agent</dfn>.
   198       </p>
   199       <p>
   200         User agents MAY impose implementation-specific limits on otherwise unconstrained inputs, e.g. to prevent denial
   201         of service attacks, to guard against running out of memory, or to work around platform-specific limitations.
   202       </p>
   203       <p>
   204         When support for a feature is disabled (e.g. as an emergency measure to mitigate a security problem, or to aid
   205         in development, or for performance reasons), user agents MUST act as if they had no support for the feature
   206         whatsoever, and as if the feature was not mentioned in this specification. For example, if a particular feature
   207         is accessed via an attribute in a Web IDL interface, the attribute itself would be omitted from the objects
   208         that implement that interface - leaving the attribute on the object but making it return null or throw an
   209         exception is insufficient.
   210       </p>
   211       <section>
   212         <h3>
   213           Dependencies
   214         </h3>This specification relies on several other underlying specifications.
   215         <dl>
   216           <dt>
   217             HTML
   218           </dt>
   219           <dd>
   220             Many fundamental concepts from HTML are used by this specification. [[!HTML5]]
   221           </dd>
   222           <dt>
   223             WebIDL
   224           </dt>
   225           <dd>
   226             The IDL blocks in this specification use the semantics of the WebIDL specification. [[!WEBIDL]]
   227           </dd>
   228         </dl>
   229       </section>
   230     </section>
   231     <section>
   232       <h3>
   233         Terminology
   234       </h3>
   235       <p>
   236         The construction "a <code>Foo</code> object", where <code>Foo</code> is actually an interface, is sometimes
   237         used instead of the more accurate "an object implementing the interface <code>Foo</code>".
   238       </p>
   239       <p>
   240         The term DOM is used to refer to the API set made available to scripts in Web applications, and does not
   241         necessarily imply the existence of an actual <code>Document</code> object or of any other <code>Node</code>
   242         objects as defined in the DOM Core specifications. [[!DOM4]]
   243       </p>
   244       <p>
   245         An IDL attribute is said to be <em>getting</em> when its value is being retrieved (e.g. by author script), and
   246         is said to be <em>setting</em> when a new value is assigned to it.
   247       </p>
   248       <p>
   249         A <dfn>valid service type</dfn> is any of the following:
   250       </p>
   251       <ul>
   252         <li>a string that begins with <code>upnp:</code> or <code>zeroconf:</code> followed by one or more characters
   253         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,
   254         U+005E to U+007E.
   255         </li>
   256         <li>a string that begins with <code>dial:</code> followed by an integer version.
   257         </li>
   258       </ul>
   259       <p>
   260         A <a>valid service type</a> provided in the <code>type</code> attribute of the <a href=
   261         "#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a> method will be matched against the
   262         services currently contained in the <a>list of available service records</a> according to the algorithms
   263         defined in this specification.
   264       </p>
   265       <p>
   266         A <dfn>user-agent generated callback url</dfn> is a Local-network accessible URL endpoint that a <a>user
   267         agent</a> generates and maintains for receiving HTTP NOTIFY requests from UPnP Event sources. It is only
   268         required when the user agent implements UPnP Service Discovery as defined in this specification.
   269       </p>
   270       <p>
   271         In this specification we use the following terms to describe the processes required for Local-networked
   272         Services management:
   273       </p>
   274       <ul>
   275         <li>A <dfn>new service</dfn> is a Local-networked Service that has not previously been discovered or registered
   276         in the <a>list of available service records</a>.
   277         </li>
   278         <li>An <dfn>existing service</dfn> is a Local-networked Service that has previously been discovered and is
   279         registered in the <a>list of available service records</a>.
   280         </li>
   281         <li>A <dfn>current service</dfn> is a Local-networked Service, represented by a <a href=
   282         "#networkservice"><code>NetworkService</code></a> object, that is currently being shared with a web page via a
   283         <a href="#networkservices"><code>NetworkServices</code></a> object registered in the <a>list of active service
   284         managers</a>.
   285         </li>
   286       </ul>
   287     </section>
   288     <section>
   289       <h2>
   290         Requesting networked services
   291       </h2>
   292       <pre class="widl">
   293 [Supplemental, NoInterfaceObject]
   294 interface <dfn id="navigatornetworkservice">NavigatorNetworkService</dfn> {
   295   // Obtain a Local-networked Service
   296   void <a href="#dom-navigator-getnetworkservices">getNetworkServices</a>( in any type,
   297                            in <a href=
   298 "#navigatornetworkservicesuccesscallback">NavigatorNetworkServiceSuccessCallback</a> successCallback,
   299                            in optional <a href=
   300 "#navigatornetworkserviceerrorcallback">NavigatorNetworkServiceErrorCallback</a> errorCallback );
   301 };
   302 <a class="externalDFN"
   303      href=
   304      "http://www.whatwg.org/specs/web-apps/current-work/complete/timers.html#navigator">Navigator</a> implements <a href=
   305      "#navigatornetworkservice">NavigatorNetworkService</a>;
   306 
   307 [Callback=FunctionOnly, NoInterfaceObject]
   308 interface <dfn id="navigatornetworkservicesuccesscallback">NavigatorNetworkServiceSuccessCallback</dfn> {
   309   void handleEvent( in <a href="#networkservices">NetworkServices</a> services );
   310 };
   311 
   312 [NoInterfaceObject]
   313 interface <dfn id="navigatornetworkserviceerror">NavigatorNetworkServiceError</dfn> {
   314   const unsigned short <a href="#dom-navigatornetworkserviceerror-permission_denied">PERMISSION_DENIED_ERR</a> = 1;
   315   const unsigned short <a href="#dom-navigatornetworkserviceerror-unknown_type_prefix">UNKNOWN_TYPE_PREFIX_ERR</a> = 2;
   316   readonly attribute unsigned short <a href="#dom-navigatornetworkserviceerror-code">code</a>;
   317 };
   318 
   319 [Callback=FunctionOnly, NoInterfaceObject]
   320 interface <dfn id="navigatornetworkserviceerrorcallback">NavigatorNetworkServiceErrorCallback</dfn> {
   321   void handleEvent( in <a href="#navigatornetworkserviceerror">NavigatorNetworkServiceError</a> error );
   322 };
   323 </pre>
   324       <section>
   325         <h2>
   326           Methods
   327         </h2>
   328         <dl class="domintro">
   329           <dt>
   330             <var title="">window</var> . <code title="dom-navigator"><a href=
   331             "http://www.whatwg.org/specs/web-apps/current-work/complete/timers.html#navigator">navigator</a></code> .
   332             <code title="dom-navigator-getNetworkServices"><a href=
   333             "#dom-navigator-getnetworkservices">getNetworkServices</a></code> ( <var title="">type</var> , <var title=
   334             "">successCallback</var> [, <var title="">errorCallback</var> ] )
   335           </dt>
   336           <dd>
   337             <p>
   338               Prompts the user to select discovered network services that have advertised support for the requested
   339               service type.
   340             </p>
   341             <p>
   342               The <var title="">type</var> argument contains one or more <a>valid service type</a> tokens that the web
   343               page would like to interact with.
   344             </p>
   345             <p>
   346               If the user accepts, the <var title="">successCallback</var> is invoked, with zero or more <a href=
   347               "#networkservice"><code>NetworkService</code></a> objects as its argument.
   348             </p>
   349             <p>
   350               If the user declines, the <var title="">errorCallback</var> (if any) is invoked.
   351             </p>
   352           </dd>
   353         </dl>
   354         <div>
   355           <p>
   356             When the <dfn id="dom-navigator-getnetworkservices"
   357                title="dom-navigator-getnetworkservices"><code>getNetworkServices(type, successCallback[,
   358                errorCallback])</code></dfn> method is called, the <a>user agent</a> MUST run the following steps:
   359           </p>
   360           <ol class="rule">
   361             <li>Let <var>requested control types</var> be initially set to an empty array.
   362             </li>
   363             <li>If <var>type</var> is an array consisting of one or more <a>valid service type</a> tokens, then let
   364             <var>requested control types</var> by the value of <var>type</var>, removing any non-<a>valid service
   365             type</a> tokens from the resulting array.
   366             </li>
   367             <li>If <var>type</var> is a string consisting of one <a>valid service type</a> token, then let
   368             <var>requested control types</var> be an array containing one item with a value of <var>type</var>.
   369             </li>
   370             <li>If <var>requested control types</var> is an array that contains at least one or more <a title=
   371             "valid service type">valid service type</a> tokens then continue to the step labeled <em>process</em>
   372             below. Otherwise, the <a>user agent</a> MUST <a href=
   373             "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
   374                   class="externalDFN">queue a task</a> to invoke <var>errorCallback</var>, if it is provided and is an
   375                   object of type <code>Function</code>, with a new <a href=
   376                   "#navigatornetworkserviceerror"><code>NavigatorNetworkServiceError</code></a> object whose <a href=
   377                   "#dom-navigatornetworkserviceerror-code"><code>code</code></a> attribute has the numeric value 2
   378                   (<a href=
   379                   "#dom-navigatornetworkserviceerror-unknown_type_prefix"><code>UNKNOWN_TYPE_PREFIX_ERR</code></a>) as
   380                   its argument, abort any remaining steps and return.
   381             </li>
   382             <li>
   383               <em>Process</em>: Let <var>services found</var> be an empty array.
   384             </li>
   385             <li>For each <var>available service</var> in the <a>list of available service records</a> run the following
   386             steps:
   387               <ol class="rule">
   388                 <li>For each <var>requested control type</var> in <var>requested control types</var>: If <var>available
   389                 service</var>'s <code>type</code> attribute equals the <var>requested control type</var> then let <var>
   390                   matched service</var> equal the value of <var>available service</var> and continue at the step
   391                   labeled <var>attach</var> below.
   392                 </li>
   393                 <li>Continue at the next <var>available service</var>.
   394                 </li>
   395                 <li>
   396                   <em>Attach</em>: If <var>matched service</var> is not empty then run the following steps:
   397                   <ol class="rule">
   398                     <li>Let <var>new service object</var> be a new <a href=
   399                     "#networkservice"><code>NetworkService</code></a> object, mapping the parameters of <var>matched
   400                     service</var> to this new object where possible.
   401                     </li>
   402                     <li>Append <var>new service object</var> to the <var>services found</var> array.
   403                     </li>
   404                   </ol>
   405                 </li>
   406               </ol>
   407             </li>
   408             <li>Return, and run the remaining steps asynchronously.
   409             </li>
   410             <li>Optionally, e.g. based on a previously-established user preference, for security reasons, or due to
   411             platform limitations, the <a>user agent</a> MAY <a href=
   412             "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
   413                   class="externalDFN">queue a task</a> to invoke <var>errorCallback</var>, if it is provided and is an
   414                   object of type <code>Function</code>, with a new <a href=
   415                   "#navigatornetworkserviceerror"><code>NavigatorNetworkServiceError</code></a> object whose <a href=
   416                   "#dom-navigatornetworkserviceerror-code"><code>code</code></a> attribute has the numeric value 1
   417                   (<a href=
   418                   "#dom-navigatornetworkserviceerror-permission_denied"><code>PERMISSION_DENIED_ERR</code></a>) as its
   419                   argument, abort any remaining steps and return.
   420             </li>
   421             <li>The user agent MUST NOT provide the entry script's origin with a <a href=
   422             "#networkservices"><code>NetworkServices</code></a> object without prior permission given by the user.
   423               <p>
   424                 If <var>services found</var> is not an empty array then the <a>user agent</a> MAY choose to prompt the
   425                 user in a user-agent-specific manner for permission to provide the <a href=
   426                 "http://www.whatwg.org/specs/web-apps/current-work/complete/browsers.html#entry-script"
   427                    class="externalDFN">entry script</a>'s <a href=
   428                    "http://www.whatwg.org/specs/web-apps/current-work/complete/origin-0.html#origin"
   429                    class="externalDFN">origin</a> with a <a href="#networkservices"><code>NetworkServices</code></a>
   430                    object representing the <a>user-authorized</a> subset of <var>services found</var>.
   431               </p>
   432               <p>
   433                 Alternatively, the user agent MAY choose to queue a task to invoke <var>successCallback</var> or
   434                 <var>errorCallback</var> immediately based on a previously-established user preference, for security
   435                 reasons, or due to platform limitations. In such an implementation, if <var>successCallback</var> is to
   436                 be fired as a result of a previously-established user preference then the <a>user agent</a> MUST
   437                 continue at the next step of this algorithm.
   438               </p>
   439               <p>
   440                 If permission has been granted by the user to access one or more networked services then the <a>user
   441                 agent</a> SHOULD include an "ongoing local-network communication" indicator.
   442               </p>
   443               <p>
   444                 If permission has been denied by the user, <a>user agent</a> or platform, then the <a>user agent</a>
   445                 MUST <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
   446                    class="externalDFN">queue a task</a> to invoke <var>errorCallback</var>, if it is provided and is an
   447                    object of type <code>Function</code>, with a new <a href=
   448                    "#navigatornetworkserviceerror"><code>NavigatorNetworkServiceError</code></a> object whose <a href=
   449                    "#dom-navigatornetworkserviceerror-code"><code>code</code></a> attribute has the numeric value 1
   450                    (<a href=
   451                    "#dom-navigatornetworkserviceerror-permission_denied"><code>PERMISSION_DENIED_ERR</code></a>) as its
   452                    argument, abort any remaining steps and return.
   453               </p>
   454               <p>
   455                 If the user never responds or no previously-established user preference has been met, this algorithm
   456                 stalls on this step.
   457               </p>
   458             </li>
   459             <li>Let <var>services</var> be an empty array.
   460             </li>
   461             <li>If <var>services found</var> is not an empty array then set <var>services</var> to be an array of one
   462             or more <a href="#networkservice"><code>NetworkService</code></a> objects for which the user granted
   463             permission above - known as the current objects <dfn>user-authorized</dfn> services.
   464             </li>
   465             <li>Remove all previously whitelisted urls from the <a>entry script origin's URL whitelist</a> granted in
   466             the current <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/browsers.html#entry-script"
   467                   class="externalDFN">entry script</a>'s <a href=
   468                   "http://www.whatwg.org/specs/web-apps/current-work/complete/origin-0.html#origin"
   469                   class="externalDFN">origin</a>.
   470             </li>
   471             <li>For each Object <var>service</var> in <var>services</var>, if any, run the following sub-steps:
   472               <ol class="rule">
   473                 <li>Add the <var>service</var>'s <code>url</code> parameter to the <a>entry script origin's
   474                   <abbr title="Uniform Resource Locator">URL</abbr> whitelist</a>.
   475                 </li>
   476                 <li>If <var>service</var>'s <code>type</code> parameter begins with the DOMString "<code>upnp:</code>"
   477                 and the <var>service</var>'s <code>eventsUrl</code> parameter is not empty then <a>setup a UPnP Events
   478                 Subscription</a> for <var>service</var>.
   479                 </li>
   480               </ol>
   481             </li>
   482             <li>Let <var>services manager</var> be a new <a href="#networkservices"><code>NetworkServices</code></a>
   483             object.
   484             </li>
   485             <li>Store <var>requested control types</var> against <var>services manager</var> as an internal variable.
   486             </li>
   487             <li>Set <var>services manager</var>'s <a href=
   488             "#dom-networkservices-servicesavailable"><code>servicesAvailable</code></a> attribute to the number of
   489             items currently found in the <a>list of available service records</a> whose <code>type</code> property
   490             matches any of the tokens requested in <var>requested control types</var>.
   491             </li>
   492             <li>Add <var>services</var>, if any, to the <var>services manager</var> object as its collection of
   493             <a>indexed properties</a>. If <var>services</var> is an empty array then the <var>services manager</var>
   494             does not have any <var>indexed properties</var>.
   495             </li>
   496             <li>Set <var>services manager</var>'s <a href="#dom-networkservices-length"><code>length</code></a>
   497             attribute to the number of items in <var>services</var>.
   498             </li>
   499             <li>Add <var>services manager</var> to the <a>list of active service managers</a>.
   500             </li>
   501             <li>The <a>user agent</a> MUST <a href=
   502             "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
   503                   class="externalDFN">queue a task</a> to invoke <var>successCallback</var> with <var>services
   504                   manager</var> as its argument.
   505             </li>
   506           </ol>
   507           <p>
   508             The <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#task-source"
   509                class="externalDFN">task source</a> for these <a href=
   510                "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#concept-task"
   511                class="externalDFN">tasks</a> is the <a href=
   512                "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#user-interaction-task-source"
   513                class="externalDFN">user interaction task source</a>.
   514           </p>
   515           <p>
   516             When a <a href="#networkservice"><code>NetworkService</code></a> object is provided to a Web page, the
   517             <a>user agent</a> MUST add the <code>url</code> property to the <dfn>entry script origin's URL
   518             whitelist</dfn>. This list enables the Web page to override and initiate cross-site resource requests
   519             towards these URLs, and any sub-resources of these URLs, within the current <a href=
   520             "http://www.whatwg.org/specs/web-apps/current-work/complete/browsers.html#entry-script"
   521                class="externalDFN">entry script</a>'s <a href=
   522                "http://www.whatwg.org/specs/web-apps/current-work/complete/origin-0.html#origin"
   523                class="externalDFN">origin</a> via various existing mechanisms (e.g. Web Sockets, Server-Sent Events,
   524                Web Messaging, XMLHttpRequest).
   525           </p>
   526           <p>
   527             If the user navigates away from the <a href=
   528             "http://www.whatwg.org/specs/web-apps/current-work/complete/browsers.html#entry-script"
   529                class="externalDFN">entry script</a>'s <a href=
   530                "http://www.whatwg.org/specs/web-apps/current-work/complete/origin-0.html#origin"
   531                class="externalDFN">origin</a> then the <a>user agent</a> <em class="ct">MUST</em> remove all previously
   532                whitelisted urls from the <a>entry script origin's URL whitelist</a>. There is no persistence to network
   533                service selections provided to a web page. It is not possible to access a previously white-listed
   534                networked service without the necessary user authorization in all of the following cases:
   535           </p>
   536           <ul>
   537             <li>If the current script is reloaded at any point in the same or different window.
   538             </li>
   539             <li>if the current script reinvokes the <a href=
   540             "#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a> method at any point in its
   541             execution.
   542             </li>
   543             <li>If the user navigates forward or back in their history to reload the current page.
   544             </li>
   545             <li>If a script is running in a different origin.
   546             </li>
   547           </ul>
   548         </div>
   549       </section>
   550       <section>
   551         <h3>
   552           Error Handling
   553         </h3>
   554         <dl class="domintro">
   555           <dt>
   556             <var title="">error</var> . <code title="dom-NavigatorNetworkServiceError-code"><a href=
   557             "#dom-navigatornetworkserviceerror-code">code</a></code>
   558           </dt>
   559           <dd>
   560             <p>
   561               Returns the current error's error code. At the current time, this will be <code>1</code> or
   562               <code>2</code>, for which the corresponding error constants <a href=
   563               "#dom-navigatornetworkserviceerror-permission_denied"><code>PERMISSION_DENIED_ERR</code></a> and <a href=
   564               "#dom-navigatornetworkserviceerror-unknown_type_prefix"><code>UNKNOWN_TYPE_PREFIX_ERR</code></a> are
   565               defined.
   566             </p>
   567           </dd>
   568         </dl>
   569         <p>
   570           The <dfn id="dom-navigatornetworkserviceerror-code"
   571              title="dom-navigatornetworkserviceerror-code"><code>code</code></dfn> attribute of a <a href=
   572              "#navigatornetworkserviceerror"><code>NavigatorNetworkServiceError</code></a> object <em class=
   573              "ct">MUST</em> return the code for the error, which will be one of the following:
   574         </p>
   575         <dl>
   576           <dt>
   577             <dfn id="dom-navigatornetworkserviceerror-permission_denied"
   578                 title="dom-navigatornetworkserviceerror-permission_denied"><code>PERMISSION_DENIED_ERR</code></dfn>
   579                 (numeric value 1)
   580           </dt>
   581           <dd>
   582             The user or user agent denied the page permission to access any services.
   583           </dd>
   584           <dt>
   585             <dfn id="dom-navigatornetworkserviceerror-unknown_type_prefix"
   586                 title="dom-navigatornetworkserviceerror-unknown_type_prefix"><code>UNKNOWN_TYPE_PREFIX_ERR</code></dfn>
   587                 (numeric value 2)
   588           </dt>
   589           <dd>
   590             No <a>valid service type</a> tokens were provided in the method invocation.
   591           </dd>
   592         </dl>
   593       </section>
   594     </section>
   595     <section>
   596       <h2>
   597         Obtaining networked services
   598       </h2>
   599       <p>
   600         The <a href="#networkservices"><code>NetworkServices</code></a> interface represents a collection of zero or
   601         more <dfn>indexed properties</dfn> that are each a <a>user-authorized</a> <a href=
   602         "#networkservice"><code>NetworkService</code></a> object.
   603       </p>
   604       <p>
   605         A <a href="#networkservices"><code>NetworkServices</code></a> object is the top level success callback
   606         parameter from a call to <a href="#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a>.
   607       </p>
   608       <pre class="widl">
   609 [NoInterfaceObject]
   610 interface <dfn id="networkservices">NetworkServices</dfn> {
   611   readonly attribute unsigned long    <a href="#dom-networkservices-length">length</a>;
   612   getter <a href="#networkservice">NetworkService</a> (unsigned long index);
   613   <a href="#networkservice">NetworkService</a>? <a href=
   614 "#dom-networkservices-getservicebyid">getServiceById</a>(DOMString id);
   615 
   616   readonly attribute unsigned long    <a href="#dom-networkservices-servicesavailable">servicesAvailable</a>;
   617 
   618   // event handler attributes
   619            attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
   620      class="externalDFN">EventHandler</a>     <a href="#dom-networkservices-onserviceavailable">onserviceavailable</a>;
   621            attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
   622      class="externalDFN">EventHandler</a>     <a href=
   623      "#dom-networkservices-onserviceunavailable">onserviceunavailable</a>;
   624 
   625 };
   626 
   627 <a href="#networkservices">NetworkServices</a> implements <a href=
   628 "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-eventtarget"
   629      class="externalDFN">EventTarget</a>;
   630 </pre>
   631 
   632       <section>
   633         <h2>
   634           Attributes
   635         </h2>
   636         <dl class="domintro">
   637           <dt>
   638             <code title="dom-networkservices-length"><a href="#dom-networkservices-length">length</a></code>
   639           </dt>
   640           <dd>
   641             <p>
   642               Returns the current number of <a>indexed properties</a> in the current object's collection.
   643             </p>
   644           </dd>
   645           <dt>
   646             <code title="dom-networkservices-servicesavailable"><a href=
   647             "#dom-networkservices-servicesavailable">servicesAvailable</a></code>
   648           </dt>
   649           <dd>
   650             <p>
   651               Returns the current number of items matching one of the app-requested <a>valid service type</a> tokens in
   652               the <a>list of available service records</a>.
   653             </p>
   654           </dd>
   655         </dl>
   656         <div>
   657           <p>
   658             The <dfn id="dom-networkservices-length"><code>length</code></dfn> attribute MUST return the number of
   659             <a href="#networkservice"><code>NetworkService</code></a> objects represented by the collection.
   660           </p>
   661           <p>
   662             The <dfn id="dom-networkservices-servicesavailable"><code>servicesAvailable</code></dfn> attribute MUST
   663             return the number of services in the <a>list of available service records</a> whose <code>type</code>
   664             attribute matches any of the <a>valid service type</a> tokens that were initially used to create the current
   665             <a href="#networkservices"><code>NetworkServices</code></a> object.
   666           </p>
   667         </div>
   668       </section>
   669       <section>
   670         <h2>
   671           Methods
   672         </h2>
   673         <dl class="domintro">
   674           <dt>
   675             <code title="networkservices-getter"><a href="#networkservices">services</a></code> [ <var title=
   676             "">index</var> ]
   677           </dt>
   678           <dd>
   679             <p>
   680               Returns the specified <a href="#networkservice"><code>NetworkService</code></a> object.
   681             </p>
   682           </dd>
   683           <dt>
   684             <code title="networkservices-getter"><a href="#networkservices">services</a></code> . <code title=
   685             "dom-networkservices-getservicebyid"><a href=
   686             "#dom-networkservices-getservicebyid">getServiceById</a></code> ( <var title="">id</var> )
   687           </dt>
   688           <dd>
   689             <p>
   690               Returns the <a href="#networkservice"><code>NetworkService</code></a> object with the given identifier,
   691               or null if no service has that identifier.
   692             </p>
   693           </dd>
   694         </dl>
   695         <p>
   696           A <a href="#networkservices"><code>NetworkServices</code></a> object represents the current collection of
   697           zero or more <a href="#networkservice"><code>NetworkService</code></a> objects - its <a>indexed
   698           properties</a>. The <a>indexed properties</a> of a <a href=
   699           "#networkservices"><code>NetworkServices</code></a> object are <span>immutable</span> meaning that <a>indexed
   700           properties</a> cannot be added and <a>indexed properties</a> cannot be removed for the lifetime of a <a href=
   701           "#networkservices"><code>NetworkServices</code></a> object.
   702         </p>
   703         <p>
   704           The <a href=
   705           "http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#supported-property-indices"
   706              class="externalDFN">supported property indices</a> of <a href=
   707              "#networkservices"><code>NetworkServices</code></a> objects at any instant are the numbers from zero to
   708              the number of the <a href="#networkservice"><code>NetworkService</code></a> objects in the collection
   709              minus one.
   710         </p>
   711         <p class="note">
   712           Each service in a <a href="#networkservices"><code>NetworkServices</code></a> object thus has an index; the
   713           first has the index 0, and each subsequent service is numbered one higher than the previous one.
   714         </p>
   715         <p>
   716           To <a href=
   717           "http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#determine-the-value-of-an-indexed-property"
   718              class="externalDFN">determine the value of an indexed property</a> for a given index <var>index</var> in a
   719              <a href="#networkservices"><code>NetworkServices</code></a> object the user agent MUST return the <a href=
   720              "#networkservice"><code>NetworkService</code></a> object that represents the <var>index</var>th item in
   721              the collection.
   722         </p>
   723         <p>
   724           The <dfn id="dom-networkservices-getservicebyid"><code>getServiceById(id)</code></dfn> method <em class=
   725           "ct">MUST</em> return the first <a href="#networkservice"><code>NetworkService</code></a> object in the
   726           collection whose <a href="#dom-networkservice-id"><code>id</code></a> attribute is equal to the value of the
   727           <var>id</var> argument provided. When no <a href="#networkservice"><code>NetworkService</code></a> objects
   728           match the given argument, the method MUST return null.
   729         </p>
   730       </section>
   731       <section>
   732         <h2>
   733           Events
   734         </h2>
   735         <p>
   736           The following are the event handlers (and their corresponding event handler event types) that <em class=
   737           "ct">MUST</em> be supported, as IDL attributes, by all objects implementing the <a href=
   738           "#networkservices"><code>NetworkServices</code></a> interface:
   739         </p>
   740         <table border="1">
   741           <thead>
   742             <tr>
   743               <th>
   744                 <span title="event handlers">Event handler</span>
   745               </th>
   746               <th>
   747                 <span>Event handler event type</span>
   748               </th>
   749             </tr>
   750           </thead>
   751           <tbody>
   752             <tr>
   753               <td>
   754                 <dfn id="dom-networkservices-onserviceavailable"
   755                     title="dom-NetworkServices-onserviceavailable"><code>onserviceavailable</code></dfn>
   756               </td>
   757               <td>
   758                 <a href="#event-serviceavailable"><code>serviceavailable</code></a>
   759               </td>
   760             </tr>
   761             <tr>
   762               <td>
   763                 <dfn id="dom-networkservices-onserviceunavailable"
   764                     title="dom-NetworkServices-onserviceunavailable"><code>onserviceunavailable</code></dfn>
   765               </td>
   766               <td>
   767                 <a href="#event-serviceunavailable"><code>serviceunavailable</code></a>
   768               </td>
   769             </tr>
   770           </tbody>
   771         </table>
   772       </section>
   773     </section>
   774     <section>
   775       <h2>
   776         Communicating with a networked service
   777       </h2>
   778       <p>
   779         The <a href="#networkservice"><code>NetworkService</code></a> interface is used to provide a set of connection
   780         information for an HTTP service endpoint and if available, service events, running on a networked device.
   781       </p>
   782       <pre class="widl">
   783 [NoInterfaceObject]
   784 interface <dfn id="networkservice">NetworkService</dfn> {
   785   readonly attribute DOMString        <a href="#dom-networkservice-id">id</a>;
   786   readonly attribute DOMString        <a href="#dom-networkservice-name">name</a>;
   787   readonly attribute DOMString        <a href="#dom-networkservice-type">type</a>;
   788   readonly attribute DOMString        <a href="#dom-networkservice-url">url</a>;
   789   readonly attribute DOMString        <a href="#dom-networkservice-config">config</a>;
   790 
   791   readonly attribute boolean          <a href="#dom-networkservice-online">online</a>;
   792 
   793   // event handler attributes
   794            attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
   795      class="externalDFN">EventHandler</a>     <a href="#dom-networkservice-onserviceonline">onserviceonline</a>;
   796            attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
   797      class="externalDFN">EventHandler</a>     <a href="#dom-networkservice-onserviceoffline">onserviceoffline</a>;
   798 
   799            attribute <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler"
   800      class="externalDFN">EventHandler</a>     <a href="#dom-networkservice-onnotify">onnotify</a>;
   801 };
   802 
   803 <a href="#networkservice">NetworkService</a> implements <a href=
   804 "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-eventtarget"
   805      class="externalDFN">EventTarget</a>;
   806 </pre>
   807       <section>
   808         <h2>
   809           Attributes
   810         </h2>
   811         <dl class="domintro">
   812           <dt>
   813             <var title="">service</var> . <code title="dom-networkservice-id"><a href=
   814             "#dom-networkservice-id">id</a></code>
   815           </dt>
   816           <dd>
   817             <p>
   818               A unique identifier for the given user-selected service instance.
   819             </p>
   820           </dd>
   821           <dt>
   822             <var title="">service</var> . <code title="dom-networkservice-name"><a href=
   823             "#dom-networkservice-name">name</a></code>
   824           </dt>
   825           <dd>
   826             <p>
   827               The name of the user-selected service.
   828             </p>
   829           </dd>
   830           <dt>
   831             <var title="">service</var> . <code title="dom-networkservice-type"><a href=
   832             "#dom-networkservice-type">type</a></code>
   833           </dt>
   834           <dd>
   835             <p>
   836               The <a>valid service type</a> token value of the user-selected service.
   837             </p>
   838           </dd>
   839           <dt>
   840             <var title="">service</var> . <code title="dom-networkservice-url"><a href=
   841             "#dom-networkservice-url">url</a></code>
   842           </dt>
   843           <dd>
   844             <p>
   845               The control URL endpoint (including any required port information) of the user-selected control service
   846               that has been added to the <a>entry script origin's URL whitelist</a>.
   847             </p>
   848           </dd>
   849           <dt>
   850             <var title="">service</var> . <code title="dom-networkservice-config"><a href=
   851             "#dom-networkservice-config">config</a></code>
   852           </dt>
   853           <dd>
   854             <p>
   855               The configuration information associated with the service depending on the requested service type.
   856             </p>
   857           </dd>
   858         </dl>
   859         <p>
   860           The <dfn id="dom-networkservice-id"><code>id</code></dfn> attribute is a unique identifier for the service.
   861           The same service provided at different times or on different objects MUST have the same <a href=
   862           "#dom-networkservice-id"><code>id</code></a> value.
   863         </p>
   864         <p>
   865           The <dfn id="dom-networkservice-name"><code>name</code></dfn> attribute represents a human-readable title for
   866           the service.
   867         </p>
   868         <p>
   869           The <dfn id="dom-networkservice-type"><code>type</code></dfn> attribute reflects the value of the <a>valid
   870           service type</a> of the service.
   871         </p>
   872         <p>
   873           The <dfn id="dom-networkservice-url"><code>url</code></dfn> attribute is an <a href=
   874           "http://www.w3.org/TR/html5/urls.html#absolute-url"
   875              class="externalDFN">absolute URL</a> pointing to the root HTTP endpoint for the service that has been
   876              added to the <a>entry script origin's URL whitelist</a>. Web pages can subsequently use this value for
   877              implicit cross-document messaging via various existing mechanisms (e.g. Web Sockets, Server-Sent Events,
   878              Web Messaging, XMLHttpRequest).
   879         </p>
   880         <p>
   881           The <dfn id="dom-networkservice-config"><code>config</code></dfn> attribute provides the raw configuration
   882           information extracted from the given network service.
   883         </p>
   884       </section>
   885       <section>
   886         <h3>
   887           States
   888         </h3>
   889         <dl class="domintro">
   890           <dt>
   891             <var title="">service</var> . <code title="dom-networkservice-online"><a href=
   892             "#dom-networkservice-online">online</a></code>
   893           </dt>
   894           <dd>
   895             <p>
   896               Returns <code>true</code> if the service is reporting that it is accessible on the local network or
   897               <code>false</code> if the service is no longer accessible (temporarily or permanently) on the local
   898               network.
   899             </p>
   900           </dd>
   901         </dl>
   902         <p>
   903           The <dfn id="dom-networkservice-online"><code>online</code></dfn> attribute indicates whether the service is
   904           either <var>online</var>, and therefore accessible on the local network, in which case this attribute will
   905           return <code>true</code> or, <var>offline</var>, and therefore not accessible on the local network, either
   906           temporarily or permanently, in which case this attribute will return <code>false</code>. This attribute MUST
   907           default to <code>true</code>.
   908         </p>
   909       </section>
   910       <section>
   911         <h3>
   912           Events
   913         </h3>
   914         <p>
   915           The following are the event handlers (and their corresponding event handler event types) that <em class=
   916           "ct">MUST</em> be supported, as IDL attributes, by all objects implementing the <a href=
   917           "#networkservice"><code>NetworkService</code></a> interface:
   918         </p>
   919         <table border="1">
   920           <thead>
   921             <tr>
   922               <th>
   923                 <span title="event handlers">Event handler</span>
   924               </th>
   925               <th>
   926                 <span>Event handler event type</span>
   927               </th>
   928             </tr>
   929           </thead>
   930           <tbody>
   931             <tr>
   932               <td>
   933                 <dfn id="dom-networkservice-onnotify"
   934                     title="dom-NetworkService-onnotify"><code>onnotify</code></dfn>
   935               </td>
   936               <td>
   937                 <a href="#event-notify"><code>notify</code></a>
   938               </td>
   939             </tr>
   940             <tr>
   941               <td>
   942                 <dfn id="dom-networkservice-onserviceonline"
   943                     title="dom-NetworkService-onserviceonline"><code>onserviceonline</code></dfn>
   944               </td>
   945               <td>
   946                 <a href="#event-serviceonline"><code>serviceonline</code></a>
   947               </td>
   948             </tr>
   949             <tr>
   950               <td>
   951                 <dfn id="dom-networkservice-onserviceoffline"
   952                     title="dom-NetworkService-onserviceoffline"><code>onserviceoffline</code></dfn>
   953               </td>
   954               <td>
   955                 <a href="#event-serviceoffline"><code>serviceoffline</code></a>
   956               </td>
   957             </tr>
   958           </tbody>
   959         </table>
   960       </section>
   961     </section>
   962     <section>
   963       <h2>
   964         Service Discovery
   965       </h2>
   966       <p>
   967         A <a>user agent</a> conforming to this specification MAY implement <abbr title=
   968         "Simple Service Discovery Protocol">SSDP</abbr> [[!UPNP-DEVICEARCH11]], Zeroconf [[!DNS-SD]] + [[!MDNS]] and/or
   969         <abbr title="Discovery and Launch Protocol">DIAL</abbr> [<a href=
   970         "https://sites.google.com/a/dial-multiscreen.org/dial/dial-protocol-specification">DIAL</a>] <dfn>service
   971         discovery mechanisms</dfn> - the requirements detailed in this section of the specification - to enable Web
   972         pages to request and connect with HTTP services running on networked devices, discovered via any of these
   973         mechanisms, through this API. When a <a>user agent</a> implements any of these <a>service discovery
   974         mechanisms</a>, then it MUST conform to the corresponding algorithms provided in this section of the
   975         specification.
   976       </p>
   977       <p>
   978         This section presents how the results of these <a>service discovery mechanisms</a> will be matched to requested
   979         service types, how the user agent stores available and active services and how their properties are applied to
   980         any resulting <a href="#networkservice"><code>NetworkService</code></a> objects.
   981       </p>
   982       <p>
   983         It is expected that user agents will perform these <a>service discovery mechanisms</a> asynchronously and
   984         periodically update the <a>list of available service records</a> as required. The timing of any <a>service
   985         discovery mechanisms</a> is an implementation detail left to the discretion of the implementer (e.g. by
   986         continuously monitoring the network as a background process or on invocation of this API from a Web page).
   987       </p>
   988       <p>
   989         The <dfn>list of available service records</dfn> is a single dynamic internal lookup table within user agents
   990         that is used to track all the services that have been discovered and are available in the current network at
   991         the current time. At any point during the running of either of the two <a>service discovery mechanisms</a> then
   992         existing entries within this table can be updated, entries can be added and entries can be removed as the
   993         status of networked services changes according to the rules defined in this specification.
   994       </p>
   995       <p>
   996         The <dfn>list of active service managers</dfn> is an internal list within user agents that is used to track all
   997         <a href="#networkservices"><code>NetworkServices</code></a> objects currently being shared with any web pages
   998         at the current time within the user agent. Each <a href="#networkservices"><code>NetworkServices</code></a>
   999         object in the <a>list of active service managers</a> represents a collection of zero or more <a href=
  1000         "#networkservice"><code>NetworkService</code></a> objects - known as its <dfn>indexed properties</dfn>.
  1001         <a href="#networkservice"><code>NetworkService</code></a> objects are attached as the <a>indexed properties</a>
  1002         of a <a href="#networkservices"><code>NetworkServices</code></a> object as part of the <a href=
  1003         "#dom-navigator-getnetworkservices"><code>getNetworkServices()</code></a> algorithm.
  1004       </p>
  1005       <p>
  1006         The rule for <dfn>adding an available service</dfn> is the process of adding a <a>new service</a> or updating
  1007         an <a>existing service</a> that is generally available on the user's current network in the <a>list of
  1008         available service records</a>. This rule takes one argument, <var>network service record</var>, and consists of
  1009         running the following steps:
  1010       </p>
  1011       <ol class="rule">
  1012         <li>For each <var>existing service record</var> in the current <a>list of available service records</a>, run
  1013         the following sub-steps:
  1014           <ol class="rule">
  1015             <li>If the <var>existing service record</var>'s <code>id</code> property does not equal <var>network
  1016             service record</var>'s <code>id</code> property then abort any remaining sub-steps and continue at the next
  1017             available <var>existing service record</var>.
  1018             </li>
  1019             <li>Replace the value of <var>existing service record</var> in the current <a>list of available service
  1020             records</a> with the value of <var>network service record</var>, aborting any remaining steps in this
  1021             algorithm and return.
  1022             </li>
  1023           </ol>
  1024         </li>
  1025         <li>Add <var>network service record</var> to the <a>list of available service records</a> as a new item.
  1026         </li>
  1027         <li>For each <var>service manager</var> in the <a>list of active service managers</a> run the following steps:
  1028           <ol class="rule">
  1029             <li>For each <var>active service</var> in <var>service manager</var> run the following steps:
  1030               <ol class="rule">
  1031                 <li>If the <var>network service record</var>'s <code>id</code> property equals the <var>active
  1032                 service</var>'s <code>id</code> attribute and <var>active service</var>'s <code>online</code> attribute
  1033                 is currently set to <code>false</code> then set <var>active service</var>'s <a href=
  1034                 "#dom-networkservice-online"><code>online</code></a> attribute to <code>true</code> and then <a href=
  1035                 "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
  1036                       class="externalDFN">queue a task</a> to dispatch a newly created event with the name <a href=
  1037                       "#event-serviceonline"><code>serviceonline</code></a> that uses the <a href=
  1038                       "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1039                       class="externalDFN"><code>Event</code></a> interface, which does not bubble, is not cancellable,
  1040                       and has no default action, at the current <var>active service</var> object.
  1041                 </li>
  1042               </ol>
  1043             </li>
  1044             <li>Let <var>'service type in current service manager' flag</var> be <code>false</code>.
  1045             </li>
  1046             <li>For each <var>requested control type</var> of the <var>requested control types</var> in <var>service
  1047             manager</var> run the following steps:
  1048               <ol class="rule">
  1049                 <li>If <var>network service record</var>'s <code>type</code> property does not equal <var>requested
  1050                 control type</var> then abort any remaining sub-steps and continue at the next available <var>requested
  1051                 control type</var>.
  1052                 </li>
  1053                 <li>Set the <var>'service type in current service manager' flag</var> to <code>true</code>, abort any
  1054                 remaining sub-steps and continue.
  1055                 </li>
  1056               </ol>
  1057             </li>
  1058             <li>If the <var>'service type in current service manager' flag</var> is set to <code>true</code> then
  1059             increment <var>service manager</var>'s <a href=
  1060             "#dom-networkservices-servicesavailable"><code>servicesAvailable</code></a> attribute by <code>1</code> and
  1061             then <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
  1062                   class="externalDFN">queue a task</a> to dispatch a newly created event with the name <a href=
  1063                   "#event-serviceavailable"><code>serviceavailable</code></a> that uses the <a href=
  1064                   "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1065                   class="externalDFN"><code>Event</code></a> interface, which does not bubble, is not cancellable, and
  1066                   has no default action, at the current <var>service manager</var> object.
  1067             </li>
  1068           </ol>
  1069         </li>
  1070       </ol>
  1071       <p>
  1072         The rule for <dfn>removing an available service</dfn> is the general process of removing an <a>existing
  1073         service</a> from the <a>list of available service records</a> that has left the user's current network or has
  1074         otherwise expired. This rule takes one argument, <var>service identifier</var>, and consists of running the
  1075         following steps:
  1076       </p>
  1077       <ol class="rule">
  1078         <li>For each <var>existing service record</var> in the current <a>list of available service records</a>, run
  1079         the following sub-steps:
  1080           <ol class="rule">
  1081             <li>If the <var>existing service record</var>'s <code>id</code> property does not match <var>service
  1082             identifier</var> then skip any remaining sub-steps for the current <var>existing service record</var> and
  1083             continue at the next available <var>existing service record</var>.
  1084             </li>
  1085             <li>Let <var>'service type in use' flag</var> be <code>false</code>.
  1086             </li>
  1087             <li>For each <var>service manager</var> in the <a>list of active service managers</a> run the following
  1088             steps:
  1089               <ol class="rule">
  1090                 <li>Let <var>'service type in current service manager' flag</var> be <code>false</code>.
  1091                 </li>
  1092                 <li>For each <var>active service</var> in <var>service manager</var> run the following steps:
  1093                   <ol class="rule">
  1094                     <li>If <var>existing service record</var>'s <code>id</code> property equals the <var>active
  1095                     service</var>'s <code>id</code> attribute and <var>active service</var>'s <a href=
  1096                     "#dom-networkservice-online"><code>online</code></a> attribute is currently set to
  1097                     <code>true</code> then set <var>active service</var>'s <a href="#dom-networkservice-online"><code>
  1098                       online</code></a> attribute to <code>false</code> and then <a href=
  1099                       "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
  1100                           class="externalDFN">queue a task</a> to dispatch a newly created event with the name <a href=
  1101                           "#event-serviceoffline"><code>serviceoffline</code></a> that uses the <a href=
  1102                           "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1103                           class="externalDFN"><code>Event</code></a> interface, which does not bubble, is not
  1104                           cancellable, and has no default action, at the current <var>active service</var>.
  1105                     </li>
  1106                   </ol>
  1107                 </li>
  1108                 <li>For each <var>requested control type</var> of the <var>requested control types</var> in
  1109                 <var>service manager</var> run the following steps:
  1110                   <ol class="rule">
  1111                     <li>If <var>existing service record</var>'s <code>type</code> property does not equal
  1112                     <var>requested control type</var> then abort any remaining sub-steps and continue at the next
  1113                     available <var>requested control type</var>.
  1114                     </li>
  1115                     <li>Set the <var>'service type in current service manager' flag</var> to <code>true</code> and the
  1116                     <var>'service type in use' flag</var> to <code>true</code>, abort any remaining sub-steps and
  1117                     continue.
  1118                     </li>
  1119                   </ol>
  1120                 </li>
  1121                 <li>If the <var>'service type in current service manager' flag</var> is set to <code>true</code> then
  1122                 decrement <var>service manager</var>'s <a href=
  1123                 "#dom-networkservices-servicesavailable"><code>servicesAvailable</code></a> attribute by <code>1</code>
  1124                 and then <a href=
  1125                 "http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
  1126                       class="externalDFN">queue a task</a> to dispatch a newly created event with the name <a href=
  1127                       "#event-serviceunavailable"><code>serviceunavailable</code></a> that uses the <a href=
  1128                       "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1129                       class="externalDFN"><code>Event</code></a> interface, which does not bubble, is not cancellable,
  1130                       and has no default action, at the current <var>service manager</var> object.
  1131                 </li>
  1132               </ol>
  1133             </li>
  1134             <li>If the <var>'service type in use' flag</var> is <code>false</code> and the <var>existing service
  1135             record</var>'s <code>type</code> property begins with the DOMString "<code>upnp:</code>" and <var>existing
  1136             service record</var>'s <code>eventsURL</code> property is set then run the rule to <a>terminate an existing
  1137             UPnP Events Subscription</a>, if one is currently active (as a result of having previously called <a>setup
  1138             a UPnP Events Subscription</a> against the current <var>existing service record</var>).
  1139             </li>
  1140             <li>Remove <var>existing service record</var> from the current <a>list of available service records</a>.
  1141             </li>
  1142           </ol>
  1143         </li>
  1144       </ol>
  1145       <p>
  1146         User agents SHOULD expire a service record from the <a>list of available service records</a> when its
  1147         <code>expiryTimestamp</code> attribute exceeds the current UTC timestamp. When this condition is met the
  1148         <a>user agent</a> SHOULD run the rule for <a>removing an available service</a>, passing in the expired service
  1149         record's <code>id</code> attribute as the only argument.
  1150       </p>
  1151       <section>
  1152         <h4>
  1153           Zeroconf (<abbr title="Multicast DNS">mDNS</abbr> + <abbr title="Domain Name System">DNS</abbr>-<abbr title=
  1154           "Service Discovery">SD</abbr>)
  1155         </h4>
  1156         <p>
  1157           For each DNS response received from a user-agent-initiated Multicast DNS Browse for <abbr title=
  1158           "DNS Pointer Record">PTR</abbr> records with the name <code>_services._dns-sd._udp</code> on the resolved
  1159           recommended automatic browsing domain [[!MDNS]], the <a>user agent</a> MUST run the following steps:
  1160         </p>
  1161         <ol class="rule">
  1162           <li>Let <var>service mDNS responses</var> be an array of PTR records received by issuing a Multicast DNS
  1163           Browse for PTR records with the name of the current discovered service type.
  1164           </li>
  1165           <li>For each Object <var>service mDNS response</var> in <var>service mDNS responses</var>, run the following
  1166           steps:
  1167             <ol>
  1168               <li>Let <var>network service record</var> be an Object consisting of the following empty properties:
  1169               <code>id</code>, <code>name</code>, <code>type</code>, <code>url</code>, <code>config</code>,
  1170               <code>expiryTimestamp</code>.
  1171               </li>
  1172               <li>Set <var>network service record</var>'s <code>id</code> property to the value of the full PTR Service
  1173               Instance Name [[!MDNS]].
  1174               </li>
  1175               <li>Set <var>network service record</var>'s <code>name</code> property to the value of the PTR Service
  1176               Instance Name's <var>Instance</var> component [[!MDNS]].
  1177               </li>
  1178               <li>Set <var>network service record</var>'s <code>type</code> property to the concatenation of the string
  1179               <code>zeroconf:</code> followed be the value of the PTR Service Instance Name's <var>Service</var>
  1180               component [[!MDNS]].
  1181               </li>
  1182               <li>Set <var>network service record</var>'s <code>url</code> property to the resolvable Service URL
  1183               obtained from performing an DNS-SD Lookup [[!DNS-SD]] of the current service from the PTR record provided
  1184               [[!MDNS]].
  1185               </li>
  1186               <li>Set <var>network service record</var>'s <code>config</code> property to the string value of the
  1187               contents of the first DNS-SD TXT record associated with the <var>service mDNS response</var> as defined
  1188               in [[!DNS-SD]].
  1189               </li>
  1190               <li>Set <var>network service record</var>'s <code>expiryTimestamp</code> property to the value of the
  1191               current date, in UTC timestamp format, plus a value of <code>120</code> seconds.
  1192               </li>
  1193               <li>Run the general rule for <a>adding an available service</a>, passing in the current <var>network
  1194               service record</var> as the only argument.
  1195               </li>
  1196             </ol>
  1197           </li>
  1198         </ol>
  1199       </section>
  1200       <section>
  1201         <h5>
  1202           Simple Service Discovery Protocol (<abbr title="Simple Service Discovery Protocol">SSDP</abbr>)
  1203         </h5>
  1204         <p>
  1205           A user agent that implements UPnP service discovery MUST issue a <dfn>search request for UPnP root
  1206           devices</dfn> against the user's current local network according to the full normative text and timing
  1207           provided in 'Section 1.3.2: Search request with M-SEARCH' detailed in [[!UPNP-DEVICEARCH11]].
  1208         </p>
  1209         <p>
  1210           The user agent MUST issue all <a title="search request for UPnP root devices">search requests for UPnP root
  1211           devices</a> with a HTTP request line equal to <code>M-SEARCH * HTTP/1.1</code>, with a HOST header equal to
  1212           the reserved multicast address and port of <code>239.255.255.250:1900</code> and a MAN header equal to
  1213           <code>ssdp:discover</code>. The <a>user agent</a> must also send an ST header with this HTTP request equal to
  1214           the String value of <code>ssdp:all</code> or <code>upnp:rootdevice</code> or a single <a>valid service
  1215           type</a> token beginning with the String value <code>upnp:</code>. If a single <a>valid service type</a>
  1216           token beginning with the String value <code>upnp:</code> is to be used, the user agent MUST strip the leading
  1217           String <code>upnp:</code> before using this value in this HTTP request. The user-agent MUST also send an MX
  1218           header equal to a <dfn>maximum UPnP advertisement response wait time</dfn> value between <code>1</code> and
  1219           <code>5</code> seconds with this HTTP request.
  1220         </p>
  1221         <p>
  1222           The user agent MUST listen for any incoming responses to any <a>search request for UPnP root devices</a>.
  1223         </p>
  1224         <p>
  1225           For each <dfn>HTTP Response</dfn> following an initial <a>search request for UPnP root devices</a> sent on a
  1226           <a>standard UPnP address and port</a> the user agent MUST run the following steps:
  1227         </p>
  1228         <ol class="rule">
  1229           <li>If the <a>HTTP Response</a> is not a HTTP 200 OK response then this response is invalid and the user
  1230           agent MUST discard this response, abort any remaining steps and return. The user agent MAY issue a new
  1231           <a>search request for UPnP root devices</a> as a result of this error occurring.
  1232           </li>
  1233           <li>If the <a>maximum UPnP advertisement response wait time</a> has been exceeded since the initial <a>search
  1234           request for UPnP root devices</a> was sent then the <a>HTTP Response</a> is invalid and the user agent MUST
  1235           discard this response, abort any remaining steps and return. The user agent MAY stop listening for responses
  1236           from the current <a>search request for UPnP root devices</a> as a result of this error occurring. Equally,
  1237           the user agent MAY issue a new <a>search request for UPnP root devices</a> as a result of this error
  1238           occurring.
  1239           </li>
  1240           <li>Let <var>ssdp device</var> be an Object with a property for each HTTP header received in the <a>HTTP
  1241           Response</a>, with each key being the name of a HTTP response header and each value being that HTTP response
  1242           header's value.
  1243           </li>
  1244           <li>If <var>ssdp device</var> does not contain at least one <var>CACHE-CONTROL</var> entry, at least one
  1245           <var>USN</var> entry, at least one <var>ST</var> entry and at least one <var>LOCATION</var> entry then the
  1246           <a>HTTP Response</a> is invalid and the <a>user agent</a> MUST discard this response, abort any remaining
  1247           steps and return.
  1248           </li>
  1249           <li>The user agent MUST run the rule for <a>obtaining a UPnP Device Description File</a> passing in the first
  1250           occurrence of <var>LOCATION</var> from <var>ssdp device</var> as the <var>device descriptor URL</var>
  1251           argument and the first occurrence of <var>USN</var> from <var>ssdp device</var> as the <var>device
  1252           identifier</var> argument and the first occurrence of <var>CACHE-CONTROL</var> from <var>ssdp device</var>
  1253           (minus the leading string of <code>max-age=</code>) as the <var>device expiry</var> argument.
  1254           </li>
  1255         </ol>
  1256         <p>
  1257           The user agent MUST listen for incoming requests on the <dfn>standard UPnP address and port</dfn> on all
  1258           current local network interface addresses with the port <code>1900</code>.
  1259         </p>
  1260         <p>
  1261           For each <dfn>HTTP Request</dfn> received on a <a>standard UPnP address and port</a> the user agent MUST run
  1262           the following steps:
  1263         </p>
  1264         <ol class="rule">
  1265           <li>If the <a>HTTP Request</a> is not a HTTP NOTIFY request then it is not a valid UPnP Request and the user
  1266           agent MUST discard this request, abort any remaining steps and return.
  1267           </li>
  1268           <li>Let <var>ssdp device</var> be an Object with a property for each HTTP header received in the <a>HTTP
  1269           Request</a>, with each key being the name of a HTTP header and each value being that HTTP header's value.
  1270           </li>
  1271           <li>If <var>ssdp device</var>'s <var>NTS</var> entry is equal to <code>ssdp:alive</code> and the <a>HTTP
  1272           Request</a> does not contain at least one <var>CACHE-CONTROL</var> entry, at least one <var>USN</var> entry,
  1273           at least one <var>NT</var> entry, at least one <var>NTS</var> entry and at least one <var>LOCATION</var>
  1274           entry, then the <a>user agent</a> MUST discard this request, abort any remaining steps and return.
  1275           </li>
  1276           <li>If <var>ssdp device</var>'s <var>NTS</var> entry is equal to <code>ssdp:alive</code> then the user agent
  1277           MUST run the rule for <a>obtaining a UPnP Device Description File</a> passing in the first occurrence of
  1278           <var>LOCATION</var> from <var>ssdp device</var> as the <var>device descriptor URL</var> argument and the
  1279           first occurrence of <var>USN</var> from <var>ssdp device</var> as the <var>device identifier</var> argument
  1280           and the first occurrence of <var>CACHE-CONTROL</var> from <var>ssdp device</var> (minus the leading string of
  1281           <code>max-age=</code>) as the <var>device expiry</var>.<br>
  1282             <br>
  1283             Otherwise, if <var>ssdp device</var>'s <var>NTS</var> entry is equal to <code>ssdp:byebye</code> then the
  1284             user agent MUST run the rule for <a>removing all services from a registered UPnP Device</a> passing in the
  1285             first occurrence of <var>USN</var> from <var>ssdp device</var> as the <var>device identifier</var>
  1286             argument.
  1287           </li>
  1288         </ol>
  1289         <p>
  1290           The rule for <dfn>obtaining a UPnP Device Description File</dfn> is the process of obtaining the contents of
  1291           a standard UPnP Device Description [[!UPNP-DEVICEARCH11]] from a URL-based resource. This rule takes three
  1292           arguments - <var>device descriptor URL</var>, <var>device identifier</var> and <var>device expiry</var> - and
  1293           when called the user agent MUST run the following steps:
  1294         </p>
  1295         <ol class="rule">
  1296           <li>Let <var>device descriptor file</var> contain the contents of the file located at the URL provided in
  1297           <var>device descriptor URL</var> obtained according to the rules defined in 'Section 2.11: Retrieving a
  1298           description using HTTP' in [[!UPNP-DEVICEARCH11]].
  1299           </li>
  1300           <li>If the value provided in <var>device descriptor URL</var> cannot be resolved as a reachable URL on the
  1301           current network or the <var>device descriptor file</var> remains empty then it is invalid and the <a>user
  1302           agent</a> MUST abort any remaining steps and return.
  1303           </li>
  1304           <li>Run the rule for <a>processing a UPnP Device Description File</a>, passing in the current <var>device
  1305           descriptor file</var>, <var>device identifier</var> and <var>device expiry</var> arguments.
  1306           </li>
  1307         </ol>
  1308         <p>
  1309           The rule for <dfn>processing a UPnP Device Description File</dfn> is the process of parsing the contents of a
  1310           standard UPnP Device Description [[!UPNP-DEVICEARCH11]] and registering the UPnP services contained therein
  1311           within the <a>list of available service records</a>.
  1312         </p>
  1313         <p>
  1314           The rule for <a>processing a UPnP Device Description File</a> takes three arguments - <var>device descriptor
  1315           file</var>, <var>device identifier</var> and <var>device expiry</var> - and when called the user agent MUST
  1316           run the following steps:
  1317         </p>
  1318         <ol class="rule">
  1319           <li>Let <var>advertised services</var> be a list of all advertised services obtained from the <var>device
  1320           descriptor file</var> containing the value of the first occurrence of the <code>&lt;serviceList&gt;</code>
  1321           element as it is defined in 'Section 2.3: Device Description' in [[!UPNP-DEVICEARCH11]].
  1322           </li>
  1323           <li>For each <code>&lt;service&gt;</code> element - known as an <var>advertised service</var> - in
  1324           <var>advertised services</var> run the following steps:
  1325             <ol class="rule">
  1326               <li>Let <var>network service record</var> be a new Object consisting of the following empty properties:
  1327               <code>id</code>, <code>deviceId</code>, <code>name</code>, <code>type</code>, <code>url</code>,
  1328               <code>eventsUrl</code>, <code>config</code>, <code>expiryTimestamp</code>.
  1329               </li>
  1330               <li>Set <var>network service record</var>'s <code>id</code> property to the concatenated string value of
  1331               the first occurrence of the <code>&lt;UDN&gt;</code> element in the <var>device descriptor file</var>
  1332               with the <var>advertised service</var>'s <code>&lt;serviceId&gt;</code> sub-element.
  1333               </li>
  1334               <li>Set <var>network service record</var>'s <code>deviceId</code> property to the value of <var>device
  1335               identifier</var>.
  1336               </li>
  1337               <li>Set <var>network service record</var>'s <code>name</code> property to the string value of the first
  1338               occurrence of the <var>advertised service</var>'s <code>&lt;serviceId&gt;</code> sub-element.
  1339               </li>
  1340               <li>Set <var>network service record</var>'s <code>type</code> property to the concatenation of the string
  1341               <code>upnp:</code> followed by the string value of the first occurrence of the <var>advertised
  1342               service</var>'s <code>&lt;serviceType&gt;</code> sub-element.
  1343               </li>
  1344               <li>Set <var>network service record</var>'s <code>url</code> property to the string value of the first
  1345               occurrence of the <var>advertised service</var>'s <code>&lt;controlURL&gt;</code> sub-element.
  1346               </li>
  1347               <li>Set <var>network service record</var>'s <code>config</code> property to the string value of the
  1348               contents of the first occurrence of the <code>&lt;device&gt;</code> element in the <var>device descriptor
  1349               file</var>.
  1350               </li>
  1351               <li>If <var>advertised service</var>'s <code>&lt;eventSubURL&gt;</code> sub-element is not empty, then
  1352               set <var>network service record</var>'s <code>eventsUrl</code> property to the string value of the first
  1353               occurrence of the <var>advertised service</var>'s <code>&lt;eventSubURL&gt;</code> sub-element.
  1354               Otherwise, do not set <var>network service record</var>'s <code>eventsUrl</code> property.
  1355               </li>
  1356               <li>Set <var>network service record</var>'s <code>expiryTimestamp</code> property to the value of the
  1357               current date, in UTC timestamp format, plus the value of <var>device expiry</var>.
  1358               </li>
  1359               <li>Run the general rule for <a>adding an available service</a>, passing in the current <var>network
  1360               service record</var> as the only argument.
  1361               </li>
  1362             </ol>
  1363           </li>
  1364           <li>If <var>device descriptor file</var> contains a <code>&lt;deviceList&gt;</code> element then for each
  1365           <code>&lt;device&gt;</code> element within <code>&lt;deviceList&gt;</code> - herein known as an <var>embedded
  1366           device descriptor file</var> - the user agent MUST run the rule for <a>processing a UPnP Device Description
  1367           File</a>, passing in the current <var>embedded device descriptor file</var> as the <var>device descriptor
  1368           file</var> argument, along with the current <var>device identifier</var> and <var>device expiry</var>
  1369           arguments.
  1370           </li>
  1371         </ol>
  1372         <p>
  1373           The rule for <dfn>removing all services from a registered UPnP Device</dfn> is the process of removing all
  1374           services associated with a device from the <a>list of available service records</a> that has left the user's
  1375           current network or has otherwise timed out or expired. This rule takes one argument, <var>device
  1376           identifier</var>, and consists of running the following steps:
  1377         </p>
  1378         <ol class="rule">
  1379           <li>For each <var>existing service record</var> in the current <a>list of available service records</a>, run
  1380           the following sub-steps:
  1381             <ol class="rule">
  1382               <li>If the <var>existing service record</var>'s <code>deviceId</code> property does not match <var>device
  1383               identifier</var> then skip any remaining sub-steps for the current <var>existing service record</var> and
  1384               continue at the next available <var>existing service record</var>.
  1385               </li>
  1386               <li>Run the general rule for <a>removing an available service</a> passing in <var>existing service
  1387               record</var>'s <code>id</code> property as the only argument.
  1388               </li>
  1389             </ol>
  1390           </li>
  1391         </ol>
  1392         <p>
  1393           When the <a>user agent</a> is to <dfn>setup a UPnP Events Subscription</dfn>, it is to run the following
  1394           steps with the current <var>network service record</var> object as defined in 'Section 4.1.2: SUBSCRIBE with
  1395           NT and CALLBACK' in [[!UPNP-DEVICEARCH11]]:
  1396         </p>
  1397         <ol class="rule">
  1398           <li>If <var>network service record</var>'s <code>eventsUrl</code> property is empty then the <a>user
  1399           agent</a> MUST abort these steps.
  1400           </li>
  1401           <li>Let <var>callback URL</var> be the value of creating a new <a>user-agent generated callback url</a>.
  1402           </li>
  1403           <li>Send a HTTP SUBSCRIBE request with a <em>NT</em> header with a string value of <code>upnp:event</code>, a
  1404           <em>TIMEOUT</em> header with a user-agent defined timeout value (in the form <code>Second-XX</code> where
  1405           <code>XX</code> is the user-agent defined timeout value in seconds) and a <em>CALLBACK</em> header with a
  1406           string value of <var>callback URL</var> towards the <var>network service record</var>'s
  1407           <code>eventsUrl</code> property.
  1408           </li>
  1409           <li>If a non-200 OK response is received from the HTTP SUBSCRIBE request then the <a>user agent</a> MUST
  1410           abort these steps.
  1411           </li>
  1412           <li>On receiving a valid 200 OK response, run the following steps:
  1413             <ol class="rule">
  1414               <li>Let <var>callback ID</var> equal the string value of the first included <em>SID</em> header, if it
  1415               exists.
  1416               </li>
  1417               <li>Let <var>timeout date</var> equal the sum of the current UTC date value plus the integer value of the
  1418               first included <em>TIMEOUT</em> header (minus the leading string of <code>Second-</code>), if it exists.
  1419               </li>
  1420               <li>Run the following steps asynchronously and continue to the step labeled <em>listen</em> below.
  1421               </li>
  1422               <li>
  1423                 <em>Refresh Subscription</em>: Run the following steps at a set interval (X) within the <a>user
  1424                 agent</a>:
  1425                 <ol class="rule">
  1426                   <li>Let <var>current date</var> equal the current UTC date.
  1427                   </li>
  1428                   <li>If <var>current date</var> is less than the <var>timeout date</var> then continue to the step
  1429                   labeled <em>refresh subscription</em> above.
  1430                   </li>
  1431                   <li>Send a HTTP SUBSCRIBE request with a <em>SID</em> header with the string value of <var>callback
  1432                   ID</var> and a user-agent defined <em>TIMEOUT</em> header (in the form <code>Second-XX</code> where
  1433                   <code>XX</code> is the user-agent defined timeout value in seconds) towards the <var>network service
  1434                   record</var>'s <code>eventsUrl</code> property.
  1435                   </li>
  1436                   <li>On receiving a valid 200 OK, update <var>callback ID</var> with the string value of the first
  1437                   included <em>SID</em> header and set <var>timeout date</var> to the sum of the current UTC date value
  1438                   plus the integer value of the first included <em>TIMEOUT</em> header (minus the leading string of
  1439                   <code>Second-</code>), if it exists. If the current date is greater than or equal to <var>timeout
  1440                   date</var> then the <a>user agent</a> SHOULD continue from the step labeled <em>refresh
  1441                   subscription</em> above. For all non 200 OK responses the <a>user agent</a> SHOULD continue from the
  1442                   step labeled <em>refresh subscription</em> above.
  1443                   </li>
  1444                 </ol>
  1445               </li>
  1446               <li>
  1447                 <em>Listen</em>: For each HTTP NOTIFY request received at the <var>callback URL</var> the <a>user
  1448                 agent</a> is to run the following steps:
  1449                 <ol class="rule">
  1450                   <li>Let <var>content clone</var> be the result of obtaining the message body of the HTTP NOTIFY
  1451                   request. If <var>content clone</var> is empty, then the <a>user agent</a> MUST abort these steps.
  1452                   </li>
  1453                   <li>Let <var>notification event</var> be a new simple event that uses the <a href=
  1454                   "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1455                         class="externalDFN"><code>Event</code></a> interface with the name <a href=
  1456                         "#event-notify"><code>notify</code></a>, which does not bubble, is not cancellable, and has no
  1457                         default action.
  1458                   </li>
  1459                   <li>Let the <code>data</code> attribute of <var>notification event</var> have the DOMString value of
  1460                   <var>content clone</var>.
  1461                   </li>
  1462                   <li>
  1463                     <a href="http://www.whatwg.org/specs/web-apps/current-work/complete/webappapis.html#queue-a-task"
  1464                         class="externalDFN">Queue a task</a> to dispatch <var>notification event</var> at the current
  1465                         <a><code>NetworkService</code></a> object.
  1466                   </li>
  1467                   <li>Return a HTTP 200 OK response to the sender of the HTTP NOTIFY request.
  1468                   </li>
  1469                 </ol>
  1470               </li>
  1471             </ol>
  1472           </li>
  1473         </ol>
  1474         <p>
  1475           A <a>user agent</a> can <dfn>terminate an existing UPnP Events Subscription</dfn> at any time for a
  1476           <var>network service record</var> by sending an HTTP UNSUBSCRIBE request - as defined in 'Section 4.1.4:
  1477           Cancelling a subscription with UNSUBSCRIBE' in [[!UPNP-DEVICEARCH11]] - with a HOST header set to that
  1478           <var>active service</var>'s <code>eventsUrl</code> property and a SID header set to the <var>callback
  1479           ID</var> obtained when the initial <a>setup a UPnP Events Subscription</a> action occurred.
  1480         </p>
  1481       </section>
  1482       <section>
  1483         <h5>
  1484           Discovery and Launch Protocol (<abbr title="Discovery and Launch Protocol">DIAL</abbr>)
  1485         </h5>
  1486         <p>
  1487           A user agent that implements DIAL service discovery MUST issue a <dfn>search request for DIAL-enabled
  1488           devices</dfn> against the user's current local network according to the full normative text and timing
  1489           provided in 'Section 1.3.2: Search request with M-SEARCH' detailed in [[!UPNP-DEVICEARCH11]].
  1490         </p>
  1491         <p>
  1492           Let <var>dial version</var> be the version number specified in the <a>valid service type</a> token. Let
  1493           <var>dial search target</var> be the concatentation of the
  1494           <code>urn:dial-multiscreen-org:service:dial:</code> string constant with the <var>dial version</var>
  1495           (currently, <var>dial version</var> can only be <code>1</code>)
  1496         </p>
  1497         <p>
  1498           The user agent MUST issue all <a title="search request for DIAL devices">search requests for DIAL devices</a>
  1499           with a HTTP request line equal to <code>M-SEARCH * HTTP/1.1</code>, with a HOST header equal to the reserved
  1500           multicast address and port of <code>239.255.255.250:1900</code>, a MAN header equal to
  1501           <code>ssdp:discover</code>, an ST header equal to <var>dial search target</var> and a user-agent defined MX
  1502           header equal to a <dfn>maximum DIAL advertisement response wait time</dfn> value between <code>1</code> and
  1503           <code>5</code> seconds.
  1504         </p>
  1505         <p>
  1506           The user agent MUST listen for any incoming responses to a <a>search request for DIAL devices</a>.
  1507         </p>
  1508         <p>
  1509           For each HTTP Response following an initial <a>search request for DIAL devices</a> sent on a <a>standard UPnP
  1510           address and port</a> the user agent MUST run the following steps:
  1511         </p>
  1512         <ol class="rule">
  1513           <li>If the HTTP Response is not a HTTP 200 OK response then this response is invalid and the user agent MUST
  1514           discard this response, abort any remaining steps and return. The user agent MAY issue a new <a>search request
  1515           for DIAL devices</a> as a result of this error occurring.
  1516           </li>
  1517           <li>If the <a>maximum DIAL advertisement response wait time</a> has been exceeded since the initial <a>search
  1518           request for DIAL devices</a> was sent then the <a>HTTP Response</a> is invalid and the user agent MUST
  1519           discard this response, abort any remaining steps and return. The user agent MAY stop listening for responses
  1520           from the current <a>search request for DIAL devices</a> as a result of this error occurring. Equally, the
  1521           user agent MAY issue a new <a>search request for DIAL devices</a> as a result of this error occurring.
  1522           </li>
  1523           <li>Let <var>dial device</var> be an Object with a property for each HTTP header received in the <a>HTTP
  1524           Response</a>, with each key being the name of a HTTP response header and each value being that HTTP response
  1525           header's value.
  1526           </li>
  1527           <li>If <var>dial device</var> does not contain at least one <var>CACHE-CONTROL</var> entry, at least one
  1528           <var>USN</var> entry, at least one <var>ST</var> entry and at least one <var>LOCATION</var> entry or the
  1529           value of its <var>ST</var> entry is not <var>dial search target</var>, then the <a>HTTP Response</a> is
  1530           invalid and the <a>user agent</a> MUST discard this response, abort any remaining steps and return.
  1531           </li>
  1532           <li>The user agent MUST run the rule for <a>obtaining a UPnP Device Description File</a> passing in the first
  1533           occurrence of <var>LOCATION</var> from <var>dial device</var> as the <var>device descriptor URL</var>
  1534           argument and the first occurrence of <var>USN</var> from <var>dial device</var> as the <var>device
  1535           identifier</var> argument and the first occurrence of <var>CACHE-CONTROL</var> from <var>dial device</var>
  1536           (minus the leading string of <code>max-age=</code>) as the <var>device expiry</var> argument.
  1537           </li>
  1538         </ol>
  1539         <p>
  1540           The rule for <dfn>obtaining a DIAL Device Description File</dfn> is the process of obtaining the contents of
  1541           a standard UPnP Device Description [[!UPNP-DEVICEARCH11]] from a URL-based resource. This rule takes three
  1542           arguments - <var>device descriptor URL</var>, <var>device identifier</var> and <var>device expiry</var> - and
  1543           when called the user agent MUST run the following steps:
  1544         </p>
  1545         <ol class="rule">
  1546           <li>Let <var>device descriptor file</var> contain the contents of the file located at the URL provided in
  1547           <var>device descriptor URL</var> obtained according to the rules defined in 'Section 2.11: Retrieving a
  1548           description using HTTP' in [[!UPNP-DEVICEARCH11]].
  1549           </li>
  1550           <li>Let <var>application url</var> be the value of the first occurrence of the <code>Application-URL</code>
  1551           response header field obtained according to the rules defined in 'Section 5.4: Device Description Response'
  1552           in [<a href="https://sites.google.com/a/dial-multiscreen.org/dial/dial-protocol-specification">DIAL</a>]
  1553           </li>
  1554           <li>If the value provided in <var>device descriptor URL</var> cannot be resolved as a reachable URL on the
  1555           current network or the <var>device descriptor file</var> remains empty or <var>application url</var> is
  1556           undefined then it is invalid and the <a>user agent</a> MUST abort any remaining steps and return.
  1557           </li>
  1558           <li>Run the following steps to add the newly discovered DIAL service:
  1559             <ol class="rule">
  1560               <li>Let <var>network service record</var> be a new Object consisting of the following empty properties:
  1561               <code>id</code>, <code>deviceId</code>, <code>name</code>, <code>type</code>, <code>url</code>,
  1562               <code>expiryTimestamp</code>.
  1563               </li>
  1564               <li>Set <var>network service record</var>'s <code>id</code> property to the first occurrence of the
  1565               <code>&lt;UDN&gt;</code> element in the <var>descriptor file</var> prefixed with the <code>dial:</code>
  1566               string constant
  1567               </li>
  1568               <li>Set <var>network service record</var>'s <code>deviceId</code> property to the value of <var>device
  1569               identifier</var>.
  1570               </li>
  1571               <li>Set <var>network service record</var>'s <code>name</code> property to the string value of the first
  1572               occurrence of the <code>&lt;friendlyName&gt;</code> element in the <var>descriptor file</var>.
  1573               </li>
  1574               <li>Set <var>network service record</var>'s <code>type</code> property to <var>dial search target</var>.
  1575               </li>
  1576               <li>Set <var>network service record</var>'s <code>url</code> property to the string value of the
  1577               <var>application url</var>.
  1578               </li>
  1579               <li>Set <var>network service record</var>'s <code>expiryTimestamp</code> property to the value of the
  1580               current date, in UTC timestamp format, plus the value of <var>device expiry</var>.
  1581               </li>
  1582               <li>Run the general rule for <a>adding an available service</a>, passing in the current <var>network
  1583               service record</var> as the only argument.
  1584               </li>
  1585             </ol>
  1586           </li>
  1587         </ol>
  1588       </section>
  1589       <section>
  1590         <h3>
  1591           Network Topology Monitoring
  1592         </h3>
  1593         <div>
  1594           <p>
  1595             When the <a>user agent</a> detects that the user has dropped from a connected network then, for each
  1596             <var>existing service record</var> in the <a>list of available service records</a> discovered via that
  1597             network connection, the <a>user agent</a> MUST run the general rule for <a>removing an available
  1598             service</a> passing in each <var>existing service record</var>'s <code>id</code> property as the only
  1599             argument for each call.
  1600           </p>
  1601           <p>
  1602             When the <a>user agent</a> detects that the user has connected to a new network or reconnected to an
  1603             existing network, then it SHOULD restart its discovery mechanisms as defined in the <a href=
  1604             "#service-discovery">Service Discovery</a> section of this specification, maintaining the existing <a>list
  1605             of active service managers</a> currently in use.
  1606           </p>
  1607         </div>
  1608       </section>
  1609     </section>
  1610     <section>
  1611       <h3>
  1612         Events Summary
  1613       </h3>
  1614       <p>
  1615         The following events are dispatched on the <a href="#networkservices"><code>NetworkServices</code></a> and/or
  1616         <a href="#networkservice"><code>NetworkService</code></a> objects:
  1617       </p>
  1618       <table border="1">
  1619         <thead>
  1620           <tr>
  1621             <th>
  1622               <span>Event name</span>
  1623             </th>
  1624             <th>
  1625               <span>Interface</span>
  1626             </th>
  1627             <th>
  1628               <span>Dispatched when...</span>
  1629             </th>
  1630           </tr>
  1631         </thead>
  1632         <tbody>
  1633           <tr>
  1634             <td>
  1635               <dfn id="event-serviceavailable"><code>serviceavailable</code></dfn>
  1636             </td>
  1637             <td>
  1638               <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1639                   class="externalDFN"><code>Event</code></a>
  1640             </td>
  1641             <td>
  1642               When a <a>new service</a> that matches one of the <a>requested type tokens</a> is found in the current
  1643               network.
  1644             </td>
  1645           </tr>
  1646           <tr>
  1647             <td>
  1648               <dfn id="event-serviceunavailable"><code>serviceunavailable</code></dfn>
  1649             </td>
  1650             <td>
  1651               <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1652                   class="externalDFN"><code>Event</code></a>
  1653             </td>
  1654             <td>
  1655               When an <a>existing service</a> that matches one of the <a>requested type tokens</a> gracefully leaves or
  1656               expires from the current network.
  1657             </td>
  1658           </tr>
  1659           <tr>
  1660             <td>
  1661               <dfn id="event-serviceonline"><code>serviceonline</code></dfn>
  1662             </td>
  1663             <td>
  1664               <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1665                   class="externalDFN"><code>Event</code></a>
  1666             </td>
  1667             <td>
  1668               When a <a>current service</a> renews its service registration within the current network.
  1669             </td>
  1670           </tr>
  1671           <tr>
  1672             <td>
  1673               <dfn id="event-serviceoffline"><code>serviceoffline</code></dfn>
  1674             </td>
  1675             <td>
  1676               <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1677                   class="externalDFN"><code>Event</code></a>
  1678             </td>
  1679             <td>
  1680               When a <a>current service</a> gracefully leaves or otherwise expires from the current network.
  1681             </td>
  1682           </tr>
  1683           <tr>
  1684             <td>
  1685               <dfn id="event-notify"><code>notify</code></dfn>
  1686             </td>
  1687             <td>
  1688               <a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#event"
  1689                   class="externalDFN"><code>Event</code></a>
  1690             </td>
  1691             <td>
  1692               When a valid UPnP Events Subscription Message is received on a <a>user-agent generated callback url</a>
  1693               for a <a>current service</a>. This event never fires for Zeroconf-based services.
  1694             </td>
  1695           </tr>
  1696         </tbody>
  1697       </table>
  1698     </section>
  1699     <section>
  1700       <h3>
  1701         Garbage collection
  1702       </h3>
  1703       <p>
  1704         A user agent MUST only garbage collect a <a><code>NetworkServices</code></a> object and remove its entry from
  1705         the <a>list of active service managers</a> when the user has navigated away from the <a href=
  1706         "http://www.whatwg.org/specs/web-apps/current-work/complete/browsers.html#entry-script"
  1707            class="externalDFN">entry script</a>'s <a href=
  1708            "http://www.whatwg.org/specs/web-apps/current-work/complete/origin-0.html#origin"
  1709            class="externalDFN">origin</a> in which the current <a><code>NetworkServices</code></a> object was provided.
  1710       </p>
  1711       <p>
  1712         A user agent MUST NOT garbage collect individual <a><code>NetworkService</code></a> objects until their parent
  1713         <a><code>NetworkServices</code></a> object has been garbage collected.
  1714       </p>
  1715       <p>
  1716         A user agent MUST garbage collect the <a><code>NetworkService</code></a> <a>indexed properties</a> of a
  1717         <a><code>NetworkServices</code></a> object, passing each indexed property's <code>id</code> attribute as the
  1718         only argument to the rule for <a>removing an available service<a>, when that <a><code>NetworkServices</code></a> object itself has been
  1719         garbage-collected.
  1720       </p>
  1721     </section>
  1722     <section>
  1723       <h3>
  1724         Use Cases and Requirements
  1725       </h3>
  1726       <p>
  1727         This section covers what the requirements are for this API, as well as illustrates some use cases.
  1728       </p>
  1729       <ul class="rule">
  1730         <li>Once a user has given permission, user agents should provide the ability for Web pages to communicate
  1731         directly with a Local-networked Service.
  1732           <ul class="rule">
  1733             <li>Example: A web-based TV remote control. A Web page wants to control the current user's TV, changing the
  1734             programming shown or increasing/decreasing/muting the volume of the Local-networked Device. The Web page
  1735             requests a service type that is known to be implemented by television sets to which it has the application
  1736             logic to communicate. Local devices providing the request service types are discovered and presented to the
  1737             user for authorization. The user selects one or more of the discovered television sets to be accessible to
  1738             the current Web page and then clicks 'Share'. The Web page can now communicate directly with the
  1739             user-authorized Local-networked services.
  1740             </li>
  1741           </ul>
  1742         </li>
  1743         <li>Web pages should be able to communicate with Local-networked Services using the messaging channel supported
  1744         by those Devices.
  1745           <ul class="rule">
  1746             <li>Example: A Web page advertises that it is capable of controlling multiple Home Media Servers. The user
  1747             can select their Home Media Server type from a drop-down list, at which point the Web page sends a request
  1748             to the user agent to connect with the associated service type of the Home Media Server. The Media Server
  1749             selected implements a Web Socket channel for bi-directional service communication and so the Web page opens
  1750             a web socket to the requested Media Server and can communicate as required via that appropriate channel.
  1751             </li>
  1752           </ul>
  1753         </li>
  1754         <li>Web pages should be able to communicate with Local-networked Services using the messaging format supported
  1755         by those Devices.
  1756           <ul class="rule">
  1757             <li>Example: A Web page advertises that it is capable of interacting with and controlling multiple types of
  1758             Home Media Server. The user can select their Home Media Server type from a drop-down list or known Media
  1759             Servers, at which point the Web page sends a request to the user agent to connect with the associated
  1760             service type (and, optionally, the associated event type) of the Home Media Server. The communication
  1761             protocols supported by Home Media Servers typically vary between UPnP, JSON-RPC, Protocol Buffers or other
  1762             messaging formats depending on the Home Media Server requested. The Web page is able to communicate with
  1763             the user-selected Home Media Server in the messaging format supported by that Device, which, in this
  1764             example is a simple key/value pair text format.
  1765             </li>
  1766           </ul>
  1767         </li>
  1768         <li>Web pages should not be able to communicate with Local-networked Services that have not been authorized by
  1769         the user thereby maintaining the user's privacy.
  1770           <ul class="rule">
  1771             <li>Example: A Web page requests access to one type of Local-networked service. The user authorizes access
  1772             to that particular service. Other services running on that same device, and on other devices within the
  1773             network, should not be accessible to the current Web page.
  1774             </li>
  1775           </ul>
  1776         </li>
  1777         <li>A user should be able to share one or more Local-networked Services based on a particular service type
  1778         request from a Web page.
  1779           <ul class="rule">
  1780             <li>Example: A Web page is capable of interacting with a specific profile of Local-networked Service. As
  1781             such, it makes a request to the user agent to access those services, of which multiple matches are found.
  1782             The user is capable of selecting one or more of the discovered services to share with the Web page. The Web
  1783             page can then implement a drag-and-drop interface for the user to drag specific actions on to one or more
  1784             of the authorized Local-networked Services.
  1785             </li>
  1786           </ul>
  1787         </li>
  1788         <li>User agents should provide an API exposed to script that exposes the features above. The user is notified
  1789         by UI anytime interaction with Local-networked Services is requested, giving the user full ability to cancel or
  1790         abort the transaction. The user selects the Local-networked Services to be connected to the current Web page,
  1791         and can cancel these at any time. No invocations to these APIs occur silently without user intervention.
  1792         </li>
  1793       </ul>
  1794     </section>
  1795     <section class="informative appendix">
  1796       <h3>
  1797         Examples
  1798       </h3>
  1799       <div class="example">
  1800         <p>
  1801           This sample code exposes a button. When clicked, this button is disabled and the user is prompted to offer a
  1802           network service. The user may also select multiple network services. When the user has authorized a network
  1803           service to be connected to the web page then the web page issues a simple command to get a list of all the
  1804           albums stored on the connected media player service.
  1805         </p>
  1806         <p>
  1807           The button is re-enabled only when the connected network service disconnects for whatever reason (the service
  1808           becomes unavailable on the network, the user disconnects from their current network or the user revokes
  1809           access to the service from the current web page). At this point the user can re-click the button to select a
  1810           new network service to connect to the web page and the above steps are repeated.
  1811         </p>
  1812         <p>
  1813           The provided service type identifier and service interaction used in this example is based on the
  1814           well-defined service type and messaging format supported by the <a href="http://xbmc.org/about/">XBMC Media
  1815           Server</a>.
  1816         </p>
  1817         <hr>
  1818         <pre class="highlight">
  1819 &lt;input type="button" value="Start" onclick="start()" id="startBtn"/&gt;
  1820 &lt;div id="debugconsole"&gt;&lt;/div&gt;
  1821 
  1822 &lt;script&gt;
  1823  var startBtn = document.getElementById('startBtn'),
  1824      debug = document.getElementById('debugconsole');
  1825 
  1826  function start() {
  1827    if(navigator.getNetworkServices) {
  1828       navigator.getNetworkServices('zeroconf:_xbmc-jsonrpc._tcp', gotXBMCService, error);
  1829       startBtn.disabled = true;
  1830    } else {
  1831       debug.innerHTML += "&lt;br&gt;Service Discovery not supported!";
  1832    }
  1833  }
  1834 
  1835  function gotXBMCService(services) {
  1836 
  1837 // Listen for service disconnect messages
  1838 
  1839    services[0].addEventListener('serviceoffline', function ( e ) {
  1840        debug.innerHTML += "&lt;br&gt;" + services[0].name + " disconnected.";
  1841        startBtn.disabled = false;
  1842    }, false);
  1843 
  1844 // Send a service message to get albums list (and process the service response)
  1845 
  1846    var svcXhr = new XMLHttpRequest();
  1847    svcXhr.open("POST", services[0].url + "/getAlbums"); // services[0].url and its sub-resources have been
  1848                                                         // whitelisted for cross-site XHR use in this
  1849                                                         // current browsing context.
  1850 
  1851    svcXhr.setRequestHeader('Content-Type', 'application/json-rpc');
  1852 
  1853    svcXhr.addEventListener('readystatechange', function ( response ) {
  1854      if( response.readyState != 4 || response.status != 200 )
  1855         return;
  1856      debug.innerHTML += "&lt;br&gt;" + services[0].name + " response received: ";
  1857      debug.textContent += JSON.parse(response.responseText);
  1858    }, false);
  1859 
  1860    var svcMsg = [
  1861      { "jsonrpc": "2.0", "method": "AudioLibrary.GetAlbums", "params": { "genreid": -1,
  1862          "artistid": -1, "start": -1, "end": -1 }, "id": "1" }
  1863    ];
  1864 
  1865    svcXhr.send(JSON.stringify(svcMsg));
  1866    debug.innerHTML += "&lt;br&gt;" + services[0].name + " request sent: ";
  1867    debug.textContent += JSON.stringify(svcMsg);
  1868 
  1869  }
  1870 
  1871  function error( err ) {
  1872    debug.innerHTML += "&lt;br&gt;An error occurred obtaining a local network service.";
  1873    startBtn.disabled = false;
  1874  }
  1875 &lt;/script&gt;
  1876 </pre>
  1877       </div>
  1878       <div class="example">
  1879         <p>
  1880           This sample exposes a drop-down list containing a number of common Home-based audio devices. When the user
  1881           selects an audio device from the list provided, they are prompted to authorize a network service based on the
  1882           service type requested. The user may also select multiple network services matching the selected service
  1883           type. In this example, the user selects their make as being <var>Sony</var> and their model as being
  1884           <var>Bravia S1000</var> from which the Web page can derive a service type
  1885           (<var>urn:schemas-upnp-org:service:RenderingControl:1</var>).<br>
  1886           <br>
  1887           Once the user has authorized the device, the web page sends a simple mute command according to the messaging
  1888           format supported by the device.
  1889         </p>
  1890         <hr>
  1891         <pre class="highlight">
  1892 &lt;select name="make" id="make"&gt;
  1893   &lt;option selected="selected" disabled="disabled"&gt;Select make&lt;/option&gt;
  1894   &lt;option&gt;Sony&lt;/option&gt;
  1895   &lt;option&gt;Philips&lt;/option&gt;
  1896   &lt;option&gt;Alba&lt;/option&gt;
  1897 &lt;/select&gt;
  1898 &lt;select name="model" id="model"&gt;&lt;/select&gt;
  1899 &lt;div id="debugconsole"&gt;&lt;/div&gt;
  1900 
  1901 &lt;script&gt;
  1902   var debug = document.getElementById('debugconsole');
  1903 
  1904   var models = {
  1905     "Sony": [
  1906       {"name": "Bravia TV S1000", "type": "upnp", "service": "urn:schemas-upnp-org:service:RenderingControl:1" },
  1907       {"name": "Bravia TV S2000", "type": "zeroconf", "service": "_mediarenderer._http._tcp" },
  1908       {"name": "HiFi WD10", "type": "upnp", "service": "urn:schemas-upnp-org:service:RenderingControl:1" }
  1909     ],
  1910     "Philips": [ /* ... */ ],
  1911     "Alba": [ /* ... */ ]
  1912   };
  1913 
  1914   var makeEl = document.getElementById("make"),
  1915       modelEl = document.getElementById("model");
  1916 
  1917   makeEl.addEventListener('change', function() {
  1918     modelEl.innerHTML = ""; // reset
  1919     var defaultOption = document.createElement("option");
  1920     defaultOption.textContent = "Select model";
  1921     defaultOption.setAttribute("disabled", "disabled");
  1922     defaultOption.setAttribute("selected", "selected");
  1923     modelEl.appendChild(defaultOption);
  1924     for(var i = 0, l = models[makeEl.value].length; i &lt; l; i++) {
  1925       var option = document.createElement("option");
  1926       option.textContent = models[makeEl.value][i]["name"];
  1927       option.setAttribute("value", models[makeEl.value][i]["type"] + ":" + models[makeEl.value][i]["service"]);
  1928       modelEl.appendChild(option);
  1929     }
  1930   }, false);
  1931 
  1932   modelEl.addEventListener('change', function() {
  1933     if(navigator.getNetworkServices &amp;&amp;
  1934          modelEl.value == "upnp:urn:schemas-upnp-org:service:RenderingControl:1") {
  1935       navigator.getNetworkServices(modelEl.value, successCallback, errorCallback);
  1936     } else if (modelEl.value == "zeroconf:_mediarenderer._http._tcp") {
  1937       debug.innerHTML += "&lt;br&gt;Service type is not implemented by this application.";
  1938     } else {
  1939       debug.innerHTML += "&lt;br&gt;Service Discovery is not supported!";
  1940     }
  1941   }, false);
  1942 &lt;/script&gt;
  1943 
  1944 &lt;script&gt;
  1945   function successCallback( services ) {
  1946 
  1947   // Listen for service push notification messages
  1948 
  1949     services[0].addEventListener('notify', function ( msg ) {
  1950          debug.innerHTML += "&lt;br&gt;" + services[0].name + " event received: ";
  1951          debug.textContent += msg.data;
  1952     }, false);
  1953 
  1954  // Send a control signal to mute the service audio
  1955 
  1956     var svcXhr = new XMLHttpRequest();
  1957     svcXhr.open("POST", services[0].url); // services[0].url and its
  1958                                           // sub-resources have been whitelisted for
  1959                                           // cross-site XHR use in this current
  1960                                           // browsing context.
  1961 
  1962     svcXhr.setRequestHeader('SOAPAction', 'urn:schemas-upnp-org:service:RenderingControl:1#SetMute');
  1963     svcXhr.setRequestHeader('Content-Type', 'text/xml; charset="utf-8";');
  1964 
  1965     svcXhr.onreadystatechange = function ( response ) {
  1966       if( response.readyState != 4 || response.status != 200 )
  1967         return;
  1968       debug.innerHTML += "&lt;br&gt;" + services[0].name + " response received: ";
  1969       debug.textContent += response.responseXML;
  1970     }
  1971 
  1972     // Service messaging to mute the provided service
  1973     var svcMsg = '&lt;?xml version="1.0" encoding="utf-8"?&gt;' +
  1974                  '&lt;s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" ' +
  1975                    'xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;' +
  1976                    '&lt;s:Body&gt;' +
  1977                      '&lt;u:SetMute xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"&gt;' +
  1978                        '&lt;InstanceID&gt;0&lt;/InstanceID&gt;' +
  1979                        '&lt;Channel&gt;Master&lt;/Channel&gt;' +
  1980                        '&lt;DesiredMute&gt;true&lt;/DesiredMute&gt;' +
  1981                      '&lt;/u:SetMute&gt;' +
  1982                    '&lt;/s:Body&gt;' +
  1983                  '&lt;/s:Envelope&gt;';
  1984 
  1985     svcXhr.send(svcMsg);
  1986     debug.innerHTML += "&lt;br&gt;" + services[0].name + " request sent: ";
  1987     debug.textContent += svcMsg;
  1988   }
  1989 
  1990   function errorCallback( error ) {
  1991     debug.innerHTML += "&lt;br&gt;An error occurred: " + error.code;
  1992   }
  1993 &lt;/script&gt;
  1994 </pre>
  1995       </div>
  1996     </section>
  1997     <section>
  1998       <h3>
  1999         Acknowledgements
  2000       </h3>
  2001       <p>
  2002         Thanks are expressed by the editor to the following individuals for their feedback on this specification to
  2003         date (in alphabetical order):<br>
  2004         <br>
  2005         Gar Bergstedt, Lars-Erik Bolstad, Cathy Chan, Hari G Kumar, Bob Lund, Giuseppe Pascale, Marcin Simonides,
  2006         Clarke Stevens, Christian Söderström, Mark Vickers.
  2007       </p>
  2008       <p>
  2009         Thanks are also expressed by the editor to the following organizations and groups for their support in
  2010         producing this specification to date (in alphabetical order):<br>
  2011         <br>
  2012         CableLabs, Opera Software ASA, W3C Device APIs Working Group, W3C Web and TV Interest Group.
  2013       </p>
  2014     </section>
  2015   </body>
  2016 </html>