Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / ntp / ntp_resource_cache.cc
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.
4
5 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
6
7 #include <string>
8 #include <vector>
9
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/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/web_resource/notification_promo.h"
36 #include "chrome/common/chrome_switches.h"
37 #include "chrome/common/pref_names.h"
38 #include "chrome/common/url_constants.h"
39 #include "chrome/grit/chromium_strings.h"
40 #include "chrome/grit/generated_resources.h"
41 #include "chrome/grit/locale_settings.h"
42 #include "components/google/core/browser/google_util.h"
43 #include "content/public/browser/browser_thread.h"
44 #include "content/public/browser/notification_service.h"
45 #include "content/public/browser/render_process_host.h"
46 #include "extensions/common/extension.h"
47 #include "extensions/common/extension_urls.h"
48 #include "grit/browser_resources.h"
49 #include "grit/components_strings.h"
50 #include "grit/theme_resources.h"
51 #include "ui/base/l10n/l10n_util.h"
52 #include "ui/base/resource/resource_bundle.h"
53 #include "ui/base/theme_provider.h"
54 #include "ui/base/webui/jstemplate_builder.h"
55 #include "ui/base/webui/web_ui_util.h"
56 #include "ui/gfx/animation/animation.h"
57 #include "ui/gfx/color_utils.h"
58 #include "ui/gfx/sys_color_change_listener.h"
59
60 #if defined(OS_CHROMEOS)
61 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
62 #include "chromeos/chromeos_switches.h"
63 #endif
64
65 #if defined(OS_MACOSX)
66 #include "chrome/browser/platform_util.h"
67 #endif
68
69 using content::BrowserThread;
70
71 namespace {
72
73 // The URL for the the Learn More page shown on incognito new tab.
74 const char kLearnMoreIncognitoUrl[] =
75 #if defined(OS_CHROMEOS)
76     "https://www.google.com/support/chromeos/bin/answer.py?answer=95464";
77 #else
78     "https://www.google.com/support/chrome/bin/answer.py?answer=95464";
79 #endif
80
81 // The URL for the Learn More page shown on guest session new tab.
82 const char kLearnMoreGuestSessionUrl[] =
83 #if defined(OS_CHROMEOS)
84     "https://www.google.com/support/chromeos/bin/answer.py?answer=1057090";
85 #else
86     "https://support.google.com/chrome/?p=ui_guest";
87 #endif
88
89 std::string SkColorToRGBAString(SkColor color) {
90   // We convert the alpha using DoubleToString because StringPrintf will use
91   // locale specific formatters (e.g., use , instead of . in German).
92   return base::StringPrintf(
93       "rgba(%d,%d,%d,%s)",
94       SkColorGetR(color),
95       SkColorGetG(color),
96       SkColorGetB(color),
97       base::DoubleToString(SkColorGetA(color) / 255.0).c_str());
98 }
99
100 // Creates an rgb string for an SkColor, but leaves the alpha blank so that the
101 // css can fill it in.
102 std::string SkColorToRGBComponents(SkColor color) {
103   return base::StringPrintf(
104       "%d,%d,%d",
105       SkColorGetR(color),
106       SkColorGetG(color),
107       SkColorGetB(color));
108 }
109
110 SkColor GetThemeColor(ui::ThemeProvider* tp, int id) {
111   SkColor color = tp->GetColor(id);
112   // If web contents are being inverted because the system is in high-contrast
113   // mode, any system theme colors we use must be inverted too to cancel out.
114   return gfx::IsInvertedColorScheme() ?
115       color_utils::InvertColor(color) : color;
116 }
117
118 // Get the CSS string for the background position on the new tab page for the
119 // states when the bar is attached or detached.
120 std::string GetNewTabBackgroundCSS(const ui::ThemeProvider* theme_provider,
121                                    bool bar_attached) {
122   // TODO(glen): This is a quick workaround to hide the notused.png image when
123   // no image is provided - we don't have time right now to figure out why
124   // this is painting as white.
125   // http://crbug.com/17593
126   if (!theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
127     return "-64px";
128   }
129
130   int alignment = theme_provider->GetDisplayProperty(
131       ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
132
133   if (bar_attached)
134     return ThemeProperties::AlignmentToString(alignment);
135
136   if (alignment & ThemeProperties::ALIGN_TOP) {
137     // The bar is detached, so we must offset the background by the bar size
138     // if it's a top-aligned bar.
139     int offset = chrome::kNTPBookmarkBarHeight;
140
141     if (alignment & ThemeProperties::ALIGN_LEFT)
142       return "left " + base::IntToString(-offset) + "px";
143     else if (alignment & ThemeProperties::ALIGN_RIGHT)
144       return "right " + base::IntToString(-offset) + "px";
145     return "center " + base::IntToString(-offset) + "px";
146   }
147
148   return ThemeProperties::AlignmentToString(alignment);
149 }
150
151 // How the background image on the new tab page should be tiled (see tiling
152 // masks in theme_service.h).
153 std::string GetNewTabBackgroundTilingCSS(
154     const ui::ThemeProvider* theme_provider) {
155   int repeat_mode = theme_provider->GetDisplayProperty(
156       ThemeProperties::NTP_BACKGROUND_TILING);
157   return ThemeProperties::TilingToString(repeat_mode);
158 }
159
160 }  // namespace
161
162 NTPResourceCache::NTPResourceCache(Profile* profile)
163     : profile_(profile), is_swipe_tracking_from_scroll_events_enabled_(false),
164       should_show_apps_page_(NewTabUI::ShouldShowApps()),
165       should_show_most_visited_page_(true),
166       should_show_other_devices_menu_(true),
167       should_show_recently_closed_menu_(true) {
168   registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
169                  content::Source<ThemeService>(
170                      ThemeServiceFactory::GetForProfile(profile)));
171   registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
172                  content::NotificationService::AllSources());
173
174   base::Closure callback = base::Bind(&NTPResourceCache::OnPreferenceChanged,
175                                       base::Unretained(this));
176
177   // Watch for pref changes that cause us to need to invalidate the HTML cache.
178   profile_pref_change_registrar_.Init(profile_->GetPrefs());
179   profile_pref_change_registrar_.Add(bookmarks::prefs::kShowBookmarkBar,
180                                      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);
185
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);
194   }
195 #endif
196 }
197
198 NTPResourceCache::~NTPResourceCache() {}
199
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;
207   }
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;
213   }
214   return false;
215 }
216
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;
228   }
229   return NTPResourceCache::NORMAL;
230 }
231
232 base::RefCountedMemory* NTPResourceCache::GetNewTabHTML(WindowType win_type) {
233   DCHECK_CURRENTLY_ON(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();
249   }
250 }
251
252 base::RefCountedMemory* NTPResourceCache::GetNewTabCSS(WindowType win_type) {
253   DCHECK_CURRENTLY_ON(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();
266   }
267 }
268
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();
281   }
282 }
283
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;
290 }
291
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_description_ids = IDS_NEW_TAB_OTR_DESCRIPTION;
297   int new_tab_heading_ids = IDS_NEW_TAB_OTR_HEADING;
298   int new_tab_link_ids = IDS_NEW_TAB_OTR_LEARN_MORE_LINK;
299   int new_tab_warning_ids = IDS_NEW_TAB_OTR_MESSAGE_WARNING;
300   int new_tab_html_idr = IDR_INCOGNITO_TAB_HTML;
301   const char* new_tab_link = kLearnMoreIncognitoUrl;
302
303   if (profile_->IsGuestSession()) {
304     localized_strings.SetString("guestTabDescription",
305         l10n_util::GetStringUTF16(new_tab_description_ids));
306     localized_strings.SetString("guestTabHeading",
307         l10n_util::GetStringUTF16(new_tab_heading_ids));
308   } else {
309     localized_strings.SetString("incognitoTabDescription",
310         l10n_util::GetStringUTF16(new_tab_description_ids));
311     localized_strings.SetString("incognitoTabHeading",
312         l10n_util::GetStringUTF16(new_tab_heading_ids));
313     localized_strings.SetString("incognitoTabWarning",
314         l10n_util::GetStringUTF16(new_tab_warning_ids));
315   }
316
317   localized_strings.SetString("learnMore",
318       l10n_util::GetStringUTF16(new_tab_link_ids));
319   localized_strings.SetString("learnMoreLink", new_tab_link);
320
321   bool bookmark_bar_attached =
322       profile_->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar);
323   localized_strings.SetBoolean("bookmarkbarattached", bookmark_bar_attached);
324
325   webui::SetFontAndTextDirection(&localized_strings);
326
327   static const base::StringPiece incognito_tab_html(
328       ResourceBundle::GetSharedInstance().GetRawDataResource(
329           new_tab_html_idr));
330
331   std::string full_html = webui::GetI18nTemplateHtml(
332       incognito_tab_html, &localized_strings);
333
334   new_tab_incognito_html_ = base::RefCountedString::TakeString(&full_html);
335 }
336
337 void NTPResourceCache::CreateNewTabGuestHTML() {
338   base::DictionaryValue localized_strings;
339   localized_strings.SetString("title",
340       l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
341   const char* guest_tab_link = kLearnMoreGuestSessionUrl;
342   int guest_tab_ids = IDR_GUEST_TAB_HTML;
343   int guest_tab_description_ids = IDS_NEW_TAB_GUEST_SESSION_DESCRIPTION;
344   int guest_tab_heading_ids = IDS_NEW_TAB_GUEST_SESSION_HEADING;
345   int guest_tab_link_ids = IDS_NEW_TAB_GUEST_SESSION_LEARN_MORE_LINK;
346
347 #if defined(OS_CHROMEOS)
348   guest_tab_ids = IDR_GUEST_SESSION_TAB_HTML;
349
350   policy::BrowserPolicyConnectorChromeOS* connector =
351       g_browser_process->platform_part()->browser_policy_connector_chromeos();
352   std::string enterprise_domain = connector->GetEnterpriseDomain();
353
354   if (!enterprise_domain.empty()) {
355     // Device is enterprise enrolled.
356     localized_strings.SetString("enterpriseInfoVisible", "true");
357     base::string16 enterprise_info = l10n_util::GetStringFUTF16(
358         IDS_DEVICE_OWNED_BY_NOTICE,
359         base::UTF8ToUTF16(enterprise_domain));
360     localized_strings.SetString("enterpriseInfoMessage", enterprise_info);
361     localized_strings.SetString("enterpriseLearnMore",
362         l10n_util::GetStringUTF16(IDS_LEARN_MORE));
363     localized_strings.SetString("enterpriseInfoHintLink",
364                                 chrome::kLearnMoreEnterpriseURL);
365   } else {
366     localized_strings.SetString("enterpriseInfoVisible", "false");
367   }
368 #endif
369
370   localized_strings.SetString("guestTabDescription",
371       l10n_util::GetStringUTF16(guest_tab_description_ids));
372   localized_strings.SetString("guestTabHeading",
373       l10n_util::GetStringUTF16(guest_tab_heading_ids));
374   localized_strings.SetString("learnMore",
375       l10n_util::GetStringUTF16(guest_tab_link_ids));
376   localized_strings.SetString("learnMoreLink", guest_tab_link);
377
378   webui::SetFontAndTextDirection(&localized_strings);
379
380   static const base::StringPiece guest_tab_html(
381       ResourceBundle::GetSharedInstance().GetRawDataResource(guest_tab_ids));
382
383   std::string full_html = webui::GetI18nTemplateHtml(
384       guest_tab_html, &localized_strings);
385
386   new_tab_guest_html_ = base::RefCountedString::TakeString(&full_html);
387 }
388
389 void NTPResourceCache::CreateNewTabHTML() {
390   // TODO(estade): these strings should be defined in their relevant handlers
391   // (in GetLocalizedValues) and should have more legible names.
392   // Show the profile name in the title and most visited labels if the current
393   // profile is not the default.
394   PrefService* prefs = profile_->GetPrefs();
395   base::DictionaryValue load_time_data;
396   load_time_data.SetBoolean("bookmarkbarattached",
397       prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar));
398   load_time_data.SetBoolean("hasattribution",
399       ThemeServiceFactory::GetForProfile(profile_)->HasCustomImage(
400           IDR_THEME_NTP_ATTRIBUTION));
401   load_time_data.SetBoolean("showMostvisited", should_show_most_visited_page_);
402   load_time_data.SetBoolean("showAppLauncherPromo",
403       ShouldShowAppLauncherPromo());
404   load_time_data.SetBoolean("showRecentlyClosed",
405       should_show_recently_closed_menu_);
406   load_time_data.SetString("title",
407       l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
408   load_time_data.SetString("mostvisited",
409       l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED));
410   load_time_data.SetString("suggestions",
411       l10n_util::GetStringUTF16(IDS_NEW_TAB_SUGGESTIONS));
412   load_time_data.SetString("restoreThumbnailsShort",
413       l10n_util::GetStringUTF16(IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK));
414   load_time_data.SetString("recentlyclosed",
415       l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED));
416   load_time_data.SetString("webStoreTitle",
417       l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE));
418   load_time_data.SetString("webStoreTitleShort",
419       l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE_SHORT));
420   load_time_data.SetString("closedwindowsingle",
421       l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_SINGLE));
422   load_time_data.SetString("closedwindowmultiple",
423       l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_MULTIPLE));
424   load_time_data.SetString("attributionintro",
425       l10n_util::GetStringUTF16(IDS_NEW_TAB_ATTRIBUTION_INTRO));
426   load_time_data.SetString("thumbnailremovednotification",
427       l10n_util::GetStringUTF16(IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION));
428   load_time_data.SetString("undothumbnailremove",
429       l10n_util::GetStringUTF16(IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE));
430   load_time_data.SetString("removethumbnailtooltip",
431       l10n_util::GetStringUTF16(IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP));
432   load_time_data.SetString("appuninstall",
433       l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
434   load_time_data.SetString("appoptions",
435       l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_OPTIONS));
436   load_time_data.SetString("appdetails",
437       l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_DETAILS));
438   load_time_data.SetString("appcreateshortcut",
439       l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_CREATE_SHORTCUT));
440   load_time_data.SetString("appDefaultPageName",
441       l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME));
442   load_time_data.SetString("applaunchtypepinned",
443       l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_PINNED));
444   load_time_data.SetString("applaunchtyperegular",
445       l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_REGULAR));
446   load_time_data.SetString("applaunchtypewindow",
447       l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW));
448   load_time_data.SetString("applaunchtypefullscreen",
449       l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN));
450   load_time_data.SetString("syncpromotext",
451       l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL));
452   load_time_data.SetString("syncLinkText",
453       l10n_util::GetStringUTF16(IDS_SYNC_ADVANCED_OPTIONS));
454   load_time_data.SetBoolean("shouldShowSyncLogin",
455                             NTPLoginHandler::ShouldShow(profile_));
456   load_time_data.SetString("otherSessions",
457       l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_LABEL));
458   load_time_data.SetString("otherSessionsEmpty",
459       l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_EMPTY));
460   load_time_data.SetString("otherSessionsLearnMoreUrl",
461       l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_LEARN_MORE_URL));
462   load_time_data.SetString("learnMore",
463       l10n_util::GetStringUTF16(IDS_LEARN_MORE));
464   load_time_data.SetString("webStoreLink",
465       google_util::AppendGoogleLocaleParam(
466           GURL(extension_urls::GetWebstoreLaunchURL()),
467           g_browser_process->GetApplicationLocale()).spec());
468   load_time_data.SetString("appInstallHintText",
469       l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_INSTALL_HINT_LABEL));
470   load_time_data.SetBoolean("isDiscoveryInNTPEnabled",
471       NewTabUI::IsDiscoveryInNTPEnabled());
472   load_time_data.SetString("collapseSessionMenuItemText",
473       l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_COLLAPSE_SESSION));
474   load_time_data.SetString("expandSessionMenuItemText",
475       l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_EXPAND_SESSION));
476   load_time_data.SetString("restoreSessionMenuItemText",
477       l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_OPEN_ALL));
478   load_time_data.SetString("learn_more",
479       l10n_util::GetStringUTF16(IDS_LEARN_MORE));
480   load_time_data.SetString("tile_grid_screenreader_accessible_description",
481       l10n_util::GetStringUTF16(IDS_NEW_TAB_TILE_GRID_ACCESSIBLE_DESCRIPTION));
482   load_time_data.SetString("page_switcher_change_title",
483       l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_CHANGE_TITLE));
484   load_time_data.SetString("page_switcher_same_title",
485       l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_SAME_TITLE));
486   load_time_data.SetString("appsPromoTitle",
487       l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_APPS_PROMO_TITLE));
488   // On Mac OS X 10.7+, horizontal scrolling can be treated as a back or
489   // forward gesture. Pass through a flag that indicates whether or not that
490   // feature is enabled.
491   load_time_data.SetBoolean("isSwipeTrackingFromScrollEventsEnabled",
492                             is_swipe_tracking_from_scroll_events_enabled_);
493
494   load_time_data.SetBoolean("showApps", should_show_apps_page_);
495   load_time_data.SetBoolean("showWebStoreIcon",
496                             !prefs->GetBoolean(prefs::kHideWebStoreIcon));
497
498   bool streamlined_hosted_apps =
499       extensions::util::IsStreamlinedHostedAppsEnabled();
500   load_time_data.SetBoolean("enableStreamlinedHostedApps",
501                             streamlined_hosted_apps);
502   // Use a different string for launching as a regular tab for streamlined
503   // hosted apps.
504   if (streamlined_hosted_apps) {
505     load_time_data.SetString("applaunchtypetab",
506         l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_TAB));
507   }
508
509 #if defined(OS_CHROMEOS)
510   load_time_data.SetString("expandMenu",
511       l10n_util::GetStringUTF16(IDS_NEW_TAB_CLOSE_MENU_EXPAND));
512 #endif
513
514   NewTabPageHandler::GetLocalizedValues(profile_, &load_time_data);
515   NTPLoginHandler::GetLocalizedValues(profile_, &load_time_data);
516
517   webui::SetFontAndTextDirection(&load_time_data);
518
519   // Control fade and resize animations.
520   load_time_data.SetBoolean("anim",
521                             gfx::Animation::ShouldRenderRichAnimation());
522
523   ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
524   int alignment = tp->GetDisplayProperty(
525       ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
526   load_time_data.SetString("themegravity",
527       (alignment & ThemeProperties::ALIGN_RIGHT) ? "right" : "");
528
529   // Disable the promo if this is the first run, otherwise set the promo string
530   // for display if there is a valid outstanding promo.
531   if (first_run::IsChromeFirstRun()) {
532     NotificationPromo::HandleClosed(NotificationPromo::NTP_NOTIFICATION_PROMO);
533   } else {
534     NotificationPromo notification_promo;
535     notification_promo.InitFromPrefs(NotificationPromo::NTP_NOTIFICATION_PROMO);
536     if (notification_promo.CanShow()) {
537       load_time_data.SetString("notificationPromoText",
538                                notification_promo.promo_text());
539       DVLOG(1) << "Notification promo:" << notification_promo.promo_text();
540     }
541
542     NotificationPromo bubble_promo;
543     bubble_promo.InitFromPrefs(NotificationPromo::NTP_BUBBLE_PROMO);
544     if (bubble_promo.CanShow()) {
545       load_time_data.SetString("bubblePromoText",
546                                bubble_promo.promo_text());
547       DVLOG(1) << "Bubble promo:" << bubble_promo.promo_text();
548     }
549   }
550
551   // Determine whether to show the menu for accessing tabs on other devices.
552   bool show_other_sessions_menu = should_show_other_devices_menu_ &&
553       !CommandLine::ForCurrentProcess()->HasSwitch(
554           switches::kDisableNTPOtherSessionsMenu);
555   load_time_data.SetBoolean("showOtherSessionsMenu", show_other_sessions_menu);
556   load_time_data.SetBoolean("isUserSignedIn",
557       !prefs->GetString(prefs::kGoogleServicesUsername).empty());
558
559   // Load the new tab page appropriate for this build.
560   base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance().
561       GetRawDataResource(IDR_NEW_TAB_4_HTML));
562   std::string full_html =
563       webui::GetI18nTemplateHtml(new_tab_html, &load_time_data);
564   new_tab_html_ = base::RefCountedString::TakeString(&full_html);
565 }
566
567 void NTPResourceCache::CreateNewTabIncognitoCSS() {
568   ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
569   DCHECK(tp);
570
571   // Get our theme colors
572   SkColor color_background =
573       GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
574
575   // Generate the replacements.
576   std::vector<std::string> subst;
577
578   // Cache-buster for background.
579   subst.push_back(
580       profile_->GetPrefs()->GetString(prefs::kCurrentThemeID));  // $1
581
582   // Colors.
583   subst.push_back(SkColorToRGBAString(color_background));  // $2
584   subst.push_back(GetNewTabBackgroundCSS(tp, false));  // $3
585   subst.push_back(GetNewTabBackgroundCSS(tp, true));  // $4
586   subst.push_back(GetNewTabBackgroundTilingCSS(tp));  // $5
587
588   // Get our template.
589   static const base::StringPiece new_tab_theme_css(
590       ResourceBundle::GetSharedInstance().GetRawDataResource(
591           IDR_NEW_INCOGNITO_TAB_THEME_CSS));
592
593   // Create the string from our template and the replacements.
594   std::string full_css = ReplaceStringPlaceholders(
595       new_tab_theme_css, subst, NULL);
596
597   new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css);
598 }
599
600 void NTPResourceCache::CreateNewTabGuestCSS() {
601   ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
602   DCHECK(tp);
603
604   // Get our theme colors
605   SkColor color_background =
606       GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
607
608   // Generate the replacements.
609   std::vector<std::string> subst;
610
611   // Cache-buster for background.
612   subst.push_back(
613       profile_->GetPrefs()->GetString(prefs::kCurrentThemeID));  // $1
614
615   // Colors.
616   subst.push_back(SkColorToRGBAString(color_background));  // $2
617   subst.push_back(GetNewTabBackgroundCSS(tp, false));  // $3
618   subst.push_back(GetNewTabBackgroundCSS(tp, true));  // $4
619   subst.push_back(GetNewTabBackgroundTilingCSS(tp));  // $5
620
621   // Get our template.
622   static const base::StringPiece new_tab_theme_css(
623       ResourceBundle::GetSharedInstance().GetRawDataResource(
624           IDR_NEW_GUEST_TAB_THEME_CSS));
625
626   // Create the string from our template and the replacements.
627   std::string full_css = ReplaceStringPlaceholders(
628       new_tab_theme_css, subst, NULL);
629
630   new_tab_guest_css_ = base::RefCountedString::TakeString(&full_css);
631 }
632
633 void NTPResourceCache::CreateNewTabCSS() {
634   ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
635   DCHECK(tp);
636
637   // Get our theme colors
638   SkColor color_background =
639       GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
640   SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT);
641   SkColor color_link = GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK);
642   SkColor color_link_underline =
643       GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK_UNDERLINE);
644
645   SkColor color_section =
646       GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION);
647   SkColor color_section_text =
648       GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_TEXT);
649   SkColor color_section_link =
650       GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK);
651   SkColor color_section_link_underline =
652       GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE);
653   SkColor color_section_header_text =
654       GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT);
655   SkColor color_section_header_text_hover =
656       GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT_HOVER);
657   SkColor color_section_header_rule =
658       GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE);
659   SkColor color_section_header_rule_light =
660       GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE_LIGHT);
661   SkColor color_text_light =
662       GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT);
663
664   SkColor color_header =
665       GetThemeColor(tp, ThemeProperties::COLOR_NTP_HEADER);
666   // Generate a lighter color for the header gradients.
667   color_utils::HSL header_lighter;
668   color_utils::SkColorToHSL(color_header, &header_lighter);
669   header_lighter.l += (1 - header_lighter.l) * 0.33;
670   SkColor color_header_gradient_light =
671       color_utils::HSLToSkColor(header_lighter, SkColorGetA(color_header));
672
673   // Generate section border color from the header color. See
674   // BookmarkBarView::Paint for how we do this for the bookmark bar
675   // borders.
676   SkColor color_section_border =
677       SkColorSetARGB(80,
678                      SkColorGetR(color_header),
679                      SkColorGetG(color_header),
680                      SkColorGetB(color_header));
681
682   // Generate the replacements.
683   std::vector<std::string> subst;
684
685   // Cache-buster for background.
686   subst.push_back(
687       profile_->GetPrefs()->GetString(prefs::kCurrentThemeID));  // $1
688
689   // Colors.
690   subst.push_back(SkColorToRGBAString(color_background));  // $2
691   subst.push_back(GetNewTabBackgroundCSS(tp, false));  // $3
692   subst.push_back(GetNewTabBackgroundCSS(tp, true));  // $4
693   subst.push_back(GetNewTabBackgroundTilingCSS(tp));  // $5
694   subst.push_back(SkColorToRGBAString(color_header));  // $6
695   subst.push_back(SkColorToRGBAString(color_header_gradient_light));  // $7
696   subst.push_back(SkColorToRGBAString(color_text));  // $8
697   subst.push_back(SkColorToRGBAString(color_link));  // $9
698   subst.push_back(SkColorToRGBAString(color_section));  // $10
699   subst.push_back(SkColorToRGBAString(color_section_border));  // $11
700   subst.push_back(SkColorToRGBAString(color_section_text));  // $12
701   subst.push_back(SkColorToRGBAString(color_section_link));  // $13
702   subst.push_back(SkColorToRGBAString(color_link_underline));  // $14
703   subst.push_back(SkColorToRGBAString(color_section_link_underline));  // $15
704   subst.push_back(SkColorToRGBAString(color_section_header_text));  // $16
705   subst.push_back(SkColorToRGBAString(
706       color_section_header_text_hover));  // $17
707   subst.push_back(SkColorToRGBAString(color_section_header_rule));  // $18
708   subst.push_back(SkColorToRGBAString(
709       color_section_header_rule_light));  // $19
710   subst.push_back(SkColorToRGBAString(
711       SkColorSetA(color_section_header_rule, 0)));  // $20
712   subst.push_back(SkColorToRGBAString(color_text_light));  // $21
713   subst.push_back(SkColorToRGBComponents(color_section_border));  // $22
714   subst.push_back(SkColorToRGBComponents(color_text));  // $23
715
716   // Get our template.
717   static const base::StringPiece new_tab_theme_css(
718       ResourceBundle::GetSharedInstance().GetRawDataResource(
719           IDR_NEW_TAB_4_THEME_CSS));
720
721   // Create the string from our template and the replacements.
722   std::string css_string;
723   css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL);
724   new_tab_css_ = base::RefCountedString::TakeString(&css_string);
725 }