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/browser/extensions/extension_icon_image_delegate.h" | |
10 #include "chrome/common/chrome_notification_types.h" | |
11 #include "chrome/common/extensions/extension.h" | |
12 #include "content/public/browser/notification_service.h" | |
13 #include "ui/gfx/image/image.h" | |
14 #include "ui/gfx/image/image_skia_source.h" | |
15 #include "ui/gfx/size.h" | |
16 | |
17 namespace { | |
18 | |
19 ExtensionResource GetExtensionIconResource( | |
20 const extensions::Extension* extension, | |
21 const ExtensionIconSet& icons, | |
22 int size, | |
23 ExtensionIconSet::MatchType match_type) { | |
24 std::string path = icons.Get(size, match_type); | |
25 if (path.empty()) | |
26 return ExtensionResource(); | |
27 | |
28 return extension->GetResource(path); | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 //////////////////////////////////////////////////////////////////////////////// | |
34 // ExtensionIconImage::Source | |
35 | |
36 class ExtensionIconImage::Source : public gfx::ImageSkiaSource { | |
37 public: | |
38 explicit Source(ExtensionIconImage* host); | |
39 virtual ~Source(); | |
40 | |
41 void ResetHost(); | |
42 | |
43 private: | |
44 // gfx::ImageSkiaSource overrides: | |
45 virtual gfx::ImageSkiaRep GetImageForScale( | |
46 ui::ScaleFactor scale_factor) OVERRIDE; | |
47 | |
48 ExtensionIconImage* host_; | |
49 | |
50 DISALLOW_COPY_AND_ASSIGN(Source); | |
51 }; | |
52 | |
53 ExtensionIconImage::Source::Source(ExtensionIconImage* host) | |
54 : host_(host) { | |
55 } | |
56 | |
57 ExtensionIconImage::Source::~Source() { | |
58 } | |
59 | |
60 void ExtensionIconImage::Source::ResetHost() { | |
61 host_ = NULL; | |
62 } | |
63 | |
64 gfx::ImageSkiaRep ExtensionIconImage::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 ExtensionIconImage::ExtensionIconImage( | |
75 const extensions::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 ExtensionIconImageDelegate* 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 // Check whether extension has at least 1x resource. | |
92 const int resource_size_in_pixel = resource_size_in_dip_; | |
93 const bool can_load = ImageLoadingTracker::CanLoadImage( | |
94 extension_, | |
95 GetExtensionIconResource(extension_, | |
96 icon_set_, | |
97 resource_size_in_pixel, | |
98 resource_match_type_)); | |
99 if (!can_load) | |
100 return; | |
101 | |
102 source_ = new Source(this); | |
103 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); | |
104 | |
105 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
106 content::NotificationService::AllSources()); | |
107 } | |
108 | |
109 ExtensionIconImage::~ExtensionIconImage() { | |
110 // |source_| could be NULL if resource does not exist. | |
111 if (source_) | |
112 source_->ResetHost(); | |
113 } | |
114 | |
115 void ExtensionIconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { | |
116 // Do nothing if extension is unloaded. | |
117 if (!extension_) | |
118 return; | |
119 | |
120 const float scale = ui::GetScaleFactorScale(scale_factor); | |
121 const int resource_size_in_pixel = | |
122 static_cast<int>(resource_size_in_dip_ * scale); | |
123 | |
124 ExtensionResource resource = GetExtensionIconResource( | |
125 extension_, icon_set_, resource_size_in_pixel, resource_match_type_); | |
126 // If resource does not exists, fallback to 1x if needed. | |
127 if (!ImageLoadingTracker::CanLoadImage(extension_, resource)) { | |
128 scale_factor = ui::SCALE_FACTOR_100P; | |
129 if (image_skia_.HasRepresentation(scale_factor)) | |
130 return; | |
131 | |
132 LoadImageForScaleFactor(scale_factor); | |
133 } | |
134 | |
135 int id = tracker_.next_id(); | |
136 load_map_[id] = scale_factor; | |
137 | |
138 std::vector<ImageLoadingTracker::ImageRepInfo> info_list; | |
139 info_list.push_back(ImageLoadingTracker::ImageRepInfo( | |
140 resource, | |
141 ImageLoadingTracker::ImageRepInfo::ALWAYS_RESIZE, | |
142 desired_size_in_dip_.Scale(scale), | |
143 scale_factor)); | |
144 tracker_.LoadImages(extension_, info_list, cache_param_); | |
145 } | |
146 | |
147 void ExtensionIconImage::OnImageLoaded(const gfx::Image& image, | |
148 const std::string& extension_id, | |
149 int index) { | |
150 if (image.IsEmpty()) | |
151 return; | |
152 | |
153 LoadMap::iterator load_map_it = load_map_.find(index); | |
154 DCHECK(load_map_it != load_map_.end()); | |
155 | |
156 ui::ScaleFactor scale_factor = load_map_it->second; | |
157 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); | |
tbarzic
2012/07/25 19:37:34
pkotwicz 2012/07/25 02:00:39
Nit: You can DCHECK o
tbarzic
2012/07/25 19:53:53
Done.
| |
158 DCHECK(!rep.is_null()); | |
159 image_skia_.AddRepresentation(rep); | |
160 | |
161 if (delegate_) | |
162 delegate_->OnExtensionIconImageChanged(this); | |
163 } | |
164 | |
165 void ExtensionIconImage::Observe(int type, | |
166 const content::NotificationSource& source, | |
167 const content::NotificationDetails& details) { | |
168 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | |
169 | |
170 const extensions::Extension* extension = | |
171 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | |
172 | |
173 if (extension_ == extension) | |
174 extension_ = NULL; | |
175 } | |
OLD | NEW |