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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
10 #include "base/command_line.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/values.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/extensions/extension_util.h"
21 #include "chrome/browser/first_run/first_run.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/search/search.h"
24 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/browser/sync/profile_sync_service.h"
26 #include "chrome/browser/sync/profile_sync_service_factory.h"
27 #include "chrome/browser/themes/theme_properties.h"
28 #include "chrome/browser/themes/theme_service.h"
29 #include "chrome/browser/themes/theme_service_factory.h"
30 #include "chrome/browser/ui/app_list/app_list_util.h"
31 #include "chrome/browser/ui/apps/app_info_dialog.h"
32 #include "chrome/browser/ui/bookmarks/bookmark_bar_constants.h"
33 #include "chrome/browser/ui/sync/sync_promo_ui.h"
34 #include "chrome/browser/ui/webui/app_launcher_login_handler.h"
35 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h"
36 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
37 #include "chrome/browser/web_resource/notification_promo_helper.h"
38 #include "chrome/common/chrome_switches.h"
39 #include "chrome/common/pref_names.h"
40 #include "chrome/common/url_constants.h"
41 #include "chrome/grit/chromium_strings.h"
42 #include "chrome/grit/generated_resources.h"
43 #include "chrome/grit/locale_settings.h"
44 #include "components/google/core/browser/google_util.h"
45 #include "components/signin/core/browser/signin_manager.h"
46 #include "components/web_resource/notification_promo.h"
47 #include "content/public/browser/browser_thread.h"
48 #include "content/public/browser/notification_service.h"
49 #include "content/public/browser/render_process_host.h"
50 #include "extensions/common/extension.h"
51 #include "extensions/common/extension_urls.h"
52 #include "grit/browser_resources.h"
53 #include "grit/components_strings.h"
54 #include "grit/theme_resources.h"
55 #include "ui/base/l10n/l10n_util.h"
56 #include "ui/base/resource/resource_bundle.h"
57 #include "ui/base/template_expressions.h"
58 #include "ui/base/theme_provider.h"
59 #include "ui/base/webui/jstemplate_builder.h"
60 #include "ui/base/webui/web_ui_util.h"
61 #include "ui/gfx/animation/animation.h"
62 #include "ui/gfx/color_utils.h"
64 #if defined(OS_CHROMEOS)
65 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
66 #include "chromeos/chromeos_switches.h"
69 #if defined(OS_MACOSX)
70 #include "chrome/browser/platform_util.h"
73 using content::BrowserThread
;
77 // The URL for the the Learn More page shown on incognito new tab.
78 const char kLearnMoreIncognitoUrl
[] =
79 #if defined(OS_CHROMEOS)
80 "https://support.google.com/chromebook/?p=incognito";
82 "https://support.google.com/chrome/?p=incognito";
85 // The URL for the Learn More page shown on guest session new tab.
86 const char kLearnMoreGuestSessionUrl
[] =
87 #if defined(OS_CHROMEOS)
88 "https://support.google.com/chromebook/answer/1057090";
90 "https://support.google.com/chrome/?p=ui_guest";
93 std::string
SkColorToRGBAString(SkColor color
) {
94 // We convert the alpha using DoubleToString because StringPrintf will use
95 // locale specific formatters (e.g., use , instead of . in German).
96 return base::StringPrintf(
101 base::DoubleToString(SkColorGetA(color
) / 255.0).c_str());
104 // Creates an rgb string for an SkColor, but leaves the alpha blank so that the
105 // css can fill it in.
106 std::string
SkColorToRGBComponents(SkColor color
) {
107 return base::StringPrintf(
114 SkColor
GetThemeColor(ui::ThemeProvider
* tp
, int id
) {
115 SkColor color
= tp
->GetColor(id
);
116 // If web contents are being inverted because the system is in high-contrast
117 // mode, any system theme colors we use must be inverted too to cancel out.
118 return color_utils::IsInvertedColorScheme() ?
119 color_utils::InvertColor(color
) : color
;
122 // Get the CSS string for the background position on the new tab page for the
123 // states when the bar is attached or detached.
124 std::string
GetNewTabBackgroundCSS(const ui::ThemeProvider
* theme_provider
,
126 // TODO(glen): This is a quick workaround to hide the notused.png image when
127 // no image is provided - we don't have time right now to figure out why
128 // this is painting as white.
129 // http://crbug.com/17593
130 if (!theme_provider
->HasCustomImage(IDR_THEME_NTP_BACKGROUND
)) {
134 int alignment
= theme_provider
->GetDisplayProperty(
135 ThemeProperties::NTP_BACKGROUND_ALIGNMENT
);
138 return ThemeProperties::AlignmentToString(alignment
);
140 if (alignment
& ThemeProperties::ALIGN_TOP
) {
141 // The bar is detached, so we must offset the background by the bar size
142 // if it's a top-aligned bar.
143 int offset
= chrome::kNTPBookmarkBarHeight
;
145 if (alignment
& ThemeProperties::ALIGN_LEFT
)
146 return "left " + base::IntToString(-offset
) + "px";
147 else if (alignment
& ThemeProperties::ALIGN_RIGHT
)
148 return "right " + base::IntToString(-offset
) + "px";
149 return "center " + base::IntToString(-offset
) + "px";
152 return ThemeProperties::AlignmentToString(alignment
);
155 // How the background image on the new tab page should be tiled (see tiling
156 // masks in theme_service.h).
157 std::string
GetNewTabBackgroundTilingCSS(
158 const ui::ThemeProvider
* theme_provider
) {
159 int repeat_mode
= theme_provider
->GetDisplayProperty(
160 ThemeProperties::NTP_BACKGROUND_TILING
);
161 return ThemeProperties::TilingToString(repeat_mode
);
166 NTPResourceCache::NTPResourceCache(Profile
* profile
)
167 : profile_(profile
), is_swipe_tracking_from_scroll_events_enabled_(false),
168 should_show_apps_page_(NewTabUI::ShouldShowApps()),
169 should_show_other_devices_menu_(true) {
170 registrar_
.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED
,
171 content::Source
<ThemeService
>(
172 ThemeServiceFactory::GetForProfile(profile
)));
173 registrar_
.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED
,
174 content::NotificationService::AllSources());
176 web_resource::PromoResourceService
* promo_service
=
177 g_browser_process
->promo_resource_service();
179 promo_resource_subscription_
= promo_service
->RegisterStateChangedCallback(
180 base::Bind(&NTPResourceCache::Invalidate
, base::Unretained(this)));
183 base::Closure callback
= base::Bind(&NTPResourceCache::OnPreferenceChanged
,
184 base::Unretained(this));
186 // Watch for pref changes that cause us to need to invalidate the HTML cache.
187 profile_pref_change_registrar_
.Init(profile_
->GetPrefs());
188 profile_pref_change_registrar_
.Add(bookmarks::prefs::kShowBookmarkBar
,
190 profile_pref_change_registrar_
.Add(prefs::kNtpShownPage
, callback
);
191 profile_pref_change_registrar_
.Add(prefs::kSignInPromoShowNTPBubble
,
193 profile_pref_change_registrar_
.Add(prefs::kHideWebStoreIcon
, callback
);
195 // Some tests don't have a local state.
196 #if defined(ENABLE_APP_LIST)
197 if (g_browser_process
->local_state()) {
198 local_state_pref_change_registrar_
.Init(g_browser_process
->local_state());
199 local_state_pref_change_registrar_
.Add(prefs::kShowAppLauncherPromo
,
201 local_state_pref_change_registrar_
.Add(
202 prefs::kAppLauncherHasBeenEnabled
, callback
);
207 NTPResourceCache::~NTPResourceCache() {}
209 bool NTPResourceCache::NewTabCacheNeedsRefresh() {
210 #if defined(OS_MACOSX)
211 // Invalidate if the current value is different from the cached value.
212 bool is_enabled
= platform_util::IsSwipeTrackingFromScrollEventsEnabled();
213 if (is_enabled
!= is_swipe_tracking_from_scroll_events_enabled_
) {
214 is_swipe_tracking_from_scroll_events_enabled_
= is_enabled
;
218 bool should_show_apps_page
= NewTabUI::ShouldShowApps();
219 if (should_show_apps_page
!= should_show_apps_page_
) {
220 should_show_apps_page_
= should_show_apps_page
;
226 NTPResourceCache::WindowType
NTPResourceCache::GetWindowType(
227 Profile
* profile
, content::RenderProcessHost
* render_host
) {
228 if (profile
->IsGuestSession()) {
229 return NTPResourceCache::GUEST
;
230 } else if (render_host
) {
231 // Sometimes the |profile| is the parent (non-incognito) version of the user
232 // so we check the |render_host| if it is provided.
233 if (render_host
->GetBrowserContext()->IsOffTheRecord())
234 return NTPResourceCache::INCOGNITO
;
235 } else if (profile
->IsOffTheRecord()) {
236 return NTPResourceCache::INCOGNITO
;
238 return NTPResourceCache::NORMAL
;
241 base::RefCountedMemory
* NTPResourceCache::GetNewTabHTML(WindowType win_type
) {
242 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
243 if (win_type
== GUEST
) {
244 if (!new_tab_guest_html_
.get())
245 CreateNewTabGuestHTML();
246 return new_tab_guest_html_
.get();
247 } else if (win_type
== INCOGNITO
) {
248 if (!new_tab_incognito_html_
.get())
249 CreateNewTabIncognitoHTML();
250 return new_tab_incognito_html_
.get();
252 // Refresh the cached HTML if necessary.
253 // NOTE: NewTabCacheNeedsRefresh() must be called every time the new tab
254 // HTML is fetched, because it needs to initialize cached values.
255 if (NewTabCacheNeedsRefresh() || !new_tab_html_
.get())
257 return new_tab_html_
.get();
261 base::RefCountedMemory
* NTPResourceCache::GetNewTabCSS(WindowType win_type
) {
262 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
263 if (win_type
== GUEST
) {
264 if (!new_tab_guest_css_
.get())
265 CreateNewTabGuestCSS();
266 return new_tab_guest_css_
.get();
267 } else if (win_type
== INCOGNITO
) {
268 if (!new_tab_incognito_css_
.get())
269 CreateNewTabIncognitoCSS();
270 return new_tab_incognito_css_
.get();
272 if (!new_tab_css_
.get())
274 return new_tab_css_
.get();
278 void NTPResourceCache::Observe(int type
,
279 const content::NotificationSource
& source
,
280 const content::NotificationDetails
& details
) {
281 // Invalidate the cache.
282 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED
== type
||
283 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED
== type
) {
290 void NTPResourceCache::OnPreferenceChanged() {
291 // A change occurred to one of the preferences we care about, so flush the
293 new_tab_incognito_html_
= NULL
;
294 new_tab_html_
= NULL
;
298 void NTPResourceCache::Invalidate() {
299 new_tab_incognito_html_
= nullptr;
300 new_tab_html_
= nullptr;
301 new_tab_incognito_css_
= nullptr;
302 // TODO(dbeam): Check if it is necessary to clear the CSS on promo changes.
303 new_tab_css_
= nullptr;
306 void NTPResourceCache::CreateNewTabIncognitoHTML() {
307 base::DictionaryValue localized_strings
;
308 localized_strings
.SetString("title",
309 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE
));
310 int new_tab_description_ids
= IDS_NEW_TAB_OTR_DESCRIPTION
;
311 int new_tab_heading_ids
= IDS_NEW_TAB_OTR_HEADING
;
312 int new_tab_link_ids
= IDS_NEW_TAB_OTR_LEARN_MORE_LINK
;
313 int new_tab_warning_ids
= IDS_NEW_TAB_OTR_MESSAGE_WARNING
;
314 int new_tab_html_idr
= IDR_INCOGNITO_TAB_HTML
;
315 const char* new_tab_link
= kLearnMoreIncognitoUrl
;
317 if (profile_
->IsGuestSession()) {
318 localized_strings
.SetString("guestTabDescription",
319 l10n_util::GetStringUTF16(new_tab_description_ids
));
320 localized_strings
.SetString("guestTabHeading",
321 l10n_util::GetStringUTF16(new_tab_heading_ids
));
323 localized_strings
.SetString("incognitoTabDescription",
324 l10n_util::GetStringUTF16(new_tab_description_ids
));
325 localized_strings
.SetString("incognitoTabHeading",
326 l10n_util::GetStringUTF16(new_tab_heading_ids
));
327 localized_strings
.SetString("incognitoTabWarning",
328 l10n_util::GetStringUTF16(new_tab_warning_ids
));
331 localized_strings
.SetString("learnMore",
332 l10n_util::GetStringUTF16(new_tab_link_ids
));
333 localized_strings
.SetString("learnMoreLink", new_tab_link
);
335 bool bookmark_bar_attached
=
336 profile_
->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar
);
337 localized_strings
.SetBoolean("bookmarkbarattached", bookmark_bar_attached
);
339 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
340 webui::SetLoadTimeDataDefaults(app_locale
, &localized_strings
);
342 static const base::StringPiece
incognito_tab_html(
343 ResourceBundle::GetSharedInstance().GetRawDataResource(
346 std::string full_html
= webui::GetI18nTemplateHtml(
347 incognito_tab_html
, &localized_strings
);
349 new_tab_incognito_html_
= base::RefCountedString::TakeString(&full_html
);
352 void NTPResourceCache::CreateNewTabGuestHTML() {
353 base::DictionaryValue localized_strings
;
354 localized_strings
.SetString("title",
355 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE
));
356 const char* guest_tab_link
= kLearnMoreGuestSessionUrl
;
357 int guest_tab_ids
= IDR_GUEST_TAB_HTML
;
358 int guest_tab_description_ids
= IDS_NEW_TAB_GUEST_SESSION_DESCRIPTION
;
359 int guest_tab_heading_ids
= IDS_NEW_TAB_GUEST_SESSION_HEADING
;
360 int guest_tab_link_ids
= IDS_NEW_TAB_GUEST_SESSION_LEARN_MORE_LINK
;
362 #if defined(OS_CHROMEOS)
363 guest_tab_ids
= IDR_GUEST_SESSION_TAB_HTML
;
365 policy::BrowserPolicyConnectorChromeOS
* connector
=
366 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
367 std::string enterprise_domain
= connector
->GetEnterpriseDomain();
369 if (!enterprise_domain
.empty()) {
370 // Device is enterprise enrolled.
371 localized_strings
.SetString("enterpriseInfoVisible", "true");
372 base::string16 enterprise_info
= l10n_util::GetStringFUTF16(
373 IDS_DEVICE_OWNED_BY_NOTICE
,
374 base::UTF8ToUTF16(enterprise_domain
));
375 localized_strings
.SetString("enterpriseInfoMessage", enterprise_info
);
376 localized_strings
.SetString("enterpriseLearnMore",
377 l10n_util::GetStringUTF16(IDS_LEARN_MORE
));
378 localized_strings
.SetString("enterpriseInfoHintLink",
379 chrome::kLearnMoreEnterpriseURL
);
381 localized_strings
.SetString("enterpriseInfoVisible", "false");
385 localized_strings
.SetString("guestTabDescription",
386 l10n_util::GetStringUTF16(guest_tab_description_ids
));
387 localized_strings
.SetString("guestTabHeading",
388 l10n_util::GetStringUTF16(guest_tab_heading_ids
));
389 localized_strings
.SetString("learnMore",
390 l10n_util::GetStringUTF16(guest_tab_link_ids
));
391 localized_strings
.SetString("learnMoreLink", guest_tab_link
);
393 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
394 webui::SetLoadTimeDataDefaults(app_locale
, &localized_strings
);
396 static const base::StringPiece
guest_tab_html(
397 ResourceBundle::GetSharedInstance().GetRawDataResource(guest_tab_ids
));
399 std::string full_html
= webui::GetI18nTemplateHtml(
400 guest_tab_html
, &localized_strings
);
402 new_tab_guest_html_
= base::RefCountedString::TakeString(&full_html
);
405 void NTPResourceCache::CreateNewTabHTML() {
406 // TODO(estade): these strings should be defined in their relevant handlers
407 // (in GetLocalizedValues) and should have more legible names.
408 // Show the profile name in the title and most visited labels if the current
409 // profile is not the default.
410 PrefService
* prefs
= profile_
->GetPrefs();
411 base::DictionaryValue load_time_data
;
412 load_time_data
.SetBoolean("bookmarkbarattached",
413 prefs
->GetBoolean(bookmarks::prefs::kShowBookmarkBar
));
414 load_time_data
.SetBoolean("showAppLauncherPromo",
415 ShouldShowAppLauncherPromo());
416 load_time_data
.SetString("title",
417 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE
));
418 load_time_data
.SetString("webStoreTitle",
419 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE
));
420 load_time_data
.SetString("webStoreTitleShort",
421 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE_SHORT
));
422 load_time_data
.SetString("attributionintro",
423 l10n_util::GetStringUTF16(IDS_NEW_TAB_ATTRIBUTION_INTRO
));
424 load_time_data
.SetString("appuninstall",
425 l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL
));
426 load_time_data
.SetString("appoptions",
427 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_OPTIONS
));
428 load_time_data
.SetString("appdetails",
429 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_DETAILS
));
430 load_time_data
.SetString("appinfodialog",
431 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_SHOW_INFO
));
432 load_time_data
.SetString("appcreateshortcut",
433 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_CREATE_SHORTCUT
));
434 load_time_data
.SetString("appDefaultPageName",
435 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME
));
436 load_time_data
.SetString("applaunchtypepinned",
437 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_PINNED
));
438 load_time_data
.SetString("applaunchtyperegular",
439 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_REGULAR
));
440 load_time_data
.SetString("applaunchtypewindow",
441 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW
));
442 load_time_data
.SetString("applaunchtypefullscreen",
443 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN
));
444 load_time_data
.SetString("syncpromotext",
445 l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL
));
446 load_time_data
.SetString("syncLinkText",
447 l10n_util::GetStringUTF16(IDS_SYNC_ADVANCED_OPTIONS
));
448 load_time_data
.SetBoolean("shouldShowSyncLogin",
449 AppLauncherLoginHandler::ShouldShow(profile_
));
450 load_time_data
.SetString("learnMore",
451 l10n_util::GetStringUTF16(IDS_LEARN_MORE
));
452 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
453 load_time_data
.SetString("webStoreLink",
454 google_util::AppendGoogleLocaleParam(
455 GURL(extension_urls::GetWebstoreLaunchURL()), app_locale
).spec());
456 load_time_data
.SetString("appInstallHintText",
457 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_INSTALL_HINT_LABEL
));
458 load_time_data
.SetString("learn_more",
459 l10n_util::GetStringUTF16(IDS_LEARN_MORE
));
460 load_time_data
.SetString("tile_grid_screenreader_accessible_description",
461 l10n_util::GetStringUTF16(IDS_NEW_TAB_TILE_GRID_ACCESSIBLE_DESCRIPTION
));
462 load_time_data
.SetString("page_switcher_change_title",
463 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_CHANGE_TITLE
));
464 load_time_data
.SetString("page_switcher_same_title",
465 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_SAME_TITLE
));
466 load_time_data
.SetString("appsPromoTitle",
467 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_APPS_PROMO_TITLE
));
468 // On Mac OS X 10.7+, horizontal scrolling can be treated as a back or
469 // forward gesture. Pass through a flag that indicates whether or not that
470 // feature is enabled.
471 load_time_data
.SetBoolean("isSwipeTrackingFromScrollEventsEnabled",
472 is_swipe_tracking_from_scroll_events_enabled_
);
474 load_time_data
.SetBoolean("showApps", should_show_apps_page_
);
475 load_time_data
.SetBoolean("showWebStoreIcon",
476 !prefs
->GetBoolean(prefs::kHideWebStoreIcon
));
478 load_time_data
.SetBoolean("enableNewBookmarkApps",
479 extensions::util::IsNewBookmarkAppsEnabled());
481 load_time_data
.SetBoolean("canHostedAppsOpenInWindows",
482 extensions::util::CanHostedAppsOpenInWindows());
484 load_time_data
.SetBoolean("canShowAppInfoDialog",
485 CanShowAppInfoDialog());
487 NewTabPageHandler::GetLocalizedValues(profile_
, &load_time_data
);
488 AppLauncherLoginHandler::GetLocalizedValues(profile_
, &load_time_data
);
490 webui::SetLoadTimeDataDefaults(app_locale
, &load_time_data
);
492 // Control fade and resize animations.
493 load_time_data
.SetBoolean("anim",
494 gfx::Animation::ShouldRenderRichAnimation());
496 // Disable the promo if this is the first run, otherwise set the promo string
497 // for display if there is a valid outstanding promo.
498 if (first_run::IsChromeFirstRun()) {
499 web_resource::HandleNotificationPromoClosed(
500 web_resource::NotificationPromo::NTP_NOTIFICATION_PROMO
);
502 web_resource::NotificationPromo
notification_promo(
503 g_browser_process
->local_state());
504 notification_promo
.InitFromPrefs(
505 web_resource::NotificationPromo::NTP_NOTIFICATION_PROMO
);
506 if (notification_promo
.CanShow()) {
507 load_time_data
.SetString("notificationPromoText",
508 notification_promo
.promo_text());
509 DVLOG(1) << "Notification promo:" << notification_promo
.promo_text();
512 web_resource::NotificationPromo
bubble_promo(
513 g_browser_process
->local_state());
514 bubble_promo
.InitFromPrefs(
515 web_resource::NotificationPromo::NTP_BUBBLE_PROMO
);
516 if (bubble_promo
.CanShow()) {
517 load_time_data
.SetString("bubblePromoText",
518 bubble_promo
.promo_text());
519 DVLOG(1) << "Bubble promo:" << bubble_promo
.promo_text();
523 load_time_data
.SetBoolean(
525 SigninManagerFactory::GetForProfile(profile_
)->IsAuthenticated());
527 // Load the new tab page appropriate for this build.
528 base::StringPiece
new_tab_html(ResourceBundle::GetSharedInstance().
529 GetRawDataResource(IDR_NEW_TAB_4_HTML
));
530 std::string full_html
=
531 webui::GetI18nTemplateHtml(new_tab_html
, &load_time_data
);
532 new_tab_html_
= base::RefCountedString::TakeString(&full_html
);
535 void NTPResourceCache::CreateNewTabIncognitoCSS() {
536 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
539 // Get our theme colors
540 SkColor color_background
=
541 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_BACKGROUND
);
543 // Generate the replacements.
544 std::map
<base::StringPiece
, std::string
> substitutions
;
546 // Cache-buster for background.
547 substitutions
["themeId"] =
548 profile_
->GetPrefs()->GetString(prefs::kCurrentThemeID
);
551 substitutions
["colorBackground"] = SkColorToRGBAString(color_background
);
552 substitutions
["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp
, false);
553 substitutions
["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp
, true);
554 substitutions
["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp
);
557 static const base::StringPiece
new_tab_theme_css(
558 ResourceBundle::GetSharedInstance().GetRawDataResource(
559 IDR_NEW_INCOGNITO_TAB_THEME_CSS
));
561 // Create the string from our template and the replacements.
562 std::string full_css
=
563 ui::ReplaceTemplateExpressions(new_tab_theme_css
, substitutions
);
565 new_tab_incognito_css_
= base::RefCountedString::TakeString(&full_css
);
568 void NTPResourceCache::CreateNewTabGuestCSS() {
569 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
572 // Get our theme colors
573 SkColor color_background
=
574 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_BACKGROUND
);
576 // Generate the replacements.
577 std::map
<base::StringPiece
, std::string
> substitutions
;
579 // Cache-buster for background.
580 substitutions
["themeId"] =
581 profile_
->GetPrefs()->GetString(prefs::kCurrentThemeID
);
584 substitutions
["colorBackground"] = SkColorToRGBAString(color_background
);
585 substitutions
["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp
, false);
586 substitutions
["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp
, true);
587 substitutions
["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp
);
590 static const base::StringPiece
new_tab_theme_css(
591 ResourceBundle::GetSharedInstance().GetRawDataResource(
592 IDR_NEW_INCOGNITO_TAB_THEME_CSS
));
594 // Create the string from our template and the replacements.
595 std::string full_css
=
596 ui::ReplaceTemplateExpressions(new_tab_theme_css
, substitutions
);
598 new_tab_guest_css_
= base::RefCountedString::TakeString(&full_css
);
601 void NTPResourceCache::CreateNewTabCSS() {
602 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
605 // Get our theme colors
606 SkColor color_background
=
607 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_BACKGROUND
);
608 SkColor color_text
= GetThemeColor(tp
, ThemeProperties::COLOR_NTP_TEXT
);
609 SkColor color_text_light
=
610 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_TEXT_LIGHT
);
612 SkColor color_header
=
613 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_HEADER
);
614 // Generate a lighter color for the header gradients.
615 color_utils::HSL header_lighter
;
616 color_utils::SkColorToHSL(color_header
, &header_lighter
);
617 header_lighter
.l
+= (1 - header_lighter
.l
) * 0.33;
619 // Generate section border color from the header color. See
620 // BookmarkBarView::Paint for how we do this for the bookmark bar
622 SkColor color_section_border
=
624 SkColorGetR(color_header
),
625 SkColorGetG(color_header
),
626 SkColorGetB(color_header
));
628 // Generate the replacements.
629 std::map
<base::StringPiece
, std::string
> substitutions
;
631 // Cache-buster for background.
632 substitutions
["themeId"] =
633 profile_
->GetPrefs()->GetString(prefs::kCurrentThemeID
);
636 substitutions
["colorBackground"] = SkColorToRGBAString(color_background
);
637 substitutions
["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp
, false);
638 substitutions
["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp
, true);
639 substitutions
["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp
);
640 substitutions
["colorTextRgba"] = SkColorToRGBAString(color_text
);
641 substitutions
["colorSectionBorder"] =
642 SkColorToRGBAString(color_section_border
);
643 substitutions
["colorTextLight"] = SkColorToRGBAString(color_text_light
);
644 substitutions
["colorSectionBorder"] =
645 SkColorToRGBComponents(color_section_border
);
646 substitutions
["colorText"] = SkColorToRGBComponents(color_text
);
648 // For themes that right-align the background, we flip the attribution to the
649 // left to avoid conflicts.
650 int alignment
= tp
->GetDisplayProperty(
651 ThemeProperties::NTP_BACKGROUND_ALIGNMENT
);
652 if (alignment
& ThemeProperties::ALIGN_RIGHT
) {
653 substitutions
["leftAlignAttribution"] = "0";
654 substitutions
["rightAlignAttribution"] = "auto";
655 substitutions
["textAlignAttribution"] = "right";
657 substitutions
["leftAlignAttribution"] = "auto";
658 substitutions
["rightAlignAttribution"] = "0";
659 substitutions
["textAlignAttribution"] = "left";
662 substitutions
["displayAttribution"] =
663 tp
->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION
) ? "inline" : "none";
666 static const base::StringPiece
new_tab_theme_css(
667 ResourceBundle::GetSharedInstance().GetRawDataResource(
668 IDR_NEW_TAB_4_THEME_CSS
));
670 // Create the string from our template and the replacements.
671 std::string css_string
=
672 ui::ReplaceTemplateExpressions(new_tab_theme_css
, substitutions
);
673 new_tab_css_
= base::RefCountedString::TakeString(&css_string
);