[email protected] | 0940871 | 2011-02-02 12:02:22 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | fd42ac30f | 2011-02-27 19:33:36 | [diff] [blame] | 5 | #include "chrome/browser/ui/webui/theme_source.h" |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 6 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 7 | #include "base/memory/ref_counted_memory.h" |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 8 | #include "base/message_loop.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 9 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 1faee3f0 | 2010-06-21 07:01:34 | [diff] [blame] | 10 | #include "chrome/browser/resources_util.h" |
[email protected] | 1828037 | 2011-03-22 18:05:22 | [diff] [blame] | 11 | #include "chrome/browser/themes/theme_service.h" |
[email protected] | a0ea76c | 2011-03-23 17:36:42 | [diff] [blame] | 12 | #include "chrome/browser/themes/theme_service_factory.h" |
[email protected] | 7c5c6f3a | 2011-04-28 19:56:49 | [diff] [blame] | 13 | #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
[email protected] | 60100b2 | 2011-06-29 23:37:25 | [diff] [blame] | 14 | #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 15 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame^] | 16 | #include "content/public/browser/browser_thread.h" |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 17 | #include "googleurl/src/gurl.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 18 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 9dd7e3d7 | 2011-01-20 18:27:06 | [diff] [blame] | 19 | #include "ui/base/theme_provider.h" |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 20 | |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 21 | // use a resource map rather than hard-coded strings. |
| 22 | static const char* kNewTabCSSPath = "css/newtab.css"; |
[email protected] | a5badef | 2009-08-06 16:37:56 | [diff] [blame] | 23 | static const char* kNewIncognitoTabCSSPath = "css/newincognitotab.css"; |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 24 | |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 25 | static 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] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 32 | // ThemeSource, public: |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 33 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 34 | ThemeSource::ThemeSource(Profile* profile) |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 35 | : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()), |
[email protected] | 9e3a9a50 | 2009-11-24 20:35:59 | [diff] [blame] | 36 | profile_(profile->GetOriginalProfile()) { |
[email protected] | 60100b2 | 2011-06-29 23:37:25 | [diff] [blame] | 37 | css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS( |
[email protected] | 156ed27b | 2009-11-23 18:31:25 | [diff] [blame] | 38 | profile->IsOffTheRecord()); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 41 | ThemeSource::~ThemeSource() { |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 42 | } |
| 43 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 44 | void ThemeSource::StartDataRequest(const std::string& path, |
[email protected] | b088811 | 2011-03-31 13:23:25 | [diff] [blame] | 45 | bool is_incognito, |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 46 | int request_id) { |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 47 | // Our path may include cachebuster arguments, so trim them off. |
| 48 | std::string uncached_path = StripQueryParams(path); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 49 | |
[email protected] | 156ed27b | 2009-11-23 18:31:25 | [diff] [blame] | 50 | if (uncached_path == kNewTabCSSPath || |
| 51 | uncached_path == kNewIncognitoTabCSSPath) { |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 52 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | b088811 | 2011-03-31 13:23:25 | [diff] [blame] | 53 | DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || |
| 54 | (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); |
[email protected] | 156ed27b | 2009-11-23 18:31:25 | [diff] [blame] | 55 | |
| 56 | SendResponse(request_id, css_bytes_); |
[email protected] | a5badef | 2009-08-06 16:37:56 | [diff] [blame] | 57 | return; |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 58 | } else { |
[email protected] | 1faee3f0 | 2010-06-21 07:01:34 | [diff] [blame] | 59 | int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 60 | if (resource_id != -1) { |
| 61 | SendThemeBitmap(request_id, resource_id); |
| 62 | return; |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 63 | } |
| 64 | } |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 65 | // We don't have any data to send back. |
| 66 | SendResponse(request_id, NULL); |
| 67 | } |
| 68 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 69 | std::string ThemeSource::GetMimeType(const std::string& path) const { |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 70 | std::string uncached_path = StripQueryParams(path); |
| 71 | |
[email protected] | 40da87c1 | 2009-09-02 20:32:45 | [diff] [blame] | 72 | if (uncached_path == kNewTabCSSPath || |
| 73 | uncached_path == kNewIncognitoTabCSSPath) { |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 74 | return "text/css"; |
[email protected] | 40da87c1 | 2009-09-02 20:32:45 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 77 | return "image/png"; |
| 78 | } |
| 79 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 80 | MessageLoop* ThemeSource::MessageLoopForRequestPath( |
[email protected] | 5d383ab | 2009-11-20 21:43:07 | [diff] [blame] | 81 | const std::string& path) const { |
| 82 | std::string uncached_path = StripQueryParams(path); |
| 83 | |
| 84 | if (uncached_path == kNewTabCSSPath || |
| 85 | uncached_path == kNewIncognitoTabCSSPath) { |
[email protected] | 156ed27b | 2009-11-23 18:31:25 | [diff] [blame] | 86 | // 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] | 5d383ab | 2009-11-20 21:43:07 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
| 90 | |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 91 | // If it's not a themeable image, we don't need to go to the UI thread. |
[email protected] | 1faee3f0 | 2010-06-21 07:01:34 | [diff] [blame] | 92 | int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
[email protected] | a0ea76c | 2011-03-23 17:36:42 | [diff] [blame] | 93 | if (!ThemeService::IsThemeableImage(resource_id)) |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 94 | return NULL; |
| 95 | |
[email protected] | 5d383ab | 2009-11-20 21:43:07 | [diff] [blame] | 96 | return DataSource::MessageLoopForRequestPath(path); |
| 97 | } |
| 98 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 99 | bool ThemeSource::ShouldReplaceExistingSource() const { |
[email protected] | f7e0ab7 | 2011-03-10 15:04:47 | [diff] [blame] | 100 | // 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] | ffe3d6f | 2011-02-23 16:42:52 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 105 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 106 | // ThemeSource, private: |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 107 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 108 | void ThemeSource::SendThemeBitmap(int request_id, int resource_id) { |
[email protected] | a0ea76c | 2011-03-23 17:36:42 | [diff] [blame] | 109 | if (ThemeService::IsThemeableImage(resource_id)) { |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 110 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 1828037 | 2011-03-22 18:05:22 | [diff] [blame] | 111 | ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 112 | DCHECK(tp); |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 113 | |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 114 | scoped_refptr<RefCountedMemory> image_data(tp->GetRawData(resource_id)); |
| 115 | SendResponse(request_id, image_data); |
| 116 | } else { |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 117 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 118 | const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 119 | SendResponse(request_id, rb.LoadDataResourceBytes(resource_id)); |
| 120 | } |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 121 | } |