no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / dom / security / PolicyTokenizer.h
blob1c3c18175d893f6e7792eb462d3552e92d11260a
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 #ifndef PolicyTokenizer_h___
8 #define PolicyTokenizer_h___
10 #include "nsContentUtils.h"
11 #include "nsString.h"
13 /**
14 * How does the parsing work?
16 * We generate tokens by splitting the policy-string by whitespace and
17 * semicolon. Interally the tokens are represented as an array of string-arrays:
19 * [
20 * [ name, src, src, src, ... ],
21 * [ name, src, src, src, ... ],
22 * [ name, src, src, src, ... ]
23 * ]
25 * for example:
26 * [
27 * [ img-src, http://www.example.com, http:www.test.com ],
28 * [ default-src, 'self'],
29 * [ script-src, 'unsafe-eval', 'unsafe-inline' ],
30 * ]
33 using policyTokens = nsTArray<CopyableTArray<nsString>>;
35 class PolicyTokenizer {
36 public:
37 static void tokenizePolicy(const nsAString& aPolicyString,
38 policyTokens& outTokens);
40 private:
41 PolicyTokenizer(const char16_t* aStart, const char16_t* aEnd);
42 ~PolicyTokenizer();
44 inline bool atEnd() { return mCurChar >= mEndChar; }
46 inline void skipWhiteSpace() {
47 while (mCurChar < mEndChar && nsContentUtils::IsHTMLWhitespace(*mCurChar)) {
48 mCurChar++;
50 mCurToken.Truncate();
53 inline void skipWhiteSpaceAndSemicolon() {
54 while (mCurChar < mEndChar &&
55 (*mCurChar == ';' || nsContentUtils::IsHTMLWhitespace(*mCurChar))) {
56 mCurChar++;
58 mCurToken.Truncate();
61 inline bool accept(char16_t aChar) {
62 NS_ASSERTION(mCurChar < mEndChar, "Trying to dereference mEndChar");
63 if (*mCurChar == aChar) {
64 mCurToken.Append(*mCurChar++);
65 return true;
67 return false;
70 void generateNextToken();
71 void generateTokens(policyTokens& outTokens);
73 const char16_t* mCurChar;
74 const char16_t* mEndChar;
75 nsString mCurToken;
78 #endif /* PolicyTokenizer_h___ */