summaryrefslogtreecommitdiffstats
path: root/parser/prototype/PrototypeDocumentParser.h
blob: 3e7935bffc02bddb65f4f1e0722efa9c2d87510b (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef mozilla_parser_PrototypeDocumentParser_h
#define mozilla_parser_PrototypeDocumentParser_h

#include "nsCycleCollectionParticipant.h"
#include "nsIContentSink.h"
#include "nsIParser.h"
#include "nsXULPrototypeDocument.h"

class nsIExpatSink;

namespace mozilla {
namespace dom {
class PrototypeDocumentContentSink;
}  // namespace dom
}  // namespace mozilla

namespace mozilla {
namespace parser {

// The PrototypeDocumentParser is more of a stub than a real parser. It is
// responsible for loading an nsXULPrototypeDocument either from the startup
// cache or creating a new prototype from the original source if a cached
// version does not exist. Once the parser finishes loading the prototype it
// will notify the content sink.
class PrototypeDocumentParser final : public nsIParser,
                                      public nsIStreamListener {
 public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS

  NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PrototypeDocumentParser, nsIParser)

  explicit PrototypeDocumentParser(nsIURI* aDocumentURI,
                                   dom::Document* aDocument);

  NS_DECL_NSIREQUESTOBSERVER
  NS_DECL_NSISTREAMLISTENER

  // Start nsIParser
  // Ideally, this would just implement nsBaseParser since most of these are
  // stubs, but Document.h expects an nsIParser.
  NS_IMETHOD_(void) SetContentSink(nsIContentSink* aSink) override;

  NS_IMETHOD_(nsIContentSink*) GetContentSink() override;

  NS_IMETHOD_(void) GetCommand(nsCString& aCommand) override {}

  NS_IMETHOD_(void) SetCommand(const char* aCommand) override {}

  NS_IMETHOD_(void) SetCommand(eParserCommands aParserCommand) override {}

  virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding,
                                  int32_t aSource,
                                  bool aForceAutoDetection) override {}

  virtual nsIStreamListener* GetStreamListener() override;

  NS_IMETHOD ContinueInterruptedParsing() override {
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  NS_IMETHOD_(void) BlockParser() override {}

  NS_IMETHOD_(void) UnblockParser() override {}

  NS_IMETHOD_(void) ContinueInterruptedParsingAsync() override {}

  NS_IMETHOD_(bool) IsParserEnabled() override { return true; }

  NS_IMETHOD_(bool) IsComplete() override;

  NS_IMETHOD Parse(nsIURI* aURL) override;

  NS_IMETHOD Terminate() override { return NS_ERROR_NOT_IMPLEMENTED; }

  virtual bool IsInsertionPointDefined() override { return false; }

  void IncrementScriptNestingLevel() final {}

  void DecrementScriptNestingLevel() final {}

  bool HasNonzeroScriptNestingLevel() const final { return false; }

  virtual bool IsScriptCreated() override { return false; }

  // End nsIParser

 private:
  virtual ~PrototypeDocumentParser();

 protected:
  nsresult PrepareToLoadPrototype(nsIURI* aURI,
                                  nsIPrincipal* aDocumentPrincipal,
                                  nsIParser** aResult);

  // This is invoked whenever the prototype for this document is loaded
  // and should be walked, regardless of whether the XUL cache is
  // disabled, whether the protototype was loaded, whether the
  // prototype was loaded from the cache or created by parsing the
  // actual XUL source, etc.
  nsresult OnPrototypeLoadDone();

  nsCOMPtr<nsIURI> mDocumentURI;
  RefPtr<dom::PrototypeDocumentContentSink> mOriginalSink;
  RefPtr<dom::Document> mDocument;

  // The XML parser that data is forwarded to when the prototype does not exist
  // and must be parsed from disk.
  nsCOMPtr<nsIStreamListener> mStreamListener;

  // The current prototype that we are walking to construct the
  // content model.
  RefPtr<nsXULPrototypeDocument> mCurrentPrototype;

  // True if there was a prototype in the cache and it finished loading
  // already.
  bool mPrototypeAlreadyLoaded;

  // True after the parser has notified the content sink that it is done.
  bool mIsComplete;
};

}  // namespace parser
}  // namespace mozilla

#endif  // mozilla_parser_PrototypeDocumentParser_h