OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/webui/theme_source.h" | 5 #include "chrome/browser/ui/webui/theme_source.h" |
6 | 6 |
7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 std::string GetThemePath() { | 34 std::string GetThemePath() { |
35 return std::string(content::kChromeUIScheme) + "://" + | 35 return std::string(content::kChromeUIScheme) + "://" + |
36 std::string(chrome::kChromeUIThemePath) + "/"; | 36 std::string(chrome::kChromeUIThemePath) + "/"; |
37 } | 37 } |
38 | 38 |
39 // use a resource map rather than hard-coded strings. | 39 // use a resource map rather than hard-coded strings. |
40 static const char* kNewTabCSSPath = "css/new_tab_theme.css"; | 40 static const char* kNewTabCSSPath = "css/new_tab_theme.css"; |
41 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; | 41 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; |
42 | 42 |
| 43 void ProcessImageOnUIThread(const gfx::ImageSkia& image, |
| 44 float scale_factor, |
| 45 scoped_refptr<base::RefCountedBytes> data) { |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 47 const gfx::ImageSkiaRep& rep = image.GetRepresentation(scale_factor); |
| 48 gfx::PNGCodec::EncodeBGRASkBitmap( |
| 49 rep.sk_bitmap(), false /* discard transparency */, &data->data()); |
| 50 } |
| 51 |
| 52 void ProcessResourceOnUIThread(int resource_id, |
| 53 float scale_factor, |
| 54 scoped_refptr<base::RefCountedBytes> data) { |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 56 ProcessImageOnUIThread( |
| 57 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id), |
| 58 scale_factor, data); |
| 59 } |
| 60 |
43 } // namespace | 61 } // namespace |
44 | 62 |
45 //////////////////////////////////////////////////////////////////////////////// | 63 //////////////////////////////////////////////////////////////////////////////// |
46 // ThemeSource, public: | 64 // ThemeSource, public: |
47 | 65 |
48 ThemeSource::ThemeSource(Profile* profile) | 66 ThemeSource::ThemeSource(Profile* profile) |
49 : profile_(profile->GetOriginalProfile()) { | 67 : profile_(profile->GetOriginalProfile()) { |
50 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType( | 68 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType( |
51 profile_, NULL); | 69 profile_, NULL); |
52 css_bytes_ = | 70 css_bytes_ = |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 190 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
173 if (ui::GetScaleForScaleFactor(rb.GetMaxScaleFactor()) >= scale_factor) { | 191 if (ui::GetScaleForScaleFactor(rb.GetMaxScaleFactor()) >= scale_factor) { |
174 SendThemeBitmap(callback, resource_id, scale_factor); | 192 SendThemeBitmap(callback, resource_id, scale_factor); |
175 return; | 193 return; |
176 } | 194 } |
177 | 195 |
178 // Otherwise, we should use gfx::ImageSkia to obtain the data. ImageSkia can | 196 // Otherwise, we should use gfx::ImageSkia to obtain the data. ImageSkia can |
179 // rescale the bitmap if its backend doesn't contain the representation for | 197 // rescale the bitmap if its backend doesn't contain the representation for |
180 // the specified scale factor. This is the fallback path in case chrome is | 198 // the specified scale factor. This is the fallback path in case chrome is |
181 // shipped without 2x resource pack but needs to use HighDPI display, which | 199 // shipped without 2x resource pack but needs to use HighDPI display, which |
182 // can happen in ChromeOS. | 200 // can happen in ChromeOS or Linux. |
183 // TODO(mukai): remove this method itself when we ship 2x resource to all | 201 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); |
184 // ChromeOS devices. | |
185 gfx::ImageSkia image; | |
186 if (BrowserThemePack::IsPersistentImageID(resource_id)) { | 202 if (BrowserThemePack::IsPersistentImageID(resource_id)) { |
187 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 203 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
188 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | 204 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
189 DCHECK(tp); | 205 DCHECK(tp); |
190 | 206 |
191 image = *tp->GetImageSkiaNamed(resource_id); | 207 ProcessImageOnUIThread(*tp->GetImageSkiaNamed(resource_id), scale_factor, |
| 208 data); |
| 209 callback.Run(data.get()); |
192 } else { | 210 } else { |
193 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 211 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
194 image = *rb.GetImageSkiaNamed(resource_id); | 212 // Fetching image data in ResourceBundle should happen on the UI thread. See |
| 213 // crbug.com/449277 |
| 214 content::BrowserThread::PostTaskAndReply( |
| 215 content::BrowserThread::UI, FROM_HERE, |
| 216 base::Bind(&ProcessResourceOnUIThread, resource_id, scale_factor, data), |
| 217 base::Bind(callback, data)); |
195 } | 218 } |
196 | |
197 const gfx::ImageSkiaRep& rep = image.GetRepresentation(scale_factor); | |
198 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); | |
199 gfx::PNGCodec::EncodeBGRASkBitmap( | |
200 rep.sk_bitmap(), false /* discard transparency */, &data->data()); | |
201 callback.Run(data.get()); | |
202 } | 219 } |
OLD | NEW |