| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/theme_source.h" | 5 #include "chrome/browser/ui/webui/theme_source.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/string_number_conversions.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/resources_util.h" | 11 #include "chrome/browser/resources_util.h" |
| 11 #include "chrome/browser/themes/theme_service.h" | 12 #include "chrome/browser/themes/theme_service.h" |
| 12 #include "chrome/browser/themes/theme_service_factory.h" | 13 #include "chrome/browser/themes/theme_service_factory.h" |
| 13 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" | 14 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
| 14 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" | 15 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
| 15 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 19 #include "ui/base/layout.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/base/theme_provider.h" | 21 #include "ui/base/theme_provider.h" |
| 20 | 22 |
| 21 using content::BrowserThread; | 23 using content::BrowserThread; |
| 22 | 24 |
| 25 namespace { |
| 26 |
| 23 // use a resource map rather than hard-coded strings. | 27 // use a resource map rather than hard-coded strings. |
| 24 static const char* kNewTabCSSPath = "css/new_tab_theme.css"; | 28 static const char* kNewTabCSSPath = "css/new_tab_theme.css"; |
| 25 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; | 29 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; |
| 26 | 30 |
| 27 static std::string StripQueryParams(const std::string& path) { | 31 struct ScaleFactorMap { |
| 32 const char* name; |
| 33 ui::ScaleFactor scale_factor; |
| 34 }; |
| 35 |
| 36 const ScaleFactorMap kScaleFactorMap[] = { |
| 37 { "1x", ui::SCALE_FACTOR_100P }, |
| 38 { "2x", ui::SCALE_FACTOR_200P }, |
| 39 }; |
| 40 |
| 41 std::string StripQueryParams(const std::string& path) { |
| 28 GURL path_url = GURL(std::string(chrome::kChromeUIScheme) + "://" + | 42 GURL path_url = GURL(std::string(chrome::kChromeUIScheme) + "://" + |
| 29 std::string(chrome::kChromeUIThemePath) + "/" + path); | 43 std::string(chrome::kChromeUIThemePath) + "/" + path); |
| 30 return path_url.path().substr(1); // path() always includes a leading '/'. | 44 return path_url.path().substr(1); // path() always includes a leading '/'. |
| 31 } | 45 } |
| 32 | 46 |
| 47 std::string ParsePathAndScale(const std::string& path, |
| 48 ui::ScaleFactor* scale_factor) { |
| 49 // Our path may include cachebuster arguments, so trim them off. |
| 50 std::string uncached_path = StripQueryParams(path); |
| 51 if (scale_factor) |
| 52 *scale_factor = ui::SCALE_FACTOR_100P; |
| 53 |
| 54 // Detect and parse resource string ending in @<scale>x. |
| 55 std::size_t pos = uncached_path.rfind('@'); |
| 56 if (pos != std::string::npos) { |
| 57 if (scale_factor) { |
| 58 for (size_t i = 0; i < arraysize(kScaleFactorMap); i++) { |
| 59 if (uncached_path.compare(pos + 1, uncached_path.length() - pos - 1, |
| 60 kScaleFactorMap[i].name) == 0) { |
| 61 *scale_factor = kScaleFactorMap[i].scale_factor; |
| 62 } |
| 63 } |
| 64 } |
| 65 // Strip scale factor specification from path. |
| 66 uncached_path = uncached_path.substr(0, pos); |
| 67 } |
| 68 return uncached_path; |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 33 //////////////////////////////////////////////////////////////////////////////// | 73 //////////////////////////////////////////////////////////////////////////////// |
| 34 // ThemeSource, public: | 74 // ThemeSource, public: |
| 35 | 75 |
| 36 ThemeSource::ThemeSource(Profile* profile) | 76 ThemeSource::ThemeSource(Profile* profile) |
| 37 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()), | 77 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()), |
| 38 profile_(profile->GetOriginalProfile()) { | 78 profile_(profile->GetOriginalProfile()) { |
| 39 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS( | 79 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS( |
| 40 profile->IsOffTheRecord()); | 80 profile->IsOffTheRecord()); |
| 41 } | 81 } |
| 42 | 82 |
| 43 ThemeSource::~ThemeSource() { | 83 ThemeSource::~ThemeSource() { |
| 44 } | 84 } |
| 45 | 85 |
| 46 void ThemeSource::StartDataRequest(const std::string& path, | 86 void ThemeSource::StartDataRequest(const std::string& path, |
| 47 bool is_incognito, | 87 bool is_incognito, |
| 48 int request_id) { | 88 int request_id) { |
| 49 // Our path may include cachebuster arguments, so trim them off. | 89 // Default scale factor if not specified. |
| 50 std::string uncached_path = StripQueryParams(path); | 90 ui::ScaleFactor scale_factor; |
| 91 std::string uncached_path = ParsePathAndScale(path, &scale_factor); |
| 51 | 92 |
| 52 if (uncached_path == kNewTabCSSPath || | 93 if (uncached_path == kNewTabCSSPath || |
| 53 uncached_path == kNewIncognitoTabCSSPath) { | 94 uncached_path == kNewIncognitoTabCSSPath) { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 55 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || | 96 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || |
| 56 (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); | 97 (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); |
| 57 | 98 |
| 58 SendResponse(request_id, css_bytes_); | 99 SendResponse(request_id, css_bytes_); |
| 59 return; | 100 return; |
| 60 } else { | 101 } else { |
| 61 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); | 102 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
| 62 if (resource_id != -1) { | 103 if (resource_id != -1) { |
| 63 SendThemeBitmap(request_id, resource_id); | 104 SendThemeBitmap(request_id, resource_id, scale_factor); |
| 64 return; | 105 return; |
| 65 } | 106 } |
| 66 } | 107 } |
| 67 // We don't have any data to send back. | 108 // We don't have any data to send back. |
| 68 SendResponse(request_id, NULL); | 109 SendResponse(request_id, NULL); |
| 69 } | 110 } |
| 70 | 111 |
| 71 std::string ThemeSource::GetMimeType(const std::string& path) const { | 112 std::string ThemeSource::GetMimeType(const std::string& path) const { |
| 72 std::string uncached_path = StripQueryParams(path); | 113 std::string uncached_path = ParsePathAndScale(path, NULL); |
| 73 | 114 |
| 74 if (uncached_path == kNewTabCSSPath || | 115 if (uncached_path == kNewTabCSSPath || |
| 75 uncached_path == kNewIncognitoTabCSSPath) { | 116 uncached_path == kNewIncognitoTabCSSPath) { |
| 76 return "text/css"; | 117 return "text/css"; |
| 77 } | 118 } |
| 78 | 119 |
| 79 return "image/png"; | 120 return "image/png"; |
| 80 } | 121 } |
| 81 | 122 |
| 82 MessageLoop* ThemeSource::MessageLoopForRequestPath( | 123 MessageLoop* ThemeSource::MessageLoopForRequestPath( |
| 83 const std::string& path) const { | 124 const std::string& path) const { |
| 84 std::string uncached_path = StripQueryParams(path); | 125 std::string uncached_path = ParsePathAndScale(path, NULL); |
| 85 | 126 |
| 86 if (uncached_path == kNewTabCSSPath || | 127 if (uncached_path == kNewTabCSSPath || |
| 87 uncached_path == kNewIncognitoTabCSSPath) { | 128 uncached_path == kNewIncognitoTabCSSPath) { |
| 88 // We generated and cached this when we initialized the object. We don't | 129 // We generated and cached this when we initialized the object. We don't |
| 89 // have to go back to the UI thread to send the data. | 130 // have to go back to the UI thread to send the data. |
| 90 return NULL; | 131 return NULL; |
| 91 } | 132 } |
| 92 | 133 |
| 93 // If it's not a themeable image, we don't need to go to the UI thread. | 134 // If it's not a themeable image, we don't need to go to the UI thread. |
| 94 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); | 135 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
| 95 if (!ThemeService::IsThemeableImage(resource_id)) | 136 if (!ThemeService::IsThemeableImage(resource_id)) |
| 96 return NULL; | 137 return NULL; |
| 97 | 138 |
| 98 return DataSource::MessageLoopForRequestPath(path); | 139 return DataSource::MessageLoopForRequestPath(path); |
| 99 } | 140 } |
| 100 | 141 |
| 101 bool ThemeSource::ShouldReplaceExistingSource() const { | 142 bool ThemeSource::ShouldReplaceExistingSource() const { |
| 102 // We currently get the css_bytes_ in the ThemeSource constructor, so we need | 143 // We currently get the css_bytes_ in the ThemeSource constructor, so we need |
| 103 // to recreate the source itself when a theme changes. | 144 // to recreate the source itself when a theme changes. |
| 104 return true; | 145 return true; |
| 105 } | 146 } |
| 106 | 147 |
| 107 //////////////////////////////////////////////////////////////////////////////// | 148 //////////////////////////////////////////////////////////////////////////////// |
| 108 // ThemeSource, private: | 149 // ThemeSource, private: |
| 109 | 150 |
| 110 void ThemeSource::SendThemeBitmap(int request_id, int resource_id) { | 151 void ThemeSource::SendThemeBitmap(int request_id, |
| 152 int resource_id, |
| 153 ui::ScaleFactor scale_factor) { |
| 111 if (ThemeService::IsThemeableImage(resource_id)) { | 154 if (ThemeService::IsThemeableImage(resource_id)) { |
| 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 113 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | 156 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
| 114 DCHECK(tp); | 157 DCHECK(tp); |
| 115 | 158 |
| 159 // TODO(flackr): Pass scale factor when fetching themeable images. |
| 116 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( | 160 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( |
| 117 resource_id)); | 161 resource_id)); |
| 118 SendResponse(request_id, image_data); | 162 SendResponse(request_id, image_data); |
| 119 } else { | 163 } else { |
| 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 121 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 165 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 122 SendResponse(request_id, rb.LoadDataResourceBytes(resource_id)); | 166 SendResponse(request_id, |
| 167 rb.LoadDataResourceBytes(resource_id, scale_factor)); |
| 123 } | 168 } |
| 124 } | 169 } |
| OLD | NEW |