blob: 26f76db793d0250e90d2ded6b059773576856a7f [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]3ea1b182013-02-08 22:38:419#include "base/strings/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]a7b8e43d2013-03-18 18:52:4312#include "chrome/browser/search/instant_io_context.h"
[email protected]e119b802013-02-18 18:55:3913#include "chrome/browser/themes/theme_properties.h"
[email protected]18280372011-03-22 18:05:2214#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4215#include "chrome/browser/themes/theme_service_factory.h"
[email protected]7c5c6f3a2011-04-28 19:56:4916#include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
[email protected]60100b22011-06-29 23:37:2517#include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
[email protected]4a190632009-05-09 01:07:4218#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4919#include "content/public/browser/browser_thread.h"
[email protected]dff6d132009-05-15 22:40:5720#include "googleurl/src/gurl.h"
[email protected]672c8c12013-03-07 12:30:0621#include "net/url_request/url_request.h"
[email protected]c49201a2012-05-24 11:04:5722#include "ui/base/layout.h"
[email protected]42ce29d2011-01-20 23:19:4623#include "ui/base/resource/resource_bundle.h"
[email protected]5053d402013-01-23 05:19:2624#include "ui/webui/web_ui_util.h"
[email protected]4a190632009-05-09 01:07:4225
[email protected]631bb742011-11-02 11:29:3926using content::BrowserThread;
27
[email protected]c49201a2012-05-24 11:04:5728namespace {
29
[email protected]daf19c52012-07-13 19:01:3230std::string GetThemePath() {
31 return std::string(chrome::kChromeUIScheme) +
32 "://" + std::string(chrome::kChromeUIThemePath) + "/";
33}
34
[email protected]dff6d132009-05-15 22:40:5735// use a resource map rather than hard-coded strings.
[email protected]dcceaa52012-02-28 20:22:2036static const char* kNewTabCSSPath = "css/new_tab_theme.css";
37static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css";
[email protected]dff6d132009-05-15 22:40:5738
[email protected]c49201a2012-05-24 11:04:5739} // namespace
40
[email protected]dff6d132009-05-15 22:40:5741////////////////////////////////////////////////////////////////////////////////
[email protected]65992592011-02-24 02:25:1542// ThemeSource, public:
[email protected]dff6d132009-05-15 22:40:5743
[email protected]65992592011-02-24 02:25:1544ThemeSource::ThemeSource(Profile* profile)
[email protected]92253622013-01-14 20:40:5045 : profile_(profile->GetOriginalProfile()) {
[email protected]60100b22011-06-29 23:37:2546 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(
[email protected]156ed27b2009-11-23 18:31:2547 profile->IsOffTheRecord());
[email protected]4a190632009-05-09 01:07:4248}
49
[email protected]65992592011-02-24 02:25:1550ThemeSource::~ThemeSource() {
[email protected]3071754432010-07-28 00:09:5451}
52
[email protected]305b8e82013-04-17 16:12:3353std::string ThemeSource::GetSource() const {
[email protected]92253622013-01-14 20:40:5054 return chrome::kChromeUIThemePath;
55}
56
[email protected]90d9f1e2013-01-16 02:46:4157void ThemeSource::StartDataRequest(
58 const std::string& path,
59 bool is_incognito,
60 const content::URLDataSource::GotDataCallback& callback) {
[email protected]c49201a2012-05-24 11:04:5761 // Default scale factor if not specified.
62 ui::ScaleFactor scale_factor;
[email protected]daf19c52012-07-13 19:01:3263 std::string uncached_path;
[email protected]5053d402013-01-23 05:19:2664 webui::ParsePathAndScale(GURL(GetThemePath() + path),
65 &uncached_path,
66 &scale_factor);
[email protected]4a190632009-05-09 01:07:4267
[email protected]156ed27b2009-11-23 18:31:2568 if (uncached_path == kNewTabCSSPath ||
69 uncached_path == kNewIncognitoTabCSSPath) {
[email protected]2dc383ce2010-10-09 03:54:0070 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]b0888112011-03-31 13:23:2571 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) ||
72 (uncached_path == kNewIncognitoTabCSSPath && is_incognito));
[email protected]156ed27b2009-11-23 18:31:2573
[email protected]90d9f1e2013-01-16 02:46:4174 callback.Run(css_bytes_);
[email protected]a5badef2009-08-06 16:37:5675 return;
[email protected]4a190632009-05-09 01:07:4276 }
[email protected]92253622013-01-14 20:40:5077
78
79 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
80 if (resource_id != -1) {
[email protected]90d9f1e2013-01-16 02:46:4181 SendThemeBitmap(callback, resource_id, scale_factor);
[email protected]92253622013-01-14 20:40:5082 return;
83 }
84
[email protected]dff6d132009-05-15 22:40:5785 // We don't have any data to send back.
[email protected]90d9f1e2013-01-16 02:46:4186 callback.Run(NULL);
[email protected]dff6d132009-05-15 22:40:5787}
88
[email protected]65992592011-02-24 02:25:1589std::string ThemeSource::GetMimeType(const std::string& path) const {
[email protected]daf19c52012-07-13 19:01:3290 std::string uncached_path;
[email protected]5053d402013-01-23 05:19:2691 webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL);
[email protected]dff6d132009-05-15 22:40:5792
[email protected]40da87c12009-09-02 20:32:4593 if (uncached_path == kNewTabCSSPath ||
94 uncached_path == kNewIncognitoTabCSSPath) {
[email protected]dff6d132009-05-15 22:40:5795 return "text/css";
[email protected]40da87c12009-09-02 20:32:4596 }
97
[email protected]dff6d132009-05-15 22:40:5798 return "image/png";
99}
100
[email protected]65992592011-02-24 02:25:15101MessageLoop* ThemeSource::MessageLoopForRequestPath(
[email protected]5d383ab2009-11-20 21:43:07102 const std::string& path) const {
[email protected]daf19c52012-07-13 19:01:32103 std::string uncached_path;
[email protected]5053d402013-01-23 05:19:26104 webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL);
[email protected]5d383ab2009-11-20 21:43:07105
106 if (uncached_path == kNewTabCSSPath ||
107 uncached_path == kNewIncognitoTabCSSPath) {
[email protected]156ed27b2009-11-23 18:31:25108 // We generated and cached this when we initialized the object. We don't
109 // have to go back to the UI thread to send the data.
[email protected]5d383ab2009-11-20 21:43:07110 return NULL;
111 }
112
[email protected]26d7ca2a2009-11-24 22:32:16113 // If it's not a themeable image, we don't need to go to the UI thread.
[email protected]1faee3f02010-06-21 07:01:34114 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
[email protected]e119b802013-02-18 18:55:39115 if (!ThemeProperties::IsThemeableImage(resource_id))
[email protected]26d7ca2a2009-11-24 22:32:16116 return NULL;
117
[email protected]90d9f1e2013-01-16 02:46:41118 return content::URLDataSource::MessageLoopForRequestPath(path);
[email protected]5d383ab2009-11-20 21:43:07119}
120
[email protected]65992592011-02-24 02:25:15121bool ThemeSource::ShouldReplaceExistingSource() const {
[email protected]f7e0ab72011-03-10 15:04:47122 // We currently get the css_bytes_ in the ThemeSource constructor, so we need
123 // to recreate the source itself when a theme changes.
124 return true;
[email protected]ffe3d6f2011-02-23 16:42:52125}
126
[email protected]672c8c12013-03-07 12:30:06127bool ThemeSource::ShouldServiceRequest(const net::URLRequest* request) const {
128 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
129 return InstantIOContext::ShouldServiceRequest(request);
130 return URLDataSource::ShouldServiceRequest(request);
131}
132
[email protected]dff6d132009-05-15 22:40:57133////////////////////////////////////////////////////////////////////////////////
[email protected]65992592011-02-24 02:25:15134// ThemeSource, private:
[email protected]dff6d132009-05-15 22:40:57135
[email protected]90d9f1e2013-01-16 02:46:41136void ThemeSource::SendThemeBitmap(
137 const content::URLDataSource::GotDataCallback& callback,
138 int resource_id,
139 ui::ScaleFactor scale_factor) {
[email protected]e119b802013-02-18 18:55:39140 if (ThemeProperties::IsThemeableImage(resource_id)) {
[email protected]2dc383ce2010-10-09 03:54:00141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]18280372011-03-22 18:05:22142 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
[email protected]26d7ca2a2009-11-24 22:32:16143 DCHECK(tp);
[email protected]dff6d132009-05-15 22:40:57144
[email protected]68c7630b2012-05-02 22:37:42145 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData(
[email protected]0a3e3c6112012-08-05 20:07:45146 resource_id, scale_factor));
[email protected]90d9f1e2013-01-16 02:46:41147 callback.Run(image_data);
[email protected]26d7ca2a2009-11-24 22:32:16148 } else {
[email protected]2dc383ce2010-10-09 03:54:00149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]26d7ca2a2009-11-24 22:32:16150 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
[email protected]90d9f1e2013-01-16 02:46:41151 callback.Run(rb.LoadDataResourceBytesForScale(resource_id, scale_factor));
[email protected]26d7ca2a2009-11-24 22:32:16152 }
[email protected]4a190632009-05-09 01:07:42153}