OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview Implements a "generic" RequestHelper that provides a default | 6 * @fileoverview Implements a "generic" RequestHelper that provides a default |
7 * response to unknown requests, and supports registering handlers for known | 7 * response to unknown requests, and supports registering handlers for known |
8 * requests. | 8 * requests. |
9 */ | 9 */ |
10 'use strict'; | 10 'use strict'; |
11 | 11 |
12 /** | 12 /** |
13 * @typedef {function(HelperRequest): RequestHandler} */ | 13 * @typedef {function(HelperRequest): RequestHandler} */ |
14 var RequestHandlerFactory; | 14 var RequestHandlerFactory; |
15 | 15 |
16 /** | 16 /** |
17 * Implements a "generic" RequestHelper that provides a default | 17 * Implements a "generic" RequestHelper that provides a default |
18 * response to unknown requests, and supports registering handlers for known | 18 * response to unknown requests, and supports registering handlers for known |
19 * @constructor | 19 * @constructor |
20 * @implements {RequestHelper} | 20 * @implements {RequestHelper} |
21 */ | 21 */ |
22 function GenericRequestHelper() { | 22 function GenericRequestHelper() { |
23 /** @private {Object.<string, RequestHandlerFactory>} */ | 23 /** @private {Object<string, RequestHandlerFactory>} */ |
24 this.handlerFactories_ = {}; | 24 this.handlerFactories_ = {}; |
25 } | 25 } |
26 | 26 |
27 /** | 27 /** |
28 * Gets a handler for a request. | 28 * Gets a handler for a request. |
29 * @param {HelperRequest} request The request to handle. | 29 * @param {HelperRequest} request The request to handle. |
30 * @return {RequestHandler} A handler for the request. | 30 * @return {RequestHandler} A handler for the request. |
31 */ | 31 */ |
32 GenericRequestHelper.prototype.getHandler = function(request) { | 32 GenericRequestHelper.prototype.getHandler = function(request) { |
33 if (this.handlerFactories_.hasOwnProperty(request.type)) { | 33 if (this.handlerFactories_.hasOwnProperty(request.type)) { |
34 return this.handlerFactories_[request.type](request); | 34 return this.handlerFactories_[request.type](request); |
35 } | 35 } |
36 return null; | 36 return null; |
37 }; | 37 }; |
38 | 38 |
39 /** | 39 /** |
40 * Registers a handler factory for a given type. | 40 * Registers a handler factory for a given type. |
41 * @param {string} type The request type. | 41 * @param {string} type The request type. |
42 * @param {RequestHandlerFactory} factory A factory that can produce a handler | 42 * @param {RequestHandlerFactory} factory A factory that can produce a handler |
43 * for a request of a given type. | 43 * for a request of a given type. |
44 */ | 44 */ |
45 GenericRequestHelper.prototype.registerHandlerFactory = | 45 GenericRequestHelper.prototype.registerHandlerFactory = |
46 function(type, factory) { | 46 function(type, factory) { |
47 this.handlerFactories_[type] = factory; | 47 this.handlerFactories_[type] = factory; |
48 }; | 48 }; |
OLD | NEW |