1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef HTMLImportLoader_h
32#define HTMLImportLoader_h
33
34#include "core/dom/DocumentParserClient.h"
35#include "core/fetch/RawResource.h"
36#include "core/fetch/ResourceOwner.h"
37#include "platform/heap/Handle.h"
38#include "wtf/OwnPtr.h"
39#include "wtf/PassOwnPtr.h"
40#include "wtf/Vector.h"
41
42namespace blink {
43
44class CustomElementSyncMicrotaskQueue;
45class Document;
46class DocumentWriter;
47class HTMLImportChild;
48class HTMLImportsController;
49
50
51//
52// Owning imported Document lifetime. It also implements ResourceClient through ResourceOwner
53// to feed fetched bytes to the DocumentWriter of the imported document.
54// HTMLImportLoader is owned by HTMLImportsController.
55//
56//
57class HTMLImportLoader FINAL : public NoBaseWillBeGarbageCollectedFinalized<HTMLImportLoader>, public ResourceOwner<RawResource>, public DocumentParserClient {
58public:
59    enum State {
60        StateLoading,
61        StateWritten,
62        StateParsed,
63        StateLoaded,
64        StateError
65    };
66
67    static PassOwnPtrWillBeRawPtr<HTMLImportLoader> create(HTMLImportsController* controller)
68    {
69        return adoptPtrWillBeNoop(new HTMLImportLoader(controller));
70    }
71
72    virtual ~HTMLImportLoader();
73
74    Document* document() const { return m_document.get(); }
75    void addImport(HTMLImportChild*);
76#if !ENABLE(OILPAN)
77    void removeImport(HTMLImportChild*);
78#endif
79    void moveToFirst(HTMLImportChild*);
80    HTMLImportChild* firstImport() const { return m_imports[0]; }
81    bool isFirstImport(const HTMLImportChild* child) const { return m_imports.size() ? firstImport() == child : false; }
82
83    bool isDone() const { return m_state == StateLoaded || m_state == StateError; }
84    bool hasError() const { return m_state == StateError; }
85    bool shouldBlockScriptExecution() const;
86
87#if !ENABLE(OILPAN)
88    void importDestroyed();
89#endif
90    void startLoading(const ResourcePtr<RawResource>&);
91
92    // Tells the loader that all of the import's stylesheets finished
93    // loading.
94    // Called by Document::didRemoveAllPendingStylesheet.
95    void didRemoveAllPendingStylesheet();
96
97    PassRefPtrWillBeRawPtr<CustomElementSyncMicrotaskQueue> microtaskQueue() const;
98
99    virtual void trace(Visitor*);
100
101private:
102    HTMLImportLoader(HTMLImportsController*);
103
104    // RawResourceClient
105    virtual void responseReceived(Resource*, const ResourceResponse&) OVERRIDE;
106    virtual void dataReceived(Resource*, const char* data, int length) OVERRIDE;
107    virtual void notifyFinished(Resource*) OVERRIDE;
108
109    // DocumentParserClient
110
111    // Called after document parse is complete after DOMContentLoaded was dispatched.
112    virtual void notifyParserStopped();
113
114    State startWritingAndParsing(const ResourceResponse&);
115    State finishWriting();
116    State finishParsing();
117    State finishLoading();
118
119    void setState(State);
120    void didFinishLoading();
121    bool hasPendingResources() const;
122#if !ENABLE(OILPAN)
123    void clear();
124#endif
125
126    RawPtrWillBeMember<HTMLImportsController> m_controller;
127    WillBeHeapVector<RawPtrWillBeMember<HTMLImportChild> > m_imports;
128    State m_state;
129    RefPtrWillBeMember<Document> m_document;
130    RefPtrWillBeMember<DocumentWriter> m_writer;
131    RefPtrWillBeMember<CustomElementSyncMicrotaskQueue> m_microtaskQueue;
132};
133
134} // namespace blink
135
136#endif // HTMLImportLoader_h
137