Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/security/PolicyTokenizer.h
Line
Count
Source
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/. */
6
7
#ifndef PolicyTokenizer_h___
8
#define PolicyTokenizer_h___
9
10
#include "nsContentUtils.h"
11
#include "nsString.h"
12
13
/**
14
 * How does the parsing work?
15
 *
16
 * We generate tokens by splitting the policy-string by whitespace and semicolon.
17
 * Interally the tokens are represented as an array of string-arrays:
18
 *
19
 *  [
20
 *    [ name, src, src, src, ... ],
21
 *    [ name, src, src, src, ... ],
22
 *    [ name, src, src, src, ... ]
23
 *  ]
24
 *
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
 *  ]
31
 */
32
33
typedef nsTArray< nsTArray<nsString> > policyTokens;
34
35
class PolicyTokenizer {
36
37
  public:
38
    static void tokenizePolicy(const nsAString &aPolicyString, policyTokens& outTokens);
39
40
  private:
41
    PolicyTokenizer(const char16_t* aStart, const char16_t* aEnd);
42
    ~PolicyTokenizer();
43
44
    inline bool atEnd()
45
51.9M
    {
46
51.9M
      return mCurChar >= mEndChar;
47
51.9M
    }
48
49
    inline void skipWhiteSpace()
50
3.70M
    {
51
7.12M
      while (mCurChar < mEndChar &&
52
7.12M
             nsContentUtils::IsHTMLWhitespace(*mCurChar)) {
53
3.42M
        mCurToken.Append(*mCurChar++);
54
3.42M
      }
55
3.70M
      mCurToken.Truncate();
56
3.70M
    }
57
58
    inline void skipWhiteSpaceAndSemicolon()
59
3.70M
    {
60
3.71M
      while (mCurChar < mEndChar && (*mCurChar == ';' ||
61
3.71M
             nsContentUtils::IsHTMLWhitespace(*mCurChar))){
62
11.2k
        mCurToken.Append(*mCurChar++);
63
11.2k
      }
64
3.70M
      mCurToken.Truncate();
65
3.70M
    }
66
67
    inline bool accept(char16_t aChar)
68
3.70M
    {
69
3.70M
      NS_ASSERTION(mCurChar < mEndChar, "Trying to dereference mEndChar");
70
3.70M
      if (*mCurChar == aChar) {
71
310k
        mCurToken.Append(*mCurChar++);
72
310k
        return true;
73
310k
      }
74
3.39M
      return false;
75
3.39M
    }
76
77
    void generateNextToken();
78
    void generateTokens(policyTokens& outTokens);
79
80
    const char16_t* mCurChar;
81
    const char16_t* mEndChar;
82
    nsString        mCurToken;
83
};
84
85
#endif /* PolicyTokenizer_h___ */