| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 Loading... |
| 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 registry->RegisterIntegerPref(prefs::kNtpShownPage, APPS_PAGE_ID); |
| 227 } |
| 228 |
| 209 void AppLauncherHandler::RegisterMessages() { | 229 void AppLauncherHandler::RegisterMessages() { |
| 210 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, | 230 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, |
| 211 content::Source<WebContents>(web_ui()->GetWebContents())); | 231 content::Source<WebContents>(web_ui()->GetWebContents())); |
| 212 | 232 |
| 213 // Some tests don't have a local state. | 233 // Some tests don't have a local state. |
| 214 #if defined(ENABLE_APP_LIST) | 234 #if defined(ENABLE_APP_LIST) |
| 215 if (g_browser_process->local_state()) { | 235 if (g_browser_process->local_state()) { |
| 216 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); | 236 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); |
| 217 local_state_pref_change_registrar_.Add( | 237 local_state_pref_change_registrar_.Add( |
| 218 prefs::kShowAppLauncherPromo, | 238 prefs::kShowAppLauncherPromo, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 244 web_ui()->RegisterMessageCallback("setPageIndex", | 264 web_ui()->RegisterMessageCallback("setPageIndex", |
| 245 base::Bind(&AppLauncherHandler::HandleSetPageIndex, | 265 base::Bind(&AppLauncherHandler::HandleSetPageIndex, |
| 246 base::Unretained(this))); | 266 base::Unretained(this))); |
| 247 web_ui()->RegisterMessageCallback("saveAppPageName", | 267 web_ui()->RegisterMessageCallback("saveAppPageName", |
| 248 base::Bind(&AppLauncherHandler::HandleSaveAppPageName, | 268 base::Bind(&AppLauncherHandler::HandleSaveAppPageName, |
| 249 base::Unretained(this))); | 269 base::Unretained(this))); |
| 250 web_ui()->RegisterMessageCallback("generateAppForLink", | 270 web_ui()->RegisterMessageCallback("generateAppForLink", |
| 251 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink, | 271 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink, |
| 252 base::Unretained(this))); | 272 base::Unretained(this))); |
| 253 web_ui()->RegisterMessageCallback("stopShowingAppLauncherPromo", | 273 web_ui()->RegisterMessageCallback("stopShowingAppLauncherPromo", |
| 254 base::Bind(&AppLauncherHandler::StopShowingAppLauncherPromo, | 274 base::Bind(&AppLauncherHandler::HandleStopShowingAppLauncherPromo, |
| 255 base::Unretained(this))); | 275 base::Unretained(this))); |
| 256 web_ui()->RegisterMessageCallback("onLearnMore", | 276 web_ui()->RegisterMessageCallback("onLearnMore", |
| 257 base::Bind(&AppLauncherHandler::OnLearnMore, | 277 base::Bind(&AppLauncherHandler::HandleOnLearnMore, |
| 258 base::Unretained(this))); | 278 base::Unretained(this))); |
| 259 web_ui()->RegisterMessageCallback("pageSelected", base::Bind(&NoOpCallback)); | 279 web_ui()->RegisterMessageCallback("pageSelected", |
| 280 base::Bind(&AppLauncherHandler::HandlePageSelected, |
| 281 base::Unretained(this))); |
| 260 } | 282 } |
| 261 | 283 |
| 262 void AppLauncherHandler::Observe(int type, | 284 void AppLauncherHandler::Observe(int type, |
| 263 const content::NotificationSource& source, | 285 const content::NotificationSource& source, |
| 264 const content::NotificationDetails& details) { | 286 const content::NotificationDetails& details) { |
| 265 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) { | 287 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) { |
| 266 highlight_app_id_ = *content::Details<const std::string>(details).ptr(); | 288 highlight_app_id_ = *content::Details<const std::string>(details).ptr(); |
| 267 if (has_loaded_apps_) | 289 if (has_loaded_apps_) |
| 268 SetAppToBeHighlighted(); | 290 SetAppToBeHighlighted(); |
| 269 return; | 291 return; |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 install_info->page_ordinal = page_ordinal; | 725 install_info->page_ordinal = page_ordinal; |
| 704 | 726 |
| 705 favicon_service->GetFaviconImageForPageURL( | 727 favicon_service->GetFaviconImageForPageURL( |
| 706 launch_url, | 728 launch_url, |
| 707 base::Bind(&AppLauncherHandler::OnFaviconForApp, | 729 base::Bind(&AppLauncherHandler::OnFaviconForApp, |
| 708 base::Unretained(this), | 730 base::Unretained(this), |
| 709 base::Passed(&install_info)), | 731 base::Passed(&install_info)), |
| 710 &cancelable_task_tracker_); | 732 &cancelable_task_tracker_); |
| 711 } | 733 } |
| 712 | 734 |
| 713 void AppLauncherHandler::StopShowingAppLauncherPromo( | 735 void AppLauncherHandler::HandleStopShowingAppLauncherPromo( |
| 714 const base::ListValue* args) { | 736 const base::ListValue* args) { |
| 715 #if defined(ENABLE_APP_LIST) | 737 #if defined(ENABLE_APP_LIST) |
| 716 g_browser_process->local_state()->SetBoolean( | 738 g_browser_process->local_state()->SetBoolean( |
| 717 prefs::kShowAppLauncherPromo, false); | 739 prefs::kShowAppLauncherPromo, false); |
| 718 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_DISMISSED); | 740 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_DISMISSED); |
| 719 #endif | 741 #endif |
| 720 } | 742 } |
| 721 | 743 |
| 722 void AppLauncherHandler::OnLearnMore(const base::ListValue* args) { | 744 void AppLauncherHandler::HandleOnLearnMore(const base::ListValue* args) { |
| 723 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_LEARN_MORE); | 745 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_LEARN_MORE); |
| 724 } | 746 } |
| 725 | 747 |
| 748 void AppLauncherHandler::HandlePageSelected(const base::ListValue* args) { |
| 749 double index_double; |
| 750 CHECK(args->GetDouble(0, &index_double)); |
| 751 int index = static_cast<int>(index_double); |
| 752 |
| 753 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); |
| 754 prefs->SetInteger(prefs::kNtpShownPage, APPS_PAGE_ID | index); |
| 755 } |
| 756 |
| 726 void AppLauncherHandler::OnFaviconForApp( | 757 void AppLauncherHandler::OnFaviconForApp( |
| 727 scoped_ptr<AppInstallInfo> install_info, | 758 scoped_ptr<AppInstallInfo> install_info, |
| 728 const favicon_base::FaviconImageResult& image_result) { | 759 const favicon_base::FaviconImageResult& image_result) { |
| 729 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo()); | 760 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo()); |
| 730 web_app->title = install_info->title; | 761 web_app->title = install_info->title; |
| 731 web_app->app_url = install_info->app_url; | 762 web_app->app_url = install_info->app_url; |
| 732 | 763 |
| 733 if (!image_result.image.IsEmpty()) { | 764 if (!image_result.image.IsEmpty()) { |
| 734 WebApplicationInfo::IconInfo icon; | 765 WebApplicationInfo::IconInfo icon; |
| 735 icon.data = image_result.image.AsBitmap(); | 766 icon.data = image_result.image.AsBitmap(); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 848 base::FundamentalValue(!extension_id_prompting_.empty())); | 879 base::FundamentalValue(!extension_id_prompting_.empty())); |
| 849 } | 880 } |
| 850 | 881 |
| 851 bool AppLauncherHandler::ShouldShow(const Extension* extension) const { | 882 bool AppLauncherHandler::ShouldShow(const Extension* extension) const { |
| 852 if (ignore_changes_ || !has_loaded_apps_ || !extension->is_app()) | 883 if (ignore_changes_ || !has_loaded_apps_ || !extension->is_app()) |
| 853 return false; | 884 return false; |
| 854 | 885 |
| 855 Profile* profile = Profile::FromWebUI(web_ui()); | 886 Profile* profile = Profile::FromWebUI(web_ui()); |
| 856 return extensions::ui_util::ShouldDisplayInNewTabPage(extension, profile); | 887 return extensions::ui_util::ShouldDisplayInNewTabPage(extension, profile); |
| 857 } | 888 } |
| OLD | NEW |