Repository: envoyproxy/envoy
File: source/extensions/filters/network/generic_proxy/filter_callbacks.h (C/C++)

Code before:
  /**
   * Add a encoder filter that is used when writing connection data.
   * @param filter supplies the filter to add.
   */
  virtual void addEncoderFilter(std::shared_ptr<EncoderFilter> filter) PURE;

  /**
   * Add a decoder/encoder filter that is used both when reading and writing connection data.
   * @param filter supplies the filter to add.
   */
  virtual void addFilter(std::shared_ptr<StreamFilter> filter) PURE;
};

/**
 * Simple struct of additional contextual information of filter, e.g. filter config name
 * from configuration.
 */
struct FilterContext {
  // The name of the filter configuration that used to create related filter factory function.
  // This could be any legitimate non-empty string.
  // This config name will have longer lifetime than any related filter instance. So string
  // view could be used here safely.
  absl::string_view config_name;
};

using FilterFactoryCb = std::function<void(FilterChainFactoryCallbacks& callbacks)>;

/**
 * The filter chain manager is provided by the connection manager to the filter chain factory.
 * The filter chain factory will post the filter factory context and filter factory to the
 * filter chain manager to create filter and construct HTTP stream filter chain.
 */
class FilterChainManager {
public:
  virtual ~FilterChainManager() = default;

  /**
   * Post filter factory context and filter factory to the filter chain manager. The filter
   * chain manager will create filter instance based on the context and factory internally.
   * @param context supplies additional contextual information of filter factory.
   * @param factory factory function used to create filter instances.
   */
  virtual void applyFilterFactoryCb(FilterContext context, FilterFactoryCb& factory) PURE;
};

/**
 * A FilterChainFactory is used by a connection manager to create a stream level filter chain
 * when a new stream is created. Typically it would be implemented by a configuration engine
 * that would install a set of filters that are able to process an application scenario on top of a
 * stream of generic requests.
 */

Review comment (suggestion):
```suggestion
   * This should be called once before adding any filter.
   * NOTE: By default, the FilterChainFactory will call this method to set the config name
   * from configuration and the per filter factory doesn't need to care about this method
   * except when it wants to override the config name.
```

Code after:
  /**
   * Add a encoder filter that is used when writing connection data.
   * @param filter supplies the filter to add.
   */
  virtual void addEncoderFilter(std::shared_ptr<EncoderFilter> filter) PURE;

  /**
   * Add a decoder/encoder filter that is used both when reading and writing connection data.
   * @param filter supplies the filter to add.
   */
  virtual void addFilter(std::shared_ptr<StreamFilter> filter) PURE;

  /**
   * @return absl::string_view the filter config name that used to create the filter. This
   * will return the latest set name by the setFilterConfigName() method.
   */
  virtual absl::string_view filterConfigName() const PURE;

  /**
   * Set the filter config name that used to create the filter. The latest set name will be
   * used when creating filters.
   *
   * This should be called once before adding any filter.
   * NOTE: By default, the FilterChainFactory will call this method to set the config name
   * from configuration and the per filter factory does not need to care this method except
   * it wants to override the config name.
   *
   * @param name supplies the filter config name.
   */
  virtual void setFilterConfigName(absl::string_view name) PURE;
};

/**
 * Simple struct of additional contextual information of filter, e.g. filter config name
 * from configuration.
 */
struct FilterContext {
  FilterContext() = default;
  explicit FilterContext(absl::string_view config_name) : config_name(config_name) {}

  // The name of the filter configuration that used to create related filter factory function.
  // This could be any legitimate non-empty string.
  // This config name will have longer lifetime than any related filter instance. So string
  // view could be used here safely.
  absl::string_view config_name;
};

using FilterFactoryCb = std::function<void(FilterChainFactoryCallbacks& callbacks)>;

/**
 * A FilterChainFactory is used by a connection manager to create a stream level filter chain