| 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 |
| 11 #include "apps/metrics_names.h" | |
| 12 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
| 13 #include "base/bind.h" | 12 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 15 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 16 #include "base/i18n/rtl.h" | 15 #include "base/i18n/rtl.h" |
| 17 #include "base/metrics/field_trial.h" | 16 #include "base/metrics/field_trial.h" |
| 18 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 19 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/values.h" | 20 #include "base/values.h" |
| 22 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
| 23 #include "chrome/browser/chrome_notification_types.h" | 22 #include "chrome/browser/chrome_notification_types.h" |
| 24 #include "chrome/browser/extensions/crx_installer.h" | 23 #include "chrome/browser/extensions/crx_installer.h" |
| 25 #include "chrome/browser/extensions/extension_service.h" | 24 #include "chrome/browser/extensions/extension_service.h" |
| 26 #include "chrome/browser/extensions/extension_ui_util.h" | 25 #include "chrome/browser/extensions/extension_ui_util.h" |
| 27 #include "chrome/browser/extensions/launch_util.h" | 26 #include "chrome/browser/extensions/launch_util.h" |
| 28 #include "chrome/browser/favicon/favicon_service_factory.h" | 27 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 29 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 30 #include "chrome/browser/ui/app_list/app_list_util.h" | |
| 31 #include "chrome/browser/ui/apps/app_info_dialog.h" | 29 #include "chrome/browser/ui/apps/app_info_dialog.h" |
| 32 #include "chrome/browser/ui/browser_dialogs.h" | 30 #include "chrome/browser/ui/browser_dialogs.h" |
| 33 #include "chrome/browser/ui/browser_finder.h" | 31 #include "chrome/browser/ui/browser_finder.h" |
| 34 #include "chrome/browser/ui/browser_tabstrip.h" | 32 #include "chrome/browser/ui/browser_tabstrip.h" |
| 35 #include "chrome/browser/ui/browser_window.h" | 33 #include "chrome/browser/ui/browser_window.h" |
| 36 #include "chrome/browser/ui/extensions/app_launch_params.h" | 34 #include "chrome/browser/ui/extensions/app_launch_params.h" |
| 37 #include "chrome/browser/ui/extensions/application_launch.h" | 35 #include "chrome/browser/ui/extensions/application_launch.h" |
| 38 #include "chrome/browser/ui/extensions/extension_enable_flow.h" | 36 #include "chrome/browser/ui/extensions/extension_enable_flow.h" |
| 39 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 37 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 40 #include "chrome/browser/ui/webui/extensions/extension_basic_info.h" | 38 #include "chrome/browser/ui/webui/extensions/extension_basic_info.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // The purpose of this enum is to track which page on the NTP is showing. | 82 // The purpose of this enum is to track which page on the NTP is showing. |
| 85 // The lower 10 bits of kNtpShownPage are used for the index within the page | 83 // The lower 10 bits of kNtpShownPage are used for the index within the page |
| 86 // group, and the rest of the bits are used for the page group ID (defined | 84 // group, and the rest of the bits are used for the page group ID (defined |
| 87 // here). | 85 // here). |
| 88 static const int kPageIdOffset = 10; | 86 static const int kPageIdOffset = 10; |
| 89 enum { | 87 enum { |
| 90 INDEX_MASK = (1 << kPageIdOffset) - 1, | 88 INDEX_MASK = (1 << kPageIdOffset) - 1, |
| 91 APPS_PAGE_ID = 2 << kPageIdOffset, | 89 APPS_PAGE_ID = 2 << kPageIdOffset, |
| 92 }; | 90 }; |
| 93 | 91 |
| 94 void RecordAppLauncherPromoHistogram( | 92 // This is used to avoid a DCHECK due to an unhandled WebUI callback. The |
| 95 apps::AppLauncherPromoHistogramValues value) { | 93 // JavaScript used to switch between pages sends "pageSelected" which is used |
| 96 DCHECK_LT(value, apps::APP_LAUNCHER_PROMO_MAX); | 94 // in the context of the NTP for recording metrics we don't need here. |
| 97 UMA_HISTOGRAM_ENUMERATION( | 95 void NoOpCallback(const base::ListValue* args) {} |
| 98 "Apps.AppLauncherPromo", value, apps::APP_LAUNCHER_PROMO_MAX); | |
| 99 } | |
| 100 | 96 |
| 101 } // namespace | 97 } // namespace |
| 102 | 98 |
| 103 AppLauncherHandler::AppInstallInfo::AppInstallInfo() {} | 99 AppLauncherHandler::AppInstallInfo::AppInstallInfo() {} |
| 104 | 100 |
| 105 AppLauncherHandler::AppInstallInfo::~AppInstallInfo() {} | 101 AppLauncherHandler::AppInstallInfo::~AppInstallInfo() {} |
| 106 | 102 |
| 107 AppLauncherHandler::AppLauncherHandler(ExtensionService* extension_service) | 103 AppLauncherHandler::AppLauncherHandler(ExtensionService* extension_service) |
| 108 : extension_service_(extension_service), | 104 : extension_service_(extension_service), |
| 109 ignore_changes_(false), | 105 ignore_changes_(false), |
| 110 attempted_bookmark_app_install_(false), | 106 attempted_bookmark_app_install_(false), |
| 111 has_loaded_apps_(false) { | 107 has_loaded_apps_(false) { |
| 112 if (IsAppLauncherEnabled()) | |
| 113 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_ALREADY_INSTALLED); | |
| 114 else if (ShouldShowAppLauncherPromo()) | |
| 115 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_SHOWN); | |
| 116 } | 108 } |
| 117 | 109 |
| 118 AppLauncherHandler::~AppLauncherHandler() { | 110 AppLauncherHandler::~AppLauncherHandler() { |
| 119 ExtensionRegistry::Get(Profile::FromWebUI(web_ui()))->RemoveObserver(this); | 111 ExtensionRegistry::Get(Profile::FromWebUI(web_ui()))->RemoveObserver(this); |
| 120 } | 112 } |
| 121 | 113 |
| 122 void AppLauncherHandler::CreateAppInfo( | 114 void AppLauncherHandler::CreateAppInfo( |
| 123 const Extension* extension, | 115 const Extension* extension, |
| 124 ExtensionService* service, | 116 ExtensionService* service, |
| 125 base::DictionaryValue* value) { | 117 base::DictionaryValue* value) { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 // static | 216 // static |
| 225 void AppLauncherHandler::RegisterProfilePrefs( | 217 void AppLauncherHandler::RegisterProfilePrefs( |
| 226 user_prefs::PrefRegistrySyncable* registry) { | 218 user_prefs::PrefRegistrySyncable* registry) { |
| 227 registry->RegisterIntegerPref(prefs::kNtpShownPage, APPS_PAGE_ID); | 219 registry->RegisterIntegerPref(prefs::kNtpShownPage, APPS_PAGE_ID); |
| 228 } | 220 } |
| 229 | 221 |
| 230 void AppLauncherHandler::RegisterMessages() { | 222 void AppLauncherHandler::RegisterMessages() { |
| 231 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, | 223 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, |
| 232 content::Source<WebContents>(web_ui()->GetWebContents())); | 224 content::Source<WebContents>(web_ui()->GetWebContents())); |
| 233 | 225 |
| 234 // Some tests don't have a local state. | |
| 235 #if defined(ENABLE_APP_LIST) | |
| 236 if (g_browser_process->local_state()) { | |
| 237 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); | |
| 238 local_state_pref_change_registrar_.Add( | |
| 239 prefs::kShowAppLauncherPromo, | |
| 240 base::Bind(&AppLauncherHandler::OnLocalStatePreferenceChanged, | |
| 241 base::Unretained(this))); | |
| 242 } | |
| 243 #endif | |
| 244 web_ui()->RegisterMessageCallback("getApps", | 226 web_ui()->RegisterMessageCallback("getApps", |
| 245 base::Bind(&AppLauncherHandler::HandleGetApps, | 227 base::Bind(&AppLauncherHandler::HandleGetApps, |
| 246 base::Unretained(this))); | 228 base::Unretained(this))); |
| 247 web_ui()->RegisterMessageCallback("launchApp", | 229 web_ui()->RegisterMessageCallback("launchApp", |
| 248 base::Bind(&AppLauncherHandler::HandleLaunchApp, | 230 base::Bind(&AppLauncherHandler::HandleLaunchApp, |
| 249 base::Unretained(this))); | 231 base::Unretained(this))); |
| 250 web_ui()->RegisterMessageCallback("setLaunchType", | 232 web_ui()->RegisterMessageCallback("setLaunchType", |
| 251 base::Bind(&AppLauncherHandler::HandleSetLaunchType, | 233 base::Bind(&AppLauncherHandler::HandleSetLaunchType, |
| 252 base::Unretained(this))); | 234 base::Unretained(this))); |
| 253 web_ui()->RegisterMessageCallback("uninstallApp", | 235 web_ui()->RegisterMessageCallback("uninstallApp", |
| (...skipping 10 matching lines...) Expand all Loading... |
| 264 base::Unretained(this))); | 246 base::Unretained(this))); |
| 265 web_ui()->RegisterMessageCallback("setPageIndex", | 247 web_ui()->RegisterMessageCallback("setPageIndex", |
| 266 base::Bind(&AppLauncherHandler::HandleSetPageIndex, | 248 base::Bind(&AppLauncherHandler::HandleSetPageIndex, |
| 267 base::Unretained(this))); | 249 base::Unretained(this))); |
| 268 web_ui()->RegisterMessageCallback("saveAppPageName", | 250 web_ui()->RegisterMessageCallback("saveAppPageName", |
| 269 base::Bind(&AppLauncherHandler::HandleSaveAppPageName, | 251 base::Bind(&AppLauncherHandler::HandleSaveAppPageName, |
| 270 base::Unretained(this))); | 252 base::Unretained(this))); |
| 271 web_ui()->RegisterMessageCallback("generateAppForLink", | 253 web_ui()->RegisterMessageCallback("generateAppForLink", |
| 272 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink, | 254 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink, |
| 273 base::Unretained(this))); | 255 base::Unretained(this))); |
| 274 web_ui()->RegisterMessageCallback("stopShowingAppLauncherPromo", | 256 web_ui()->RegisterMessageCallback("pageSelected", base::Bind(&NoOpCallback)); |
| 275 base::Bind(&AppLauncherHandler::HandleStopShowingAppLauncherPromo, | |
| 276 base::Unretained(this))); | |
| 277 web_ui()->RegisterMessageCallback("onLearnMore", | |
| 278 base::Bind(&AppLauncherHandler::HandleOnLearnMore, | |
| 279 base::Unretained(this))); | |
| 280 web_ui()->RegisterMessageCallback("pageSelected", | |
| 281 base::Bind(&AppLauncherHandler::HandlePageSelected, | |
| 282 base::Unretained(this))); | |
| 283 } | 257 } |
| 284 | 258 |
| 285 void AppLauncherHandler::Observe(int type, | 259 void AppLauncherHandler::Observe(int type, |
| 286 const content::NotificationSource& source, | 260 const content::NotificationSource& source, |
| 287 const content::NotificationDetails& details) { | 261 const content::NotificationDetails& details) { |
| 288 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) { | 262 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) { |
| 289 highlight_app_id_ = *content::Details<const std::string>(details).ptr(); | 263 highlight_app_id_ = *content::Details<const std::string>(details).ptr(); |
| 290 if (has_loaded_apps_) | 264 if (has_loaded_apps_) |
| 291 SetAppToBeHighlighted(); | 265 SetAppToBeHighlighted(); |
| 292 return; | 266 return; |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 install_info->page_ordinal = page_ordinal; | 701 install_info->page_ordinal = page_ordinal; |
| 728 | 702 |
| 729 favicon_service->GetFaviconImageForPageURL( | 703 favicon_service->GetFaviconImageForPageURL( |
| 730 launch_url, | 704 launch_url, |
| 731 base::Bind(&AppLauncherHandler::OnFaviconForApp, | 705 base::Bind(&AppLauncherHandler::OnFaviconForApp, |
| 732 base::Unretained(this), | 706 base::Unretained(this), |
| 733 base::Passed(&install_info)), | 707 base::Passed(&install_info)), |
| 734 &cancelable_task_tracker_); | 708 &cancelable_task_tracker_); |
| 735 } | 709 } |
| 736 | 710 |
| 737 void AppLauncherHandler::HandleStopShowingAppLauncherPromo( | |
| 738 const base::ListValue* args) { | |
| 739 #if defined(ENABLE_APP_LIST) | |
| 740 g_browser_process->local_state()->SetBoolean( | |
| 741 prefs::kShowAppLauncherPromo, false); | |
| 742 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_DISMISSED); | |
| 743 #endif | |
| 744 } | |
| 745 | |
| 746 void AppLauncherHandler::HandleOnLearnMore(const base::ListValue* args) { | |
| 747 RecordAppLauncherPromoHistogram(apps::APP_LAUNCHER_PROMO_LEARN_MORE); | |
| 748 } | |
| 749 | |
| 750 void AppLauncherHandler::HandlePageSelected(const base::ListValue* args) { | 711 void AppLauncherHandler::HandlePageSelected(const base::ListValue* args) { |
| 751 double index_double; | 712 double index_double; |
| 752 CHECK(args->GetDouble(0, &index_double)); | 713 CHECK(args->GetDouble(0, &index_double)); |
| 753 int index = static_cast<int>(index_double); | 714 int index = static_cast<int>(index_double); |
| 754 | 715 |
| 755 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | 716 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); |
| 756 prefs->SetInteger(prefs::kNtpShownPage, APPS_PAGE_ID | index); | 717 prefs->SetInteger(prefs::kNtpShownPage, APPS_PAGE_ID | index); |
| 757 } | 718 } |
| 758 | 719 |
| 759 void AppLauncherHandler::OnFaviconForApp( | 720 void AppLauncherHandler::OnFaviconForApp( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 787 web_ui()->CallJavascriptFunction("ntp.setAppToBeHighlighted", app_id); | 748 web_ui()->CallJavascriptFunction("ntp.setAppToBeHighlighted", app_id); |
| 788 highlight_app_id_.clear(); | 749 highlight_app_id_.clear(); |
| 789 } | 750 } |
| 790 | 751 |
| 791 void AppLauncherHandler::OnExtensionPreferenceChanged() { | 752 void AppLauncherHandler::OnExtensionPreferenceChanged() { |
| 792 base::DictionaryValue dictionary; | 753 base::DictionaryValue dictionary; |
| 793 FillAppDictionary(&dictionary); | 754 FillAppDictionary(&dictionary); |
| 794 web_ui()->CallJavascriptFunction("ntp.appsPrefChangeCallback", dictionary); | 755 web_ui()->CallJavascriptFunction("ntp.appsPrefChangeCallback", dictionary); |
| 795 } | 756 } |
| 796 | 757 |
| 797 void AppLauncherHandler::OnLocalStatePreferenceChanged() { | |
| 798 #if defined(ENABLE_APP_LIST) | |
| 799 web_ui()->CallJavascriptFunction( | |
| 800 "ntp.appLauncherPromoPrefChangeCallback", | |
| 801 base::FundamentalValue(g_browser_process->local_state()->GetBoolean( | |
| 802 prefs::kShowAppLauncherPromo))); | |
| 803 #endif | |
| 804 } | |
| 805 | |
| 806 void AppLauncherHandler::CleanupAfterUninstall() { | 758 void AppLauncherHandler::CleanupAfterUninstall() { |
| 807 extension_id_prompting_.clear(); | 759 extension_id_prompting_.clear(); |
| 808 } | 760 } |
| 809 | 761 |
| 810 void AppLauncherHandler::PromptToEnableApp(const std::string& extension_id) { | 762 void AppLauncherHandler::PromptToEnableApp(const std::string& extension_id) { |
| 811 if (!extension_id_prompting_.empty()) | 763 if (!extension_id_prompting_.empty()) |
| 812 return; // Only one prompt at a time. | 764 return; // Only one prompt at a time. |
| 813 | 765 |
| 814 extension_id_prompting_ = extension_id; | 766 extension_id_prompting_ = extension_id; |
| 815 extension_enable_flow_.reset(new ExtensionEnableFlow( | 767 extension_enable_flow_.reset(new ExtensionEnableFlow( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 881 base::FundamentalValue(!extension_id_prompting_.empty())); | 833 base::FundamentalValue(!extension_id_prompting_.empty())); |
| 882 } | 834 } |
| 883 | 835 |
| 884 bool AppLauncherHandler::ShouldShow(const Extension* extension) const { | 836 bool AppLauncherHandler::ShouldShow(const Extension* extension) const { |
| 885 if (ignore_changes_ || !has_loaded_apps_ || !extension->is_app()) | 837 if (ignore_changes_ || !has_loaded_apps_ || !extension->is_app()) |
| 886 return false; | 838 return false; |
| 887 | 839 |
| 888 Profile* profile = Profile::FromWebUI(web_ui()); | 840 Profile* profile = Profile::FromWebUI(web_ui()); |
| 889 return extensions::ui_util::ShouldDisplayInNewTabPage(extension, profile); | 841 return extensions::ui_util::ShouldDisplayInNewTabPage(extension, profile); |
| 890 } | 842 } |
| OLD | NEW |