blob: f33b546837a00a23ff267ec534f6221f677c50f7 [file] [log] [blame]
[email protected]09408712011-02-02 12:02:221// Copyright (c) 2011 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]8ecad5e2010-12-02 21:18:339#include "chrome/browser/profiles/profile.h"
[email protected]1faee3f02010-06-21 07:01:3410#include "chrome/browser/resources_util.h"
[email protected]18280372011-03-22 18:05:2211#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4212#include "chrome/browser/themes/theme_service_factory.h"
[email protected]7c5c6f3a2011-04-28 19:56:4913#include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
[email protected]60100b22011-06-29 23:37:2514#include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
[email protected]4a190632009-05-09 01:07:4215#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4916#include "content/public/browser/browser_thread.h"
[email protected]dff6d132009-05-15 22:40:5717#include "googleurl/src/gurl.h"
[email protected]42ce29d2011-01-20 23:19:4618#include "ui/base/resource/resource_bundle.h"
[email protected]9dd7e3d72011-01-20 18:27:0619#include "ui/base/theme_provider.h"
[email protected]4a190632009-05-09 01:07:4220
[email protected]dff6d132009-05-15 22:40:5721// use a resource map rather than hard-coded strings.
22static const char* kNewTabCSSPath = "css/newtab.css";
[email protected]a5badef2009-08-06 16:37:5623static const char* kNewIncognitoTabCSSPath = "css/newincognitotab.css";
[email protected]dff6d132009-05-15 22:40:5724
[email protected]dff6d132009-05-15 22:40:5725static std::string StripQueryParams(const std::string& path) {
26 GURL path_url = GURL(std::string(chrome::kChromeUIScheme) + "://" +
27 std::string(chrome::kChromeUIThemePath) + "/" + path);
28 return path_url.path().substr(1); // path() always includes a leading '/'.
29}
30
31////////////////////////////////////////////////////////////////////////////////
[email protected]65992592011-02-24 02:25:1532// ThemeSource, public:
[email protected]dff6d132009-05-15 22:40:5733
[email protected]65992592011-02-24 02:25:1534ThemeSource::ThemeSource(Profile* profile)
[email protected]dff6d132009-05-15 22:40:5735 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()),
[email protected]9e3a9a502009-11-24 20:35:5936 profile_(profile->GetOriginalProfile()) {
[email protected]60100b22011-06-29 23:37:2537 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(
[email protected]156ed27b2009-11-23 18:31:2538 profile->IsOffTheRecord());
[email protected]4a190632009-05-09 01:07:4239}
40
[email protected]65992592011-02-24 02:25:1541ThemeSource::~ThemeSource() {
[email protected]3071754432010-07-28 00:09:5442}
43
[email protected]65992592011-02-24 02:25:1544void ThemeSource::StartDataRequest(const std::string& path,
[email protected]b0888112011-03-31 13:23:2545 bool is_incognito,
[email protected]65992592011-02-24 02:25:1546 int request_id) {
[email protected]dff6d132009-05-15 22:40:5747 // Our path may include cachebuster arguments, so trim them off.
48 std::string uncached_path = StripQueryParams(path);
[email protected]4a190632009-05-09 01:07:4249
[email protected]156ed27b2009-11-23 18:31:2550 if (uncached_path == kNewTabCSSPath ||
51 uncached_path == kNewIncognitoTabCSSPath) {
[email protected]2dc383ce2010-10-09 03:54:0052 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]b0888112011-03-31 13:23:2553 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) ||
54 (uncached_path == kNewIncognitoTabCSSPath && is_incognito));
[email protected]156ed27b2009-11-23 18:31:2555
56 SendResponse(request_id, css_bytes_);
[email protected]a5badef2009-08-06 16:37:5657 return;
[email protected]dff6d132009-05-15 22:40:5758 } else {
[email protected]1faee3f02010-06-21 07:01:3459 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
[email protected]dff6d132009-05-15 22:40:5760 if (resource_id != -1) {
61 SendThemeBitmap(request_id, resource_id);
62 return;
[email protected]4a190632009-05-09 01:07:4263 }
64 }
[email protected]dff6d132009-05-15 22:40:5765 // We don't have any data to send back.
66 SendResponse(request_id, NULL);
67}
68
[email protected]65992592011-02-24 02:25:1569std::string ThemeSource::GetMimeType(const std::string& path) const {
[email protected]dff6d132009-05-15 22:40:5770 std::string uncached_path = StripQueryParams(path);
71
[email protected]40da87c12009-09-02 20:32:4572 if (uncached_path == kNewTabCSSPath ||
73 uncached_path == kNewIncognitoTabCSSPath) {
[email protected]dff6d132009-05-15 22:40:5774 return "text/css";
[email protected]40da87c12009-09-02 20:32:4575 }
76
[email protected]dff6d132009-05-15 22:40:5777 return "image/png";
78}
79
[email protected]65992592011-02-24 02:25:1580MessageLoop* ThemeSource::MessageLoopForRequestPath(
[email protected]5d383ab2009-11-20 21:43:0781 const std::string& path) const {
82 std::string uncached_path = StripQueryParams(path);
83
84 if (uncached_path == kNewTabCSSPath ||
85 uncached_path == kNewIncognitoTabCSSPath) {
[email protected]156ed27b2009-11-23 18:31:2586 // We generated and cached this when we initialized the object. We don't
87 // have to go back to the UI thread to send the data.
[email protected]5d383ab2009-11-20 21:43:0788 return NULL;
89 }
90
[email protected]26d7ca2a2009-11-24 22:32:1691 // If it's not a themeable image, we don't need to go to the UI thread.
[email protected]1faee3f02010-06-21 07:01:3492 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
[email protected]a0ea76c2011-03-23 17:36:4293 if (!ThemeService::IsThemeableImage(resource_id))
[email protected]26d7ca2a2009-11-24 22:32:1694 return NULL;
95
[email protected]5d383ab2009-11-20 21:43:0796 return DataSource::MessageLoopForRequestPath(path);
97}
98
[email protected]65992592011-02-24 02:25:1599bool ThemeSource::ShouldReplaceExistingSource() const {
[email protected]f7e0ab72011-03-10 15:04:47100 // We currently get the css_bytes_ in the ThemeSource constructor, so we need
101 // to recreate the source itself when a theme changes.
102 return true;
[email protected]ffe3d6f2011-02-23 16:42:52103}
104
[email protected]dff6d132009-05-15 22:40:57105////////////////////////////////////////////////////////////////////////////////
[email protected]65992592011-02-24 02:25:15106// ThemeSource, private:
[email protected]dff6d132009-05-15 22:40:57107
[email protected]65992592011-02-24 02:25:15108void ThemeSource::SendThemeBitmap(int request_id, int resource_id) {
[email protected]a0ea76c2011-03-23 17:36:42109 if (ThemeService::IsThemeableImage(resource_id)) {
[email protected]2dc383ce2010-10-09 03:54:00110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]18280372011-03-22 18:05:22111 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
[email protected]26d7ca2a2009-11-24 22:32:16112 DCHECK(tp);
[email protected]dff6d132009-05-15 22:40:57113
[email protected]26d7ca2a2009-11-24 22:32:16114 scoped_refptr<RefCountedMemory> image_data(tp->GetRawData(resource_id));
115 SendResponse(request_id, image_data);
116 } else {
[email protected]2dc383ce2010-10-09 03:54:00117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]26d7ca2a2009-11-24 22:32:16118 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
119 SendResponse(request_id, rb.LoadDataResourceBytes(resource_id));
120 }
[email protected]4a190632009-05-09 01:07:42121}