1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38

headless / lib / renderer / allowlist.cc [blame]

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

#include "headless/lib/renderer/allowlist.h"

#include "base/check.h"
#include "base/strings/string_split.h"

namespace headless {

Allowlist::Allowlist(std::string list, bool default_allow)
    : storage_(std::move(list)),
      default_allow_(default_allow),
      entries_(base::SplitStringPiece(storage_,
                                      ",",
                                      base::WhitespaceHandling::TRIM_WHITESPACE,
                                      base::SplitResult::SPLIT_WANT_NONEMPTY)) {
}

Allowlist::~Allowlist() = default;

bool Allowlist::IsAllowed(std::string_view str) const {
  for (auto entry : entries_) {
    CHECK(!entry.empty());  // Per SplitResult::SPLIT_WANT_NONEMPTY
    bool entry_is_allow = true;
    if (entry.starts_with('-')) {
      entry_is_allow = false;
      entry.remove_prefix(1);
    }
    if (entry == "*" || entry == str) {
      return entry_is_allow;
    }
  }
  return default_allow_;
}

}  // namespace headless