Bug 1882593 [wpt PR 44836] - Add test for unknown, invalid ancillary chunk which...
[gecko.git] / dom / security / PolicyTokenizer.cpp
blob8e28e6ce32770268656fa308ee432226767d77c6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "PolicyTokenizer.h"
9 #include "mozilla/Logging.h"
11 static mozilla::LogModule* GetPolicyTokenizerLog() {
12 static mozilla::LazyLogModule gPolicyTokenizerPRLog("PolicyTokenizer");
13 return gPolicyTokenizerPRLog;
16 #define POLICYTOKENIZERLOG(args) \
17 MOZ_LOG(GetPolicyTokenizerLog(), mozilla::LogLevel::Debug, args)
19 static const char16_t SEMICOL = ';';
21 PolicyTokenizer::PolicyTokenizer(const char16_t* aStart, const char16_t* aEnd)
22 : mCurChar(aStart), mEndChar(aEnd) {
23 POLICYTOKENIZERLOG(("PolicyTokenizer::PolicyTokenizer"));
26 PolicyTokenizer::~PolicyTokenizer() {
27 POLICYTOKENIZERLOG(("PolicyTokenizer::~PolicyTokenizer"));
30 void PolicyTokenizer::generateNextToken() {
31 skipWhiteSpaceAndSemicolon();
32 MOZ_ASSERT(mCurToken.Length() == 0);
33 const char16_t* const start = mCurChar;
34 while (!atEnd() && !nsContentUtils::IsHTMLWhitespace(*mCurChar) &&
35 *mCurChar != SEMICOL) {
36 mCurChar++;
38 if (start != mCurChar) {
39 mCurToken.Append(start, mCurChar - start);
41 POLICYTOKENIZERLOG(("PolicyTokenizer::generateNextToken: %s",
42 NS_ConvertUTF16toUTF8(mCurToken).get()));
45 void PolicyTokenizer::generateTokens(policyTokens& outTokens) {
46 POLICYTOKENIZERLOG(("PolicyTokenizer::generateTokens"));
48 // dirAndSrcs holds one set of [ name, src, src, src, ... ]
49 nsTArray<nsString> dirAndSrcs;
51 while (!atEnd()) {
52 generateNextToken();
53 dirAndSrcs.AppendElement(mCurToken);
54 skipWhiteSpace();
55 if (atEnd() || accept(SEMICOL)) {
56 outTokens.AppendElement(std::move(dirAndSrcs));
57 dirAndSrcs.ClearAndRetainStorage();
62 void PolicyTokenizer::tokenizePolicy(const nsAString& aPolicyString,
63 policyTokens& outTokens) {
64 POLICYTOKENIZERLOG(("PolicyTokenizer::tokenizePolicy"));
66 PolicyTokenizer tokenizer(aPolicyString.BeginReading(),
67 aPolicyString.EndReading());
69 tokenizer.generateTokens(outTokens);