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.
6 * @fileoverview Implements a "generic" RequestHelper that provides a default
7 * response to unknown requests, and supports registering handlers for known
13 * @typedef {function(HelperRequest): RequestHandler} */
14 var RequestHandlerFactory;
17 * Implements a "generic" RequestHelper that provides a default
18 * response to unknown requests, and supports registering handlers for known
20 * @implements {RequestHelper}
22 function GenericRequestHelper() {
23 /** @private {Object<string, RequestHandlerFactory>} */
24 this.handlerFactories_ = {};
28 * Gets a handler for a request.
29 * @param {HelperRequest} request The request to handle.
30 * @return {RequestHandler} A handler for the request.
32 GenericRequestHelper.prototype.getHandler = function(request) {
33 if (this.handlerFactories_.hasOwnProperty(request.type)) {
34 return this.handlerFactories_[request.type](request);
40 * Registers a handler factory for a given type.
41 * @param {string} type The request type.
42 * @param {RequestHandlerFactory} factory A factory that can produce a handler
43 * for a request of a given type.
45 GenericRequestHelper.prototype.registerHandlerFactory =
46 function(type, factory) {
47 this.handlerFactories_[type] = factory;