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