Backed out 8 changesets (bug 1918596, bug 1917575, bug 1874689, bug 1925181, bug...
[gecko.git] / dom / quota / OriginParser.h
blobf4fecf1c0aa6c7d54e827fa71e326e2f124a34d8
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_QUOTA_ORIGINPARSER_H_
8 #define DOM_QUOTA_ORIGINPARSER_H_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/Nullable.h"
12 #include "nsCharSeparatedTokenizer.h"
13 #include "nsString.h"
14 #include "nsTArray.h"
16 namespace mozilla {
18 class OriginAttributes;
20 namespace dom::quota {
22 class MOZ_STACK_CLASS OriginParser final {
23 public:
24 enum ResultType { InvalidOrigin, ObsoleteOrigin, ValidOrigin };
26 private:
27 using Tokenizer =
28 nsCCharSeparatedTokenizerTemplate<NS_TokenizerIgnoreNothing>;
30 enum SchemeType { eNone, eFile, eAbout, eChrome };
32 enum State {
33 eExpectingAppIdOrScheme,
34 eExpectingInMozBrowser,
35 eExpectingScheme,
36 eExpectingEmptyToken1,
37 eExpectingEmptyToken2,
38 eExpectingEmptyTokenOrUniversalFileOrigin,
39 eExpectingHost,
40 eExpectingPort,
41 eExpectingEmptyTokenOrDriveLetterOrPathnameComponent,
42 eExpectingEmptyTokenOrPathnameComponent,
43 eExpectingEmptyToken1OrHost,
45 // We transit from eExpectingHost to this state when we encounter a host
46 // beginning with "[" which indicates an IPv6 literal. Because we mangle the
47 // IPv6 ":" delimiter to be a "+", we will receive separate tokens for each
48 // portion of the IPv6 address, including a final token that ends with "]".
49 // (Note that we do not mangle "[" or "]".) Note that the URL spec
50 // explicitly disclaims support for "<zone_id>" and so we don't have to deal
51 // with that.
52 eExpectingIPV6Token,
53 eComplete,
54 eHandledTrailingSeparator
57 const nsCString mOrigin;
58 Tokenizer mTokenizer;
60 nsCString mScheme;
61 nsCString mHost;
62 Nullable<uint32_t> mPort;
63 nsTArray<nsCString> mPathnameComponents;
64 nsCString mHandledTokens;
66 SchemeType mSchemeType;
67 State mState;
68 bool mUniversalFileOrigin;
69 bool mMaybeDriveLetter;
70 bool mError;
71 bool mMaybeObsolete;
73 // Number of group which a IPv6 address has. Should be less than 9.
74 uint8_t mIPGroup;
76 public:
77 explicit OriginParser(const nsACString& aOrigin)
78 : mOrigin(aOrigin),
79 mTokenizer(aOrigin, '+'),
80 mSchemeType(eNone),
81 mState(eExpectingAppIdOrScheme),
82 mUniversalFileOrigin(false),
83 mMaybeDriveLetter(false),
84 mError(false),
85 mMaybeObsolete(false),
86 mIPGroup(0) {}
88 static ResultType ParseOrigin(const nsACString& aOrigin, nsCString& aSpec,
89 OriginAttributes* aAttrs,
90 nsCString& aOriginalSuffix);
92 ResultType Parse(nsACString& aSpec);
94 private:
95 void HandleScheme(const nsDependentCSubstring& aToken);
97 void HandlePathnameComponent(const nsDependentCSubstring& aToken);
99 void HandleToken(const nsDependentCSubstring& aToken);
101 void HandleTrailingSeparator();
104 bool IsUUIDOrigin(const nsCString& aOrigin);
106 } // namespace dom::quota
107 } // namespace mozilla
109 #endif // DOM_QUOTA_ORIGINPARSER_H_