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