OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/extension_icon_image.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "chrome/common/chrome_notification_types.h" | |
10 #include "chrome/common/extensions/extension.h" | |
11 #include "content/public/browser/notification_service.h" | |
12 #include "ui/gfx/image/image.h" | |
13 #include "ui/gfx/image/image_skia_source.h" | |
14 #include "ui/gfx/size.h" | |
15 | |
16 namespace { | |
17 | |
18 ExtensionResource GetExtensionIconResource( | |
19 const extensions::Extension* extension, | |
20 const ExtensionIconSet& icons, | |
21 int size, | |
22 ExtensionIconSet::MatchType match_type) { | |
23 std::string path = icons.Get(size, match_type); | |
24 if (path.empty()) | |
25 return ExtensionResource(); | |
26 | |
27 return extension->GetResource(path); | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 namespace extensions { | |
33 | |
34 //////////////////////////////////////////////////////////////////////////////// | |
35 // ExtensionIconImage::Source | |
36 | |
37 class IconImage::Source : public gfx::ImageSkiaSource { | |
38 public: | |
39 explicit Source(IconImage* host); | |
40 virtual ~Source(); | |
41 | |
42 void ResetHost(); | |
43 | |
44 private: | |
45 // gfx::ImageSkiaSource overrides: | |
46 virtual gfx::ImageSkiaRep GetImageForScale( | |
47 ui::ScaleFactor scale_factor) OVERRIDE; | |
48 | |
49 IconImage* host_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(Source); | |
52 }; | |
53 | |
54 IconImage::Source::Source(IconImage* host) : host_(host) { | |
55 } | |
56 | |
57 IconImage::Source::~Source() { | |
58 } | |
59 | |
60 void IconImage::Source::ResetHost() { | |
61 host_ = NULL; | |
62 } | |
63 | |
64 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( | |
65 ui::ScaleFactor scale_factor) { | |
66 if (host_) | |
67 host_->LoadImageForScaleFactor(scale_factor); | |
68 return gfx::ImageSkiaRep(); | |
69 } | |
70 | |
71 //////////////////////////////////////////////////////////////////////////////// | |
72 // ExtensionIconImage | |
73 | |
74 IconImage::IconImage( | |
75 const Extension* extension, | |
76 const ExtensionIconSet& icon_set, | |
77 int resource_size_in_dip, | |
78 ExtensionIconSet::MatchType resource_match_type, | |
79 const gfx::Size& desired_size_in_dip, | |
80 ImageLoadingTracker::CacheParam cache_param, | |
81 Observer* observer) | |
82 : extension_(extension), | |
83 icon_set_(icon_set), | |
84 resource_size_in_dip_(resource_size_in_dip), | |
Aaron Boodman
2012/08/10 21:42:08
I don't understand why resource_size_in_dip is nec
tbarzic
2012/08/10 23:01:23
semantically, those have different meanings:
resou
| |
85 resource_match_type_(resource_match_type), | |
86 desired_size_in_dip_(desired_size_in_dip), | |
87 cache_param_(cache_param), | |
88 observer_(observer), | |
89 source_(NULL), | |
90 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { | |
91 source_ = new Source(this); | |
92 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); | |
93 | |
94 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
95 content::NotificationService::AllSources()); | |
96 } | |
97 | |
98 IconImage::~IconImage() { | |
99 // |source_| could be NULL if resource does not exist. | |
100 if (source_) | |
101 source_->ResetHost(); | |
102 } | |
103 | |
104 void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { | |
105 // Do nothing if extension is unloaded. | |
106 if (!extension_) | |
107 return; | |
108 | |
109 const float scale = ui::GetScaleFactorScale(scale_factor); | |
110 const int resource_size_in_pixel = | |
111 static_cast<int>(resource_size_in_dip_ * scale); | |
112 | |
113 ExtensionResource resource = GetExtensionIconResource( | |
114 extension_, icon_set_, resource_size_in_pixel, resource_match_type_); | |
115 | |
116 int id = tracker_.next_id(); | |
117 load_map_[id] = scale_factor; | |
118 | |
119 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; | |
120 info_list.push_back(ImageLoadingTracker::ImageRepresentation( | |
121 resource, | |
122 ImageLoadingTracker::ImageRepresentation::ALWAYS_RESIZE, | |
123 desired_size_in_dip_.Scale(scale), | |
124 scale_factor)); | |
125 tracker_.LoadImages(extension_, info_list, cache_param_); | |
126 } | |
127 | |
128 void IconImage::OnImageLoaded(const gfx::Image& image, | |
129 const std::string& extension_id, | |
130 int index) { | |
131 LoadMap::iterator load_map_it = load_map_.find(index); | |
132 DCHECK(load_map_it != load_map_.end()); | |
133 | |
134 ui::ScaleFactor scale_factor = load_map_it->second; | |
135 | |
136 load_map_.erase(load_map_it); | |
137 | |
138 // If we were unable to load desired image, fallback to 1x scale factor. | |
139 if (image.IsEmpty()) { | |
Aaron Boodman
2012/08/10 21:42:08
I think that resource_match_type_ can handle this
xiyuan
2012/08/10 22:03:44
If |resource_match_type_| could handle the fallbac
| |
140 if (scale_factor != ui::SCALE_FACTOR_100P && | |
141 !image_skia_.HasRepresentation(ui::SCALE_FACTOR_100P)) { | |
142 LoadImageForScaleFactor(ui::SCALE_FACTOR_100P); | |
143 } else if (scale_factor == ui::SCALE_FACTOR_100P) { | |
144 // TODO(tbarzic): We should probably use a default image at this point. | |
145 LOG(WARNING) << "Unable to load icon for the extension " << extension_id; | |
146 } | |
147 return; | |
148 } | |
149 | |
150 DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor)); | |
151 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); | |
152 DCHECK(!rep.is_null()); | |
153 image_skia_.AddRepresentation(rep); | |
154 | |
155 if (observer_) | |
156 observer_->OnExtensionIconImageChanged(this); | |
157 } | |
158 | |
159 void IconImage::Observe(int type, | |
160 const content::NotificationSource& source, | |
161 const content::NotificationDetails& details) { | |
162 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | |
163 | |
164 const Extension* extension = | |
165 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | |
166 | |
167 if (extension_ == extension) | |
168 extension_ = NULL; | |
169 } | |
170 | |
171 } // namespace extensions | |
OLD | NEW |