Chromium Code Reviews| 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 Delegate* delegate) | |
| 82 : extension_(extension), | |
| 83 icon_set_(icon_set), | |
| 84 resource_size_in_dip_(resource_size_in_dip), | |
| 85 resource_match_type_(resource_match_type), | |
| 86 desired_size_in_dip_(desired_size_in_dip), | |
| 87 cache_param_(cache_param), | |
| 88 delegate_(delegate), | |
| 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()) { | |
| 140 if (scale_factor != ui::SCALE_FACTOR_100P && | |
| 141 !image_skia_.HasRepresentation(ui::SCALE_FACTOR_100P)) { | |
| 142 LoadImageForScaleFactor(ui::SCALE_FACTOR_100P); | |
| 143 } | |
|
oshima
2012/08/07 07:43:05
you may want to add log when the scale is 100P but
tbarzic
2012/08/07 18:10:26
Done.
| |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor)); | |
| 148 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); | |
| 149 DCHECK(!rep.is_null()); | |
| 150 image_skia_.AddRepresentation(rep); | |
| 151 | |
| 152 if (delegate_) | |
| 153 delegate_->OnExtensionIconImageChanged(this); | |
| 154 } | |
| 155 | |
| 156 void IconImage::Observe(int type, | |
| 157 const content::NotificationSource& source, | |
| 158 const content::NotificationDetails& details) { | |
| 159 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | |
| 160 | |
| 161 const Extension* extension = | |
| 162 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | |
| 163 | |
| 164 if (extension_ == extension) | |
| 165 extension_ = NULL; | |
| 166 } | |
| 167 | |
| 168 } // namespace extensions | |
| OLD | NEW |