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