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