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 //////////////////////////////////////////////////////////////////////////////// | |
18 // ExtensionIconImage::Source | |
19 | |
20 class ExtensionIconImage::Source : public gfx::ImageSkiaSource { | |
21 public: | |
22 explicit Source(ExtensionIconImage* host); | |
23 virtual ~Source(); | |
24 | |
25 void ResetHost(); | |
26 | |
27 private: | |
28 // gfx::ImageSkiaSource overrides: | |
29 virtual gfx::ImageSkiaRep GetImageForScale( | |
30 ui::ScaleFactor scale_factor) OVERRIDE; | |
31 | |
32 ExtensionIconImage* host_; | |
33 | |
34 DISALLOW_COPY_AND_ASSIGN(Source); | |
35 }; | |
36 | |
37 ExtensionIconImage::Source::Source(ExtensionIconImage* host) | |
38 : host_(host) { | |
39 } | |
40 | |
41 ExtensionIconImage::Source::~Source() { | |
42 } | |
43 | |
44 void ExtensionIconImage::Source::ResetHost() { | |
45 host_ = NULL; | |
46 } | |
47 | |
48 gfx::ImageSkiaRep ExtensionIconImage::Source::GetImageForScale( | |
49 ui::ScaleFactor scale_factor) { | |
50 if (host_) | |
51 host_->LoadImageForScaleFactor(scale_factor); | |
52 return gfx::ImageSkiaRep(); | |
53 } | |
54 | |
55 //////////////////////////////////////////////////////////////////////////////// | |
56 // ExtensionIconImage | |
57 | |
58 ExtensionIconImage::ExtensionIconImage( | |
59 const extensions::Extension* extension, | |
60 int resource_size_in_dip, | |
61 ExtensionIconSet::MatchType resource_match_type, | |
62 const gfx::Size& desired_size_in_dip, | |
63 ImageLoadingTracker::CacheParam cache_param, | |
64 ExtensionIconImageDelegate* delegate) | |
65 : extension_(extension), | |
66 resource_size_in_dip_(resource_size_in_dip), | |
67 resource_match_type_(resource_match_type), | |
68 desired_size_in_dip_(desired_size_in_dip), | |
69 cache_param_(cache_param), | |
70 delegate_(delegate), | |
71 source_(NULL), | |
72 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { | |
73 Init(); | |
74 } | |
75 | |
76 ExtensionIconImage::~ExtensionIconImage() { | |
77 // |source_| could be NULL if resource does not exist. | |
78 if (source_) | |
79 source_->ResetHost(); | |
80 } | |
81 | |
82 void ExtensionIconImage::Init() { | |
83 // Check whether extension has at least 1x resource. | |
Aaron Boodman
2012/07/20 22:12:39
Nit: just put this code in the constructor?
xiyuan
2012/07/20 22:43:16
Done.
| |
84 const int resource_size_in_pixel = resource_size_in_dip_; | |
85 const bool can_load = ImageLoadingTracker::CanLoadImage( | |
86 extension_, | |
87 extension_->GetIconResource(resource_size_in_pixel, | |
88 resource_match_type_)); | |
89 if (!can_load) | |
90 return; | |
91 | |
92 source_ = new Source(this); | |
93 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); | |
94 | |
95 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
96 content::NotificationService::AllSources()); | |
97 } | |
98 | |
99 void ExtensionIconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { | |
100 // Do nothing if extension is unloaded. | |
101 if (!extension_) | |
102 return; | |
103 | |
104 const float scale = ui::GetScaleFactorScale(scale_factor); | |
105 const int resource_size_in_pixel = | |
106 static_cast<int>(resource_size_in_dip_ * scale); | |
107 | |
108 ExtensionResource resource = extension_->GetIconResource( | |
109 resource_size_in_pixel, resource_match_type_); | |
110 // If resource does not exists, fallback to 1x if needed. | |
111 if (!ImageLoadingTracker::CanLoadImage(extension_, resource)) { | |
112 scale_factor = ui::SCALE_FACTOR_100P; | |
113 if (image_skia_.HasRepresentation(scale_factor)) | |
114 return; | |
115 | |
116 LoadImageForScaleFactor(scale_factor); | |
117 } | |
118 | |
119 int id = tracker_.next_id(); | |
120 load_map_[id] = scale_factor; | |
121 | |
122 std::vector<ImageLoadingTracker::ImageRepInfo> info_list; | |
123 info_list.push_back(ImageLoadingTracker::ImageRepInfo( | |
124 resource, | |
125 ImageLoadingTracker::ImageRepInfo::ALWAYS_RESIZE, | |
126 desired_size_in_dip_.Scale(scale), | |
127 scale_factor)); | |
128 tracker_.LoadImages(extension_, info_list, cache_param_); | |
129 } | |
130 | |
131 void ExtensionIconImage::OnImageLoaded(const gfx::Image& image, | |
132 const std::string& extension_id, | |
133 int index) { | |
134 if (image.IsEmpty()) | |
135 return; | |
136 | |
137 LoadMap::iterator load_map_it = load_map_.find(index); | |
138 DCHECK(load_map_it != load_map_.end()); | |
139 | |
140 ui::ScaleFactor scale_factor = load_map_it->second; | |
141 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); | |
142 DCHECK(!rep.is_null()); | |
143 image_skia_.AddRepresentation(rep); | |
144 | |
145 if (delegate_) | |
146 delegate_->OnExtensionIconImageChanged(this); | |
147 } | |
148 | |
149 void ExtensionIconImage::Observe(int type, | |
150 const content::NotificationSource& source, | |
151 const content::NotificationDetails& details) { | |
152 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | |
153 | |
154 const extensions::Extension* extension = | |
155 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | |
156 | |
157 if (extension_ == extension) | |
158 extension_ = NULL; | |
159 } | |
OLD | NEW |