• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Implements a "generic" RequestHelper that provides a default
7 * response to unknown requests, and supports registering handlers for known
8 * requests.
9 */
10'use strict';
11
12/**
13 * @typedef {function(HelperRequest): RequestHandler} */
14var RequestHandlerFactory;
15
16/**
17 * Implements a "generic" RequestHelper that provides a default
18 * response to unknown requests, and supports registering handlers for known
19 * @constructor
20 * @implements {RequestHelper}
21 */
22function GenericRequestHelper() {
23  /** @private {Object.<string, RequestHandlerFactory>} */
24  this.handlerFactories_ = {};
25}
26
27/**
28 * Gets a handler for a request.
29 * @param {HelperRequest} request The request to handle.
30 * @return {RequestHandler} A handler for the request.
31 */
32GenericRequestHelper.prototype.getHandler = function(request) {
33  if (this.handlerFactories_.hasOwnProperty(request.type)) {
34    return this.handlerFactories_[request.type](request);
35  }
36  return null;
37};
38
39/**
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.
44 */
45GenericRequestHelper.prototype.registerHandlerFactory =
46    function(type, factory) {
47  this.handlerFactories_[type] = factory;
48};
49