Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / theme_source.cc
bloba1efb62425cde2f72c44fc9e4ebb42614bc10600
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/channel_info.h"
20 #include "chrome/common/url_constants.h"
21 #include "components/version_info/version_info.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "grit/theme_resources.h"
24 #include "net/url_request/url_request.h"
25 #include "ui/base/layout.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/base/webui/web_ui_util.h"
28 #include "ui/gfx/codec/png_codec.h"
29 #include "ui/gfx/image/image_skia.h"
30 #include "ui/gfx/image/image_skia_rep.h"
31 #include "url/gurl.h"
33 using content::BrowserThread;
35 namespace {
37 std::string GetThemePath() {
38 return std::string(content::kChromeUIScheme) + "://" +
39 std::string(chrome::kChromeUIThemePath) + "/";
42 // use a resource map rather than hard-coded strings.
43 static const char* kNewTabCSSPath = "css/new_tab_theme.css";
44 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css";
46 void ProcessImageOnUIThread(const gfx::ImageSkia& image,
47 float scale_factor,
48 scoped_refptr<base::RefCountedBytes> data) {
49 DCHECK_CURRENTLY_ON(BrowserThread::UI);
50 const gfx::ImageSkiaRep& rep = image.GetRepresentation(scale_factor);
51 gfx::PNGCodec::EncodeBGRASkBitmap(
52 rep.sk_bitmap(), false /* discard transparency */, &data->data());
55 void ProcessResourceOnUIThread(int resource_id,
56 float scale_factor,
57 scoped_refptr<base::RefCountedBytes> data) {
58 DCHECK_CURRENTLY_ON(BrowserThread::UI);
59 ProcessImageOnUIThread(
60 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id),
61 scale_factor, data);
64 } // namespace
66 ////////////////////////////////////////////////////////////////////////////////
67 // ThemeSource, public:
69 ThemeSource::ThemeSource(Profile* profile)
70 : profile_(profile->GetOriginalProfile()) {
71 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType(
72 profile_, NULL);
73 css_bytes_ =
74 NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(win_type);
77 ThemeSource::~ThemeSource() {
80 std::string ThemeSource::GetSource() const {
81 return chrome::kChromeUIThemePath;
84 void ThemeSource::StartDataRequest(
85 const std::string& path,
86 int render_process_id,
87 int render_frame_id,
88 const content::URLDataSource::GotDataCallback& callback) {
89 // Default scale factor if not specified.
90 float scale_factor = 1.0f;
91 std::string uncached_path;
92 webui::ParsePathAndScale(GURL(GetThemePath() + path),
93 &uncached_path,
94 &scale_factor);
95 scale_factor =
96 ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactor(scale_factor));
98 if (uncached_path == kNewTabCSSPath ||
99 uncached_path == kNewIncognitoTabCSSPath) {
100 DCHECK_CURRENTLY_ON(BrowserThread::IO);
102 callback.Run(css_bytes_.get());
103 return;
106 int resource_id = -1;
107 if (uncached_path == "current-channel-logo") {
108 switch (chrome::GetChannel()) {
109 #if defined(GOOGLE_CHROME_BUILD)
110 case version_info::Channel::CANARY:
111 resource_id = IDR_PRODUCT_LOGO_32_CANARY;
112 break;
113 case version_info::Channel::DEV:
114 resource_id = IDR_PRODUCT_LOGO_32_DEV;
115 break;
116 case version_info::Channel::BETA:
117 resource_id = IDR_PRODUCT_LOGO_32_BETA;
118 break;
119 case version_info::Channel::STABLE:
120 resource_id = IDR_PRODUCT_LOGO_32;
121 break;
122 #else
123 case version_info::Channel::CANARY:
124 case version_info::Channel::DEV:
125 case version_info::Channel::BETA:
126 case version_info::Channel::STABLE:
127 NOTREACHED();
128 #endif
129 case version_info::Channel::UNKNOWN:
130 resource_id = IDR_PRODUCT_LOGO_32;
131 break;
133 } else {
134 resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
136 if (resource_id != -1) {
137 if (GetMimeType(path) == "image/png")
138 SendThemeImage(callback, resource_id, scale_factor);
139 else
140 SendThemeBitmap(callback, resource_id, scale_factor);
141 return;
144 // We don't have any data to send back. This shouldn't happen normally, as
145 // chrome://theme/ data source is used only by chrome WebUI pages and
146 // component extensions. We don't want to crash in Release build though, as
147 // it is possible that a user has entered an unexisting chrome://theme URL
148 // into the address bar.
149 NOTREACHED() << path << " not found.";
150 callback.Run(NULL);
153 std::string ThemeSource::GetMimeType(const std::string& path) const {
154 std::string uncached_path;
155 webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL);
157 if (uncached_path == kNewTabCSSPath ||
158 uncached_path == kNewIncognitoTabCSSPath) {
159 return "text/css";
162 return "image/png";
165 base::MessageLoop* ThemeSource::MessageLoopForRequestPath(
166 const std::string& path) const {
167 std::string uncached_path;
168 webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL);
170 if (uncached_path == kNewTabCSSPath ||
171 uncached_path == kNewIncognitoTabCSSPath) {
172 // We generated and cached this when we initialized the object. We don't
173 // have to go back to the UI thread to send the data.
174 return NULL;
177 // If it's not a themeable image, we don't need to go to the UI thread.
178 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
179 if (!BrowserThemePack::IsPersistentImageID(resource_id))
180 return NULL;
182 return content::URLDataSource::MessageLoopForRequestPath(path);
185 bool ThemeSource::ShouldReplaceExistingSource() const {
186 // We currently get the css_bytes_ in the ThemeSource constructor, so we need
187 // to recreate the source itself when a theme changes.
188 return true;
191 bool ThemeSource::ShouldServiceRequest(const net::URLRequest* request) const {
192 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
193 return InstantIOContext::ShouldServiceRequest(request);
194 return URLDataSource::ShouldServiceRequest(request);
197 ////////////////////////////////////////////////////////////////////////////////
198 // ThemeSource, private:
200 void ThemeSource::SendThemeBitmap(
201 const content::URLDataSource::GotDataCallback& callback,
202 int resource_id,
203 float scale_factor) {
204 ui::ScaleFactor resource_scale_factor =
205 ui::GetSupportedScaleFactor(scale_factor);
206 if (BrowserThemePack::IsPersistentImageID(resource_id)) {
207 DCHECK_CURRENTLY_ON(BrowserThread::UI);
208 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
209 DCHECK(tp);
211 scoped_refptr<base::RefCountedMemory> image_data(
212 tp->GetRawData(resource_id, resource_scale_factor));
213 callback.Run(image_data.get());
214 } else {
215 DCHECK_CURRENTLY_ON(BrowserThread::IO);
216 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
217 callback.Run(
218 rb.LoadDataResourceBytesForScale(resource_id, resource_scale_factor));
222 void ThemeSource::SendThemeImage(
223 const content::URLDataSource::GotDataCallback& callback,
224 int resource_id,
225 float scale_factor) {
226 // If the resource bundle contains the data pack for |scale_factor|, we can
227 // safely fallback to SendThemeBitmap().
228 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
229 if (ui::GetScaleForScaleFactor(rb.GetMaxScaleFactor()) >= scale_factor) {
230 SendThemeBitmap(callback, resource_id, scale_factor);
231 return;
234 // Otherwise, we should use gfx::ImageSkia to obtain the data. ImageSkia can
235 // rescale the bitmap if its backend doesn't contain the representation for
236 // the specified scale factor. This is the fallback path in case chrome is
237 // shipped without 2x resource pack but needs to use HighDPI display, which
238 // can happen in ChromeOS or Linux.
239 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
240 if (BrowserThemePack::IsPersistentImageID(resource_id)) {
241 DCHECK_CURRENTLY_ON(BrowserThread::UI);
242 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
243 DCHECK(tp);
245 ProcessImageOnUIThread(*tp->GetImageSkiaNamed(resource_id), scale_factor,
246 data);
247 callback.Run(data.get());
248 } else {
249 DCHECK_CURRENTLY_ON(BrowserThread::IO);
250 // Fetching image data in ResourceBundle should happen on the UI thread. See
251 // crbug.com/449277
252 content::BrowserThread::PostTaskAndReply(
253 content::BrowserThread::UI, FROM_HERE,
254 base::Bind(&ProcessResourceOnUIThread, resource_id, scale_factor, data),
255 base::Bind(callback, data));