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/ntp/new_tab_page_handler.h"
35 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
36 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h"
37 #include "chrome/browser/web_resource/notification_promo.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 "content/public/browser/browser_thread.h"
47 #include "content/public/browser/notification_service.h"
48 #include "content/public/browser/render_process_host.h"
49 #include "extensions/common/extension.h"
50 #include "extensions/common/extension_urls.h"
51 #include "grit/browser_resources.h"
52 #include "grit/components_strings.h"
53 #include "grit/theme_resources.h"
54 #include "ui/base/l10n/l10n_util.h"
55 #include "ui/base/resource/resource_bundle.h"
56 #include "ui/base/theme_provider.h"
57 #include "ui/base/webui/jstemplate_builder.h"
58 #include "ui/base/webui/web_ui_util.h"
59 #include "ui/gfx/animation/animation.h"
60 #include "ui/gfx/color_utils.h"
61 #include "ui/gfx/sys_color_change_listener.h"
63 #if defined(OS_CHROMEOS)
64 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
65 #include "chromeos/chromeos_switches.h"
68 #if defined(OS_MACOSX)
69 #include "chrome/browser/platform_util.h"
72 using content::BrowserThread
;
76 // The URL for the the Learn More page shown on incognito new tab.
77 const char kLearnMoreIncognitoUrl
[] =
78 #if defined(OS_CHROMEOS)
79 "https://support.google.com/chromebook/?p=incognito";
81 "https://support.google.com/chrome/?p=incognito";
84 // The URL for the Learn More page shown on guest session new tab.
85 const char kLearnMoreGuestSessionUrl
[] =
86 #if defined(OS_CHROMEOS)
87 "https://www.google.com/support/chromeos/bin/answer.py?answer=1057090";
89 "https://support.google.com/chrome/?p=ui_guest";
92 std::string
SkColorToRGBAString(SkColor color
) {
93 // We convert the alpha using DoubleToString because StringPrintf will use
94 // locale specific formatters (e.g., use , instead of . in German).
95 return base::StringPrintf(
100 base::DoubleToString(SkColorGetA(color
) / 255.0).c_str());
103 // Creates an rgb string for an SkColor, but leaves the alpha blank so that the
104 // css can fill it in.
105 std::string
SkColorToRGBComponents(SkColor color
) {
106 return base::StringPrintf(
113 SkColor
GetThemeColor(ui::ThemeProvider
* tp
, int id
) {
114 SkColor color
= tp
->GetColor(id
);
115 // If web contents are being inverted because the system is in high-contrast
116 // mode, any system theme colors we use must be inverted too to cancel out.
117 return gfx::IsInvertedColorScheme() ?
118 color_utils::InvertColor(color
) : color
;
121 // Get the CSS string for the background position on the new tab page for the
122 // states when the bar is attached or detached.
123 std::string
GetNewTabBackgroundCSS(const ui::ThemeProvider
* theme_provider
,
125 // TODO(glen): This is a quick workaround to hide the notused.png image when
126 // no image is provided - we don't have time right now to figure out why
127 // this is painting as white.
128 // http://crbug.com/17593
129 if (!theme_provider
->HasCustomImage(IDR_THEME_NTP_BACKGROUND
)) {
133 int alignment
= theme_provider
->GetDisplayProperty(
134 ThemeProperties::NTP_BACKGROUND_ALIGNMENT
);
137 return ThemeProperties::AlignmentToString(alignment
);
139 if (alignment
& ThemeProperties::ALIGN_TOP
) {
140 // The bar is detached, so we must offset the background by the bar size
141 // if it's a top-aligned bar.
142 int offset
= chrome::kNTPBookmarkBarHeight
;
144 if (alignment
& ThemeProperties::ALIGN_LEFT
)
145 return "left " + base::IntToString(-offset
) + "px";
146 else if (alignment
& ThemeProperties::ALIGN_RIGHT
)
147 return "right " + base::IntToString(-offset
) + "px";
148 return "center " + base::IntToString(-offset
) + "px";
151 return ThemeProperties::AlignmentToString(alignment
);
154 // How the background image on the new tab page should be tiled (see tiling
155 // masks in theme_service.h).
156 std::string
GetNewTabBackgroundTilingCSS(
157 const ui::ThemeProvider
* theme_provider
) {
158 int repeat_mode
= theme_provider
->GetDisplayProperty(
159 ThemeProperties::NTP_BACKGROUND_TILING
);
160 return ThemeProperties::TilingToString(repeat_mode
);
165 NTPResourceCache::NTPResourceCache(Profile
* profile
)
166 : profile_(profile
), is_swipe_tracking_from_scroll_events_enabled_(false),
167 should_show_apps_page_(NewTabUI::ShouldShowApps()),
168 should_show_most_visited_page_(true),
169 should_show_other_devices_menu_(true),
170 should_show_recently_closed_menu_(true) {
171 registrar_
.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED
,
172 content::Source
<ThemeService
>(
173 ThemeServiceFactory::GetForProfile(profile
)));
174 registrar_
.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED
,
175 content::NotificationService::AllSources());
177 PromoResourceService
* promo_service
=
178 g_browser_process
->promo_resource_service();
180 promo_resource_subscription_
= promo_service
->RegisterStateChangedCallback(
181 base::Bind(&NTPResourceCache::Invalidate
, base::Unretained(this)));
184 base::Closure callback
= base::Bind(&NTPResourceCache::OnPreferenceChanged
,
185 base::Unretained(this));
187 // Watch for pref changes that cause us to need to invalidate the HTML cache.
188 profile_pref_change_registrar_
.Init(profile_
->GetPrefs());
189 profile_pref_change_registrar_
.Add(bookmarks::prefs::kShowBookmarkBar
,
191 profile_pref_change_registrar_
.Add(prefs::kNtpShownPage
, callback
);
192 profile_pref_change_registrar_
.Add(prefs::kSignInPromoShowNTPBubble
,
194 profile_pref_change_registrar_
.Add(prefs::kHideWebStoreIcon
, callback
);
196 // Some tests don't have a local state.
197 #if defined(ENABLE_APP_LIST)
198 if (g_browser_process
->local_state()) {
199 local_state_pref_change_registrar_
.Init(g_browser_process
->local_state());
200 local_state_pref_change_registrar_
.Add(prefs::kShowAppLauncherPromo
,
202 local_state_pref_change_registrar_
.Add(
203 prefs::kAppLauncherHasBeenEnabled
, callback
);
208 NTPResourceCache::~NTPResourceCache() {}
210 bool NTPResourceCache::NewTabCacheNeedsRefresh() {
211 #if defined(OS_MACOSX)
212 // Invalidate if the current value is different from the cached value.
213 bool is_enabled
= platform_util::IsSwipeTrackingFromScrollEventsEnabled();
214 if (is_enabled
!= is_swipe_tracking_from_scroll_events_enabled_
) {
215 is_swipe_tracking_from_scroll_events_enabled_
= is_enabled
;
219 bool should_show_apps_page
= NewTabUI::ShouldShowApps();
220 if (should_show_apps_page
!= should_show_apps_page_
) {
221 should_show_apps_page_
= should_show_apps_page
;
227 NTPResourceCache::WindowType
NTPResourceCache::GetWindowType(
228 Profile
* profile
, content::RenderProcessHost
* render_host
) {
229 if (profile
->IsGuestSession()) {
230 return NTPResourceCache::GUEST
;
231 } else if (render_host
) {
232 // Sometimes the |profile| is the parent (non-incognito) version of the user
233 // so we check the |render_host| if it is provided.
234 if (render_host
->GetBrowserContext()->IsOffTheRecord())
235 return NTPResourceCache::INCOGNITO
;
236 } else if (profile
->IsOffTheRecord()) {
237 return NTPResourceCache::INCOGNITO
;
239 return NTPResourceCache::NORMAL
;
242 base::RefCountedMemory
* NTPResourceCache::GetNewTabHTML(WindowType win_type
) {
243 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
244 if (win_type
== GUEST
) {
245 if (!new_tab_guest_html_
.get())
246 CreateNewTabGuestHTML();
247 return new_tab_guest_html_
.get();
248 } else if (win_type
== INCOGNITO
) {
249 if (!new_tab_incognito_html_
.get())
250 CreateNewTabIncognitoHTML();
251 return new_tab_incognito_html_
.get();
253 // Refresh the cached HTML if necessary.
254 // NOTE: NewTabCacheNeedsRefresh() must be called every time the new tab
255 // HTML is fetched, because it needs to initialize cached values.
256 if (NewTabCacheNeedsRefresh() || !new_tab_html_
.get())
258 return new_tab_html_
.get();
262 base::RefCountedMemory
* NTPResourceCache::GetNewTabCSS(WindowType win_type
) {
263 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
264 if (win_type
== GUEST
) {
265 if (!new_tab_guest_css_
.get())
266 CreateNewTabGuestCSS();
267 return new_tab_guest_css_
.get();
268 } else if (win_type
== INCOGNITO
) {
269 if (!new_tab_incognito_css_
.get())
270 CreateNewTabIncognitoCSS();
271 return new_tab_incognito_css_
.get();
273 if (!new_tab_css_
.get())
275 return new_tab_css_
.get();
279 void NTPResourceCache::Observe(int type
,
280 const content::NotificationSource
& source
,
281 const content::NotificationDetails
& details
) {
282 // Invalidate the cache.
283 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED
== type
||
284 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED
== type
) {
291 void NTPResourceCache::OnPreferenceChanged() {
292 // A change occurred to one of the preferences we care about, so flush the
294 new_tab_incognito_html_
= NULL
;
295 new_tab_html_
= NULL
;
299 void NTPResourceCache::Invalidate() {
300 new_tab_incognito_html_
= nullptr;
301 new_tab_html_
= nullptr;
302 new_tab_incognito_css_
= nullptr;
303 // TODO(dbeam): Check if it is necessary to clear the CSS on promo changes.
304 new_tab_css_
= nullptr;
307 void NTPResourceCache::CreateNewTabIncognitoHTML() {
308 base::DictionaryValue localized_strings
;
309 localized_strings
.SetString("title",
310 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE
));
311 int new_tab_description_ids
= IDS_NEW_TAB_OTR_DESCRIPTION
;
312 int new_tab_heading_ids
= IDS_NEW_TAB_OTR_HEADING
;
313 int new_tab_link_ids
= IDS_NEW_TAB_OTR_LEARN_MORE_LINK
;
314 int new_tab_warning_ids
= IDS_NEW_TAB_OTR_MESSAGE_WARNING
;
315 int new_tab_html_idr
= IDR_INCOGNITO_TAB_HTML
;
316 const char* new_tab_link
= kLearnMoreIncognitoUrl
;
318 if (profile_
->IsGuestSession()) {
319 localized_strings
.SetString("guestTabDescription",
320 l10n_util::GetStringUTF16(new_tab_description_ids
));
321 localized_strings
.SetString("guestTabHeading",
322 l10n_util::GetStringUTF16(new_tab_heading_ids
));
324 localized_strings
.SetString("incognitoTabDescription",
325 l10n_util::GetStringUTF16(new_tab_description_ids
));
326 localized_strings
.SetString("incognitoTabHeading",
327 l10n_util::GetStringUTF16(new_tab_heading_ids
));
328 localized_strings
.SetString("incognitoTabWarning",
329 l10n_util::GetStringUTF16(new_tab_warning_ids
));
332 localized_strings
.SetString("learnMore",
333 l10n_util::GetStringUTF16(new_tab_link_ids
));
334 localized_strings
.SetString("learnMoreLink", new_tab_link
);
336 bool bookmark_bar_attached
=
337 profile_
->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar
);
338 localized_strings
.SetBoolean("bookmarkbarattached", bookmark_bar_attached
);
340 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
341 webui::SetLoadTimeDataDefaults(app_locale
, &localized_strings
);
343 static const base::StringPiece
incognito_tab_html(
344 ResourceBundle::GetSharedInstance().GetRawDataResource(
347 std::string full_html
= webui::GetI18nTemplateHtml(
348 incognito_tab_html
, &localized_strings
);
350 new_tab_incognito_html_
= base::RefCountedString::TakeString(&full_html
);
353 void NTPResourceCache::CreateNewTabGuestHTML() {
354 base::DictionaryValue localized_strings
;
355 localized_strings
.SetString("title",
356 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE
));
357 const char* guest_tab_link
= kLearnMoreGuestSessionUrl
;
358 int guest_tab_ids
= IDR_GUEST_TAB_HTML
;
359 int guest_tab_description_ids
= IDS_NEW_TAB_GUEST_SESSION_DESCRIPTION
;
360 int guest_tab_heading_ids
= IDS_NEW_TAB_GUEST_SESSION_HEADING
;
361 int guest_tab_link_ids
= IDS_NEW_TAB_GUEST_SESSION_LEARN_MORE_LINK
;
363 #if defined(OS_CHROMEOS)
364 guest_tab_ids
= IDR_GUEST_SESSION_TAB_HTML
;
366 policy::BrowserPolicyConnectorChromeOS
* connector
=
367 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
368 std::string enterprise_domain
= connector
->GetEnterpriseDomain();
370 if (!enterprise_domain
.empty()) {
371 // Device is enterprise enrolled.
372 localized_strings
.SetString("enterpriseInfoVisible", "true");
373 base::string16 enterprise_info
= l10n_util::GetStringFUTF16(
374 IDS_DEVICE_OWNED_BY_NOTICE
,
375 base::UTF8ToUTF16(enterprise_domain
));
376 localized_strings
.SetString("enterpriseInfoMessage", enterprise_info
);
377 localized_strings
.SetString("enterpriseLearnMore",
378 l10n_util::GetStringUTF16(IDS_LEARN_MORE
));
379 localized_strings
.SetString("enterpriseInfoHintLink",
380 chrome::kLearnMoreEnterpriseURL
);
382 localized_strings
.SetString("enterpriseInfoVisible", "false");
386 localized_strings
.SetString("guestTabDescription",
387 l10n_util::GetStringUTF16(guest_tab_description_ids
));
388 localized_strings
.SetString("guestTabHeading",
389 l10n_util::GetStringUTF16(guest_tab_heading_ids
));
390 localized_strings
.SetString("learnMore",
391 l10n_util::GetStringUTF16(guest_tab_link_ids
));
392 localized_strings
.SetString("learnMoreLink", guest_tab_link
);
394 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
395 webui::SetLoadTimeDataDefaults(app_locale
, &localized_strings
);
397 static const base::StringPiece
guest_tab_html(
398 ResourceBundle::GetSharedInstance().GetRawDataResource(guest_tab_ids
));
400 std::string full_html
= webui::GetI18nTemplateHtml(
401 guest_tab_html
, &localized_strings
);
403 new_tab_guest_html_
= base::RefCountedString::TakeString(&full_html
);
406 void NTPResourceCache::CreateNewTabHTML() {
407 // TODO(estade): these strings should be defined in their relevant handlers
408 // (in GetLocalizedValues) and should have more legible names.
409 // Show the profile name in the title and most visited labels if the current
410 // profile is not the default.
411 PrefService
* prefs
= profile_
->GetPrefs();
412 base::DictionaryValue load_time_data
;
413 load_time_data
.SetBoolean("bookmarkbarattached",
414 prefs
->GetBoolean(bookmarks::prefs::kShowBookmarkBar
));
415 load_time_data
.SetBoolean("hasattribution",
416 ThemeServiceFactory::GetForProfile(profile_
)->HasCustomImage(
417 IDR_THEME_NTP_ATTRIBUTION
));
418 load_time_data
.SetBoolean("showMostvisited", should_show_most_visited_page_
);
419 load_time_data
.SetBoolean("showAppLauncherPromo",
420 ShouldShowAppLauncherPromo());
421 load_time_data
.SetBoolean("showRecentlyClosed",
422 should_show_recently_closed_menu_
);
423 load_time_data
.SetString("title",
424 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE
));
425 load_time_data
.SetString("mostvisited",
426 l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED
));
427 load_time_data
.SetString("suggestions",
428 l10n_util::GetStringUTF16(IDS_NEW_TAB_SUGGESTIONS
));
429 load_time_data
.SetString("restoreThumbnailsShort",
430 l10n_util::GetStringUTF16(IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK
));
431 load_time_data
.SetString("recentlyclosed",
432 l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED
));
433 load_time_data
.SetString("webStoreTitle",
434 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE
));
435 load_time_data
.SetString("webStoreTitleShort",
436 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE_SHORT
));
437 load_time_data
.SetString("closedwindowsingle",
438 l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_SINGLE
));
439 load_time_data
.SetString("closedwindowmultiple",
440 l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_MULTIPLE
));
441 load_time_data
.SetString("attributionintro",
442 l10n_util::GetStringUTF16(IDS_NEW_TAB_ATTRIBUTION_INTRO
));
443 load_time_data
.SetString("thumbnailremovednotification",
444 l10n_util::GetStringUTF16(IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION
));
445 load_time_data
.SetString("undothumbnailremove",
446 l10n_util::GetStringUTF16(IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE
));
447 load_time_data
.SetString("removethumbnailtooltip",
448 l10n_util::GetStringUTF16(IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP
));
449 load_time_data
.SetString("appuninstall",
450 l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL
));
451 load_time_data
.SetString("appoptions",
452 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_OPTIONS
));
453 load_time_data
.SetString("appdetails",
454 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_DETAILS
));
455 load_time_data
.SetString("appinfodialog",
456 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_SHOW_INFO
));
457 load_time_data
.SetString("appcreateshortcut",
458 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_CREATE_SHORTCUT
));
459 load_time_data
.SetString("appDefaultPageName",
460 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME
));
461 load_time_data
.SetString("applaunchtypepinned",
462 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_PINNED
));
463 load_time_data
.SetString("applaunchtyperegular",
464 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_REGULAR
));
465 load_time_data
.SetString("applaunchtypewindow",
466 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW
));
467 load_time_data
.SetString("applaunchtypefullscreen",
468 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN
));
469 load_time_data
.SetString("syncpromotext",
470 l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL
));
471 load_time_data
.SetString("syncLinkText",
472 l10n_util::GetStringUTF16(IDS_SYNC_ADVANCED_OPTIONS
));
473 load_time_data
.SetBoolean("shouldShowSyncLogin",
474 NTPLoginHandler::ShouldShow(profile_
));
475 load_time_data
.SetString("otherSessions",
476 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_LABEL
));
477 load_time_data
.SetString("otherSessionsEmpty",
478 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_EMPTY
));
479 load_time_data
.SetString("otherSessionsLearnMoreUrl",
480 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_LEARN_MORE_URL
));
481 load_time_data
.SetString("learnMore",
482 l10n_util::GetStringUTF16(IDS_LEARN_MORE
));
483 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
484 load_time_data
.SetString("webStoreLink",
485 google_util::AppendGoogleLocaleParam(
486 GURL(extension_urls::GetWebstoreLaunchURL()), app_locale
).spec());
487 load_time_data
.SetString("appInstallHintText",
488 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_INSTALL_HINT_LABEL
));
489 load_time_data
.SetBoolean("isDiscoveryInNTPEnabled",
490 NewTabUI::IsDiscoveryInNTPEnabled());
491 load_time_data
.SetString("collapseSessionMenuItemText",
492 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_COLLAPSE_SESSION
));
493 load_time_data
.SetString("expandSessionMenuItemText",
494 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_EXPAND_SESSION
));
495 load_time_data
.SetString("restoreSessionMenuItemText",
496 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_OPEN_ALL
));
497 load_time_data
.SetString("learn_more",
498 l10n_util::GetStringUTF16(IDS_LEARN_MORE
));
499 load_time_data
.SetString("tile_grid_screenreader_accessible_description",
500 l10n_util::GetStringUTF16(IDS_NEW_TAB_TILE_GRID_ACCESSIBLE_DESCRIPTION
));
501 load_time_data
.SetString("page_switcher_change_title",
502 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_CHANGE_TITLE
));
503 load_time_data
.SetString("page_switcher_same_title",
504 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_SAME_TITLE
));
505 load_time_data
.SetString("appsPromoTitle",
506 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_APPS_PROMO_TITLE
));
507 // On Mac OS X 10.7+, horizontal scrolling can be treated as a back or
508 // forward gesture. Pass through a flag that indicates whether or not that
509 // feature is enabled.
510 load_time_data
.SetBoolean("isSwipeTrackingFromScrollEventsEnabled",
511 is_swipe_tracking_from_scroll_events_enabled_
);
513 load_time_data
.SetBoolean("showApps", should_show_apps_page_
);
514 load_time_data
.SetBoolean("showWebStoreIcon",
515 !prefs
->GetBoolean(prefs::kHideWebStoreIcon
));
517 bool bookmark_apps_enabled
= extensions::util::IsNewBookmarkAppsEnabled();
518 load_time_data
.SetBoolean("enableNewBookmarkApps", bookmark_apps_enabled
);
520 load_time_data
.SetBoolean("canShowAppInfoDialog",
521 CanShowAppInfoDialog());
523 #if defined(OS_CHROMEOS)
524 load_time_data
.SetString("expandMenu",
525 l10n_util::GetStringUTF16(IDS_NEW_TAB_CLOSE_MENU_EXPAND
));
528 NewTabPageHandler::GetLocalizedValues(profile_
, &load_time_data
);
529 NTPLoginHandler::GetLocalizedValues(profile_
, &load_time_data
);
531 webui::SetLoadTimeDataDefaults(app_locale
, &load_time_data
);
533 // Control fade and resize animations.
534 load_time_data
.SetBoolean("anim",
535 gfx::Animation::ShouldRenderRichAnimation());
537 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
538 int alignment
= tp
->GetDisplayProperty(
539 ThemeProperties::NTP_BACKGROUND_ALIGNMENT
);
540 load_time_data
.SetString("themegravity",
541 (alignment
& ThemeProperties::ALIGN_RIGHT
) ? "right" : "");
543 // Disable the promo if this is the first run, otherwise set the promo string
544 // for display if there is a valid outstanding promo.
545 if (first_run::IsChromeFirstRun()) {
546 NotificationPromo::HandleClosed(NotificationPromo::NTP_NOTIFICATION_PROMO
);
548 NotificationPromo notification_promo
;
549 notification_promo
.InitFromPrefs(NotificationPromo::NTP_NOTIFICATION_PROMO
);
550 if (notification_promo
.CanShow()) {
551 load_time_data
.SetString("notificationPromoText",
552 notification_promo
.promo_text());
553 DVLOG(1) << "Notification promo:" << notification_promo
.promo_text();
556 NotificationPromo bubble_promo
;
557 bubble_promo
.InitFromPrefs(NotificationPromo::NTP_BUBBLE_PROMO
);
558 if (bubble_promo
.CanShow()) {
559 load_time_data
.SetString("bubblePromoText",
560 bubble_promo
.promo_text());
561 DVLOG(1) << "Bubble promo:" << bubble_promo
.promo_text();
565 // Determine whether to show the menu for accessing tabs on other devices.
566 bool show_other_sessions_menu
=
567 should_show_other_devices_menu_
&&
568 !base::CommandLine::ForCurrentProcess()->HasSwitch(
569 switches::kDisableNTPOtherSessionsMenu
);
570 load_time_data
.SetBoolean("showOtherSessionsMenu", show_other_sessions_menu
);
571 load_time_data
.SetBoolean(
573 SigninManagerFactory::GetForProfile(profile_
)->IsAuthenticated());
575 // Load the new tab page appropriate for this build.
576 base::StringPiece
new_tab_html(ResourceBundle::GetSharedInstance().
577 GetRawDataResource(IDR_NEW_TAB_4_HTML
));
578 std::string full_html
=
579 webui::GetI18nTemplateHtml(new_tab_html
, &load_time_data
);
580 new_tab_html_
= base::RefCountedString::TakeString(&full_html
);
583 void NTPResourceCache::CreateNewTabIncognitoCSS() {
584 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
587 // Get our theme colors
588 SkColor color_background
=
589 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_BACKGROUND
);
591 // Generate the replacements.
592 std::vector
<std::string
> subst
;
594 // Cache-buster for background.
596 profile_
->GetPrefs()->GetString(prefs::kCurrentThemeID
)); // $1
599 subst
.push_back(SkColorToRGBAString(color_background
)); // $2
600 subst
.push_back(GetNewTabBackgroundCSS(tp
, false)); // $3
601 subst
.push_back(GetNewTabBackgroundCSS(tp
, true)); // $4
602 subst
.push_back(GetNewTabBackgroundTilingCSS(tp
)); // $5
605 static const base::StringPiece
new_tab_theme_css(
606 ResourceBundle::GetSharedInstance().GetRawDataResource(
607 IDR_NEW_INCOGNITO_TAB_THEME_CSS
));
609 // Create the string from our template and the replacements.
610 std::string full_css
= ReplaceStringPlaceholders(
611 new_tab_theme_css
, subst
, NULL
);
613 new_tab_incognito_css_
= base::RefCountedString::TakeString(&full_css
);
616 void NTPResourceCache::CreateNewTabGuestCSS() {
617 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
620 // Get our theme colors
621 SkColor color_background
=
622 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_BACKGROUND
);
624 // Generate the replacements.
625 std::vector
<std::string
> subst
;
627 // Cache-buster for background.
629 profile_
->GetPrefs()->GetString(prefs::kCurrentThemeID
)); // $1
632 subst
.push_back(SkColorToRGBAString(color_background
)); // $2
633 subst
.push_back(GetNewTabBackgroundCSS(tp
, false)); // $3
634 subst
.push_back(GetNewTabBackgroundCSS(tp
, true)); // $4
635 subst
.push_back(GetNewTabBackgroundTilingCSS(tp
)); // $5
638 static const base::StringPiece
new_tab_theme_css(
639 ResourceBundle::GetSharedInstance().GetRawDataResource(
640 IDR_NEW_INCOGNITO_TAB_THEME_CSS
));
642 // Create the string from our template and the replacements.
643 std::string full_css
= ReplaceStringPlaceholders(
644 new_tab_theme_css
, subst
, NULL
);
646 new_tab_guest_css_
= base::RefCountedString::TakeString(&full_css
);
649 void NTPResourceCache::CreateNewTabCSS() {
650 ui::ThemeProvider
* tp
= ThemeServiceFactory::GetForProfile(profile_
);
653 // Get our theme colors
654 SkColor color_background
=
655 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_BACKGROUND
);
656 SkColor color_text
= GetThemeColor(tp
, ThemeProperties::COLOR_NTP_TEXT
);
657 SkColor color_link
= GetThemeColor(tp
, ThemeProperties::COLOR_NTP_LINK
);
658 SkColor color_link_underline
=
659 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_LINK_UNDERLINE
);
661 SkColor color_section
=
662 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_SECTION
);
663 SkColor color_section_text
=
664 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_SECTION_TEXT
);
665 SkColor color_section_link
=
666 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_SECTION_LINK
);
667 SkColor color_section_link_underline
=
668 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
);
669 SkColor color_section_header_text
=
670 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT
);
671 SkColor color_section_header_text_hover
=
672 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT_HOVER
);
673 SkColor color_section_header_rule
=
674 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE
);
675 SkColor color_section_header_rule_light
=
676 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE_LIGHT
);
677 SkColor color_text_light
=
678 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_TEXT_LIGHT
);
680 SkColor color_header
=
681 GetThemeColor(tp
, ThemeProperties::COLOR_NTP_HEADER
);
682 // Generate a lighter color for the header gradients.
683 color_utils::HSL header_lighter
;
684 color_utils::SkColorToHSL(color_header
, &header_lighter
);
685 header_lighter
.l
+= (1 - header_lighter
.l
) * 0.33;
686 SkColor color_header_gradient_light
=
687 color_utils::HSLToSkColor(header_lighter
, SkColorGetA(color_header
));
689 // Generate section border color from the header color. See
690 // BookmarkBarView::Paint for how we do this for the bookmark bar
692 SkColor color_section_border
=
694 SkColorGetR(color_header
),
695 SkColorGetG(color_header
),
696 SkColorGetB(color_header
));
698 // Generate the replacements.
699 std::vector
<std::string
> subst
;
701 // Cache-buster for background.
703 profile_
->GetPrefs()->GetString(prefs::kCurrentThemeID
)); // $1
706 subst
.push_back(SkColorToRGBAString(color_background
)); // $2
707 subst
.push_back(GetNewTabBackgroundCSS(tp
, false)); // $3
708 subst
.push_back(GetNewTabBackgroundCSS(tp
, true)); // $4
709 subst
.push_back(GetNewTabBackgroundTilingCSS(tp
)); // $5
710 subst
.push_back(SkColorToRGBAString(color_header
)); // $6
711 subst
.push_back(SkColorToRGBAString(color_header_gradient_light
)); // $7
712 subst
.push_back(SkColorToRGBAString(color_text
)); // $8
713 subst
.push_back(SkColorToRGBAString(color_link
)); // $9
714 subst
.push_back(SkColorToRGBAString(color_section
)); // $10
715 subst
.push_back(SkColorToRGBAString(color_section_border
)); // $11
716 subst
.push_back(SkColorToRGBAString(color_section_text
)); // $12
717 subst
.push_back(SkColorToRGBAString(color_section_link
)); // $13
718 subst
.push_back(SkColorToRGBAString(color_link_underline
)); // $14
719 subst
.push_back(SkColorToRGBAString(color_section_link_underline
)); // $15
720 subst
.push_back(SkColorToRGBAString(color_section_header_text
)); // $16
721 subst
.push_back(SkColorToRGBAString(
722 color_section_header_text_hover
)); // $17
723 subst
.push_back(SkColorToRGBAString(color_section_header_rule
)); // $18
724 subst
.push_back(SkColorToRGBAString(
725 color_section_header_rule_light
)); // $19
726 subst
.push_back(SkColorToRGBAString(
727 SkColorSetA(color_section_header_rule
, 0))); // $20
728 subst
.push_back(SkColorToRGBAString(color_text_light
)); // $21
729 subst
.push_back(SkColorToRGBComponents(color_section_border
)); // $22
730 subst
.push_back(SkColorToRGBComponents(color_text
)); // $23
733 static const base::StringPiece
new_tab_theme_css(
734 ResourceBundle::GetSharedInstance().GetRawDataResource(
735 IDR_NEW_TAB_4_THEME_CSS
));
737 // Create the string from our template and the replacements.
738 std::string css_string
;
739 css_string
= ReplaceStringPlaceholders(new_tab_theme_css
, subst
, NULL
);
740 new_tab_css_
= base::RefCountedString::TakeString(&css_string
);