Commit 2a565a3b authored by dpapad's avatar dpapad Committed by Chromium LUCI CQ

Serve chrome://apps from a regular WebUIDataSource.

Previously because of the long history of this page (which at some
point served as the NTP, aka NTP4) it was served by a custom class
which delegated logic to NTPResourceCache. There is no good reason to
use the NTPResourceCache for chrome://apps anymore, AFAIK.

Using a WebUIDataSource makes a lot of things simpler, like registering
resources, localized strings, load time data, and many more.

This is in preparation of possibly migrating chrome://apps to JS modules
(TBD). Regardless, moving away from NTPResourceCache is a positive step.

Bug: 1168053
Change-Id: If82d634c8d7e699dc1c906517c23c40bfaa0b6b8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2629855Reviewed-by: default avatarEsmael Elmoslimany <aee@chromium.org>
Commit-Queue: dpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846035}
parent 90f41215
......@@ -16,6 +16,8 @@
<link id="themecss" rel="stylesheet">
<script src="../../../../ui/webui/resources/js/assert.js"></script>
<script src="../../../../ui/webui/resources/js/util.js"></script>
<script src="../../../../ui/webui/resources/js/load_time_data.js"></script>
<script src="chrome://apps/strings.js"></script>
<script>
// Until themes can clear the cache, force-reload the theme stylesheet.
$('themecss').href = 'chrome://theme/css/new_tab_theme.css?' + Date.now();
......
......@@ -6,7 +6,7 @@
#define CHROME_BROWSER_UI_WEBUI_APP_LAUNCHER_PAGE_UI_H_
#include "base/macros.h"
#include "content/public/browser/url_data_source.h"
#include "components/prefs/pref_change_registrar.h"
#include "content/public/browser/web_ui_controller.h"
#include "ui/base/layout.h"
......@@ -31,32 +31,10 @@ class AppLauncherPageUI : public content::WebUIController {
const base::ListValue& args) override;
private:
class HTMLSource : public content::URLDataSource {
public:
explicit HTMLSource(Profile* profile);
~HTMLSource() override;
// content::URLDataSource implementation.
std::string GetSource() override;
void StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) override;
std::string GetMimeType(const std::string&) override;
bool ShouldReplaceExistingSource() override;
bool AllowCaching() override;
std::string GetContentSecurityPolicy(
network::mojom::CSPDirectiveName directive) override;
private:
// Pointer back to the original profile.
Profile* profile_;
DISALLOW_COPY_AND_ASSIGN(HTMLSource);
};
void OnHideWebStoreIconChanged();
Profile* GetProfile() const;
PrefChangeRegistrar pref_change_registrar_;
DISALLOW_COPY_AND_ASSIGN(AppLauncherPageUI);
};
......
......@@ -404,11 +404,12 @@ void AppLauncherHandler::CreateExtensionInfo(const Extension* extension,
}
// static
void AppLauncherHandler::GetLocalizedValues(Profile* profile,
base::DictionaryValue* values) {
void AppLauncherHandler::RegisterLoadTimeData(
Profile* profile,
content::WebUIDataSource* source) {
PrefService* prefs = profile->GetPrefs();
int shown_page = prefs->GetInteger(prefs::kNtpShownPage);
values->SetInteger("shown_page_index", shown_page & INDEX_MASK);
source->AddInteger("shown_page_index", shown_page & INDEX_MASK);
}
// static
......
......@@ -25,6 +25,7 @@
#include "components/sync/model/string_ordinal.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/extension.h"
......@@ -69,8 +70,8 @@ class AppLauncherHandler
base::DictionaryValue* value);
// Registers values (strings etc.) for the page.
static void GetLocalizedValues(Profile* profile,
base::DictionaryValue* values);
static void RegisterLoadTimeData(Profile* profile,
content::WebUIDataSource* source);
// Register per-profile preferences.
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
......
......@@ -21,12 +21,10 @@
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/apps/app_info_dialog.h"
#include "chrome/browser/ui/cookie_controls/cookie_controls_service.h"
#include "chrome/browser/ui/cookie_controls/cookie_controls_service_factory.h"
#include "chrome/browser/ui/layout_constants.h"
......@@ -43,16 +41,13 @@
#include "components/bookmarks/common/bookmark_pref_names.h"
#include "components/content_settings/core/common/cookie_controls_enforcement.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/google/core/common/google_util.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/common/extension_urls.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/template_expressions.h"
......@@ -60,7 +55,6 @@
#include "ui/base/webui/jstemplate_builder.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/chromeos/devicetype_utils.h"
#include "ui/gfx/animation/animation.h"
#include "ui/gfx/color_utils.h"
#include "ui/native_theme/native_theme.h"
......@@ -69,10 +63,6 @@
#include "chromeos/strings/grit/chromeos_strings.h"
#endif
#if defined(OS_MAC)
#include "chrome/browser/platform_util.h"
#endif
using content::BrowserThread;
namespace {
......@@ -175,7 +165,6 @@ NTPResourceCache::NTPResourceCache(Profile* profile)
profile_pref_change_registrar_.Add(bookmarks::prefs::kShowBookmarkBar,
callback);
profile_pref_change_registrar_.Add(prefs::kNtpShownPage, callback);
profile_pref_change_registrar_.Add(prefs::kHideWebStoreIcon, callback);
profile_pref_change_registrar_.Add(prefs::kCookieControlsMode, callback);
theme_observation_.Observe(ui::NativeTheme::GetInstanceForNativeUi());
......@@ -191,18 +180,6 @@ NTPResourceCache::NTPResourceCache(Profile* profile)
NTPResourceCache::~NTPResourceCache() = default;
bool NTPResourceCache::NewTabHTMLNeedsRefresh() {
#if defined(OS_MAC)
// Invalidate if the current value is different from the cached value.
bool is_enabled = platform_util::IsSwipeTrackingFromScrollEventsEnabled();
if (is_enabled != is_swipe_tracking_from_scroll_events_enabled_) {
is_swipe_tracking_from_scroll_events_enabled_ = is_enabled;
return true;
}
#endif
return false;
}
NTPResourceCache::WindowType NTPResourceCache::GetWindowType(
Profile* profile, content::RenderProcessHost* render_host) {
if (profile->IsGuestSession() || profile->IsEphemeralGuestProfile()) {
......@@ -240,18 +217,10 @@ base::RefCountedMemory* NTPResourceCache::GetNewTabHTML(WindowType win_type) {
return GetNewTabGuestHTML();
}
if (win_type == INCOGNITO) {
DCHECK(win_type == INCOGNITO);
if (!new_tab_incognito_html_)
CreateNewTabIncognitoHTML();
return new_tab_incognito_html_.get();
}
// Refresh the cached HTML if necessary.
// NOTE: NewTabHTMLNeedsRefresh() must be called every time the new tab
// HTML is fetched, because it needs to initialize cached values.
if (NewTabHTMLNeedsRefresh() || !new_tab_html_)
CreateNewTabHTML();
return new_tab_html_.get();
}
base::RefCountedMemory* NTPResourceCache::GetNewTabCSS(WindowType win_type) {
......@@ -483,125 +452,6 @@ scoped_refptr<base::RefCountedString> NTPResourceCache::CreateNewTabGuestHTML(
return base::RefCountedString::TakeString(&full_html);
}
// TODO(alancutter): Consider moving this utility function up somewhere where it
// can be shared with bookmarks_ui.cc.
// Ampersands are used by menus to determine which characters to use as shortcut
// keys. This functionality is not implemented for NTP.
static base::string16 GetLocalizedString(int message_id) {
base::string16 result = l10n_util::GetStringUTF16(message_id);
base::Erase(result, '&');
return result;
}
void NTPResourceCache::CreateNewTabHTML() {
// TODO(estade): these strings should be defined in their relevant handlers
// (in GetLocalizedValues) and should have more legible names.
// Show the profile name in the title and most visited labels if the current
// profile is not the default.
PrefService* prefs = profile_->GetPrefs();
base::DictionaryValue load_time_data;
load_time_data.SetString(
"bookmarkbarattached",
prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar) ? "true" : "false");
load_time_data.SetString("title", GetLocalizedString(IDS_NEW_TAB_TITLE));
load_time_data.SetString("webStoreTitle",
GetLocalizedString(IDS_EXTENSION_WEB_STORE_TITLE));
load_time_data.SetString(
"webStoreTitleShort",
GetLocalizedString(IDS_EXTENSION_WEB_STORE_TITLE_SHORT));
load_time_data.SetString("attributionintro",
GetLocalizedString(IDS_NEW_TAB_ATTRIBUTION_INTRO));
load_time_data.SetString("appuninstall",
GetLocalizedString(IDS_EXTENSIONS_UNINSTALL));
load_time_data.SetString("appoptions",
GetLocalizedString(IDS_NEW_TAB_APP_OPTIONS));
load_time_data.SetString("appdetails",
GetLocalizedString(IDS_NEW_TAB_APP_DETAILS));
load_time_data.SetString("appinfodialog",
GetLocalizedString(IDS_APP_CONTEXT_MENU_SHOW_INFO));
load_time_data.SetString("appcreateshortcut",
GetLocalizedString(IDS_NEW_TAB_APP_CREATE_SHORTCUT));
load_time_data.SetString("appinstalllocally",
GetLocalizedString(IDS_NEW_TAB_APP_INSTALL_LOCALLY));
load_time_data.SetString("appDefaultPageName",
GetLocalizedString(IDS_APP_DEFAULT_PAGE_NAME));
load_time_data.SetString(
"applaunchtypepinned",
GetLocalizedString(IDS_APP_CONTEXT_MENU_OPEN_PINNED));
load_time_data.SetString(
"applaunchtyperegular",
GetLocalizedString(IDS_APP_CONTEXT_MENU_OPEN_REGULAR));
load_time_data.SetString(
"applaunchtypewindow",
GetLocalizedString(IDS_APP_CONTEXT_MENU_OPEN_WINDOW));
load_time_data.SetString(
"applaunchtypefullscreen",
GetLocalizedString(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN));
load_time_data.SetString(
"syncpromotext", GetLocalizedString(IDS_SYNC_START_SYNC_BUTTON_LABEL));
load_time_data.SetString("syncLinkText",
GetLocalizedString(IDS_SYNC_ADVANCED_OPTIONS));
load_time_data.SetBoolean("shouldShowSyncLogin",
AppLauncherLoginHandler::ShouldShow(profile_));
load_time_data.SetString("learnMore", GetLocalizedString(IDS_LEARN_MORE));
const std::string& app_locale = g_browser_process->GetApplicationLocale();
load_time_data.SetString(
"webStoreLink", google_util::AppendGoogleLocaleParam(
extension_urls::GetWebstoreLaunchURL(), app_locale)
.spec());
load_time_data.SetString(
"appInstallHintText",
GetLocalizedString(IDS_NEW_TAB_APP_INSTALL_HINT_LABEL));
load_time_data.SetString("learn_more", GetLocalizedString(IDS_LEARN_MORE));
load_time_data.SetString(
"tile_grid_screenreader_accessible_description",
GetLocalizedString(IDS_NEW_TAB_TILE_GRID_ACCESSIBLE_DESCRIPTION));
load_time_data.SetString(
"page_switcher_change_title",
GetLocalizedString(IDS_NEW_TAB_PAGE_SWITCHER_CHANGE_TITLE));
load_time_data.SetString(
"page_switcher_same_title",
GetLocalizedString(IDS_NEW_TAB_PAGE_SWITCHER_SAME_TITLE));
load_time_data.SetString(
"runonoslogin", GetLocalizedString(IDS_APP_CONTEXT_MENU_RUN_ON_OS_LOGIN));
// On Mac OS X 10.7+, horizontal scrolling can be treated as a back or
// forward gesture. Pass through a flag that indicates whether or not that
// feature is enabled.
load_time_data.SetBoolean("isSwipeTrackingFromScrollEventsEnabled",
is_swipe_tracking_from_scroll_events_enabled_);
load_time_data.SetBoolean("showWebStoreIcon",
!prefs->GetBoolean(prefs::kHideWebStoreIcon));
load_time_data.SetBoolean("canShowAppInfoDialog",
CanPlatformShowAppInfoDialog());
AppLauncherHandler::GetLocalizedValues(profile_, &load_time_data);
webui::SetLoadTimeDataDefaults(app_locale, &load_time_data);
// Control fade and resize animations.
load_time_data.SetBoolean("anim",
gfx::Animation::ShouldRenderRichAnimation());
load_time_data.SetBoolean(
"isUserSignedIn",
IdentityManagerFactory::GetForProfile(profile_)->HasPrimaryAccount());
// Load the new tab page template and localize it.
static const base::NoDestructor<scoped_refptr<base::RefCountedMemory>>
new_tab_html(
ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
IDR_NEW_TAB_4_HTML));
CHECK(*new_tab_html);
std::string full_html = webui::GetI18nTemplateHtml(
base::StringPiece(reinterpret_cast<const char*>((*new_tab_html)->front()),
(*new_tab_html)->size()),
&load_time_data);
new_tab_html_ = base::RefCountedString::TakeString(&full_html);
}
void NTPResourceCache::CreateNewTabIncognitoCSS() {
// Same theme is used by all off-the-record profiles, so just getting it from
// the first one.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment