Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSImageValue.cpp
1 /*
2  * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "core/css/CSSImageValue.h"
23
24 #include "FetchInitiatorTypeNames.h"
25 #include "core/css/CSSMarkup.h"
26 #include "core/dom/Document.h"
27 #include "core/fetch/CrossOriginAccessControl.h"
28 #include "core/fetch/FetchRequest.h"
29 #include "core/fetch/ImageResource.h"
30 #include "core/rendering/style/StyleFetchedImage.h"
31 #include "core/rendering/style/StylePendingImage.h"
32 #include "platform/weborigin/KURL.h"
33
34 namespace WebCore {
35
36 CSSImageValue::CSSImageValue(const String& rawValue, const KURL& url, StyleImage* image)
37     : CSSValue(ImageClass)
38     , m_relativeURL(rawValue)
39     , m_absoluteURL(url.string())
40     , m_image(image)
41     , m_accessedImage(image)
42 {
43 }
44
45 CSSImageValue::~CSSImageValue()
46 {
47 }
48
49 StyleImage* CSSImageValue::cachedOrPendingImage()
50 {
51     if (!m_image)
52         m_image = StylePendingImage::create(this);
53
54     return m_image.get();
55 }
56
57 StyleFetchedImage* CSSImageValue::cachedImage(ResourceFetcher* fetcher, const ResourceLoaderOptions& options)
58 {
59     ASSERT(fetcher);
60
61     if (!m_accessedImage) {
62         m_accessedImage = true;
63
64         FetchRequest request(ResourceRequest(m_absoluteURL), m_initiatorName.isEmpty() ? FetchInitiatorTypeNames::css : m_initiatorName, options);
65         if (!m_referrer.isEmpty())
66             request.mutableResourceRequest().setHTTPReferrer(Referrer(m_referrer, ReferrerPolicyDefault));
67
68         if (options.corsEnabled == IsCORSEnabled)
69             request.setCrossOriginAccessControl(fetcher->document()->securityOrigin(), options.allowCredentials, options.credentialsRequested);
70
71         if (ResourcePtr<ImageResource> cachedImage = fetcher->fetchImage(request))
72             m_image = StyleFetchedImage::create(cachedImage.get());
73     }
74
75     return (m_image && m_image->isImageResource()) ? toStyleFetchedImage(m_image) : 0;
76 }
77
78 void CSSImageValue::restoreCachedResourceIfNeeded(Document& document)
79 {
80     if (!m_accessedImage || !m_image->isImageResource() || !document.fetcher())
81         return;
82     if (document.fetcher()->cachedResource(KURL(ParsedURLString, m_absoluteURL)))
83         return;
84
85     ImageResource* resource = m_image->cachedImage();
86     if (!resource)
87         return;
88
89     FetchRequest request(ResourceRequest(m_absoluteURL), m_initiatorName.isEmpty() ? FetchInitiatorTypeNames::css : m_initiatorName, resource->options());
90     document.fetcher()->requestLoadStarted(resource, request, ResourceFetcher::ResourceLoadingFromCache);
91 }
92
93 bool CSSImageValue::hasFailedOrCanceledSubresources() const
94 {
95     if (!m_image || !m_image->isImageResource())
96         return false;
97     if (Resource* cachedResource = toStyleFetchedImage(m_image)->cachedImage())
98         return cachedResource->loadFailedOrCanceled();
99     return true;
100 }
101
102 bool CSSImageValue::equals(const CSSImageValue& other) const
103 {
104     return m_absoluteURL == other.m_absoluteURL;
105 }
106
107 String CSSImageValue::customCSSText() const
108 {
109     return "url(" + quoteCSSURLIfNeeded(m_absoluteURL) + ")";
110 }
111
112 PassRefPtrWillBeRawPtr<CSSValue> CSSImageValue::cloneForCSSOM() const
113 {
114     // NOTE: We expose CSSImageValues as URI primitive values in CSSOM to maintain old behavior.
115     RefPtrWillBeRawPtr<CSSPrimitiveValue> uriValue = CSSPrimitiveValue::create(m_absoluteURL, CSSPrimitiveValue::CSS_URI);
116     uriValue->setCSSOMSafe();
117     return uriValue.release();
118 }
119
120 bool CSSImageValue::knownToBeOpaque(const RenderObject* renderer) const
121 {
122     return m_image ? m_image->knownToBeOpaque(renderer) : false;
123 }
124
125 void CSSImageValue::traceAfterDispatch(Visitor* visitor)
126 {
127     CSSValue::traceAfterDispatch(visitor);
128 }
129
130 void CSSImageValue::reResolveURL(const Document& document)
131 {
132     KURL url = document.completeURL(m_relativeURL);
133     if (url == m_absoluteURL)
134         return;
135     m_absoluteURL = url.string();
136     m_accessedImage = false;
137     m_image.clear();
138 }
139
140 } // namespace WebCore