| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/dom_ui/dom_ui_theme_source.h" | 5 #include "chrome/browser/dom_ui/dom_ui_theme_source.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "app/theme_provider.h" | 9 #include "app/theme_provider.h" |
| 10 #include "base/gfx/png_encoder.h" | 10 #include "base/gfx/png_encoder.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 std::string(chrome::kChromeUIThemePath) + "/" + path); | 43 std::string(chrome::kChromeUIThemePath) + "/" + path); |
| 44 return path_url.path().substr(1); // path() always includes a leading '/'. | 44 return path_url.path().substr(1); // path() always includes a leading '/'. |
| 45 } | 45 } |
| 46 | 46 |
| 47 //////////////////////////////////////////////////////////////////////////////// | 47 //////////////////////////////////////////////////////////////////////////////// |
| 48 // DOMUIThemeSource, public: | 48 // DOMUIThemeSource, public: |
| 49 | 49 |
| 50 DOMUIThemeSource::DOMUIThemeSource(Profile* profile) | 50 DOMUIThemeSource::DOMUIThemeSource(Profile* profile) |
| 51 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()), | 51 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()), |
| 52 profile_(profile) { | 52 profile_(profile) { |
| 53 InitNewTabCSS(); |
| 54 InitNewIncognitoTabCSS(); |
| 53 } | 55 } |
| 54 | 56 |
| 55 void DOMUIThemeSource::StartDataRequest(const std::string& path, | 57 void DOMUIThemeSource::StartDataRequest(const std::string& path, |
| 56 int request_id) { | 58 int request_id) { |
| 57 // Our path may include cachebuster arguments, so trim them off. | 59 // Our path may include cachebuster arguments, so trim them off. |
| 58 std::string uncached_path = StripQueryParams(path); | 60 std::string uncached_path = StripQueryParams(path); |
| 59 | 61 |
| 60 if (strcmp(uncached_path.c_str(), kNewTabCSSPath) == 0) { | 62 if (uncached_path == kNewTabCSSPath) { |
| 61 SendNewTabCSS(request_id); | 63 SendNewTabCSS(request_id, new_tab_css_); |
| 62 return; | 64 return; |
| 63 } else if (strcmp(uncached_path.c_str(), kNewIncognitoTabCSSPath) == 0) { | 65 } else if (uncached_path == kNewIncognitoTabCSSPath) { |
| 64 SendNewIncognitoTabCSS(request_id); | 66 SendNewTabCSS(request_id, new_incognito_tab_css_); |
| 65 return; | 67 return; |
| 66 } else { | 68 } else { |
| 67 int resource_id = ThemeResourcesUtil::GetId(uncached_path); | 69 int resource_id = ThemeResourcesUtil::GetId(uncached_path); |
| 68 if (resource_id != -1) { | 70 if (resource_id != -1) { |
| 69 SendThemeBitmap(request_id, resource_id); | 71 SendThemeBitmap(request_id, resource_id); |
| 70 return; | 72 return; |
| 71 } | 73 } |
| 72 } | 74 } |
| 73 // We don't have any data to send back. | 75 // We don't have any data to send back. |
| 74 SendResponse(request_id, NULL); | 76 SendResponse(request_id, NULL); |
| 75 } | 77 } |
| 76 | 78 |
| 77 std::string DOMUIThemeSource::GetMimeType(const std::string& path) const { | 79 std::string DOMUIThemeSource::GetMimeType(const std::string& path) const { |
| 78 std::string uncached_path = StripQueryParams(path); | 80 std::string uncached_path = StripQueryParams(path); |
| 79 | 81 |
| 80 if (strcmp(uncached_path.c_str(), kNewTabCSSPath) == 0 || | 82 if (uncached_path == kNewTabCSSPath || |
| 81 strcmp(uncached_path.c_str(), kNewIncognitoTabCSSPath) == 0) | 83 uncached_path == kNewIncognitoTabCSSPath) { |
| 82 return "text/css"; | 84 return "text/css"; |
| 85 } |
| 86 |
| 83 return "image/png"; | 87 return "image/png"; |
| 84 } | 88 } |
| 85 | 89 |
| 86 void DOMUIThemeSource::SendResponse(int request_id, RefCountedBytes* data) { | 90 void DOMUIThemeSource::SendResponse(int request_id, RefCountedBytes* data) { |
| 87 ChromeURLDataManager::DataSource::SendResponse(request_id, data); | 91 ChromeURLDataManager::DataSource::SendResponse(request_id, data); |
| 88 } | 92 } |
| 89 | 93 |
| 94 MessageLoop* DOMUIThemeSource::MessageLoopForRequestPath( |
| 95 const std::string& path) const { |
| 96 std::string uncached_path = StripQueryParams(path); |
| 97 |
| 98 if (uncached_path == kNewTabCSSPath || |
| 99 uncached_path == kNewIncognitoTabCSSPath) { |
| 100 // All of the operations that need to be on the UI thread for these |
| 101 // requests are performed in InitNewTabCSS and InitNewIncognitoTabCSS, |
| 102 // called by the constructor. It is safe to call StartDataRequest for |
| 103 // these resources from any thread, so return NULL. |
| 104 return NULL; |
| 105 } |
| 106 |
| 107 // Superclass |
| 108 return DataSource::MessageLoopForRequestPath(path); |
| 109 } |
| 110 |
| 90 //////////////////////////////////////////////////////////////////////////////// | 111 //////////////////////////////////////////////////////////////////////////////// |
| 91 // DOMUIThemeSource, private: | 112 // DOMUIThemeSource, private: |
| 92 | 113 |
| 93 void DOMUIThemeSource::SendNewTabCSS(int request_id) { | 114 void DOMUIThemeSource::InitNewTabCSS() { |
| 94 ThemeProvider* tp = profile_->GetThemeProvider(); | 115 ThemeProvider* tp = profile_->GetThemeProvider(); |
| 95 DCHECK(tp); | 116 DCHECK(tp); |
| 96 | 117 |
| 97 // Get our theme colors | 118 // Get our theme colors |
| 98 SkColor color_background = | 119 SkColor color_background = |
| 99 tp->GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND); | 120 tp->GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND); |
| 100 SkColor color_text = tp->GetColor(BrowserThemeProvider::COLOR_NTP_TEXT); | 121 SkColor color_text = tp->GetColor(BrowserThemeProvider::COLOR_NTP_TEXT); |
| 101 SkColor color_link = tp->GetColor(BrowserThemeProvider::COLOR_NTP_LINK); | 122 SkColor color_link = tp->GetColor(BrowserThemeProvider::COLOR_NTP_LINK); |
| 102 SkColor color_section = | 123 SkColor color_section = |
| 103 tp->GetColor(BrowserThemeProvider::COLOR_NTP_SECTION); | 124 tp->GetColor(BrowserThemeProvider::COLOR_NTP_SECTION); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 175 |
| 155 // Get our template. | 176 // Get our template. |
| 156 static const StringPiece new_tab_theme_css( | 177 static const StringPiece new_tab_theme_css( |
| 157 ResourceBundle::GetSharedInstance().GetRawDataResource( | 178 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 158 IDR_NEW_TAB_THEME_CSS)); | 179 IDR_NEW_TAB_THEME_CSS)); |
| 159 | 180 |
| 160 // Create the string from our template and the replacements. | 181 // Create the string from our template and the replacements. |
| 161 string16 format_string = ASCIIToUTF16(new_tab_theme_css.as_string()); | 182 string16 format_string = ASCIIToUTF16(new_tab_theme_css.as_string()); |
| 162 const std::string css_string = UTF16ToASCII(ReplaceStringPlaceholders( | 183 const std::string css_string = UTF16ToASCII(ReplaceStringPlaceholders( |
| 163 format_string, subst, NULL)); | 184 format_string, subst, NULL)); |
| 164 const std::string css_string2 = UTF16ToASCII(ReplaceStringPlaceholders( | 185 new_tab_css_ = UTF16ToASCII(ReplaceStringPlaceholders( |
| 165 ASCIIToUTF16(css_string), subst2, NULL)); | 186 ASCIIToUTF16(css_string), subst2, NULL)); |
| 166 | |
| 167 // Convert to a format appropriate for sending. | |
| 168 scoped_refptr<RefCountedBytes> css_bytes(new RefCountedBytes); | |
| 169 css_bytes->data.resize(css_string2.size()); | |
| 170 std::copy(css_string2.begin(), css_string2.end(), css_bytes->data.begin()); | |
| 171 | |
| 172 // Send. | |
| 173 SendResponse(request_id, css_bytes); | |
| 174 } | 187 } |
| 175 | 188 |
| 176 void DOMUIThemeSource::SendNewIncognitoTabCSS(int request_id) { | 189 void DOMUIThemeSource::InitNewIncognitoTabCSS() { |
| 177 ThemeProvider* tp = profile_->GetThemeProvider(); | 190 ThemeProvider* tp = profile_->GetThemeProvider(); |
| 178 DCHECK(tp); | 191 DCHECK(tp); |
| 179 | 192 |
| 180 // Get our theme colors | 193 // Get our theme colors |
| 181 SkColor color_background = | 194 SkColor color_background = |
| 182 tp->GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND); | 195 tp->GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND); |
| 183 | 196 |
| 184 // Generate the replacements. | 197 // Generate the replacements. |
| 185 std::vector<string16> subst; | 198 std::vector<string16> subst; |
| 186 | 199 |
| 187 // Cache-buster for background. | 200 // Cache-buster for background. |
| 188 subst.push_back(WideToUTF16( | 201 subst.push_back(WideToUTF16( |
| 189 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID))); // $1 | 202 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID))); // $1 |
| 190 | 203 |
| 191 // Colors. | 204 // Colors. |
| 192 subst.push_back(SkColorToRGBAString(color_background)); // $2 | 205 subst.push_back(SkColorToRGBAString(color_background)); // $2 |
| 193 subst.push_back(UTF8ToUTF16(GetNewTabBackgroundCSS(false))); // $3 | 206 subst.push_back(UTF8ToUTF16(GetNewTabBackgroundCSS(false))); // $3 |
| 194 subst.push_back(UTF8ToUTF16(GetNewTabBackgroundCSS(true))); // $4 | 207 subst.push_back(UTF8ToUTF16(GetNewTabBackgroundCSS(true))); // $4 |
| 195 subst.push_back(UTF8ToUTF16(GetNewTabBackgroundTilingCSS())); // $5 | 208 subst.push_back(UTF8ToUTF16(GetNewTabBackgroundTilingCSS())); // $5 |
| 196 | 209 |
| 197 // Get our template. | 210 // Get our template. |
| 198 static const StringPiece new_tab_theme_css( | 211 static const StringPiece new_tab_theme_css( |
| 199 ResourceBundle::GetSharedInstance().GetRawDataResource( | 212 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 200 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); | 213 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); |
| 201 | 214 |
| 202 // Create the string from our template and the replacements. | 215 // Create the string from our template and the replacements. |
| 203 string16 format_string = ASCIIToUTF16(new_tab_theme_css.as_string()); | 216 string16 format_string = ASCIIToUTF16(new_tab_theme_css.as_string()); |
| 204 const std::string css_string = UTF16ToASCII(ReplaceStringPlaceholders( | 217 new_incognito_tab_css_ = UTF16ToASCII(ReplaceStringPlaceholders( |
| 205 format_string, subst, NULL)); | 218 format_string, subst, NULL)); |
| 219 } |
| 206 | 220 |
| 221 void DOMUIThemeSource::SendNewTabCSS(int request_id, |
| 222 const std::string& css_string) { |
| 207 // Convert to a format appropriate for sending. | 223 // Convert to a format appropriate for sending. |
| 208 scoped_refptr<RefCountedBytes> css_bytes(new RefCountedBytes); | 224 scoped_refptr<RefCountedBytes> css_bytes(new RefCountedBytes); |
| 209 css_bytes->data.resize(css_string.size()); | 225 css_bytes->data.resize(css_string.size()); |
| 210 std::copy(css_string.begin(), css_string.end(), css_bytes->data.begin()); | 226 std::copy(css_string.begin(), css_string.end(), css_bytes->data.begin()); |
| 211 | 227 |
| 212 // Send. | 228 // Send. |
| 213 SendResponse(request_id, css_bytes); | 229 SendResponse(request_id, css_bytes); |
| 214 } | 230 } |
| 215 | 231 |
| 216 void DOMUIThemeSource::SendThemeBitmap(int request_id, int resource_id) { | 232 void DOMUIThemeSource::SendThemeBitmap(int request_id, int resource_id) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 return BrowserThemeProvider::AlignmentToString(alignment); | 277 return BrowserThemeProvider::AlignmentToString(alignment); |
| 262 } | 278 } |
| 263 | 279 |
| 264 std::string DOMUIThemeSource::GetNewTabBackgroundTilingCSS() { | 280 std::string DOMUIThemeSource::GetNewTabBackgroundTilingCSS() { |
| 265 int repeat_mode; | 281 int repeat_mode; |
| 266 profile_->GetThemeProvider()->GetDisplayProperty( | 282 profile_->GetThemeProvider()->GetDisplayProperty( |
| 267 BrowserThemeProvider::NTP_BACKGROUND_TILING, &repeat_mode); | 283 BrowserThemeProvider::NTP_BACKGROUND_TILING, &repeat_mode); |
| 268 return BrowserThemeProvider::TilingToString(repeat_mode); | 284 return BrowserThemeProvider::TilingToString(repeat_mode); |
| 269 } | 285 } |
| 270 | 286 |
| OLD | NEW |