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