OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/extension_icon_image.h" | 5 #include "extensions/browser/extension_icon_image.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 content::BrowserContext* context, | 127 content::BrowserContext* context, |
128 const Extension* extension, | 128 const Extension* extension, |
129 const ExtensionIconSet& icon_set, | 129 const ExtensionIconSet& icon_set, |
130 int resource_size_in_dip, | 130 int resource_size_in_dip, |
131 const gfx::ImageSkia& default_icon, | 131 const gfx::ImageSkia& default_icon, |
132 Observer* observer) | 132 Observer* observer) |
133 : browser_context_(context), | 133 : browser_context_(context), |
134 extension_(extension), | 134 extension_(extension), |
135 icon_set_(icon_set), | 135 icon_set_(icon_set), |
136 resource_size_in_dip_(resource_size_in_dip), | 136 resource_size_in_dip_(resource_size_in_dip), |
137 observer_(observer), | |
138 source_(NULL), | 137 source_(NULL), |
139 default_icon_(gfx::ImageSkiaOperations::CreateResizedImage( | 138 default_icon_(gfx::ImageSkiaOperations::CreateResizedImage( |
140 default_icon, | 139 default_icon, |
141 skia::ImageOperations::RESIZE_BEST, | 140 skia::ImageOperations::RESIZE_BEST, |
142 gfx::Size(resource_size_in_dip, resource_size_in_dip))), | 141 gfx::Size(resource_size_in_dip, resource_size_in_dip))), |
143 weak_ptr_factory_(this) { | 142 weak_ptr_factory_(this) { |
| 143 if (observer) |
| 144 AddObserver(observer); |
144 gfx::Size resource_size(resource_size_in_dip, resource_size_in_dip); | 145 gfx::Size resource_size(resource_size_in_dip, resource_size_in_dip); |
145 source_ = new Source(this, resource_size); | 146 source_ = new Source(this, resource_size); |
146 image_skia_ = gfx::ImageSkia(source_, resource_size); | 147 image_skia_ = gfx::ImageSkia(source_, resource_size); |
147 | 148 |
148 registrar_.Add(this, | 149 registrar_.Add(this, |
149 extensions::NOTIFICATION_EXTENSION_REMOVED, | 150 extensions::NOTIFICATION_EXTENSION_REMOVED, |
150 content::NotificationService::AllSources()); | 151 content::NotificationService::AllSources()); |
151 } | 152 } |
152 | 153 |
| 154 void IconImage::AddObserver(Observer* observer) { |
| 155 observers_.AddObserver(observer); |
| 156 } |
| 157 |
| 158 void IconImage::RemoveObserver(Observer* observer) { |
| 159 observers_.RemoveObserver(observer); |
| 160 } |
| 161 |
153 IconImage::~IconImage() { | 162 IconImage::~IconImage() { |
| 163 FOR_EACH_OBSERVER(Observer, observers_, OnExtensionIconImageDestroyed(this)); |
154 source_->ResetHost(); | 164 source_->ResetHost(); |
155 } | 165 } |
156 | 166 |
157 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor( | 167 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor( |
158 ui::ScaleFactor scale_factor) { | 168 ui::ScaleFactor scale_factor) { |
159 // Do nothing if extension is unloaded. | 169 // Do nothing if extension is unloaded. |
160 if (!extension_) | 170 if (!extension_) |
161 return gfx::ImageSkiaRep(); | 171 return gfx::ImageSkiaRep(); |
162 | 172 |
163 const float scale = ui::GetScaleForScaleFactor(scale_factor); | 173 const float scale = ui::GetScaleForScaleFactor(scale_factor); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 return; | 219 return; |
210 | 220 |
211 gfx::ImageSkiaRep rep = image->GetRepresentation(scale); | 221 gfx::ImageSkiaRep rep = image->GetRepresentation(scale); |
212 DCHECK(!rep.is_null()); | 222 DCHECK(!rep.is_null()); |
213 DCHECK_EQ(scale, rep.scale()); | 223 DCHECK_EQ(scale, rep.scale()); |
214 | 224 |
215 // Remove old representation if there is one. | 225 // Remove old representation if there is one. |
216 image_skia_.RemoveRepresentation(scale); | 226 image_skia_.RemoveRepresentation(scale); |
217 image_skia_.AddRepresentation(rep); | 227 image_skia_.AddRepresentation(rep); |
218 | 228 |
219 if (observer_) | 229 FOR_EACH_OBSERVER(Observer, observers_, OnExtensionIconImageChanged(this)); |
220 observer_->OnExtensionIconImageChanged(this); | |
221 } | 230 } |
222 | 231 |
223 void IconImage::Observe(int type, | 232 void IconImage::Observe(int type, |
224 const content::NotificationSource& source, | 233 const content::NotificationSource& source, |
225 const content::NotificationDetails& details) { | 234 const content::NotificationDetails& details) { |
226 DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_REMOVED); | 235 DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_REMOVED); |
227 | 236 |
228 const Extension* extension = content::Details<const Extension>(details).ptr(); | 237 const Extension* extension = content::Details<const Extension>(details).ptr(); |
229 | 238 |
230 if (extension_ == extension) | 239 if (extension_ == extension) |
231 extension_ = NULL; | 240 extension_ = NULL; |
232 } | 241 } |
233 | 242 |
234 } // namespace extensions | 243 } // namespace extensions |
OLD | NEW |