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