Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / ntp / ntp_resource_cache.cc
blob3315a9627dd14ffb59a6145694054e3dad97a041
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.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"
66 #endif
68 #if defined(OS_MACOSX)
69 #include "chrome/browser/platform_util.h"
70 #endif
72 using content::BrowserThread;
74 namespace {
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";
80 #else
81 "https://support.google.com/chrome/?p=incognito";
82 #endif
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://support.google.com/chromebook/answer/1057090";
88 #else
89 "https://support.google.com/chrome/?p=ui_guest";
90 #endif
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(
96 "rgba(%d,%d,%d,%s)",
97 SkColorGetR(color),
98 SkColorGetG(color),
99 SkColorGetB(color),
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(
107 "%d,%d,%d",
108 SkColorGetR(color),
109 SkColorGetG(color),
110 SkColorGetB(color));
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,
124 bool bar_attached) {
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)) {
130 return "-64px";
133 int alignment = theme_provider->GetDisplayProperty(
134 ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
136 if (bar_attached)
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);
163 } // namespace
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_other_devices_menu_(true) {
169 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
170 content::Source<ThemeService>(
171 ThemeServiceFactory::GetForProfile(profile)));
172 registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
173 content::NotificationService::AllSources());
175 PromoResourceService* promo_service =
176 g_browser_process->promo_resource_service();
177 if (promo_service) {
178 promo_resource_subscription_ = promo_service->RegisterStateChangedCallback(
179 base::Bind(&NTPResourceCache::Invalidate, base::Unretained(this)));
182 base::Closure callback = base::Bind(&NTPResourceCache::OnPreferenceChanged,
183 base::Unretained(this));
185 // Watch for pref changes that cause us to need to invalidate the HTML cache.
186 profile_pref_change_registrar_.Init(profile_->GetPrefs());
187 profile_pref_change_registrar_.Add(bookmarks::prefs::kShowBookmarkBar,
188 callback);
189 profile_pref_change_registrar_.Add(prefs::kNtpShownPage, callback);
190 profile_pref_change_registrar_.Add(prefs::kSignInPromoShowNTPBubble,
191 callback);
192 profile_pref_change_registrar_.Add(prefs::kHideWebStoreIcon, callback);
194 // Some tests don't have a local state.
195 #if defined(ENABLE_APP_LIST)
196 if (g_browser_process->local_state()) {
197 local_state_pref_change_registrar_.Init(g_browser_process->local_state());
198 local_state_pref_change_registrar_.Add(prefs::kShowAppLauncherPromo,
199 callback);
200 local_state_pref_change_registrar_.Add(
201 prefs::kAppLauncherHasBeenEnabled, callback);
203 #endif
206 NTPResourceCache::~NTPResourceCache() {}
208 bool NTPResourceCache::NewTabCacheNeedsRefresh() {
209 #if defined(OS_MACOSX)
210 // Invalidate if the current value is different from the cached value.
211 bool is_enabled = platform_util::IsSwipeTrackingFromScrollEventsEnabled();
212 if (is_enabled != is_swipe_tracking_from_scroll_events_enabled_) {
213 is_swipe_tracking_from_scroll_events_enabled_ = is_enabled;
214 return true;
216 #endif
217 bool should_show_apps_page = NewTabUI::ShouldShowApps();
218 if (should_show_apps_page != should_show_apps_page_) {
219 should_show_apps_page_ = should_show_apps_page;
220 return true;
222 return false;
225 NTPResourceCache::WindowType NTPResourceCache::GetWindowType(
226 Profile* profile, content::RenderProcessHost* render_host) {
227 if (profile->IsGuestSession()) {
228 return NTPResourceCache::GUEST;
229 } else if (render_host) {
230 // Sometimes the |profile| is the parent (non-incognito) version of the user
231 // so we check the |render_host| if it is provided.
232 if (render_host->GetBrowserContext()->IsOffTheRecord())
233 return NTPResourceCache::INCOGNITO;
234 } else if (profile->IsOffTheRecord()) {
235 return NTPResourceCache::INCOGNITO;
237 return NTPResourceCache::NORMAL;
240 base::RefCountedMemory* NTPResourceCache::GetNewTabHTML(WindowType win_type) {
241 DCHECK_CURRENTLY_ON(BrowserThread::UI);
242 if (win_type == GUEST) {
243 if (!new_tab_guest_html_.get())
244 CreateNewTabGuestHTML();
245 return new_tab_guest_html_.get();
246 } else if (win_type == INCOGNITO) {
247 if (!new_tab_incognito_html_.get())
248 CreateNewTabIncognitoHTML();
249 return new_tab_incognito_html_.get();
250 } else {
251 // Refresh the cached HTML if necessary.
252 // NOTE: NewTabCacheNeedsRefresh() must be called every time the new tab
253 // HTML is fetched, because it needs to initialize cached values.
254 if (NewTabCacheNeedsRefresh() || !new_tab_html_.get())
255 CreateNewTabHTML();
256 return new_tab_html_.get();
260 base::RefCountedMemory* NTPResourceCache::GetNewTabCSS(WindowType win_type) {
261 DCHECK_CURRENTLY_ON(BrowserThread::UI);
262 if (win_type == GUEST) {
263 if (!new_tab_guest_css_.get())
264 CreateNewTabGuestCSS();
265 return new_tab_guest_css_.get();
266 } else if (win_type == INCOGNITO) {
267 if (!new_tab_incognito_css_.get())
268 CreateNewTabIncognitoCSS();
269 return new_tab_incognito_css_.get();
270 } else {
271 if (!new_tab_css_.get())
272 CreateNewTabCSS();
273 return new_tab_css_.get();
277 void NTPResourceCache::Observe(int type,
278 const content::NotificationSource& source,
279 const content::NotificationDetails& details) {
280 // Invalidate the cache.
281 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type ||
282 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) {
283 Invalidate();
284 } else {
285 NOTREACHED();
289 void NTPResourceCache::OnPreferenceChanged() {
290 // A change occurred to one of the preferences we care about, so flush the
291 // cache.
292 new_tab_incognito_html_ = NULL;
293 new_tab_html_ = NULL;
294 new_tab_css_ = NULL;
297 void NTPResourceCache::Invalidate() {
298 new_tab_incognito_html_ = nullptr;
299 new_tab_html_ = nullptr;
300 new_tab_incognito_css_ = nullptr;
301 // TODO(dbeam): Check if it is necessary to clear the CSS on promo changes.
302 new_tab_css_ = nullptr;
305 void NTPResourceCache::CreateNewTabIncognitoHTML() {
306 base::DictionaryValue localized_strings;
307 localized_strings.SetString("title",
308 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
309 int new_tab_description_ids = IDS_NEW_TAB_OTR_DESCRIPTION;
310 int new_tab_heading_ids = IDS_NEW_TAB_OTR_HEADING;
311 int new_tab_link_ids = IDS_NEW_TAB_OTR_LEARN_MORE_LINK;
312 int new_tab_warning_ids = IDS_NEW_TAB_OTR_MESSAGE_WARNING;
313 int new_tab_html_idr = IDR_INCOGNITO_TAB_HTML;
314 const char* new_tab_link = kLearnMoreIncognitoUrl;
316 if (profile_->IsGuestSession()) {
317 localized_strings.SetString("guestTabDescription",
318 l10n_util::GetStringUTF16(new_tab_description_ids));
319 localized_strings.SetString("guestTabHeading",
320 l10n_util::GetStringUTF16(new_tab_heading_ids));
321 } else {
322 localized_strings.SetString("incognitoTabDescription",
323 l10n_util::GetStringUTF16(new_tab_description_ids));
324 localized_strings.SetString("incognitoTabHeading",
325 l10n_util::GetStringUTF16(new_tab_heading_ids));
326 localized_strings.SetString("incognitoTabWarning",
327 l10n_util::GetStringUTF16(new_tab_warning_ids));
330 localized_strings.SetString("learnMore",
331 l10n_util::GetStringUTF16(new_tab_link_ids));
332 localized_strings.SetString("learnMoreLink", new_tab_link);
334 bool bookmark_bar_attached =
335 profile_->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar);
336 localized_strings.SetBoolean("bookmarkbarattached", bookmark_bar_attached);
338 const std::string& app_locale = g_browser_process->GetApplicationLocale();
339 webui::SetLoadTimeDataDefaults(app_locale, &localized_strings);
341 static const base::StringPiece incognito_tab_html(
342 ResourceBundle::GetSharedInstance().GetRawDataResource(
343 new_tab_html_idr));
345 std::string full_html = webui::GetI18nTemplateHtml(
346 incognito_tab_html, &localized_strings);
348 new_tab_incognito_html_ = base::RefCountedString::TakeString(&full_html);
351 void NTPResourceCache::CreateNewTabGuestHTML() {
352 base::DictionaryValue localized_strings;
353 localized_strings.SetString("title",
354 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
355 const char* guest_tab_link = kLearnMoreGuestSessionUrl;
356 int guest_tab_ids = IDR_GUEST_TAB_HTML;
357 int guest_tab_description_ids = IDS_NEW_TAB_GUEST_SESSION_DESCRIPTION;
358 int guest_tab_heading_ids = IDS_NEW_TAB_GUEST_SESSION_HEADING;
359 int guest_tab_link_ids = IDS_NEW_TAB_GUEST_SESSION_LEARN_MORE_LINK;
361 #if defined(OS_CHROMEOS)
362 guest_tab_ids = IDR_GUEST_SESSION_TAB_HTML;
364 policy::BrowserPolicyConnectorChromeOS* connector =
365 g_browser_process->platform_part()->browser_policy_connector_chromeos();
366 std::string enterprise_domain = connector->GetEnterpriseDomain();
368 if (!enterprise_domain.empty()) {
369 // Device is enterprise enrolled.
370 localized_strings.SetString("enterpriseInfoVisible", "true");
371 base::string16 enterprise_info = l10n_util::GetStringFUTF16(
372 IDS_DEVICE_OWNED_BY_NOTICE,
373 base::UTF8ToUTF16(enterprise_domain));
374 localized_strings.SetString("enterpriseInfoMessage", enterprise_info);
375 localized_strings.SetString("enterpriseLearnMore",
376 l10n_util::GetStringUTF16(IDS_LEARN_MORE));
377 localized_strings.SetString("enterpriseInfoHintLink",
378 chrome::kLearnMoreEnterpriseURL);
379 } else {
380 localized_strings.SetString("enterpriseInfoVisible", "false");
382 #endif
384 localized_strings.SetString("guestTabDescription",
385 l10n_util::GetStringUTF16(guest_tab_description_ids));
386 localized_strings.SetString("guestTabHeading",
387 l10n_util::GetStringUTF16(guest_tab_heading_ids));
388 localized_strings.SetString("learnMore",
389 l10n_util::GetStringUTF16(guest_tab_link_ids));
390 localized_strings.SetString("learnMoreLink", guest_tab_link);
392 const std::string& app_locale = g_browser_process->GetApplicationLocale();
393 webui::SetLoadTimeDataDefaults(app_locale, &localized_strings);
395 static const base::StringPiece guest_tab_html(
396 ResourceBundle::GetSharedInstance().GetRawDataResource(guest_tab_ids));
398 std::string full_html = webui::GetI18nTemplateHtml(
399 guest_tab_html, &localized_strings);
401 new_tab_guest_html_ = base::RefCountedString::TakeString(&full_html);
404 void NTPResourceCache::CreateNewTabHTML() {
405 // TODO(estade): these strings should be defined in their relevant handlers
406 // (in GetLocalizedValues) and should have more legible names.
407 // Show the profile name in the title and most visited labels if the current
408 // profile is not the default.
409 PrefService* prefs = profile_->GetPrefs();
410 base::DictionaryValue load_time_data;
411 load_time_data.SetBoolean("bookmarkbarattached",
412 prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar));
413 load_time_data.SetBoolean("hasattribution",
414 ThemeServiceFactory::GetForProfile(profile_)->HasCustomImage(
415 IDR_THEME_NTP_ATTRIBUTION));
416 load_time_data.SetBoolean("showAppLauncherPromo",
417 ShouldShowAppLauncherPromo());
418 load_time_data.SetString("title",
419 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
420 load_time_data.SetString("webStoreTitle",
421 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE));
422 load_time_data.SetString("webStoreTitleShort",
423 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE_SHORT));
424 load_time_data.SetString("attributionintro",
425 l10n_util::GetStringUTF16(IDS_NEW_TAB_ATTRIBUTION_INTRO));
426 load_time_data.SetString("appuninstall",
427 l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
428 load_time_data.SetString("appoptions",
429 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_OPTIONS));
430 load_time_data.SetString("appdetails",
431 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_DETAILS));
432 load_time_data.SetString("appinfodialog",
433 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_SHOW_INFO));
434 load_time_data.SetString("appcreateshortcut",
435 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_CREATE_SHORTCUT));
436 load_time_data.SetString("appDefaultPageName",
437 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME));
438 load_time_data.SetString("applaunchtypepinned",
439 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_PINNED));
440 load_time_data.SetString("applaunchtyperegular",
441 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_REGULAR));
442 load_time_data.SetString("applaunchtypewindow",
443 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW));
444 load_time_data.SetString("applaunchtypefullscreen",
445 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN));
446 load_time_data.SetString("syncpromotext",
447 l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL));
448 load_time_data.SetString("syncLinkText",
449 l10n_util::GetStringUTF16(IDS_SYNC_ADVANCED_OPTIONS));
450 load_time_data.SetBoolean("shouldShowSyncLogin",
451 AppLauncherLoginHandler::ShouldShow(profile_));
452 load_time_data.SetString("learnMore",
453 l10n_util::GetStringUTF16(IDS_LEARN_MORE));
454 const std::string& app_locale = g_browser_process->GetApplicationLocale();
455 load_time_data.SetString("webStoreLink",
456 google_util::AppendGoogleLocaleParam(
457 GURL(extension_urls::GetWebstoreLaunchURL()), app_locale).spec());
458 load_time_data.SetString("appInstallHintText",
459 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_INSTALL_HINT_LABEL));
460 load_time_data.SetString("learn_more",
461 l10n_util::GetStringUTF16(IDS_LEARN_MORE));
462 load_time_data.SetString("tile_grid_screenreader_accessible_description",
463 l10n_util::GetStringUTF16(IDS_NEW_TAB_TILE_GRID_ACCESSIBLE_DESCRIPTION));
464 load_time_data.SetString("page_switcher_change_title",
465 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_CHANGE_TITLE));
466 load_time_data.SetString("page_switcher_same_title",
467 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_SAME_TITLE));
468 load_time_data.SetString("appsPromoTitle",
469 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_APPS_PROMO_TITLE));
470 // On Mac OS X 10.7+, horizontal scrolling can be treated as a back or
471 // forward gesture. Pass through a flag that indicates whether or not that
472 // feature is enabled.
473 load_time_data.SetBoolean("isSwipeTrackingFromScrollEventsEnabled",
474 is_swipe_tracking_from_scroll_events_enabled_);
476 load_time_data.SetBoolean("showApps", should_show_apps_page_);
477 load_time_data.SetBoolean("showWebStoreIcon",
478 !prefs->GetBoolean(prefs::kHideWebStoreIcon));
480 bool bookmark_apps_enabled = extensions::util::IsNewBookmarkAppsEnabled();
481 load_time_data.SetBoolean("enableNewBookmarkApps", bookmark_apps_enabled);
483 load_time_data.SetBoolean("canShowAppInfoDialog",
484 CanShowAppInfoDialog());
486 #if defined(OS_CHROMEOS)
487 load_time_data.SetString("expandMenu",
488 l10n_util::GetStringUTF16(IDS_NEW_TAB_CLOSE_MENU_EXPAND));
489 #endif
491 NewTabPageHandler::GetLocalizedValues(profile_, &load_time_data);
492 AppLauncherLoginHandler::GetLocalizedValues(profile_, &load_time_data);
494 webui::SetLoadTimeDataDefaults(app_locale, &load_time_data);
496 // Control fade and resize animations.
497 load_time_data.SetBoolean("anim",
498 gfx::Animation::ShouldRenderRichAnimation());
500 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
501 int alignment = tp->GetDisplayProperty(
502 ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
503 load_time_data.SetString("themegravity",
504 (alignment & ThemeProperties::ALIGN_RIGHT) ? "right" : "");
506 // Disable the promo if this is the first run, otherwise set the promo string
507 // for display if there is a valid outstanding promo.
508 if (first_run::IsChromeFirstRun()) {
509 NotificationPromo::HandleClosed(NotificationPromo::NTP_NOTIFICATION_PROMO);
510 } else {
511 NotificationPromo notification_promo;
512 notification_promo.InitFromPrefs(NotificationPromo::NTP_NOTIFICATION_PROMO);
513 if (notification_promo.CanShow()) {
514 load_time_data.SetString("notificationPromoText",
515 notification_promo.promo_text());
516 DVLOG(1) << "Notification promo:" << notification_promo.promo_text();
519 NotificationPromo bubble_promo;
520 bubble_promo.InitFromPrefs(NotificationPromo::NTP_BUBBLE_PROMO);
521 if (bubble_promo.CanShow()) {
522 load_time_data.SetString("bubblePromoText",
523 bubble_promo.promo_text());
524 DVLOG(1) << "Bubble promo:" << bubble_promo.promo_text();
528 load_time_data.SetBoolean(
529 "isUserSignedIn",
530 SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated());
532 // Load the new tab page appropriate for this build.
533 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance().
534 GetRawDataResource(IDR_NEW_TAB_4_HTML));
535 std::string full_html =
536 webui::GetI18nTemplateHtml(new_tab_html, &load_time_data);
537 new_tab_html_ = base::RefCountedString::TakeString(&full_html);
540 void NTPResourceCache::CreateNewTabIncognitoCSS() {
541 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
542 DCHECK(tp);
544 // Get our theme colors
545 SkColor color_background =
546 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
548 // Generate the replacements.
549 std::vector<std::string> subst;
551 // Cache-buster for background.
552 subst.push_back(
553 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1
555 // Colors.
556 subst.push_back(SkColorToRGBAString(color_background)); // $2
557 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3
558 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4
559 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5
561 // Get our template.
562 static const base::StringPiece new_tab_theme_css(
563 ResourceBundle::GetSharedInstance().GetRawDataResource(
564 IDR_NEW_INCOGNITO_TAB_THEME_CSS));
566 // Create the string from our template and the replacements.
567 std::string full_css = ReplaceStringPlaceholders(
568 new_tab_theme_css, subst, NULL);
570 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css);
573 void NTPResourceCache::CreateNewTabGuestCSS() {
574 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
575 DCHECK(tp);
577 // Get our theme colors
578 SkColor color_background =
579 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
581 // Generate the replacements.
582 std::vector<std::string> subst;
584 // Cache-buster for background.
585 subst.push_back(
586 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1
588 // Colors.
589 subst.push_back(SkColorToRGBAString(color_background)); // $2
590 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3
591 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4
592 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5
594 // Get our template.
595 static const base::StringPiece new_tab_theme_css(
596 ResourceBundle::GetSharedInstance().GetRawDataResource(
597 IDR_NEW_INCOGNITO_TAB_THEME_CSS));
599 // Create the string from our template and the replacements.
600 std::string full_css = ReplaceStringPlaceholders(
601 new_tab_theme_css, subst, NULL);
603 new_tab_guest_css_ = base::RefCountedString::TakeString(&full_css);
606 void NTPResourceCache::CreateNewTabCSS() {
607 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
608 DCHECK(tp);
610 // Get our theme colors
611 SkColor color_background =
612 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
613 SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT);
614 SkColor color_link = GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK);
615 SkColor color_link_underline =
616 GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK_UNDERLINE);
618 SkColor color_section =
619 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION);
620 SkColor color_section_text =
621 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_TEXT);
622 SkColor color_section_link =
623 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK);
624 SkColor color_section_link_underline =
625 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE);
626 SkColor color_section_header_text =
627 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT);
628 SkColor color_section_header_text_hover =
629 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT_HOVER);
630 SkColor color_section_header_rule =
631 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE);
632 SkColor color_section_header_rule_light =
633 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE_LIGHT);
634 SkColor color_text_light =
635 GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT);
637 SkColor color_header =
638 GetThemeColor(tp, ThemeProperties::COLOR_NTP_HEADER);
639 // Generate a lighter color for the header gradients.
640 color_utils::HSL header_lighter;
641 color_utils::SkColorToHSL(color_header, &header_lighter);
642 header_lighter.l += (1 - header_lighter.l) * 0.33;
643 SkColor color_header_gradient_light =
644 color_utils::HSLToSkColor(header_lighter, SkColorGetA(color_header));
646 // Generate section border color from the header color. See
647 // BookmarkBarView::Paint for how we do this for the bookmark bar
648 // borders.
649 SkColor color_section_border =
650 SkColorSetARGB(80,
651 SkColorGetR(color_header),
652 SkColorGetG(color_header),
653 SkColorGetB(color_header));
655 // Generate the replacements.
656 std::vector<std::string> subst;
658 // Cache-buster for background.
659 subst.push_back(
660 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1
662 // Colors.
663 subst.push_back(SkColorToRGBAString(color_background)); // $2
664 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3
665 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4
666 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5
667 subst.push_back(SkColorToRGBAString(color_header)); // $6
668 subst.push_back(SkColorToRGBAString(color_header_gradient_light)); // $7
669 subst.push_back(SkColorToRGBAString(color_text)); // $8
670 subst.push_back(SkColorToRGBAString(color_link)); // $9
671 subst.push_back(SkColorToRGBAString(color_section)); // $10
672 subst.push_back(SkColorToRGBAString(color_section_border)); // $11
673 subst.push_back(SkColorToRGBAString(color_section_text)); // $12
674 subst.push_back(SkColorToRGBAString(color_section_link)); // $13
675 subst.push_back(SkColorToRGBAString(color_link_underline)); // $14
676 subst.push_back(SkColorToRGBAString(color_section_link_underline)); // $15
677 subst.push_back(SkColorToRGBAString(color_section_header_text)); // $16
678 subst.push_back(SkColorToRGBAString(
679 color_section_header_text_hover)); // $17
680 subst.push_back(SkColorToRGBAString(color_section_header_rule)); // $18
681 subst.push_back(SkColorToRGBAString(
682 color_section_header_rule_light)); // $19
683 subst.push_back(SkColorToRGBAString(
684 SkColorSetA(color_section_header_rule, 0))); // $20
685 subst.push_back(SkColorToRGBAString(color_text_light)); // $21
686 subst.push_back(SkColorToRGBComponents(color_section_border)); // $22
687 subst.push_back(SkColorToRGBComponents(color_text)); // $23
689 // Get our template.
690 static const base::StringPiece new_tab_theme_css(
691 ResourceBundle::GetSharedInstance().GetRawDataResource(
692 IDR_NEW_TAB_4_THEME_CSS));
694 // Create the string from our template and the replacements.
695 std::string css_string;
696 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL);
697 new_tab_css_ = base::RefCountedString::TakeString(&css_string);