[email protected] | dcceaa5 | 2012-02-28 20:22:20 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 9 | #include "base/strings/string_number_conversions.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 1faee3f0 | 2010-06-21 07:01:34 | [diff] [blame] | 11 | #include "chrome/browser/resources_util.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 12 | #include "chrome/browser/search/instant_io_context.h" |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 13 | #include "chrome/browser/themes/theme_properties.h" |
[email protected] | 1828037 | 2011-03-22 18:05:22 | [diff] [blame] | 14 | #include "chrome/browser/themes/theme_service.h" |
[email protected] | a0ea76c | 2011-03-23 17:36:42 | [diff] [blame] | 15 | #include "chrome/browser/themes/theme_service_factory.h" |
[email protected] | 7c5c6f3a | 2011-04-28 19:56:49 | [diff] [blame] | 16 | #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
[email protected] | 60100b2 | 2011-06-29 23:37:25 | [diff] [blame] | 17 | #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 18 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 20 | #include "googleurl/src/gurl.h" |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 21 | #include "net/url_request/url_request.h" |
[email protected] | c49201a | 2012-05-24 11:04:57 | [diff] [blame] | 22 | #include "ui/base/layout.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 23 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 24 | #include "ui/webui/web_ui_util.h" |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 25 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 26 | using content::BrowserThread; |
| 27 | |
[email protected] | c49201a | 2012-05-24 11:04:57 | [diff] [blame] | 28 | namespace { |
| 29 | |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 30 | std::string GetThemePath() { |
| 31 | return std::string(chrome::kChromeUIScheme) + |
| 32 | "://" + std::string(chrome::kChromeUIThemePath) + "/"; |
| 33 | } |
| 34 | |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 35 | // use a resource map rather than hard-coded strings. |
[email protected] | dcceaa5 | 2012-02-28 20:22:20 | [diff] [blame] | 36 | static const char* kNewTabCSSPath = "css/new_tab_theme.css"; |
| 37 | static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 38 | |
[email protected] | c49201a | 2012-05-24 11:04:57 | [diff] [blame] | 39 | } // namespace |
| 40 | |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 41 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 42 | // ThemeSource, public: |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 43 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 44 | ThemeSource::ThemeSource(Profile* profile) |
[email protected] | 9225362 | 2013-01-14 20:40:50 | [diff] [blame] | 45 | : profile_(profile->GetOriginalProfile()) { |
[email protected] | 60100b2 | 2011-06-29 23:37:25 | [diff] [blame] | 46 | css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS( |
[email protected] | 156ed27b | 2009-11-23 18:31:25 | [diff] [blame] | 47 | profile->IsOffTheRecord()); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 50 | ThemeSource::~ThemeSource() { |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 305b8e8 | 2013-04-17 16:12:33 | [diff] [blame] | 53 | std::string ThemeSource::GetSource() const { |
[email protected] | 9225362 | 2013-01-14 20:40:50 | [diff] [blame] | 54 | return chrome::kChromeUIThemePath; |
| 55 | } |
| 56 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 57 | void ThemeSource::StartDataRequest( |
| 58 | const std::string& path, |
[email protected] | e671c27 | 2013-04-23 22:45:03 | [diff] [blame] | 59 | int render_process_id, |
| 60 | int render_view_id, |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 61 | const content::URLDataSource::GotDataCallback& callback) { |
[email protected] | c49201a | 2012-05-24 11:04:57 | [diff] [blame] | 62 | // Default scale factor if not specified. |
| 63 | ui::ScaleFactor scale_factor; |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 64 | std::string uncached_path; |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 65 | webui::ParsePathAndScale(GURL(GetThemePath() + path), |
| 66 | &uncached_path, |
| 67 | &scale_factor); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 68 | |
[email protected] | 156ed27b | 2009-11-23 18:31:25 | [diff] [blame] | 69 | if (uncached_path == kNewTabCSSPath || |
| 70 | uncached_path == kNewIncognitoTabCSSPath) { |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 71 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 156ed27b | 2009-11-23 18:31:25 | [diff] [blame] | 72 | |
[email protected] | 39790b7f | 2013-06-03 00:10:59 | [diff] [blame] | 73 | callback.Run(css_bytes_.get()); |
[email protected] | a5badef | 2009-08-06 16:37:56 | [diff] [blame] | 74 | return; |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 75 | } |
[email protected] | 9225362 | 2013-01-14 20:40:50 | [diff] [blame] | 76 | |
| 77 | |
| 78 | int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
| 79 | if (resource_id != -1) { |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 80 | SendThemeBitmap(callback, resource_id, scale_factor); |
[email protected] | 9225362 | 2013-01-14 20:40:50 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 84 | // We don't have any data to send back. |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 85 | callback.Run(NULL); |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 88 | std::string ThemeSource::GetMimeType(const std::string& path) const { |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 89 | std::string uncached_path; |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 90 | webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL); |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 91 | |
[email protected] | 40da87c1 | 2009-09-02 20:32:45 | [diff] [blame] | 92 | if (uncached_path == kNewTabCSSPath || |
| 93 | uncached_path == kNewIncognitoTabCSSPath) { |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 94 | return "text/css"; |
[email protected] | 40da87c1 | 2009-09-02 20:32:45 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 97 | return "image/png"; |
| 98 | } |
| 99 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 100 | base::MessageLoop* ThemeSource::MessageLoopForRequestPath( |
[email protected] | 5d383ab | 2009-11-20 21:43:07 | [diff] [blame] | 101 | const std::string& path) const { |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 102 | std::string uncached_path; |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 103 | webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL); |
[email protected] | 5d383ab | 2009-11-20 21:43:07 | [diff] [blame] | 104 | |
| 105 | if (uncached_path == kNewTabCSSPath || |
| 106 | uncached_path == kNewIncognitoTabCSSPath) { |
[email protected] | 156ed27b | 2009-11-23 18:31:25 | [diff] [blame] | 107 | // We generated and cached this when we initialized the object. We don't |
| 108 | // have to go back to the UI thread to send the data. |
[email protected] | 5d383ab | 2009-11-20 21:43:07 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 112 | // 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] | 113 | int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 114 | if (!ThemeProperties::IsThemeableImage(resource_id)) |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 115 | return NULL; |
| 116 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 117 | return content::URLDataSource::MessageLoopForRequestPath(path); |
[email protected] | 5d383ab | 2009-11-20 21:43:07 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 120 | bool ThemeSource::ShouldReplaceExistingSource() const { |
[email protected] | f7e0ab7 | 2011-03-10 15:04:47 | [diff] [blame] | 121 | // We currently get the css_bytes_ in the ThemeSource constructor, so we need |
| 122 | // to recreate the source itself when a theme changes. |
| 123 | return true; |
[email protected] | ffe3d6f | 2011-02-23 16:42:52 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 126 | bool ThemeSource::ShouldServiceRequest(const net::URLRequest* request) const { |
| 127 | if (request->url().SchemeIs(chrome::kChromeSearchScheme)) |
| 128 | return InstantIOContext::ShouldServiceRequest(request); |
| 129 | return URLDataSource::ShouldServiceRequest(request); |
| 130 | } |
| 131 | |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 132 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 6599259 | 2011-02-24 02:25:15 | [diff] [blame] | 133 | // ThemeSource, private: |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 134 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 135 | void ThemeSource::SendThemeBitmap( |
| 136 | const content::URLDataSource::GotDataCallback& callback, |
| 137 | int resource_id, |
| 138 | ui::ScaleFactor scale_factor) { |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 139 | if (ThemeProperties::IsThemeableImage(resource_id)) { |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 140 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 1828037 | 2011-03-22 18:05:22 | [diff] [blame] | 141 | ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 142 | DCHECK(tp); |
[email protected] | dff6d13 | 2009-05-15 22:40:57 | [diff] [blame] | 143 | |
[email protected] | 68c7630b | 2012-05-02 22:37:42 | [diff] [blame] | 144 | scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( |
[email protected] | 0a3e3c611 | 2012-08-05 20:07:45 | [diff] [blame] | 145 | resource_id, scale_factor)); |
[email protected] | 39790b7f | 2013-06-03 00:10:59 | [diff] [blame] | 146 | callback.Run(image_data.get()); |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 147 | } else { |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 148 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 149 | const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 150 | callback.Run(rb.LoadDataResourceBytesForScale(resource_id, scale_factor)); |
[email protected] | 26d7ca2a | 2009-11-24 22:32:16 | [diff] [blame] | 151 | } |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 152 | } |