Upstream version 5.34.104.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 KURL& url)
37     : CSSValue(ImageClass)
38     , m_url(url.string())
39     , m_accessedImage(false)
40 {
41 }
42
43 CSSImageValue::CSSImageValue(const KURL& url, StyleImage* image)
44     : CSSValue(ImageClass)
45     , m_url(url.string())
46     , m_image(image)
47     , m_accessedImage(true)
48 {
49 }
50
51 CSSImageValue::~CSSImageValue()
52 {
53 }
54
55 StyleImage* CSSImageValue::cachedOrPendingImage()
56 {
57     if (!m_image)
58         m_image = StylePendingImage::create(this);
59
60     return m_image.get();
61 }
62
63 StyleFetchedImage* CSSImageValue::cachedImage(ResourceFetcher* fetcher, const ResourceLoaderOptions& options)
64 {
65     ASSERT(fetcher);
66
67     if (!m_accessedImage) {
68         m_accessedImage = true;
69
70         FetchRequest request(ResourceRequest(m_url), m_initiatorName.isEmpty() ? FetchInitiatorTypeNames::css : m_initiatorName, options);
71
72         if (options.corsEnabled == IsCORSEnabled)
73             request.setCrossOriginAccessControl(fetcher->document()->securityOrigin(), options.allowCredentials);
74
75         if (ResourcePtr<ImageResource> cachedImage = fetcher->fetchImage(request))
76             m_image = StyleFetchedImage::create(cachedImage.get());
77     }
78
79     return (m_image && m_image->isImageResource()) ? toStyleFetchedImage(m_image) : 0;
80 }
81
82 bool CSSImageValue::hasFailedOrCanceledSubresources() const
83 {
84     if (!m_image || !m_image->isImageResource())
85         return false;
86     if (Resource* cachedResource = toStyleFetchedImage(m_image)->cachedImage())
87         return cachedResource->loadFailedOrCanceled();
88     return true;
89 }
90
91 bool CSSImageValue::equals(const CSSImageValue& other) const
92 {
93     return m_url == other.m_url;
94 }
95
96 String CSSImageValue::customCSSText() const
97 {
98     return "url(" + quoteCSSURLIfNeeded(m_url) + ")";
99 }
100
101 PassRefPtr<CSSValue> CSSImageValue::cloneForCSSOM() const
102 {
103     // NOTE: We expose CSSImageValues as URI primitive values in CSSOM to maintain old behavior.
104     RefPtrWillBeRawPtr<CSSPrimitiveValue> uriValue = CSSPrimitiveValue::create(m_url, CSSPrimitiveValue::CSS_URI);
105     uriValue->setCSSOMSafe();
106     return uriValue.release();
107 }
108
109 bool CSSImageValue::knownToBeOpaque(const RenderObject* renderer) const
110 {
111     return m_image ? m_image->knownToBeOpaque(renderer) : false;
112 }
113
114 void CSSImageValue::traceAfterDispatch(Visitor* visitor)
115 {
116     CSSValue::traceAfterDispatch(visitor);
117 }
118
119 } // namespace WebCore