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/theme_properties.h"
14 #include "chrome/browser/themes/theme_service.h"
15 #include "chrome/browser/themes/theme_service_factory.h"
16 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
17 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
18 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "net/url_request/url_request.h"
21 #include "ui/base/layout.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/base/webui/web_ui_util.h"
26 using content::BrowserThread
;
30 std::string
GetThemePath() {
31 return std::string(chrome::kChromeUIScheme
) +
32 "://" + std::string(chrome::kChromeUIThemePath
) + "/";
35 // use a resource map rather than hard-coded strings.
36 static const char* kNewTabCSSPath
= "css/new_tab_theme.css";
37 static const char* kNewIncognitoTabCSSPath
= "css/incognito_new_tab_theme.css";
41 ////////////////////////////////////////////////////////////////////////////////
42 // ThemeSource, public:
44 ThemeSource::ThemeSource(Profile
* profile
)
45 : profile_(profile
->GetOriginalProfile()) {
46 NTPResourceCache::WindowType win_type
= NTPResourceCache::GetWindowType(
49 NTPResourceCacheFactory::GetForProfile(profile
)->GetNewTabCSS(win_type
);
52 ThemeSource::~ThemeSource() {
55 std::string
ThemeSource::GetSource() const {
56 return chrome::kChromeUIThemePath
;
59 void ThemeSource::StartDataRequest(
60 const std::string
& path
,
61 int render_process_id
,
63 const content::URLDataSource::GotDataCallback
& callback
) {
64 // Default scale factor if not specified.
65 ui::ScaleFactor scale_factor
;
66 std::string uncached_path
;
67 webui::ParsePathAndScale(GURL(GetThemePath() + path
),
71 if (uncached_path
== kNewTabCSSPath
||
72 uncached_path
== kNewIncognitoTabCSSPath
) {
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
75 callback
.Run(css_bytes_
.get());
80 int resource_id
= ResourcesUtil::GetThemeResourceId(uncached_path
);
81 if (resource_id
!= -1) {
82 SendThemeBitmap(callback
, resource_id
, scale_factor
);
86 // We don't have any data to send back.
90 std::string
ThemeSource::GetMimeType(const std::string
& path
) const {
91 std::string uncached_path
;
92 webui::ParsePathAndScale(GURL(GetThemePath() + path
), &uncached_path
, NULL
);
94 if (uncached_path
== kNewTabCSSPath
||
95 uncached_path
== kNewIncognitoTabCSSPath
) {
102 base::MessageLoop
* ThemeSource::MessageLoopForRequestPath(
103 const std::string
& path
) const {
104 std::string uncached_path
;
105 webui::ParsePathAndScale(GURL(GetThemePath() + path
), &uncached_path
, NULL
);
107 if (uncached_path
== kNewTabCSSPath
||
108 uncached_path
== kNewIncognitoTabCSSPath
) {
109 // We generated and cached this when we initialized the object. We don't
110 // have to go back to the UI thread to send the data.
114 // If it's not a themeable image, we don't need to go to the UI thread.
115 int resource_id
= ResourcesUtil::GetThemeResourceId(uncached_path
);
116 if (!ThemeProperties::IsThemeableImage(resource_id
))
119 return content::URLDataSource::MessageLoopForRequestPath(path
);
122 bool ThemeSource::ShouldReplaceExistingSource() const {
123 // We currently get the css_bytes_ in the ThemeSource constructor, so we need
124 // to recreate the source itself when a theme changes.
128 bool ThemeSource::ShouldServiceRequest(const net::URLRequest
* request
) const {
129 if (request
->url().SchemeIs(chrome::kChromeSearchScheme
))
130 return InstantIOContext::ShouldServiceRequest(request
);
131 return URLDataSource::ShouldServiceRequest(request
);
134 ////////////////////////////////////////////////////////////////////////////////
135 // ThemeSource, private:
137 void ThemeSource::SendThemeBitmap(
138 const content::URLDataSource::GotDataCallback
& callback
,
140 ui::ScaleFactor scale_factor
) {
141 if (ThemeProperties::IsThemeableImage(resource_id
)) {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
143 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
146 scoped_refptr
<base::RefCountedMemory
> image_data(tp
->GetRawData(
147 resource_id
, scale_factor
));
148 callback
.Run(image_data
.get());
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
151 const ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
152 callback
.Run(rb
.LoadDataResourceBytesForScale(resource_id
, scale_factor
));