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