Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: chrome/browser/ui/webui/ntp/ntp_resource_cache.cc

Issue 7397021: Re-land r93365 - add RefCountedString (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unit_tests Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/ntp/ntp_resource_cache.h" 5 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 162
163 // Watch for pref changes that cause us to need to invalidate the HTML cache. 163 // Watch for pref changes that cause us to need to invalidate the HTML cache.
164 pref_change_registrar_.Init(profile_->GetPrefs()); 164 pref_change_registrar_.Init(profile_->GetPrefs());
165 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); 165 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this);
166 pref_change_registrar_.Add(prefs::kEnableBookmarkBar, this); 166 pref_change_registrar_.Add(prefs::kEnableBookmarkBar, this);
167 pref_change_registrar_.Add(prefs::kNTPShownSections, this); 167 pref_change_registrar_.Add(prefs::kNTPShownSections, this);
168 } 168 }
169 169
170 NTPResourceCache::~NTPResourceCache() {} 170 NTPResourceCache::~NTPResourceCache() {}
171 171
172 RefCountedBytes* NTPResourceCache::GetNewTabHTML(bool is_incognito) { 172 RefCountedMemory* NTPResourceCache::GetNewTabHTML(bool is_incognito) {
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
174 if (is_incognito) { 174 if (is_incognito) {
175 if (!new_tab_incognito_html_.get()) 175 if (!new_tab_incognito_html_.get())
176 CreateNewTabIncognitoHTML(); 176 CreateNewTabIncognitoHTML();
177 } else { 177 } else {
178 if (!new_tab_html_.get()) 178 if (!new_tab_html_.get())
179 CreateNewTabHTML(); 179 CreateNewTabHTML();
180 } 180 }
181 return is_incognito ? new_tab_incognito_html_.get() 181 return is_incognito ? new_tab_incognito_html_.get()
182 : new_tab_html_.get(); 182 : new_tab_html_.get();
183 } 183 }
184 184
185 RefCountedBytes* NTPResourceCache::GetNewTabCSS(bool is_incognito) { 185 RefCountedMemory* NTPResourceCache::GetNewTabCSS(bool is_incognito) {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
187 if (is_incognito) { 187 if (is_incognito) {
188 if (!new_tab_incognito_css_.get()) 188 if (!new_tab_incognito_css_.get())
189 CreateNewTabIncognitoCSS(); 189 CreateNewTabIncognitoCSS();
190 } else { 190 } else {
191 if (!new_tab_css_.get()) 191 if (!new_tab_css_.get())
192 CreateNewTabCSS(); 192 CreateNewTabCSS();
193 } 193 }
194 return is_incognito ? new_tab_incognito_css_.get() 194 return is_incognito ? new_tab_incognito_css_.get()
195 : new_tab_css_.get(); 195 : new_tab_css_.get();
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 249
250 ChromeURLDataManager::DataSource::SetFontAndTextDirection(&localized_strings); 250 ChromeURLDataManager::DataSource::SetFontAndTextDirection(&localized_strings);
251 251
252 static const base::StringPiece incognito_tab_html( 252 static const base::StringPiece incognito_tab_html(
253 ResourceBundle::GetSharedInstance().GetRawDataResource( 253 ResourceBundle::GetSharedInstance().GetRawDataResource(
254 new_tab_html_idr)); 254 new_tab_html_idr));
255 255
256 std::string full_html = jstemplate_builder::GetI18nTemplateHtml( 256 std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
257 incognito_tab_html, &localized_strings); 257 incognito_tab_html, &localized_strings);
258 258
259 new_tab_incognito_html_ = new RefCountedBytes; 259 new_tab_incognito_html_ = base::RefCountedString::TakeString(&full_html);
260 new_tab_incognito_html_->data.resize(full_html.size());
261 std::copy(full_html.begin(), full_html.end(),
262 new_tab_incognito_html_->data.begin());
263 } 260 }
264 261
265 void NTPResourceCache::CreateNewTabHTML() { 262 void NTPResourceCache::CreateNewTabHTML() {
266 // TODO(estade): these strings should be defined in their relevant handlers 263 // TODO(estade): these strings should be defined in their relevant handlers
267 // (in GetLocalizedValues) and should have more legible names. 264 // (in GetLocalizedValues) and should have more legible names.
268 // Show the profile name in the title and most visited labels if the current 265 // Show the profile name in the title and most visited labels if the current
269 // profile is not the default. 266 // profile is not the default.
270 string16 apps = l10n_util::GetStringUTF16(IDS_NEW_TAB_APPS); 267 string16 apps = l10n_util::GetStringUTF16(IDS_NEW_TAB_APPS);
271 string16 title = l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE); 268 string16 title = l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE);
272 string16 most_visited = l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED); 269 string16 most_visited = l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED);
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 full_html.append(json_html); 422 full_html.append(json_html);
426 size_t after_offset = pos + template_data_placeholder.size(); 423 size_t after_offset = pos + template_data_placeholder.size();
427 full_html.append(new_tab_html.data() + after_offset, 424 full_html.append(new_tab_html.data() + after_offset,
428 new_tab_html.size() - after_offset); 425 new_tab_html.size() - after_offset);
429 } else { 426 } else {
430 NOTREACHED(); 427 NOTREACHED();
431 full_html.assign(new_tab_html.data(), new_tab_html.size()); 428 full_html.assign(new_tab_html.data(), new_tab_html.size());
432 } 429 }
433 } 430 }
434 431
435 new_tab_html_ = new RefCountedBytes; 432 new_tab_html_ = base::RefCountedString::TakeString(&full_html);
436 new_tab_html_->data.resize(full_html.size());
437 std::copy(full_html.begin(), full_html.end(), new_tab_html_->data.begin());
438 } 433 }
439 434
440 void NTPResourceCache::CreateNewTabIncognitoCSS() { 435 void NTPResourceCache::CreateNewTabIncognitoCSS() {
441 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 436 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
442 DCHECK(tp); 437 DCHECK(tp);
443 438
444 // Get our theme colors 439 // Get our theme colors
445 SkColor color_background = 440 SkColor color_background =
446 tp->GetColor(ThemeService::COLOR_NTP_BACKGROUND); 441 tp->GetColor(ThemeService::COLOR_NTP_BACKGROUND);
447 442
(...skipping 12 matching lines...) Expand all
460 455
461 // Get our template. 456 // Get our template.
462 static const base::StringPiece new_tab_theme_css( 457 static const base::StringPiece new_tab_theme_css(
463 ResourceBundle::GetSharedInstance().GetRawDataResource( 458 ResourceBundle::GetSharedInstance().GetRawDataResource(
464 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); 459 IDR_NEW_INCOGNITO_TAB_THEME_CSS));
465 460
466 // Create the string from our template and the replacements. 461 // Create the string from our template and the replacements.
467 std::string full_css = ReplaceStringPlaceholders( 462 std::string full_css = ReplaceStringPlaceholders(
468 new_tab_theme_css, subst, NULL); 463 new_tab_theme_css, subst, NULL);
469 464
470 new_tab_incognito_css_ = new RefCountedBytes; 465 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css);
471 new_tab_incognito_css_->data.resize(full_css.size());
472 std::copy(full_css.begin(), full_css.end(),
473 new_tab_incognito_css_->data.begin());
474 } 466 }
475 467
476 void NTPResourceCache::CreateNewTabCSS() { 468 void NTPResourceCache::CreateNewTabCSS() {
477 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 469 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
478 DCHECK(tp); 470 DCHECK(tp);
479 471
480 // Get our theme colors 472 // Get our theme colors
481 SkColor color_background = 473 SkColor color_background =
482 tp->GetColor(ThemeService::COLOR_NTP_BACKGROUND); 474 tp->GetColor(ThemeService::COLOR_NTP_BACKGROUND);
483 SkColor color_text = tp->GetColor(ThemeService::COLOR_NTP_TEXT); 475 SkColor color_text = tp->GetColor(ThemeService::COLOR_NTP_TEXT);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 int ntp_css_resource_id = 550 int ntp_css_resource_id =
559 CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4) ? 551 CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4) ?
560 IDR_NEW_TAB_4_THEME_CSS : IDR_NEW_TAB_THEME_CSS; 552 IDR_NEW_TAB_4_THEME_CSS : IDR_NEW_TAB_THEME_CSS;
561 static const base::StringPiece new_tab_theme_css( 553 static const base::StringPiece new_tab_theme_css(
562 ResourceBundle::GetSharedInstance().GetRawDataResource( 554 ResourceBundle::GetSharedInstance().GetRawDataResource(
563 ntp_css_resource_id)); 555 ntp_css_resource_id));
564 556
565 // Create the string from our template and the replacements. 557 // Create the string from our template and the replacements.
566 std::string css_string; 558 std::string css_string;
567 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); 559 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL);
568 new_tab_css_ = new RefCountedBytes; 560 new_tab_css_ = base::RefCountedString::TakeString(&css_string);
569 new_tab_css_->data.resize(css_string.size());
570 std::copy(css_string.begin(), css_string.end(),
571 new_tab_css_->data.begin());
572 } 561 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698