Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: chrome/browser/ui/webui/ntp/app_launcher_handler.cc

Issue 1757673002: NTP4/apps page: fix page stickiness (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@remove-promo-cpp
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" 5 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 30 matching lines...) Expand all
41 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" 41 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
42 #include "chrome/common/chrome_switches.h" 42 #include "chrome/common/chrome_switches.h"
43 #include "chrome/common/extensions/extension_constants.h" 43 #include "chrome/common/extensions/extension_constants.h"
44 #include "chrome/common/extensions/extension_metrics.h" 44 #include "chrome/common/extensions/extension_metrics.h"
45 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" 45 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
46 #include "chrome/common/pref_names.h" 46 #include "chrome/common/pref_names.h"
47 #include "chrome/common/url_constants.h" 47 #include "chrome/common/url_constants.h"
48 #include "chrome/common/web_application_info.h" 48 #include "chrome/common/web_application_info.h"
49 #include "chrome/grit/generated_resources.h" 49 #include "chrome/grit/generated_resources.h"
50 #include "components/favicon_base/favicon_types.h" 50 #include "components/favicon_base/favicon_types.h"
51 #include "components/pref_registry/pref_registry_syncable.h"
51 #include "components/prefs/pref_service.h" 52 #include "components/prefs/pref_service.h"
52 #include "components/prefs/scoped_user_pref_update.h" 53 #include "components/prefs/scoped_user_pref_update.h"
53 #include "content/public/browser/notification_service.h" 54 #include "content/public/browser/notification_service.h"
54 #include "content/public/browser/web_ui.h" 55 #include "content/public/browser/web_ui.h"
55 #include "content/public/common/favicon_url.h" 56 #include "content/public/common/favicon_url.h"
56 #include "extensions/browser/app_sorting.h" 57 #include "extensions/browser/app_sorting.h"
57 #include "extensions/browser/extension_prefs.h" 58 #include "extensions/browser/extension_prefs.h"
58 #include "extensions/browser/extension_registry.h" 59 #include "extensions/browser/extension_registry.h"
59 #include "extensions/browser/extension_system.h" 60 #include "extensions/browser/extension_system.h"
60 #include "extensions/browser/management_policy.h" 61 #include "extensions/browser/management_policy.h"
(...skipping 11 matching lines...) Expand all
72 using extensions::AppSorting; 73 using extensions::AppSorting;
73 using extensions::CrxInstaller; 74 using extensions::CrxInstaller;
74 using extensions::Extension; 75 using extensions::Extension;
75 using extensions::ExtensionPrefs; 76 using extensions::ExtensionPrefs;
76 using extensions::ExtensionRegistry; 77 using extensions::ExtensionRegistry;
77 using extensions::ExtensionSet; 78 using extensions::ExtensionSet;
78 using extensions::ExtensionSystem; 79 using extensions::ExtensionSystem;
79 80
80 namespace { 81 namespace {
81 82
83 // The purpose of this enum is to track which page on the NTP is showing.
84 // The lower 10 bits of kNtpShownPage are used for the index within the page
85 // group, and the rest of the bits are used for the page group ID (defined
86 // here).
87 static const int kPageIdOffset = 10;
88 enum {
89 INDEX_MASK = (1 << kPageIdOffset) - 1,
90 APPS_PAGE_ID = 2 << kPageIdOffset,
91 };
92
82 void RecordAppLauncherPromoHistogram( 93 void RecordAppLauncherPromoHistogram(
83 apps::AppLauncherPromoHistogramValues value) { 94 apps::AppLauncherPromoHistogramValues value) {
84 DCHECK_LT(value, apps::APP_LAUNCHER_PROMO_MAX); 95 DCHECK_LT(value, apps::APP_LAUNCHER_PROMO_MAX);
85 UMA_HISTOGRAM_ENUMERATION( 96 UMA_HISTOGRAM_ENUMERATION(
86 "Apps.AppLauncherPromo", value, apps::APP_LAUNCHER_PROMO_MAX); 97 "Apps.AppLauncherPromo", value, apps::APP_LAUNCHER_PROMO_MAX);
87 } 98 }
88 99
89 // This is used to avoid a DCHECK due to an unhandled WebUI callback. The
90 // JavaScript used to switch between pages sends "pageSelected" which is used
91 // in the context of the NTP for recording metrics we don't need here.
92 void NoOpCallback(const base::ListValue* args) {}
93
94 } // namespace 100 } // namespace
95 101
96 AppLauncherHandler::AppInstallInfo::AppInstallInfo() {} 102 AppLauncherHandler::AppInstallInfo::AppInstallInfo() {}
97 103
98 AppLauncherHandler::AppInstallInfo::~AppInstallInfo() {} 104 AppLauncherHandler::AppInstallInfo::~AppInstallInfo() {}
99 105
100 AppLauncherHandler::AppLauncherHandler(ExtensionService* extension_service) 106 AppLauncherHandler::AppLauncherHandler(ExtensionService* extension_service)
101 : extension_service_(extension_service), 107 : extension_service_(extension_service),
102 ignore_changes_(false), 108 ignore_changes_(false),
103 attempted_bookmark_app_install_(false), 109 attempted_bookmark_app_install_(false),
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 // ordinal). The webstore's app launch ordinal is always set to the first 205 // ordinal). The webstore's app launch ordinal is always set to the first
200 // position. 206 // position.
201 app_launch_ordinal = extension->id() == extensions::kWebStoreAppId ? 207 app_launch_ordinal = extension->id() == extensions::kWebStoreAppId ?
202 sorting->CreateFirstAppLaunchOrdinal(page_ordinal) : 208 sorting->CreateFirstAppLaunchOrdinal(page_ordinal) :
203 sorting->CreateNextAppLaunchOrdinal(page_ordinal); 209 sorting->CreateNextAppLaunchOrdinal(page_ordinal);
204 sorting->SetAppLaunchOrdinal(extension->id(), app_launch_ordinal); 210 sorting->SetAppLaunchOrdinal(extension->id(), app_launch_ordinal);
205 } 211 }
206 value->SetString("app_launch_ordinal", app_launch_ordinal.ToInternalValue()); 212 value->SetString("app_launch_ordinal", app_launch_ordinal.ToInternalValue());
207 } 213 }
208 214
215 // static
216 void AppLauncherHandler::GetLocalizedValues(Profile* profile,
217 base::DictionaryValue* values) {
218 PrefService* prefs = profile->GetPrefs();
219 int shown_page = prefs->GetInteger(prefs::kNtpShownPage);
220 values->SetInteger("shown_page_index", shown_page & INDEX_MASK);
221 }
222
223 // static
224 void AppLauncherHandler::RegisterProfilePrefs(
225 user_prefs::PrefRegistrySyncable* registry) {
226 // TODO(estade): should be syncable.
Evan Stade 2016/03/03 19:59:53 this is so meh-worthy I wouldn't mind if you remov
Dan Beam 2016/03/03 21:09:41 Done.
227 registry->RegisterIntegerPref(prefs::kNtpShownPage, APPS_PAGE_ID);
228 }
229
209 void AppLauncherHandler::RegisterMessages() { 230 void AppLauncherHandler::RegisterMessages() {
210 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, 231 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP,
211 content::Source<WebContents>(web_ui()->GetWebContents())); 232 content::Source<WebContents>(web_ui()->GetWebContents()));
212 233
213 // Some tests don't have a local state. 234 // Some tests don't have a local state.
214 #if defined(ENABLE_APP_LIST) 235 #if defined(ENABLE_APP_LIST)
215 if (g_browser_process->local_state()) { 236 if (g_browser_process->local_state()) {
216 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); 237 local_state_pref_change_registrar_.Init(g_browser_process->local_state());
217 local_state_pref_change_registrar_.Add( 238 local_state_pref_change_registrar_.Add(
218 prefs::kShowAppLauncherPromo, 239 prefs::kShowAppLauncherPromo,
(...skipping 25 matching lines...) Expand all
244 web_ui()->RegisterMessageCallback("setPageIndex", 265 web_ui()->RegisterMessageCallback("setPageIndex",
245 base::Bind(&AppLauncherHandler::HandleSetPageIndex, 266 base::Bind(&AppLauncherHandler::HandleSetPageIndex,
246 base::Unretained(this))); 267 base::Unretained(this)));
247 web_ui()->RegisterMessageCallback("saveAppPageName", 268 web_ui()->RegisterMessageCallback("saveAppPageName",
248 base::Bind(&AppLauncherHandler::HandleSaveAppPageName, 269 base::Bind(&AppLauncherHandler::HandleSaveAppPageName,
249 base::Unretained(this))); 270 base::Unretained(this)));
250 web_ui()->RegisterMessageCallback("generateAppForLink", 271 web_ui()->RegisterMessageCallback("generateAppForLink",
251 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink, 272 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink,
252 base::Unretained(this))); 273 base::Unretained(this)));
253 web_ui()->RegisterMessageCallback("stopShowingAppLauncherPromo", 274 web_ui()->RegisterMessageCallback("stopShowingAppLauncherPromo",
254 base::Bind(&AppLauncherHandler::StopShowingAppLauncherPromo, 275 base::Bind(&AppLauncherHandler::HandleStopShowingAppLauncherPromo,
255 base::Unretained(this))); 276 base::Unretained(this)));
256 web_ui()->RegisterMessageCallback("onLearnMore", 277 web_ui()->RegisterMessageCallback("onLearnMore",
257 base::Bind(&AppLauncherHandler::OnLearnMore, 278 base::Bind(&AppLauncherHandler::HandleOnLearnMore,
258 base::Unretained(this))); 279 base::Unretained(this)));
259 web_ui()->RegisterMessageCallback("pageSelected", base::Bind(&NoOpCallback)); 280 web_ui()->RegisterMessageCallback("pageSelected",
281 base::Bind(&AppLauncherHandler::HandlePageSelected,
282 base::Unretained(this)));
260 } 283 }
261 284
262 void AppLauncherHandler::Observe(int type, 285 void AppLauncherHandler::Observe(int type,
263 const content::NotificationSource& source, 286 const content::NotificationSource& source,
264 const content::NotificationDetails& details) { 287 const content::NotificationDetails& details) {
265 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) { 288 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) {
266 highlight_app_id_ = *content::Details<const std::string>(details).ptr(); 289 highlight_app_id_ = *content::Details<const std::string>(details).ptr();
267 if (has_loaded_apps_) 290 if (has_loaded_apps_)
268 SetAppToBeHighlighted(); 291 SetAppToBeHighlighted();
269 return; 292 return;
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 install_info->page_ordinal = page_ordinal; 726 install_info->page_ordinal = page_ordinal;
704 727
705 favicon_service->GetFaviconImageForPageURL( 728 favicon_service->GetFaviconImageForPageURL(
706 launch_url, 729 launch_url,
707 base::Bind(&AppLauncherHandler::OnFaviconForApp, 730 base::Bind(&AppLauncherHandler::OnFaviconForApp,
708 base::Unretained(this), 731 base::Unretained(this),
709 base::Passed(&install_info)), 732 base::Passed(&install_info)),
710 &cancelable_task_tracker_); 733 &cancelable_task_tracker_);
711 } 734 }
712 735
713 void AppLauncherHandler::StopShowingAppLauncherPromo( 736 void AppLauncherHandler::HandleStopShowingAppLauncherPromo(
714 const base::ListValue* args) { 737 const base::ListValue* args) {
715 #if defined(ENABLE_APP_LIST) 738 #if defined(ENABLE_APP_LIST)
716 g_browser_process->local_state()->SetBoolean( 739 g_browser_process->local_state()->SetBoolean(
717 prefs::kShowAppLauncherPromo, false); 740 prefs::kShowAppLauncherPromo, false);
718 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_DISMISSED); 741 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_DISMISSED);
719 #endif 742 #endif
720 } 743 }
721 744
722 void AppLauncherHandler::OnLearnMore(const base::ListValue* args) { 745 void AppLauncherHandler::HandleOnLearnMore(const base::ListValue* args) {
723 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_LEARN_MORE); 746 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_LEARN_MORE);
724 } 747 }
725 748
749 void AppLauncherHandler::HandlePageSelected(const base::ListValue* args) {
750 double index_double;
751 CHECK(args->GetDouble(0, &index_double));
752 int index = static_cast<int>(index_double);
753
754 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
755 prefs->SetInteger(prefs::kNtpShownPage, APPS_PAGE_ID | index);
756 }
757
726 void AppLauncherHandler::OnFaviconForApp( 758 void AppLauncherHandler::OnFaviconForApp(
727 scoped_ptr<AppInstallInfo> install_info, 759 scoped_ptr<AppInstallInfo> install_info,
728 const favicon_base::FaviconImageResult& image_result) { 760 const favicon_base::FaviconImageResult& image_result) {
729 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo()); 761 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo());
730 web_app->title = install_info->title; 762 web_app->title = install_info->title;
731 web_app->app_url = install_info->app_url; 763 web_app->app_url = install_info->app_url;
732 764
733 if (!image_result.image.IsEmpty()) { 765 if (!image_result.image.IsEmpty()) {
734 WebApplicationInfo::IconInfo icon; 766 WebApplicationInfo::IconInfo icon;
735 icon.data = image_result.image.AsBitmap(); 767 icon.data = image_result.image.AsBitmap();
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 base::FundamentalValue(!extension_id_prompting_.empty())); 880 base::FundamentalValue(!extension_id_prompting_.empty()));
849 } 881 }
850 882
851 bool AppLauncherHandler::ShouldShow(const Extension* extension) const { 883 bool AppLauncherHandler::ShouldShow(const Extension* extension) const {
852 if (ignore_changes_ || !has_loaded_apps_ || !extension->is_app()) 884 if (ignore_changes_ || !has_loaded_apps_ || !extension->is_app())
853 return false; 885 return false;
854 886
855 Profile* profile = Profile::FromWebUI(web_ui()); 887 Profile* profile = Profile::FromWebUI(web_ui());
856 return extensions::ui_util::ShouldDisplayInNewTabPage(extension, profile); 888 return extensions::ui_util::ShouldDisplayInNewTabPage(extension, profile);
857 } 889 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698