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