Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / StyleRuleImport.cpp
1 /*
2  * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3  * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4  * Copyright (C) 2002, 2005, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23 #include "core/css/StyleRuleImport.h"
24
25 #include "FetchInitiatorTypeNames.h"
26 #include "core/css/StyleSheetContents.h"
27 #include "core/dom/Document.h"
28 #include "core/fetch/CSSStyleSheetResource.h"
29 #include "core/fetch/FetchRequest.h"
30 #include "core/fetch/ResourceFetcher.h"
31
32 namespace WebCore {
33
34 PassRefPtr<StyleRuleImport> StyleRuleImport::create(const String& href, PassRefPtr<MediaQuerySet> media)
35 {
36     return adoptRef(new StyleRuleImport(href, media));
37 }
38
39 StyleRuleImport::StyleRuleImport(const String& href, PassRefPtr<MediaQuerySet> media)
40     : StyleRuleBase(Import)
41     , m_parentStyleSheet(0)
42     , m_styleSheetClient(this)
43     , m_strHref(href)
44     , m_mediaQueries(media)
45     , m_resource(0)
46     , m_loading(false)
47 {
48     if (!m_mediaQueries)
49         m_mediaQueries = MediaQuerySet::create(String());
50 }
51
52 StyleRuleImport::~StyleRuleImport()
53 {
54     if (m_styleSheet)
55         m_styleSheet->clearOwnerRule();
56     if (m_resource)
57         m_resource->removeClient(&m_styleSheetClient);
58 }
59
60 void StyleRuleImport::setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CSSStyleSheetResource* cachedStyleSheet)
61 {
62     if (m_styleSheet)
63         m_styleSheet->clearOwnerRule();
64
65     CSSParserContext context = m_parentStyleSheet ? m_parentStyleSheet->parserContext() : strictCSSParserContext();
66     context.setCharset(charset);
67     if (!baseURL.isNull())
68         context.setBaseURL(baseURL);
69
70     m_styleSheet = StyleSheetContents::create(this, href, context);
71
72     Document* document = m_parentStyleSheet ? m_parentStyleSheet->singleOwnerDocument() : 0;
73     m_styleSheet->parseAuthorStyleSheet(cachedStyleSheet, document ? document->securityOrigin() : 0);
74
75     m_loading = false;
76
77     if (m_parentStyleSheet) {
78         m_parentStyleSheet->notifyLoadedSheet(cachedStyleSheet);
79         m_parentStyleSheet->checkLoaded();
80     }
81 }
82
83 bool StyleRuleImport::isLoading() const
84 {
85     return m_loading || (m_styleSheet && m_styleSheet->isLoading());
86 }
87
88 void StyleRuleImport::requestStyleSheet()
89 {
90     if (!m_parentStyleSheet)
91         return;
92     Document* document = m_parentStyleSheet->singleOwnerDocument();
93     if (!document)
94         return;
95
96     ResourceFetcher* fetcher = document->fetcher();
97     if (!fetcher)
98         return;
99
100     KURL absURL;
101     if (!m_parentStyleSheet->baseURL().isNull())
102         // use parent styleheet's URL as the base URL
103         absURL = KURL(m_parentStyleSheet->baseURL(), m_strHref);
104     else
105         absURL = document->completeURL(m_strHref);
106
107     // Check for a cycle in our import chain.  If we encounter a stylesheet
108     // in our parent chain with the same URL, then just bail.
109     StyleSheetContents* rootSheet = m_parentStyleSheet;
110     for (StyleSheetContents* sheet = m_parentStyleSheet; sheet; sheet = sheet->parentStyleSheet()) {
111         if (equalIgnoringFragmentIdentifier(absURL, sheet->baseURL())
112             || equalIgnoringFragmentIdentifier(absURL, document->completeURL(sheet->originalURL())))
113             return;
114         rootSheet = sheet;
115     }
116
117     FetchRequest request(ResourceRequest(absURL), FetchInitiatorTypeNames::css, m_parentStyleSheet->charset());
118     m_resource = fetcher->fetchCSSStyleSheet(request);
119     if (m_resource) {
120         // if the import rule is issued dynamically, the sheet may be
121         // removed from the pending sheet count, so let the doc know
122         // the sheet being imported is pending.
123         if (m_parentStyleSheet && m_parentStyleSheet->loadCompleted() && rootSheet == m_parentStyleSheet)
124             m_parentStyleSheet->startLoadingDynamicSheet();
125         m_loading = true;
126         m_resource->addClient(&m_styleSheetClient);
127     }
128 }
129
130 } // namespace WebCore