Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / cryptotoken / requesthelper.js
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6  * @fileoverview Provides a "bottom half" helper to assist with raw requests.
7  * This fills the same role as the Authenticator-Specific Module component of
8  * U2F documents, although the API is different.
9  */
10 'use strict';
11
12 /**
13  * @typedef {{
14  *   type: string,
15  *   timeout: number
16  * }}
17  */
18 var HelperRequest;
19
20 /**
21  * @typedef {{
22  *   type: string,
23  *   code: (number|undefined)
24  * }}
25  */
26 var HelperReply;
27
28 /**
29  * A helper to process requests.
30  * @interface
31  */
32 function RequestHelper() {}
33
34 /**
35  * Gets a handler for a request.
36  * @param {HelperRequest} request The request to handle.
37  * @return {RequestHandler} A handler for the request.
38  */
39 RequestHelper.prototype.getHandler = function(request) {};
40
41 /**
42  * A handler to track an outstanding request.
43  * @extends {Closeable}
44  * @interface
45  */
46 function RequestHandler() {}
47
48 /** @typedef {function(HelperReply, string=)} */
49 var RequestHandlerCallback;
50
51 /**
52  * @param {RequestHandlerCallback} cb Called with the result of the request,
53  *     and an optional source for the result.
54  * @return {boolean} Whether this handler could be run.
55  */
56 RequestHandler.prototype.run = function(cb) {};
57
58 /** Closes this handler. */
59 RequestHandler.prototype.close = function() {};
60
61 /**
62  * Makes a response to a helper request with an error code.
63  * @param {HelperRequest} request The request to make a response to.
64  * @param {DeviceStatusCodes} code The error code to return.
65  * @param {string=} opt_defaultType The default response type, if none is
66  *     present in the request.
67  * @return {HelperReply} The helper error response.
68  */
69 function makeHelperErrorResponse(request, code, opt_defaultType) {
70   var type;
71   if (request && request.type) {
72     type = request.type.replace(/_request$/, '_reply');
73   } else {
74     type = opt_defaultType || 'unknown_type_reply';
75   }
76   var reply = {
77     'type': type,
78     'code': /** @type {number} */ (code)
79   };
80   return reply;
81 }