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"
30 using content::BrowserThread
;
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
,
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
,
54 scoped_refptr
<base::RefCountedBytes
> data
) {
55 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
56 ProcessImageOnUIThread(
57 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id
),
63 ////////////////////////////////////////////////////////////////////////////////
64 // ThemeSource, public:
66 ThemeSource::ThemeSource(Profile
* profile
)
67 : profile_(profile
->GetOriginalProfile()) {
68 NTPResourceCache::WindowType win_type
= NTPResourceCache::GetWindowType(
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
,
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
),
93 if (uncached_path
== kNewTabCSSPath
||
94 uncached_path
== kNewIncognitoTabCSSPath
) {
95 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
97 callback
.Run(css_bytes_
.get());
102 int resource_id
= ResourcesUtil::GetThemeResourceId(uncached_path
);
103 if (resource_id
!= -1) {
104 if (GetMimeType(path
) == "image/png")
105 SendThemeImage(callback
, resource_id
, scale_factor
);
107 SendThemeBitmap(callback
, resource_id
, scale_factor
);
111 // We don't have any data to send back.
115 std::string
ThemeSource::GetMimeType(const std::string
& path
) const {
116 std::string uncached_path
;
117 webui::ParsePathAndScale(GURL(GetThemePath() + path
), &uncached_path
, NULL
);
119 if (uncached_path
== kNewTabCSSPath
||
120 uncached_path
== kNewIncognitoTabCSSPath
) {
127 base::MessageLoop
* ThemeSource::MessageLoopForRequestPath(
128 const std::string
& path
) const {
129 std::string uncached_path
;
130 webui::ParsePathAndScale(GURL(GetThemePath() + path
), &uncached_path
, NULL
);
132 if (uncached_path
== kNewTabCSSPath
||
133 uncached_path
== kNewIncognitoTabCSSPath
) {
134 // We generated and cached this when we initialized the object. We don't
135 // have to go back to the UI thread to send the data.
139 // If it's not a themeable image, we don't need to go to the UI thread.
140 int resource_id
= ResourcesUtil::GetThemeResourceId(uncached_path
);
141 if (!BrowserThemePack::IsPersistentImageID(resource_id
))
144 return content::URLDataSource::MessageLoopForRequestPath(path
);
147 bool ThemeSource::ShouldReplaceExistingSource() const {
148 // We currently get the css_bytes_ in the ThemeSource constructor, so we need
149 // to recreate the source itself when a theme changes.
153 bool ThemeSource::ShouldServiceRequest(const net::URLRequest
* request
) const {
154 if (request
->url().SchemeIs(chrome::kChromeSearchScheme
))
155 return InstantIOContext::ShouldServiceRequest(request
);
156 return URLDataSource::ShouldServiceRequest(request
);
159 ////////////////////////////////////////////////////////////////////////////////
160 // ThemeSource, private:
162 void ThemeSource::SendThemeBitmap(
163 const content::URLDataSource::GotDataCallback
& callback
,
165 float scale_factor
) {
166 ui::ScaleFactor resource_scale_factor
=
167 ui::GetSupportedScaleFactor(scale_factor
);
168 if (BrowserThemePack::IsPersistentImageID(resource_id
)) {
169 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
170 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
173 scoped_refptr
<base::RefCountedMemory
> image_data(
174 tp
->GetRawData(resource_id
, resource_scale_factor
));
175 callback
.Run(image_data
.get());
177 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
178 const ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
180 rb
.LoadDataResourceBytesForScale(resource_id
, resource_scale_factor
));
184 void ThemeSource::SendThemeImage(
185 const content::URLDataSource::GotDataCallback
& callback
,
187 float scale_factor
) {
188 // If the resource bundle contains the data pack for |scale_factor|, we can
189 // safely fallback to SendThemeBitmap().
190 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
191 if (ui::GetScaleForScaleFactor(rb
.GetMaxScaleFactor()) >= scale_factor
) {
192 SendThemeBitmap(callback
, resource_id
, scale_factor
);
196 // Otherwise, we should use gfx::ImageSkia to obtain the data. ImageSkia can
197 // rescale the bitmap if its backend doesn't contain the representation for
198 // the specified scale factor. This is the fallback path in case chrome is
199 // shipped without 2x resource pack but needs to use HighDPI display, which
200 // can happen in ChromeOS or Linux.
201 scoped_refptr
<base::RefCountedBytes
> data(new base::RefCountedBytes());
202 if (BrowserThemePack::IsPersistentImageID(resource_id
)) {
203 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
204 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
207 ProcessImageOnUIThread(*tp
->GetImageSkiaNamed(resource_id
), scale_factor
,
209 callback
.Run(data
.get());
211 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
212 // Fetching image data in ResourceBundle should happen on the UI thread. See
214 content::BrowserThread::PostTaskAndReply(
215 content::BrowserThread::UI
, FROM_HERE
,
216 base::Bind(&ProcessResourceOnUIThread
, resource_id
, scale_factor
, data
),
217 base::Bind(callback
, data
));