Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / LinkImport.cpp
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 #include "config.h"
32 #include "core/html/LinkImport.h"
33
34 #include "core/dom/Document.h"
35 #include "core/fetch/CrossOriginAccessControl.h"
36 #include "core/html/HTMLImportChild.h"
37 #include "core/html/HTMLImportsController.h"
38 #include "core/html/HTMLLinkElement.h"
39
40
41 namespace WebCore {
42
43 PassOwnPtr<LinkImport> LinkImport::create(HTMLLinkElement* owner)
44 {
45     return adoptPtr(new LinkImport(owner));
46 }
47
48 LinkImport::LinkImport(HTMLLinkElement* owner)
49     : LinkResource(owner)
50     , m_child(0)
51 {
52 }
53
54 LinkImport::~LinkImport()
55 {
56     clear();
57 }
58
59 Document* LinkImport::importedDocument() const
60 {
61     if (!m_child)
62         return 0;
63     return m_child->importedDocument();
64 }
65
66 void LinkImport::process()
67 {
68     if (m_child)
69         return;
70     if (!m_owner)
71         return;
72     if (!shouldLoadResource())
73         return;
74
75     if (!m_owner->document().import()) {
76         ASSERT(m_owner->document().frame()); // The document should be the master.
77         HTMLImportsController::provideTo(&m_owner->document());
78     }
79
80     LinkRequestBuilder builder(m_owner);
81     if (!builder.isValid()) {
82         didFinish();
83         return;
84     }
85
86     HTMLImport* parent = m_owner->document().import();
87     HTMLImportsController* controller = parent->controller();
88     m_child = controller->load(parent, this, builder.build(true));
89     if (!m_child) {
90         didFinish();
91         return;
92     }
93 }
94
95 void LinkImport::clear()
96 {
97     m_owner = 0;
98     if (m_child) {
99         m_child->clearClient();
100         m_child = 0;
101     }
102 }
103
104 void LinkImport::ownerRemoved()
105 {
106     clear();
107 }
108
109 void LinkImport::didFinish()
110 {
111     if (!m_owner)
112         return;
113     // Because didFinish() is called from import's own scheduler in HTMLImportsController,
114     // we don't need to scheduleEvent() here.
115     m_owner->dispatchEventImmediately();
116 }
117
118 void LinkImport::importChildWasDestroyed(HTMLImportChild* child)
119 {
120     ASSERT(m_child == child);
121     m_child = 0;
122     clear();
123 }
124
125 bool LinkImport::isCreatedByParser() const
126 {
127     return m_owner && m_owner->isCreatedByParser();
128 }
129
130 HTMLLinkElement* LinkImport::link()
131 {
132     return m_owner;
133 }
134
135 bool LinkImport::hasLoaded() const
136 {
137     return m_child && m_child->isDone() && !m_child->loaderHasError();
138 }
139
140 bool LinkImport::ownsLoader() const
141 {
142     return m_child && m_child->hasLoader() && m_child->ownsLoader();
143 }
144
145 } // namespace WebCore