NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / webui / ntp / ntp_resource_cache.cc
blobc8fd292bd3e894e07f2098929bc74d99150b8879
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"
7 #include <string>
8 #include <vector>
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/first_run/first_run.h"
21 #include "chrome/browser/google/google_util.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/search/search.h"
24 #include "chrome/browser/sync/profile_sync_service.h"
25 #include "chrome/browser/sync/profile_sync_service_factory.h"
26 #include "chrome/browser/themes/theme_properties.h"
27 #include "chrome/browser/themes/theme_service.h"
28 #include "chrome/browser/themes/theme_service_factory.h"
29 #include "chrome/browser/ui/app_list/app_list_util.h"
30 #include "chrome/browser/ui/bookmarks/bookmark_bar_constants.h"
31 #include "chrome/browser/ui/sync/sync_promo_ui.h"
32 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h"
33 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
34 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h"
35 #include "chrome/browser/ui/webui/sync_setup_handler.h"
36 #include "chrome/browser/web_resource/notification_promo.h"
37 #include "chrome/common/chrome_switches.h"
38 #include "chrome/common/extensions/extension_constants.h"
39 #include "chrome/common/pref_names.h"
40 #include "chrome/common/url_constants.h"
41 #include "content/public/browser/browser_thread.h"
42 #include "content/public/browser/notification_service.h"
43 #include "content/public/browser/render_process_host.h"
44 #include "extensions/common/extension.h"
45 #include "grit/browser_resources.h"
46 #include "grit/chromium_strings.h"
47 #include "grit/generated_resources.h"
48 #include "grit/locale_settings.h"
49 #include "grit/theme_resources.h"
50 #include "ui/base/l10n/l10n_util.h"
51 #include "ui/base/resource/resource_bundle.h"
52 #include "ui/base/theme_provider.h"
53 #include "ui/base/webui/jstemplate_builder.h"
54 #include "ui/base/webui/web_ui_util.h"
55 #include "ui/gfx/animation/animation.h"
56 #include "ui/gfx/color_utils.h"
57 #include "ui/gfx/sys_color_change_listener.h"
59 #if defined(OS_CHROMEOS)
60 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
61 #include "chromeos/chromeos_switches.h"
62 #endif
64 #if defined(OS_MACOSX)
65 #include "chrome/browser/platform_util.h"
66 #endif
68 using content::BrowserThread;
70 namespace {
72 // The URL for the the Learn More page shown on incognito new tab.
73 const char kLearnMoreIncognitoUrl[] =
74 #if defined(OS_CHROMEOS)
75 "https://www.google.com/support/chromeos/bin/answer.py?answer=95464";
76 #else
77 "https://www.google.com/support/chrome/bin/answer.py?answer=95464";
78 #endif
80 // The URL for the Learn More page shown on guest session new tab.
81 const char kLearnMoreGuestSessionUrl[] =
82 "https://www.google.com/support/chromeos/bin/answer.py?answer=1057090";
84 base::string16 GetUrlWithLang(const GURL& url) {
85 return base::ASCIIToUTF16(google_util::AppendGoogleLocaleParam(url).spec());
88 std::string SkColorToRGBAString(SkColor color) {
89 // We convert the alpha using DoubleToString because StringPrintf will use
90 // locale specific formatters (e.g., use , instead of . in German).
91 return base::StringPrintf(
92 "rgba(%d,%d,%d,%s)",
93 SkColorGetR(color),
94 SkColorGetG(color),
95 SkColorGetB(color),
96 base::DoubleToString(SkColorGetA(color) / 255.0).c_str());
99 // Creates an rgb string for an SkColor, but leaves the alpha blank so that the
100 // css can fill it in.
101 std::string SkColorToRGBComponents(SkColor color) {
102 return base::StringPrintf(
103 "%d,%d,%d",
104 SkColorGetR(color),
105 SkColorGetG(color),
106 SkColorGetB(color));
109 SkColor GetThemeColor(ui::ThemeProvider* tp, int id) {
110 SkColor color = tp->GetColor(id);
111 // If web contents are being inverted because the system is in high-contrast
112 // mode, any system theme colors we use must be inverted too to cancel out.
113 return gfx::IsInvertedColorScheme() ?
114 color_utils::InvertColor(color) : color;
117 // Get the CSS string for the background position on the new tab page for the
118 // states when the bar is attached or detached.
119 std::string GetNewTabBackgroundCSS(const ui::ThemeProvider* theme_provider,
120 bool bar_attached) {
121 // TODO(glen): This is a quick workaround to hide the notused.png image when
122 // no image is provided - we don't have time right now to figure out why
123 // this is painting as white.
124 // http://crbug.com/17593
125 if (!theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
126 return "-64px";
129 int alignment = theme_provider->GetDisplayProperty(
130 ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
132 if (bar_attached)
133 return ThemeProperties::AlignmentToString(alignment);
135 if (alignment & ThemeProperties::ALIGN_TOP) {
136 // The bar is detached, so we must offset the background by the bar size
137 // if it's a top-aligned bar.
138 int offset = chrome::kNTPBookmarkBarHeight;
140 if (alignment & ThemeProperties::ALIGN_LEFT)
141 return "left " + base::IntToString(-offset) + "px";
142 else if (alignment & ThemeProperties::ALIGN_RIGHT)
143 return "right " + base::IntToString(-offset) + "px";
144 return "center " + base::IntToString(-offset) + "px";
147 return ThemeProperties::AlignmentToString(alignment);
150 // How the background image on the new tab page should be tiled (see tiling
151 // masks in theme_service.h).
152 std::string GetNewTabBackgroundTilingCSS(
153 const ui::ThemeProvider* theme_provider) {
154 int repeat_mode = theme_provider->GetDisplayProperty(
155 ThemeProperties::NTP_BACKGROUND_TILING);
156 return ThemeProperties::TilingToString(repeat_mode);
159 } // namespace
161 NTPResourceCache::NTPResourceCache(Profile* profile)
162 : profile_(profile), is_swipe_tracking_from_scroll_events_enabled_(false),
163 should_show_apps_page_(NewTabUI::ShouldShowApps()),
164 should_show_most_visited_page_(true),
165 should_show_other_devices_menu_(true),
166 should_show_recently_closed_menu_(true) {
167 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
168 content::Source<ThemeService>(
169 ThemeServiceFactory::GetForProfile(profile)));
170 registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
171 content::NotificationService::AllSources());
173 base::Closure callback = base::Bind(&NTPResourceCache::OnPreferenceChanged,
174 base::Unretained(this));
176 // Watch for pref changes that cause us to need to invalidate the HTML cache.
177 profile_pref_change_registrar_.Init(profile_->GetPrefs());
178 profile_pref_change_registrar_.Add(prefs::kSyncAcknowledgedSyncTypes,
179 callback);
180 profile_pref_change_registrar_.Add(prefs::kShowBookmarkBar, callback);
181 profile_pref_change_registrar_.Add(prefs::kNtpShownPage, callback);
182 profile_pref_change_registrar_.Add(prefs::kSignInPromoShowNTPBubble,
183 callback);
184 profile_pref_change_registrar_.Add(prefs::kHideWebStoreIcon, callback);
186 // Some tests don't have a local state.
187 #if defined(ENABLE_APP_LIST)
188 if (g_browser_process->local_state()) {
189 local_state_pref_change_registrar_.Init(g_browser_process->local_state());
190 local_state_pref_change_registrar_.Add(prefs::kShowAppLauncherPromo,
191 callback);
192 local_state_pref_change_registrar_.Add(
193 prefs::kAppLauncherHasBeenEnabled, callback);
195 #endif
198 NTPResourceCache::~NTPResourceCache() {}
200 bool NTPResourceCache::NewTabCacheNeedsRefresh() {
201 #if defined(OS_MACOSX)
202 // Invalidate if the current value is different from the cached value.
203 bool is_enabled = platform_util::IsSwipeTrackingFromScrollEventsEnabled();
204 if (is_enabled != is_swipe_tracking_from_scroll_events_enabled_) {
205 is_swipe_tracking_from_scroll_events_enabled_ = is_enabled;
206 return true;
208 #endif
209 bool should_show_apps_page = NewTabUI::ShouldShowApps();
210 if (should_show_apps_page != should_show_apps_page_) {
211 should_show_apps_page_ = should_show_apps_page;
212 return true;
214 return false;
217 NTPResourceCache::WindowType NTPResourceCache::GetWindowType(
218 Profile* profile, content::RenderProcessHost* render_host) {
219 if (profile->IsGuestSession()) {
220 return NTPResourceCache::GUEST;
221 } else if (render_host) {
222 // Sometimes the |profile| is the parent (non-incognito) version of the user
223 // so we check the |render_host| if it is provided.
224 if (render_host->GetBrowserContext()->IsOffTheRecord())
225 return NTPResourceCache::INCOGNITO;
226 } else if (profile->IsOffTheRecord()) {
227 return NTPResourceCache::INCOGNITO;
229 return NTPResourceCache::NORMAL;
232 base::RefCountedMemory* NTPResourceCache::GetNewTabHTML(WindowType win_type) {
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
234 if (win_type == GUEST) {
235 if (!new_tab_guest_html_.get())
236 CreateNewTabGuestHTML();
237 return new_tab_guest_html_.get();
238 } else if (win_type == INCOGNITO) {
239 if (!new_tab_incognito_html_.get())
240 CreateNewTabIncognitoHTML();
241 return new_tab_incognito_html_.get();
242 } else {
243 // Refresh the cached HTML if necessary.
244 // NOTE: NewTabCacheNeedsRefresh() must be called every time the new tab
245 // HTML is fetched, because it needs to initialize cached values.
246 if (NewTabCacheNeedsRefresh() || !new_tab_html_.get())
247 CreateNewTabHTML();
248 return new_tab_html_.get();
252 base::RefCountedMemory* NTPResourceCache::GetNewTabCSS(WindowType win_type) {
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
254 if (win_type == GUEST) {
255 if (!new_tab_guest_css_.get())
256 CreateNewTabGuestCSS();
257 return new_tab_guest_css_.get();
258 } else if (win_type == INCOGNITO) {
259 if (!new_tab_incognito_css_.get())
260 CreateNewTabIncognitoCSS();
261 return new_tab_incognito_css_.get();
262 } else {
263 if (!new_tab_css_.get())
264 CreateNewTabCSS();
265 return new_tab_css_.get();
269 void NTPResourceCache::Observe(int type,
270 const content::NotificationSource& source,
271 const content::NotificationDetails& details) {
272 // Invalidate the cache.
273 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type ||
274 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) {
275 new_tab_incognito_html_ = NULL;
276 new_tab_html_ = NULL;
277 new_tab_incognito_css_ = NULL;
278 new_tab_css_ = NULL;
279 } else {
280 NOTREACHED();
284 void NTPResourceCache::OnPreferenceChanged() {
285 // A change occurred to one of the preferences we care about, so flush the
286 // cache.
287 new_tab_incognito_html_ = NULL;
288 new_tab_html_ = NULL;
289 new_tab_css_ = NULL;
292 void NTPResourceCache::CreateNewTabIncognitoHTML() {
293 base::DictionaryValue localized_strings;
294 localized_strings.SetString("title",
295 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
296 int new_tab_message_ids = IDS_NEW_TAB_OTR_MESSAGE;
297 int new_tab_html_idr = IDR_INCOGNITO_TAB_HTML;
298 const char* new_tab_link = kLearnMoreIncognitoUrl;
299 // TODO(altimofeev): consider implementation without 'if def' usage.
300 #if defined(OS_CHROMEOS)
301 if (CommandLine::ForCurrentProcess()->HasSwitch(
302 chromeos::switches::kGuestSession)) {
303 new_tab_message_ids = IDS_NEW_TAB_GUEST_SESSION_MESSAGE;
304 new_tab_html_idr = IDR_GUEST_SESSION_TAB_HTML;
305 new_tab_link = kLearnMoreGuestSessionUrl;
307 policy::BrowserPolicyConnectorChromeOS* connector =
308 g_browser_process->platform_part()->browser_policy_connector_chromeos();
309 std::string enterprise_domain = connector->GetEnterpriseDomain();
310 if (!enterprise_domain.empty()) {
311 // Device is enterprise enrolled.
312 localized_strings.SetString("enterpriseInfoVisible", "true");
313 base::string16 enterprise_info = l10n_util::GetStringFUTF16(
314 IDS_DEVICE_OWNED_BY_NOTICE,
315 base::UTF8ToUTF16(enterprise_domain));
316 localized_strings.SetString("enterpriseInfoMessage", enterprise_info);
317 localized_strings.SetString("learnMore",
318 l10n_util::GetStringUTF16(IDS_LEARN_MORE));
319 localized_strings.SetString("enterpriseInfoHintLink",
320 GetUrlWithLang(GURL(chrome::kLearnMoreEnterpriseURL)));
321 } else {
322 localized_strings.SetString("enterpriseInfoVisible", "false");
325 #endif
326 localized_strings.SetString("content",
327 l10n_util::GetStringFUTF16(new_tab_message_ids,
328 GetUrlWithLang(GURL(new_tab_link))));
329 localized_strings.SetString("extensionsmessage",
330 l10n_util::GetStringFUTF16(
331 IDS_NEW_TAB_OTR_EXTENSIONS_MESSAGE,
332 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
333 base::ASCIIToUTF16(chrome::kChromeUIExtensionsURL)));
334 bool bookmark_bar_attached = profile_->GetPrefs()->GetBoolean(
335 prefs::kShowBookmarkBar);
336 localized_strings.SetBoolean("bookmarkbarattached", bookmark_bar_attached);
338 webui::SetFontAndTextDirection(&localized_strings);
340 static const base::StringPiece incognito_tab_html(
341 ResourceBundle::GetSharedInstance().GetRawDataResource(
342 new_tab_html_idr));
344 std::string full_html = webui::GetI18nTemplateHtml(
345 incognito_tab_html, &localized_strings);
347 new_tab_incognito_html_ = base::RefCountedString::TakeString(&full_html);
350 void NTPResourceCache::CreateNewTabGuestHTML() {
351 base::DictionaryValue localized_strings;
352 localized_strings.SetString("title",
353 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
354 const char* new_tab_link = kLearnMoreGuestSessionUrl;
355 localized_strings.SetString("content",
356 l10n_util::GetStringFUTF16(IDS_NEW_TAB_GUEST_SESSION_MESSAGE,
357 GetUrlWithLang(GURL(new_tab_link))));
359 webui::SetFontAndTextDirection(&localized_strings);
361 static const base::StringPiece guest_tab_html(
362 ResourceBundle::GetSharedInstance().GetRawDataResource(
363 IDR_GUEST_TAB_HTML));
365 std::string full_html = webui::GetI18nTemplateHtml(
366 guest_tab_html, &localized_strings);
368 new_tab_guest_html_ = base::RefCountedString::TakeString(&full_html);
371 void NTPResourceCache::CreateNewTabHTML() {
372 // TODO(estade): these strings should be defined in their relevant handlers
373 // (in GetLocalizedValues) and should have more legible names.
374 // Show the profile name in the title and most visited labels if the current
375 // profile is not the default.
376 PrefService* prefs = profile_->GetPrefs();
377 base::DictionaryValue load_time_data;
378 load_time_data.SetBoolean("bookmarkbarattached",
379 prefs->GetBoolean(prefs::kShowBookmarkBar));
380 load_time_data.SetBoolean("hasattribution",
381 ThemeServiceFactory::GetForProfile(profile_)->HasCustomImage(
382 IDR_THEME_NTP_ATTRIBUTION));
383 load_time_data.SetBoolean("showMostvisited", should_show_most_visited_page_);
384 load_time_data.SetBoolean("showAppLauncherPromo",
385 ShouldShowAppLauncherPromo());
386 load_time_data.SetBoolean("showRecentlyClosed",
387 should_show_recently_closed_menu_);
388 load_time_data.SetString("title",
389 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
390 load_time_data.SetString("mostvisited",
391 l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED));
392 load_time_data.SetString("suggestions",
393 l10n_util::GetStringUTF16(IDS_NEW_TAB_SUGGESTIONS));
394 load_time_data.SetString("restoreThumbnailsShort",
395 l10n_util::GetStringUTF16(IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK));
396 load_time_data.SetString("recentlyclosed",
397 l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED));
398 load_time_data.SetString("webStoreTitle",
399 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE));
400 load_time_data.SetString("webStoreTitleShort",
401 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE_SHORT));
402 load_time_data.SetString("closedwindowsingle",
403 l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_SINGLE));
404 load_time_data.SetString("closedwindowmultiple",
405 l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_MULTIPLE));
406 load_time_data.SetString("attributionintro",
407 l10n_util::GetStringUTF16(IDS_NEW_TAB_ATTRIBUTION_INTRO));
408 load_time_data.SetString("thumbnailremovednotification",
409 l10n_util::GetStringUTF16(IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION));
410 load_time_data.SetString("undothumbnailremove",
411 l10n_util::GetStringUTF16(IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE));
412 load_time_data.SetString("removethumbnailtooltip",
413 l10n_util::GetStringUTF16(IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP));
414 load_time_data.SetString("appuninstall",
415 l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
416 load_time_data.SetString("appoptions",
417 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_OPTIONS));
418 load_time_data.SetString("appdetails",
419 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_DETAILS));
420 load_time_data.SetString("appcreateshortcut",
421 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_CREATE_SHORTCUT));
422 load_time_data.SetString("appDefaultPageName",
423 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME));
424 load_time_data.SetString("applaunchtypepinned",
425 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_PINNED));
426 load_time_data.SetString("applaunchtyperegular",
427 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_REGULAR));
428 load_time_data.SetString("applaunchtypewindow",
429 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW));
430 load_time_data.SetString("applaunchtypefullscreen",
431 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN));
432 load_time_data.SetString("syncpromotext",
433 l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL));
434 load_time_data.SetString("syncLinkText",
435 l10n_util::GetStringUTF16(IDS_SYNC_ADVANCED_OPTIONS));
436 load_time_data.SetBoolean("shouldShowSyncLogin",
437 NTPLoginHandler::ShouldShow(profile_));
438 load_time_data.SetString("otherSessions",
439 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_LABEL));
440 load_time_data.SetString("otherSessionsEmpty",
441 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_EMPTY));
442 load_time_data.SetString("otherSessionsLearnMoreUrl",
443 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_LEARN_MORE_URL));
444 load_time_data.SetString("learnMore",
445 l10n_util::GetStringUTF16(IDS_LEARN_MORE));
446 load_time_data.SetString("webStoreLink",
447 GetUrlWithLang(GURL(extension_urls::GetWebstoreLaunchURL())));
448 load_time_data.SetString("appInstallHintText",
449 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_INSTALL_HINT_LABEL));
450 load_time_data.SetBoolean("isDiscoveryInNTPEnabled",
451 NewTabUI::IsDiscoveryInNTPEnabled());
452 load_time_data.SetString("collapseSessionMenuItemText",
453 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_COLLAPSE_SESSION));
454 load_time_data.SetString("expandSessionMenuItemText",
455 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_EXPAND_SESSION));
456 load_time_data.SetString("restoreSessionMenuItemText",
457 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_OPEN_ALL));
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_);
473 // Managed users can not have apps installed currently so there's no need to
474 // show the app cards.
475 if (profile_->IsManaged())
476 should_show_apps_page_ = false;
478 load_time_data.SetBoolean("showApps", should_show_apps_page_);
479 load_time_data.SetBoolean("showWebStoreIcon",
480 !prefs->GetBoolean(prefs::kHideWebStoreIcon));
482 bool streamlined_hosted_apps = CommandLine::ForCurrentProcess()->HasSwitch(
483 switches::kEnableStreamlinedHostedApps);
484 load_time_data.SetBoolean("enableStreamlinedHostedApps",
485 streamlined_hosted_apps);
486 // Use a different string for launching as a regular tab for streamlined
487 // hosted apps.
488 if (streamlined_hosted_apps) {
489 load_time_data.SetString("applaunchtypetab",
490 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_TAB));
493 #if defined(OS_MACOSX)
494 load_time_data.SetBoolean(
495 "disableCreateAppShortcut",
496 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableAppShims));
497 #endif
499 #if defined(OS_CHROMEOS)
500 load_time_data.SetString("expandMenu",
501 l10n_util::GetStringUTF16(IDS_NEW_TAB_CLOSE_MENU_EXPAND));
502 #endif
504 NewTabPageHandler::GetLocalizedValues(profile_, &load_time_data);
505 NTPLoginHandler::GetLocalizedValues(profile_, &load_time_data);
507 webui::SetFontAndTextDirection(&load_time_data);
509 // Control fade and resize animations.
510 load_time_data.SetBoolean("anim",
511 gfx::Animation::ShouldRenderRichAnimation());
513 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
514 int alignment = tp->GetDisplayProperty(
515 ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
516 load_time_data.SetString("themegravity",
517 (alignment & ThemeProperties::ALIGN_RIGHT) ? "right" : "");
519 // Disable the promo if this is the first run, otherwise set the promo string
520 // for display if there is a valid outstanding promo.
521 if (first_run::IsChromeFirstRun()) {
522 NotificationPromo::HandleClosed(NotificationPromo::NTP_NOTIFICATION_PROMO);
523 } else {
524 NotificationPromo notification_promo;
525 notification_promo.InitFromPrefs(NotificationPromo::NTP_NOTIFICATION_PROMO);
526 if (notification_promo.CanShow()) {
527 load_time_data.SetString("notificationPromoText",
528 notification_promo.promo_text());
529 DVLOG(1) << "Notification promo:" << notification_promo.promo_text();
532 NotificationPromo bubble_promo;
533 bubble_promo.InitFromPrefs(NotificationPromo::NTP_BUBBLE_PROMO);
534 if (bubble_promo.CanShow()) {
535 load_time_data.SetString("bubblePromoText",
536 bubble_promo.promo_text());
537 DVLOG(1) << "Bubble promo:" << bubble_promo.promo_text();
541 // Determine whether to show the menu for accessing tabs on other devices.
542 bool show_other_sessions_menu = should_show_other_devices_menu_ &&
543 !CommandLine::ForCurrentProcess()->HasSwitch(
544 switches::kDisableNTPOtherSessionsMenu);
545 load_time_data.SetBoolean("showOtherSessionsMenu", show_other_sessions_menu);
546 load_time_data.SetBoolean("isUserSignedIn",
547 !prefs->GetString(prefs::kGoogleServicesUsername).empty());
549 // Load the new tab page appropriate for this build.
550 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance().
551 GetRawDataResource(IDR_NEW_TAB_4_HTML));
552 webui::UseVersion2 version2;
553 std::string full_html =
554 webui::GetI18nTemplateHtml(new_tab_html, &load_time_data);
555 new_tab_html_ = base::RefCountedString::TakeString(&full_html);
558 void NTPResourceCache::CreateNewTabIncognitoCSS() {
559 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
560 DCHECK(tp);
562 // Get our theme colors
563 SkColor color_background =
564 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
566 // Generate the replacements.
567 std::vector<std::string> subst;
569 // Cache-buster for background.
570 subst.push_back(
571 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1
573 // Colors.
574 subst.push_back(SkColorToRGBAString(color_background)); // $2
575 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3
576 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4
577 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5
579 // Get our template.
580 static const base::StringPiece new_tab_theme_css(
581 ResourceBundle::GetSharedInstance().GetRawDataResource(
582 IDR_NEW_INCOGNITO_TAB_THEME_CSS));
584 // Create the string from our template and the replacements.
585 std::string full_css = ReplaceStringPlaceholders(
586 new_tab_theme_css, subst, NULL);
588 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css);
591 void NTPResourceCache::CreateNewTabGuestCSS() {
592 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
593 DCHECK(tp);
595 // Get our theme colors
596 SkColor color_background =
597 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
599 // Generate the replacements.
600 std::vector<std::string> subst;
602 // Cache-buster for background.
603 subst.push_back(
604 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1
606 // Colors.
607 subst.push_back(SkColorToRGBAString(color_background)); // $2
608 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3
609 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4
610 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5
612 // Get our template.
613 static const base::StringPiece new_tab_theme_css(
614 ResourceBundle::GetSharedInstance().GetRawDataResource(
615 IDR_NEW_GUEST_TAB_THEME_CSS));
617 // Create the string from our template and the replacements.
618 std::string full_css = ReplaceStringPlaceholders(
619 new_tab_theme_css, subst, NULL);
621 new_tab_guest_css_ = base::RefCountedString::TakeString(&full_css);
624 void NTPResourceCache::CreateNewTabCSS() {
625 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
626 DCHECK(tp);
628 // Get our theme colors
629 SkColor color_background =
630 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
631 SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT);
632 SkColor color_link = GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK);
633 SkColor color_link_underline =
634 GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK_UNDERLINE);
636 SkColor color_section =
637 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION);
638 SkColor color_section_text =
639 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_TEXT);
640 SkColor color_section_link =
641 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK);
642 SkColor color_section_link_underline =
643 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE);
644 SkColor color_section_header_text =
645 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT);
646 SkColor color_section_header_text_hover =
647 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT_HOVER);
648 SkColor color_section_header_rule =
649 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE);
650 SkColor color_section_header_rule_light =
651 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE_LIGHT);
652 SkColor color_text_light =
653 GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT);
655 SkColor color_header =
656 GetThemeColor(tp, ThemeProperties::COLOR_NTP_HEADER);
657 // Generate a lighter color for the header gradients.
658 color_utils::HSL header_lighter;
659 color_utils::SkColorToHSL(color_header, &header_lighter);
660 header_lighter.l += (1 - header_lighter.l) * 0.33;
661 SkColor color_header_gradient_light =
662 color_utils::HSLToSkColor(header_lighter, SkColorGetA(color_header));
664 // Generate section border color from the header color. See
665 // BookmarkBarView::Paint for how we do this for the bookmark bar
666 // borders.
667 SkColor color_section_border =
668 SkColorSetARGB(80,
669 SkColorGetR(color_header),
670 SkColorGetG(color_header),
671 SkColorGetB(color_header));
673 // Generate the replacements.
674 std::vector<std::string> subst;
676 // Cache-buster for background.
677 subst.push_back(
678 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1
680 // Colors.
681 subst.push_back(SkColorToRGBAString(color_background)); // $2
682 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3
683 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4
684 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5
685 subst.push_back(SkColorToRGBAString(color_header)); // $6
686 subst.push_back(SkColorToRGBAString(color_header_gradient_light)); // $7
687 subst.push_back(SkColorToRGBAString(color_text)); // $8
688 subst.push_back(SkColorToRGBAString(color_link)); // $9
689 subst.push_back(SkColorToRGBAString(color_section)); // $10
690 subst.push_back(SkColorToRGBAString(color_section_border)); // $11
691 subst.push_back(SkColorToRGBAString(color_section_text)); // $12
692 subst.push_back(SkColorToRGBAString(color_section_link)); // $13
693 subst.push_back(SkColorToRGBAString(color_link_underline)); // $14
694 subst.push_back(SkColorToRGBAString(color_section_link_underline)); // $15
695 subst.push_back(SkColorToRGBAString(color_section_header_text)); // $16
696 subst.push_back(SkColorToRGBAString(
697 color_section_header_text_hover)); // $17
698 subst.push_back(SkColorToRGBAString(color_section_header_rule)); // $18
699 subst.push_back(SkColorToRGBAString(
700 color_section_header_rule_light)); // $19
701 subst.push_back(SkColorToRGBAString(
702 SkColorSetA(color_section_header_rule, 0))); // $20
703 subst.push_back(SkColorToRGBAString(color_text_light)); // $21
704 subst.push_back(SkColorToRGBComponents(color_section_border)); // $22
705 subst.push_back(SkColorToRGBComponents(color_text)); // $23
707 // Get our template.
708 static const base::StringPiece new_tab_theme_css(
709 ResourceBundle::GetSharedInstance().GetRawDataResource(
710 IDR_NEW_TAB_4_THEME_CSS));
712 // Create the string from our template and the replacements.
713 std::string css_string;
714 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL);
715 new_tab_css_ = base::RefCountedString::TakeString(&css_string);