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