forked from google/u2f-ref-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerichelper.js
50 lines (45 loc) · 1.49 KB
/
generichelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2014 Google Inc. All rights reserved
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
/**
* @fileoverview Implements a "generic" RequestHelper that provides a default
* response to unknown requests, and supports registering handlers for known
* requests.
*/
'use strict';
/**
* @typedef {function(HelperRequest): RequestHandler} */
var RequestHandlerFactory;
/**
* Implements a "generic" RequestHelper that provides a default
* response to unknown requests, and supports registering handlers for known
* @constructor
* @implements {RequestHelper}
*/
function GenericRequestHelper() {
/** @private {Object<string, RequestHandlerFactory>} */
this.handlerFactories_ = {};
}
/**
* Gets a handler for a request.
* @param {HelperRequest} request The request to handle.
* @return {RequestHandler} A handler for the request.
*/
GenericRequestHelper.prototype.getHandler = function(request) {
if (this.handlerFactories_.hasOwnProperty(request.type)) {
return this.handlerFactories_[request.type](request);
}
return null;
};
/**
* Registers a handler factory for a given type.
* @param {string} type The request type.
* @param {RequestHandlerFactory} factory A factory that can produce a handler
* for a request of a given type.
*/
GenericRequestHelper.prototype.registerHandlerFactory =
function(type, factory) {
this.handlerFactories_[type] = factory;
};