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 const int kMatchBiggerTreshold = 32; |
| 19 |
| 20 ExtensionResource GetExtensionIconResource( |
| 21 const extensions::Extension* extension, |
| 22 const ExtensionIconSet& icons, |
| 23 int size, |
| 24 ExtensionIconSet::MatchType match_type) { |
| 25 std::string path = icons.Get(size, match_type); |
| 26 if (path.empty()) |
| 27 return ExtensionResource(); |
| 28 |
| 29 return extension->GetResource(path); |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 34 namespace extensions { |
| 35 |
| 36 //////////////////////////////////////////////////////////////////////////////// |
| 37 // ExtensionIconImage::Source |
| 38 |
| 39 class IconImage::Source : public gfx::ImageSkiaSource { |
| 40 public: |
| 41 explicit Source(IconImage* host); |
| 42 virtual ~Source(); |
| 43 |
| 44 void ResetHost(); |
| 45 |
| 46 private: |
| 47 // gfx::ImageSkiaSource overrides: |
| 48 virtual gfx::ImageSkiaRep GetImageForScale( |
| 49 ui::ScaleFactor scale_factor) OVERRIDE; |
| 50 |
| 51 IconImage* host_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(Source); |
| 54 }; |
| 55 |
| 56 IconImage::Source::Source(IconImage* host) : host_(host) { |
| 57 } |
| 58 |
| 59 IconImage::Source::~Source() { |
| 60 } |
| 61 |
| 62 void IconImage::Source::ResetHost() { |
| 63 host_ = NULL; |
| 64 } |
| 65 |
| 66 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( |
| 67 ui::ScaleFactor scale_factor) { |
| 68 if (host_) |
| 69 host_->LoadImageForScaleFactor(scale_factor); |
| 70 return gfx::ImageSkiaRep(); |
| 71 } |
| 72 |
| 73 //////////////////////////////////////////////////////////////////////////////// |
| 74 // ExtensionIconImage |
| 75 |
| 76 IconImage::IconImage( |
| 77 const Extension* extension, |
| 78 const ExtensionIconSet& icon_set, |
| 79 int resource_size_in_dip, |
| 80 Observer* observer) |
| 81 : extension_(extension), |
| 82 icon_set_(icon_set), |
| 83 resource_size_in_dip_(resource_size_in_dip), |
| 84 desired_size_in_dip_(resource_size_in_dip, resource_size_in_dip), |
| 85 observer_(observer), |
| 86 source_(NULL), |
| 87 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { |
| 88 source_ = new Source(this); |
| 89 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); |
| 90 |
| 91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 92 content::NotificationService::AllSources()); |
| 93 } |
| 94 |
| 95 IconImage::~IconImage() { |
| 96 // |source_| could be NULL if resource does not exist. |
| 97 if (source_) |
| 98 source_->ResetHost(); |
| 99 } |
| 100 |
| 101 void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { |
| 102 // Do nothing if extension is unloaded. |
| 103 if (!extension_) |
| 104 return; |
| 105 |
| 106 const float scale = ui::GetScaleFactorScale(scale_factor); |
| 107 const int resource_size_in_pixel = |
| 108 static_cast<int>(resource_size_in_dip_ * scale); |
| 109 |
| 110 ExtensionResource resource; |
| 111 // We try loading bigger image only if resource size is >= 32. |
| 112 if (resource_size_in_pixel >= kMatchBiggerTreshold) { |
| 113 resource = GetExtensionIconResource(extension_, icon_set_, |
| 114 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); |
| 115 } |
| 116 |
| 117 // If resource is not found by now, try matching smaller one. |
| 118 if (resource.empty()) { |
| 119 resource = GetExtensionIconResource(extension_, icon_set_, |
| 120 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); |
| 121 } |
| 122 |
| 123 // If there is no resource found, bail out and notify observer of failure. |
| 124 if (resource.empty()) { |
| 125 if (observer_) |
| 126 observer_->OnIconImageLoadFailed(this, scale_factor); |
| 127 return; |
| 128 } |
| 129 |
| 130 int id = tracker_.next_id(); |
| 131 load_map_[id] = scale_factor; |
| 132 |
| 133 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; |
| 134 info_list.push_back(ImageLoadingTracker::ImageRepresentation( |
| 135 resource, |
| 136 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, |
| 137 desired_size_in_dip_.Scale(scale), |
| 138 scale_factor)); |
| 139 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE); |
| 140 } |
| 141 |
| 142 void IconImage::OnImageLoaded(const gfx::Image& image, |
| 143 const std::string& extension_id, |
| 144 int index) { |
| 145 LoadMap::iterator load_map_it = load_map_.find(index); |
| 146 DCHECK(load_map_it != load_map_.end()); |
| 147 |
| 148 ui::ScaleFactor scale_factor = load_map_it->second; |
| 149 |
| 150 load_map_.erase(load_map_it); |
| 151 |
| 152 if (image.IsEmpty()) { |
| 153 // There waas an error loading the image. |
| 154 if (observer_) |
| 155 observer_->OnIconImageLoadFailed(this, scale_factor); |
| 156 return; |
| 157 } |
| 158 |
| 159 DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor)); |
| 160 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); |
| 161 DCHECK(!rep.is_null()); |
| 162 image_skia_.AddRepresentation(rep); |
| 163 |
| 164 if (observer_) |
| 165 observer_->OnExtensionIconImageChanged(this); |
| 166 } |
| 167 |
| 168 void IconImage::Observe(int type, |
| 169 const content::NotificationSource& source, |
| 170 const content::NotificationDetails& details) { |
| 171 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 172 |
| 173 const Extension* extension = |
| 174 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 175 |
| 176 if (extension_ == extension) |
| 177 extension_ = NULL; |
| 178 } |
| 179 |
| 180 } // namespace extensions |
OLD | NEW |