blob: ca6124af10f993658d0eb0f5acf0082a109bf9be [file] [log] [blame]
[email protected]dcceaa52012-02-28 20:22:201// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4a190632009-05-09 01:07:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]fd42ac30f2011-02-27 19:33:365#include "chrome/browser/ui/webui/theme_source.h"
[email protected]4a190632009-05-09 01:07:426
[email protected]3b63f8f42011-03-28 01:54:157#include "base/memory/ref_counted_memory.h"
[email protected]4a190632009-05-09 01:07:428#include "base/message_loop.h"
[email protected]c49201a2012-05-24 11:04:579#include "base/string_number_conversions.h"
[email protected]8ecad5e2010-12-02 21:18:3310#include "chrome/browser/profiles/profile.h"
[email protected]1faee3f02010-06-21 07:01:3411#include "chrome/browser/resources_util.h"
[email protected]18280372011-03-22 18:05:2212#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4213#include "chrome/browser/themes/theme_service_factory.h"
[email protected]7c5c6f3a2011-04-28 19:56:4914#include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
[email protected]60100b22011-06-29 23:37:2515#include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
[email protected]daf19c52012-07-13 19:01:3216#include "chrome/browser/ui/webui/web_ui_util.h"
[email protected]4a190632009-05-09 01:07:4217#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4918#include "content/public/browser/browser_thread.h"
[email protected]dff6d132009-05-15 22:40:5719#include "googleurl/src/gurl.h"
[email protected]c49201a2012-05-24 11:04:5720#include "ui/base/layout.h"
[email protected]42ce29d2011-01-20 23:19:4621#include "ui/base/resource/resource_bundle.h"
[email protected]9dd7e3d72011-01-20 18:27:0622#include "ui/base/theme_provider.h"
[email protected]4a190632009-05-09 01:07:4223
[email protected]631bb742011-11-02 11:29:3924using content::BrowserThread;
25
[email protected]c49201a2012-05-24 11:04:5726namespace {
27
[email protected]daf19c52012-07-13 19:01:3228std::string GetThemePath() {
29 return std::string(chrome::kChromeUIScheme) +
30 "://" + std::string(chrome::kChromeUIThemePath) + "/";
31}
32
[email protected]dff6d132009-05-15 22:40:5733// use a resource map rather than hard-coded strings.
[email protected]dcceaa52012-02-28 20:22:2034static const char* kNewTabCSSPath = "css/new_tab_theme.css";
35static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css";
[email protected]dff6d132009-05-15 22:40:5736
[email protected]c49201a2012-05-24 11:04:5737} // namespace
38
[email protected]dff6d132009-05-15 22:40:5739////////////////////////////////////////////////////////////////////////////////
[email protected]65992592011-02-24 02:25:1540// ThemeSource, public:
[email protected]dff6d132009-05-15 22:40:5741
[email protected]65992592011-02-24 02:25:1542ThemeSource::ThemeSource(Profile* profile)
[email protected]dff6d132009-05-15 22:40:5743 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()),
[email protected]9e3a9a502009-11-24 20:35:5944 profile_(profile->GetOriginalProfile()) {
[email protected]60100b22011-06-29 23:37:2545 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(
[email protected]156ed27b2009-11-23 18:31:2546 profile->IsOffTheRecord());
[email protected]4a190632009-05-09 01:07:4247}
48
[email protected]65992592011-02-24 02:25:1549ThemeSource::~ThemeSource() {
[email protected]3071754432010-07-28 00:09:5450}
51
[email protected]65992592011-02-24 02:25:1552void ThemeSource::StartDataRequest(const std::string& path,
[email protected]b0888112011-03-31 13:23:2553 bool is_incognito,
[email protected]65992592011-02-24 02:25:1554 int request_id) {
[email protected]c49201a2012-05-24 11:04:5755 // Default scale factor if not specified.
56 ui::ScaleFactor scale_factor;
[email protected]daf19c52012-07-13 19:01:3257 std::string uncached_path;
58 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path),
59 &uncached_path,
60 &scale_factor);
[email protected]4a190632009-05-09 01:07:4261
[email protected]156ed27b2009-11-23 18:31:2562 if (uncached_path == kNewTabCSSPath ||
63 uncached_path == kNewIncognitoTabCSSPath) {
[email protected]2dc383ce2010-10-09 03:54:0064 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]b0888112011-03-31 13:23:2565 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) ||
66 (uncached_path == kNewIncognitoTabCSSPath && is_incognito));
[email protected]156ed27b2009-11-23 18:31:2567
68 SendResponse(request_id, css_bytes_);
[email protected]a5badef2009-08-06 16:37:5669 return;
[email protected]dff6d132009-05-15 22:40:5770 } else {
[email protected]1faee3f02010-06-21 07:01:3471 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
[email protected]dff6d132009-05-15 22:40:5772 if (resource_id != -1) {
[email protected]c49201a2012-05-24 11:04:5773 SendThemeBitmap(request_id, resource_id, scale_factor);
[email protected]dff6d132009-05-15 22:40:5774 return;
[email protected]4a190632009-05-09 01:07:4275 }
76 }
[email protected]dff6d132009-05-15 22:40:5777 // We don't have any data to send back.
78 SendResponse(request_id, NULL);
79}
80
[email protected]65992592011-02-24 02:25:1581std::string ThemeSource::GetMimeType(const std::string& path) const {
[email protected]daf19c52012-07-13 19:01:3282 std::string uncached_path;
83 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path),
84 &uncached_path, NULL);
[email protected]dff6d132009-05-15 22:40:5785
[email protected]40da87c12009-09-02 20:32:4586 if (uncached_path == kNewTabCSSPath ||
87 uncached_path == kNewIncognitoTabCSSPath) {
[email protected]dff6d132009-05-15 22:40:5788 return "text/css";
[email protected]40da87c12009-09-02 20:32:4589 }
90
[email protected]dff6d132009-05-15 22:40:5791 return "image/png";
92}
93
[email protected]65992592011-02-24 02:25:1594MessageLoop* ThemeSource::MessageLoopForRequestPath(
[email protected]5d383ab2009-11-20 21:43:0795 const std::string& path) const {
[email protected]daf19c52012-07-13 19:01:3296 std::string uncached_path;
97 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path),
98 &uncached_path, NULL);
[email protected]5d383ab2009-11-20 21:43:0799
100 if (uncached_path == kNewTabCSSPath ||
101 uncached_path == kNewIncognitoTabCSSPath) {
[email protected]156ed27b2009-11-23 18:31:25102 // We generated and cached this when we initialized the object. We don't
103 // have to go back to the UI thread to send the data.
[email protected]5d383ab2009-11-20 21:43:07104 return NULL;
105 }
106
[email protected]26d7ca2a2009-11-24 22:32:16107 // If it's not a themeable image, we don't need to go to the UI thread.
[email protected]1faee3f02010-06-21 07:01:34108 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
[email protected]a0ea76c2011-03-23 17:36:42109 if (!ThemeService::IsThemeableImage(resource_id))
[email protected]26d7ca2a2009-11-24 22:32:16110 return NULL;
111
[email protected]5d383ab2009-11-20 21:43:07112 return DataSource::MessageLoopForRequestPath(path);
113}
114
[email protected]65992592011-02-24 02:25:15115bool ThemeSource::ShouldReplaceExistingSource() const {
[email protected]f7e0ab72011-03-10 15:04:47116 // We currently get the css_bytes_ in the ThemeSource constructor, so we need
117 // to recreate the source itself when a theme changes.
118 return true;
[email protected]ffe3d6f2011-02-23 16:42:52119}
120
[email protected]dff6d132009-05-15 22:40:57121////////////////////////////////////////////////////////////////////////////////
[email protected]65992592011-02-24 02:25:15122// ThemeSource, private:
[email protected]dff6d132009-05-15 22:40:57123
[email protected]c49201a2012-05-24 11:04:57124void ThemeSource::SendThemeBitmap(int request_id,
125 int resource_id,
126 ui::ScaleFactor scale_factor) {
[email protected]a0ea76c2011-03-23 17:36:42127 if (ThemeService::IsThemeableImage(resource_id)) {
[email protected]2dc383ce2010-10-09 03:54:00128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]18280372011-03-22 18:05:22129 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
[email protected]26d7ca2a2009-11-24 22:32:16130 DCHECK(tp);
[email protected]dff6d132009-05-15 22:40:57131
[email protected]68c7630b2012-05-02 22:37:42132 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData(
[email protected]0a3e3c6112012-08-05 20:07:45133 resource_id, scale_factor));
[email protected]26d7ca2a2009-11-24 22:32:16134 SendResponse(request_id, image_data);
135 } else {
[email protected]2dc383ce2010-10-09 03:54:00136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]26d7ca2a2009-11-24 22:32:16137 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
[email protected]c49201a2012-05-24 11:04:57138 SendResponse(request_id,
139 rb.LoadDataResourceBytes(resource_id, scale_factor));
[email protected]26d7ca2a2009-11-24 22:32:16140 }
[email protected]4a190632009-05-09 01:07:42141}