Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / ui / webui / theme_source.cc
blob0a7a57d45939854b186c8a353fa1cbae4403bdf0
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.
5 #include "chrome/browser/ui/webui/theme_source.h"
7 #include "base/memory/ref_counted_memory.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/resources_util.h"
12 #include "chrome/browser/search/instant_io_context.h"
13 #include "chrome/browser/themes/browser_theme_pack.h"
14 #include "chrome/browser/themes/theme_properties.h"
15 #include "chrome/browser/themes/theme_service.h"
16 #include "chrome/browser/themes/theme_service_factory.h"
17 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
18 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
19 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "net/url_request/url_request.h"
22 #include "ui/base/layout.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/base/webui/web_ui_util.h"
25 #include "ui/gfx/codec/png_codec.h"
26 #include "ui/gfx/image/image_skia.h"
27 #include "ui/gfx/image/image_skia_rep.h"
28 #include "url/gurl.h"
30 using content::BrowserThread;
32 namespace {
34 std::string GetThemePath() {
35 return std::string(content::kChromeUIScheme) + "://" +
36 std::string(chrome::kChromeUIThemePath) + "/";
39 // use a resource map rather than hard-coded strings.
40 static const char* kNewTabCSSPath = "css/new_tab_theme.css";
41 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css";
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());
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);
61 } // namespace
63 ////////////////////////////////////////////////////////////////////////////////
64 // ThemeSource, public:
66 ThemeSource::ThemeSource(Profile* profile)
67 : profile_(profile->GetOriginalProfile()) {
68 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType(
69 profile_, NULL);
70 css_bytes_ =
71 NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(win_type);
74 ThemeSource::~ThemeSource() {
77 std::string ThemeSource::GetSource() const {
78 return chrome::kChromeUIThemePath;
81 void ThemeSource::StartDataRequest(
82 const std::string& path,
83 int render_process_id,
84 int render_frame_id,
85 const content::URLDataSource::GotDataCallback& callback) {
86 // Default scale factor if not specified.
87 float scale_factor = 1.0f;
88 std::string uncached_path;
89 webui::ParsePathAndScale(GURL(GetThemePath() + path),
90 &uncached_path,
91 &scale_factor);
92 scale_factor =
93 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactor(scale_factor));
95 if (uncached_path == kNewTabCSSPath ||
96 uncached_path == kNewIncognitoTabCSSPath) {
97 DCHECK_CURRENTLY_ON(BrowserThread::IO);
99 callback.Run(css_bytes_.get());
100 return;
104 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
105 if (resource_id != -1) {
106 if (GetMimeType(path) == "image/png")
107 SendThemeImage(callback, resource_id, scale_factor);
108 else
109 SendThemeBitmap(callback, resource_id, scale_factor);
110 return;
113 // We don't have any data to send back.
114 callback.Run(NULL);
117 std::string ThemeSource::GetMimeType(const std::string& path) const {
118 std::string uncached_path;
119 webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL);
121 if (uncached_path == kNewTabCSSPath ||
122 uncached_path == kNewIncognitoTabCSSPath) {
123 return "text/css";
126 return "image/png";
129 base::MessageLoop* ThemeSource::MessageLoopForRequestPath(
130 const std::string& path) const {
131 std::string uncached_path;
132 webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL);
134 if (uncached_path == kNewTabCSSPath ||
135 uncached_path == kNewIncognitoTabCSSPath) {
136 // We generated and cached this when we initialized the object. We don't
137 // have to go back to the UI thread to send the data.
138 return NULL;
141 // If it's not a themeable image, we don't need to go to the UI thread.
142 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
143 if (!BrowserThemePack::IsPersistentImageID(resource_id))
144 return NULL;
146 return content::URLDataSource::MessageLoopForRequestPath(path);
149 bool ThemeSource::ShouldReplaceExistingSource() const {
150 // We currently get the css_bytes_ in the ThemeSource constructor, so we need
151 // to recreate the source itself when a theme changes.
152 return true;
155 bool ThemeSource::ShouldServiceRequest(const net::URLRequest* request) const {
156 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
157 return InstantIOContext::ShouldServiceRequest(request);
158 return URLDataSource::ShouldServiceRequest(request);
161 ////////////////////////////////////////////////////////////////////////////////
162 // ThemeSource, private:
164 void ThemeSource::SendThemeBitmap(
165 const content::URLDataSource::GotDataCallback& callback,
166 int resource_id,
167 float scale_factor) {
168 ui::ScaleFactor resource_scale_factor =
169 ui::GetSupportedScaleFactor(scale_factor);
170 if (BrowserThemePack::IsPersistentImageID(resource_id)) {
171 DCHECK_CURRENTLY_ON(BrowserThread::UI);
172 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
173 DCHECK(tp);
175 scoped_refptr<base::RefCountedMemory> image_data(
176 tp->GetRawData(resource_id, resource_scale_factor));
177 callback.Run(image_data.get());
178 } else {
179 DCHECK_CURRENTLY_ON(BrowserThread::IO);
180 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
181 callback.Run(
182 rb.LoadDataResourceBytesForScale(resource_id, resource_scale_factor));
186 void ThemeSource::SendThemeImage(
187 const content::URLDataSource::GotDataCallback& callback,
188 int resource_id,
189 float scale_factor) {
190 // If the resource bundle contains the data pack for |scale_factor|, we can
191 // safely fallback to SendThemeBitmap().
192 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
193 if (ui::GetScaleForScaleFactor(rb.GetMaxScaleFactor()) >= scale_factor) {
194 SendThemeBitmap(callback, resource_id, scale_factor);
195 return;
198 // Otherwise, we should use gfx::ImageSkia to obtain the data. ImageSkia can
199 // rescale the bitmap if its backend doesn't contain the representation for
200 // the specified scale factor. This is the fallback path in case chrome is
201 // shipped without 2x resource pack but needs to use HighDPI display, which
202 // can happen in ChromeOS or Linux.
203 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
204 if (BrowserThemePack::IsPersistentImageID(resource_id)) {
205 DCHECK_CURRENTLY_ON(BrowserThread::UI);
206 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
207 DCHECK(tp);
209 ProcessImageOnUIThread(*tp->GetImageSkiaNamed(resource_id), scale_factor,
210 data);
211 callback.Run(data.get());
212 } else {
213 DCHECK_CURRENTLY_ON(BrowserThread::IO);
214 // Fetching image data in ResourceBundle should happen on the UI thread. See
215 // crbug.com/449277
216 content::BrowserThread::PostTaskAndReply(
217 content::BrowserThread::UI, FROM_HERE,
218 base::Bind(&ProcessResourceOnUIThread, resource_id, scale_factor, data),
219 base::Bind(callback, data));