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