chromium/components/url_rewrite/mojom/url_request_rewrite.mojom

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module url_rewrite.mojom;

import "url/mojom/url.mojom";

// Used by the browser to push URL rewrite rules to renderers. There will be
// one receiver per web frame.
// TODO(crbug.com/40632933): Support URL rewriting for service workers.
interface UrlRequestRulesReceiver {
  // Receives a set of rules to apply to URL requests.
  OnRulesUpdated(UrlRequestRewriteRules rules);
};

// Collection of UrlRequestRules.
struct UrlRequestRewriteRules {
  array<UrlRequestRule> rules;
};

// A URL request modification rule.
struct UrlRequestRule {
  // Set of hosts to apply the rewrites to. If empty, the rule will apply to
  // every request, independent of host.
  array<string>? hosts_filter;

  // Set of schemes to apply the rewrites to. If empty, the rule will apply to
  // every request, independent of scheme.
  array<string>? schemes_filter;

  // URL request rewrites to apply.
  array<UrlRequestAction> actions;
};

union UrlRequestAction {
  // Adds a set of headers to a URL request.
  UrlRequestRewriteAddHeaders add_headers;

  // Removes a header based on the presence of a pattern in the URL query.
  UrlRequestRewriteRemoveHeader remove_header;

  // Substitutes a pattern in the URL query.
  UrlRequestRewriteSubstituteQueryPattern substitute_query_pattern;

  // Replaces a URL if the original URL ends with a pattern.
  UrlRequestRewriteReplaceUrl replace_url;

  // Appends to the URL query.
  UrlRequestRewriteAppendToQuery append_to_query;

  // Specifies whether the request should be allowed or blocked.
  // Cannot be combined with other UrlRequestActions.
  UrlRequestAccessPolicy policy;
};

// Adds |headers| to the URL request. If a header is already present in the
// original URL request, it will be overwritten.
struct UrlRequestRewriteAddHeaders {
  // The headers to add.
  array<UrlHeader> headers;
};

// An HTTP header field.
struct UrlHeader {
  string name;
  string value;
};

// If |query_pattern| in the URL query, removes |header_name| from the list of
// headers. If |query_pattern| is empty, removes |header_name| from the list
// of headers unconditionally
struct UrlRequestRewriteRemoveHeader {
  // The pattern to look for in the URL request.
  string? query_pattern;

  // The header to remove.
  string header_name;
};

// If |pattern| is found in the URL request query, replaces it with
// |substitution|.
struct UrlRequestRewriteSubstituteQueryPattern {
  // The pattern to look for in the URL request.
  string pattern;

  // The string to repalce |pattern| with.
  string substitution;
};

// If the URL in the URL request ends with |url_ends_with|, rewrites the URL to
// |new_url|.
struct UrlRequestRewriteReplaceUrl {
  // The pattern to look for in the URL.
  string url_ends_with;

  // The replacement URL.
  url.mojom.Url new_url;
};

// Appends |query| to the URL request query. If the URL request already has a
// non-empty query, appends |query| to it, preceded by "&". Otherwise, sets the
// URL request query to |query|.
struct UrlRequestRewriteAppendToQuery {
  // The query to be added or set to the URL.
  string query;
};

enum UrlRequestAccessPolicy {
  kAllow,
  kDeny,
};