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 "StyleRuleImport.h"
24
25#include "CSSStyleSheet.h"
26#include "CachedCSSStyleSheet.h"
27#include "CachedResourceLoader.h"
28#include "CachedResourceRequest.h"
29#include "CachedResourceRequestInitiators.h"
30#include "Document.h"
31#include "SecurityOrigin.h"
32#include "StyleSheetContents.h"
33#include <wtf/StdLibExtras.h>
34
35namespace WebCore {
36
37PassRefPtr<StyleRuleImport> StyleRuleImport::create(const String& href, PassRefPtr<MediaQuerySet> media)
38{
39    return adoptRef(new StyleRuleImport(href, media));
40}
41
42StyleRuleImport::StyleRuleImport(const String& href, PassRefPtr<MediaQuerySet> media)
43    : StyleRuleBase(Import, 0)
44    , m_parentStyleSheet(0)
45    , m_styleSheetClient(this)
46    , m_strHref(href)
47    , m_mediaQueries(media)
48    , m_cachedSheet(0)
49    , m_loading(false)
50{
51    if (!m_mediaQueries)
52        m_mediaQueries = MediaQuerySet::create(String());
53}
54
55StyleRuleImport::~StyleRuleImport()
56{
57    if (m_styleSheet)
58        m_styleSheet->clearOwnerRule();
59    if (m_cachedSheet)
60        m_cachedSheet->removeClient(&m_styleSheetClient);
61}
62
63void StyleRuleImport::setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CachedCSSStyleSheet* cachedStyleSheet)
64{
65    if (m_styleSheet)
66        m_styleSheet->clearOwnerRule();
67
68    CSSParserContext context = m_parentStyleSheet ? m_parentStyleSheet->parserContext() : CSSStrictMode;
69    context.charset = charset;
70    if (!baseURL.isNull())
71        context.baseURL = baseURL;
72
73    m_styleSheet = StyleSheetContents::create(this, href, context);
74
75    Document* document = m_parentStyleSheet ? m_parentStyleSheet->singleOwnerDocument() : 0;
76    m_styleSheet->parseAuthorStyleSheet(cachedStyleSheet, document ? document->securityOrigin() : 0);
77
78    m_loading = false;
79
80    if (m_parentStyleSheet) {
81        m_parentStyleSheet->notifyLoadedSheet(cachedStyleSheet);
82        m_parentStyleSheet->checkLoaded();
83    }
84}
85
86bool StyleRuleImport::isLoading() const
87{
88    return m_loading || (m_styleSheet && m_styleSheet->isLoading());
89}
90
91void StyleRuleImport::requestStyleSheet()
92{
93    if (!m_parentStyleSheet)
94        return;
95    Document* document = m_parentStyleSheet->singleOwnerDocument();
96    if (!document)
97        return;
98
99    CachedResourceLoader* cachedResourceLoader = document->cachedResourceLoader();
100    if (!cachedResourceLoader)
101        return;
102
103    KURL absURL;
104    if (!m_parentStyleSheet->baseURL().isNull())
105        // use parent styleheet's URL as the base URL
106        absURL = KURL(m_parentStyleSheet->baseURL(), m_strHref);
107    else
108        absURL = document->completeURL(m_strHref);
109
110    // Check for a cycle in our import chain.  If we encounter a stylesheet
111    // in our parent chain with the same URL, then just bail.
112    StyleSheetContents* rootSheet = m_parentStyleSheet;
113    for (StyleSheetContents* sheet = m_parentStyleSheet; sheet; sheet = sheet->parentStyleSheet()) {
114        if (equalIgnoringFragmentIdentifier(absURL, sheet->baseURL())
115            || equalIgnoringFragmentIdentifier(absURL, document->completeURL(sheet->originalURL())))
116            return;
117        rootSheet = sheet;
118    }
119
120    CachedResourceRequest request(ResourceRequest(absURL), m_parentStyleSheet->charset());
121    request.setInitiator(cachedResourceRequestInitiators().css);
122    if (m_parentStyleSheet->isUserStyleSheet())
123        m_cachedSheet = cachedResourceLoader->requestUserCSSStyleSheet(request);
124    else
125        m_cachedSheet = cachedResourceLoader->requestCSSStyleSheet(request);
126    if (m_cachedSheet) {
127        // if the import rule is issued dynamically, the sheet may be
128        // removed from the pending sheet count, so let the doc know
129        // the sheet being imported is pending.
130        if (m_parentStyleSheet && m_parentStyleSheet->loadCompleted() && rootSheet == m_parentStyleSheet)
131            m_parentStyleSheet->startLoadingDynamicSheet();
132        m_loading = true;
133        m_cachedSheet->addClient(&m_styleSheetClient);
134    }
135}
136
137} // namespace WebCore
138