1 /*
2  * Copyright (C) 2012 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_STYLE_FETCHED_IMAGE_SET_H_
27 #define THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_STYLE_FETCHED_IMAGE_SET_H_
28 
29 #include "third_party/blink/renderer/core/loader/resource/image_resource_observer.h"
30 #include "third_party/blink/renderer/core/style/style_image.h"
31 #include "third_party/blink/renderer/platform/geometry/layout_size.h"
32 #include "third_party/blink/renderer/platform/weborigin/kurl.h"
33 #include "third_party/blink/renderer/platform/wtf/casting.h"
34 
35 namespace blink {
36 
37 class CSSImageSetValue;
38 class ImageResourceObserver;
39 
40 // This class represents an <image> that loads one image resource out of a set
41 // of alternatives (the -webkit-image-set(...) function.)
42 //
43 // This class keeps one cached image from the set, and has access to a set of
44 // alternatives via the referenced CSSImageSetValue.
45 class StyleFetchedImageSet final : public StyleImage,
46                                    public ImageResourceObserver {
47   USING_PRE_FINALIZER(StyleFetchedImageSet, Dispose);
48 
49  public:
50   StyleFetchedImageSet(ImageResourceContent*,
51                        float image_scale_factor,
52                        CSSImageSetValue*,
53                        const KURL&);
54   ~StyleFetchedImageSet() override;
55 
56   CSSValue* CssValue() const override;
57   CSSValue* ComputedCSSValue(const ComputedStyle&,
58                              bool allow_visited_style) const override;
59 
60   // FIXME: This is used by StyleImage for equals comparison, but this
61   // implementation only looks at the image from the set that we have loaded.
62   // I'm not sure if that is meaningful enough or not.
63   WrappedImagePtr Data() const override;
64 
65   bool CanRender() const override;
66   bool IsLoaded() const override;
67   bool ErrorOccurred() const override;
68   FloatSize ImageSize(const Document&,
69                       float multiplier,
70                       const FloatSize& default_object_size,
71                       RespectImageOrientationEnum) const override;
72   bool HasIntrinsicSize() const override;
73   void AddClient(ImageResourceObserver*) override;
74   void RemoveClient(ImageResourceObserver*) override;
75   scoped_refptr<Image> GetImage(const ImageResourceObserver&,
76                                 const Document&,
77                                 const ComputedStyle&,
78                                 const FloatSize& target_size) const override;
ImageScaleFactor()79   float ImageScaleFactor() const override { return image_scale_factor_; }
80   bool KnownToBeOpaque(const Document&, const ComputedStyle&) const override;
81   ImageResourceContent* CachedImage() const override;
82 
83   void Trace(Visitor*) const override;
84 
85  private:
86   bool IsEqual(const StyleImage& other) const override;
87   void Dispose();
88 
89   // ImageResourceObserver overrides
DebugName()90   String DebugName() const override { return "StyleFetchedImageSet"; }
91 
92   Member<ImageResourceContent> best_fit_image_;
93   float image_scale_factor_;
94 
95   Member<CSSImageSetValue> image_set_value_;  // Not retained; it owns us.
96   const KURL url_;
97 };
98 
99 template <>
100 struct DowncastTraits<StyleFetchedImageSet> {
101   static bool AllowFrom(const StyleImage& styleImage) {
102     return styleImage.IsImageResourceSet();
103   }
104 };
105 
106 }  // namespace blink
107 
108 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_STYLE_FETCHED_IMAGE_SET_H_
109