Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 CHECK(bucket < extension_misc::APP_LAUNCH_BUCKET_BOUNDARY); | 68 CHECK(bucket < extension_misc::APP_LAUNCH_BUCKET_BOUNDARY); |
| 69 return bucket; | 69 return bucket; |
| 70 } | 70 } |
| 71 | 71 |
| 72 } // namespace | 72 } // namespace |
| 73 | 73 |
| 74 AppLauncherHandler::AppLauncherHandler(ExtensionService* extension_service) | 74 AppLauncherHandler::AppLauncherHandler(ExtensionService* extension_service) |
| 75 : extension_service_(extension_service), | 75 : extension_service_(extension_service), |
| 76 promo_active_(false), | 76 promo_active_(false), |
| 77 ignore_changes_(false), | 77 ignore_changes_(false), |
| 78 attempted_bookmark_app_install_(false) { | 78 attempted_bookmark_app_install_(false), |
| 79 has_loaded_apps_(false) { | |
| 79 } | 80 } |
| 80 | 81 |
| 81 AppLauncherHandler::~AppLauncherHandler() {} | 82 AppLauncherHandler::~AppLauncherHandler() {} |
| 82 | 83 |
| 83 // Serializes |notification| into a new DictionaryValue which the caller then | 84 // Serializes |notification| into a new DictionaryValue which the caller then |
| 84 // owns. | 85 // owns. |
| 85 static DictionaryValue* SerializeNotification( | 86 static DictionaryValue* SerializeNotification( |
| 86 const AppNotification& notification) { | 87 const AppNotification& notification) { |
| 87 DictionaryValue* dictionary = new DictionaryValue(); | 88 DictionaryValue* dictionary = new DictionaryValue(); |
| 88 dictionary->SetString("title", notification.title); | 89 dictionary->SetString("title", notification.title); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 RecordWebStoreLaunch(is_promo_active); | 215 RecordWebStoreLaunch(is_promo_active); |
| 215 } else { | 216 } else { |
| 216 CHECK(params.size() == 3); | 217 CHECK(params.size() == 3); |
| 217 RecordAppLaunchByID(is_promo_active, ParseLaunchSource(params.at(2))); | 218 RecordAppLaunchByID(is_promo_active, ParseLaunchSource(params.at(2))); |
| 218 } | 219 } |
| 219 | 220 |
| 220 return true; | 221 return true; |
| 221 } | 222 } |
| 222 | 223 |
| 223 WebUIMessageHandler* AppLauncherHandler::Attach(WebUI* web_ui) { | 224 WebUIMessageHandler* AppLauncherHandler::Attach(WebUI* web_ui) { |
| 225 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, | |
| 226 Source<TabContents>(web_ui->tab_contents())); | |
| 224 return WebUIMessageHandler::Attach(web_ui); | 227 return WebUIMessageHandler::Attach(web_ui); |
| 225 } | 228 } |
| 226 | 229 |
| 227 void AppLauncherHandler::RegisterMessages() { | 230 void AppLauncherHandler::RegisterMessages() { |
| 228 web_ui_->RegisterMessageCallback("getApps", | 231 web_ui_->RegisterMessageCallback("getApps", |
| 229 NewCallback(this, &AppLauncherHandler::HandleGetApps)); | 232 NewCallback(this, &AppLauncherHandler::HandleGetApps)); |
| 230 web_ui_->RegisterMessageCallback("launchApp", | 233 web_ui_->RegisterMessageCallback("launchApp", |
| 231 NewCallback(this, &AppLauncherHandler::HandleLaunchApp)); | 234 NewCallback(this, &AppLauncherHandler::HandleLaunchApp)); |
| 232 web_ui_->RegisterMessageCallback("setLaunchType", | 235 web_ui_->RegisterMessageCallback("setLaunchType", |
| 233 NewCallback(this, &AppLauncherHandler::HandleSetLaunchType)); | 236 NewCallback(this, &AppLauncherHandler::HandleSetLaunchType)); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 262 const std::string& id = *Details<const std::string>(details).ptr(); | 265 const std::string& id = *Details<const std::string>(details).ptr(); |
| 263 const AppNotification* notification = | 266 const AppNotification* notification = |
| 264 extension_service_->app_notification_manager()->GetLast(id); | 267 extension_service_->app_notification_manager()->GetLast(id); |
| 265 ListValue args; | 268 ListValue args; |
| 266 args.Append(new StringValue(id)); | 269 args.Append(new StringValue(id)); |
| 267 if (notification) | 270 if (notification) |
| 268 args.Append(SerializeNotification(*notification)); | 271 args.Append(SerializeNotification(*notification)); |
| 269 web_ui_->CallJavascriptFunction("appNotificationChanged", args); | 272 web_ui_->CallJavascriptFunction("appNotificationChanged", args); |
| 270 break; | 273 break; |
| 271 } | 274 } |
| 275 case chrome::NOTIFICATION_APP_INSTALLED_TO_NTP: { | |
| 276 if (!NewTabUI::NTP4Enabled()) | |
| 277 break; | |
| 278 | |
| 279 highlight_app_id_ = *Details<const std::string>(details).ptr(); | |
| 280 if (has_loaded_apps_) | |
| 281 SetAppToBeHighlighted(); | |
|
Rick Byers
2011/08/29 19:51:12
I assume we can't do this unconditionally because
Evan Stade
2011/08/29 20:30:51
yes, the js isn't ready yet.
| |
| 282 break; | |
| 283 } | |
| 272 case chrome::NOTIFICATION_EXTENSION_LOADED: { | 284 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| 273 const Extension* extension = Details<const Extension>(details).ptr(); | 285 const Extension* extension = Details<const Extension>(details).ptr(); |
| 274 if (!extension->is_app()) | 286 if (!extension->is_app()) |
| 275 return; | 287 return; |
| 276 | 288 |
| 277 if (!NewTabUI::NTP4Enabled()) { | 289 if (!NewTabUI::NTP4Enabled()) { |
| 278 HandleGetApps(NULL); | 290 HandleGetApps(NULL); |
| 279 break; | 291 break; |
| 280 } | 292 } |
| 281 | 293 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 | 468 |
| 457 // If the default apps have just expired (user viewed them too many times with | 469 // If the default apps have just expired (user viewed them too many times with |
| 458 // no interaction), then we uninstall them and focus the recent sites section. | 470 // no interaction), then we uninstall them and focus the recent sites section. |
| 459 if (apps_promo_just_expired) { | 471 if (apps_promo_just_expired) { |
| 460 ignore_changes_ = true; | 472 ignore_changes_ = true; |
| 461 UninstallDefaultApps(); | 473 UninstallDefaultApps(); |
| 462 ignore_changes_ = false; | 474 ignore_changes_ = false; |
| 463 ShownSectionsHandler::SetShownSection(prefs, THUMB); | 475 ShownSectionsHandler::SetShownSection(prefs, THUMB); |
| 464 } | 476 } |
| 465 | 477 |
| 478 SetAppToBeHighlighted(); | |
| 466 FillAppDictionary(&dictionary); | 479 FillAppDictionary(&dictionary); |
| 467 web_ui_->CallJavascriptFunction("getAppsCallback", dictionary); | 480 web_ui_->CallJavascriptFunction("getAppsCallback", dictionary); |
| 468 | 481 |
| 469 // First time we get here we set up the observer so that we can tell update | 482 // First time we get here we set up the observer so that we can tell update |
| 470 // the apps as they change. | 483 // the apps as they change. |
| 471 if (registrar_.IsEmpty()) { | 484 if (!has_loaded_apps_) { |
| 485 pref_change_registrar_.Init( | |
| 486 extension_service_->extension_prefs()->pref_service()); | |
| 487 pref_change_registrar_.Add(ExtensionPrefs::kExtensionsPref, this); | |
| 488 pref_change_registrar_.Add(prefs::kNTPAppPageNames, this); | |
| 489 | |
| 472 registrar_.Add(this, chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED, | 490 registrar_.Add(this, chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED, |
| 473 NotificationService::AllSources()); | 491 NotificationService::AllSources()); |
| 474 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 492 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 475 NotificationService::AllSources()); | 493 NotificationService::AllSources()); |
| 476 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 494 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 477 NotificationService::AllSources()); | 495 NotificationService::AllSources()); |
| 478 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, | 496 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, |
| 479 NotificationService::AllSources()); | 497 NotificationService::AllSources()); |
| 480 registrar_.Add(this, chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED, | 498 registrar_.Add(this, chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED, |
| 481 NotificationService::AllSources()); | 499 NotificationService::AllSources()); |
| 482 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | 500 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| 483 NotificationService::AllSources()); | 501 NotificationService::AllSources()); |
| 484 } | 502 } |
| 485 if (pref_change_registrar_.IsEmpty()) { | 503 |
| 486 pref_change_registrar_.Init( | 504 has_loaded_apps_ = true; |
| 487 extension_service_->extension_prefs()->pref_service()); | |
| 488 pref_change_registrar_.Add(ExtensionPrefs::kExtensionsPref, this); | |
| 489 pref_change_registrar_.Add(prefs::kNTPAppPageNames, this); | |
| 490 } | |
| 491 } | 505 } |
| 492 | 506 |
| 493 void AppLauncherHandler::HandleLaunchApp(const ListValue* args) { | 507 void AppLauncherHandler::HandleLaunchApp(const ListValue* args) { |
| 494 std::string extension_id; | 508 std::string extension_id; |
| 495 double source = -1.0; | 509 double source = -1.0; |
| 496 bool alt_key = false; | 510 bool alt_key = false; |
| 497 bool ctrl_key = false; | 511 bool ctrl_key = false; |
| 498 bool meta_key = false; | 512 bool meta_key = false; |
| 499 bool shift_key = false; | 513 bool shift_key = false; |
| 500 double button = 0.0; | 514 double button = 0.0; |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 788 web_app->icons.clear(); | 802 web_app->icons.clear(); |
| 789 } | 803 } |
| 790 | 804 |
| 791 scoped_refptr<CrxInstaller> installer( | 805 scoped_refptr<CrxInstaller> installer( |
| 792 extension_service_->MakeCrxInstaller(NULL)); | 806 extension_service_->MakeCrxInstaller(NULL)); |
| 793 installer->set_page_index(install_info->page_index); | 807 installer->set_page_index(install_info->page_index); |
| 794 installer->InstallWebApp(*web_app); | 808 installer->InstallWebApp(*web_app); |
| 795 attempted_bookmark_app_install_ = true; | 809 attempted_bookmark_app_install_ = true; |
| 796 } | 810 } |
| 797 | 811 |
| 812 void AppLauncherHandler::SetAppToBeHighlighted() { | |
| 813 if (highlight_app_id_.empty()) | |
| 814 return; | |
| 815 | |
| 816 scoped_ptr<StringValue> app_id(Value::CreateStringValue(highlight_app_id_)); | |
| 817 web_ui_->CallJavascriptFunction("ntp4.setAppToBeHighlighted", *app_id); | |
| 818 highlight_app_id_.clear(); | |
| 819 } | |
| 820 | |
| 798 // static | 821 // static |
| 799 void AppLauncherHandler::RegisterUserPrefs(PrefService* pref_service) { | 822 void AppLauncherHandler::RegisterUserPrefs(PrefService* pref_service) { |
| 800 // TODO(csilv): We will want this to be a syncable preference instead. | 823 // TODO(csilv): We will want this to be a syncable preference instead. |
| 801 pref_service->RegisterListPref(prefs::kNTPAppPageNames, | 824 pref_service->RegisterListPref(prefs::kNTPAppPageNames, |
| 802 PrefService::UNSYNCABLE_PREF); | 825 PrefService::UNSYNCABLE_PREF); |
| 803 } | 826 } |
| 804 | 827 |
| 805 // statiic | 828 // statiic |
| 806 void AppLauncherHandler::RecordWebStoreLaunch(bool promo_active) { | 829 void AppLauncherHandler::RecordWebStoreLaunch(bool promo_active) { |
| 807 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppLaunchHistogram, | 830 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppLaunchHistogram, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 858 extension = extension_service_->GetExtensionById(extension_id, true); | 881 extension = extension_service_->GetExtensionById(extension_id, true); |
| 859 } | 882 } |
| 860 | 883 |
| 861 ExtensionPrefs* extension_prefs = extension_service_->extension_prefs(); | 884 ExtensionPrefs* extension_prefs = extension_service_->extension_prefs(); |
| 862 if (!extension_prefs->DidExtensionEscalatePermissions(extension_id)) { | 885 if (!extension_prefs->DidExtensionEscalatePermissions(extension_id)) { |
| 863 // Enable the extension immediately if its privileges weren't escalated. | 886 // Enable the extension immediately if its privileges weren't escalated. |
| 864 // This is a no-op if the extension was previously terminated. | 887 // This is a no-op if the extension was previously terminated. |
| 865 extension_service_->EnableExtension(extension_id); | 888 extension_service_->EnableExtension(extension_id); |
| 866 | 889 |
| 867 // Launch app asynchronously so the image will update. | 890 // Launch app asynchronously so the image will update. |
| 868 StringValue* app_id = Value::CreateStringValue(extension_id); | 891 scoped_ptr<StringValue> app_id(Value::CreateStringValue(extension_id)); |
| 869 web_ui_->CallJavascriptFunction("launchAppAfterEnable", *app_id); | 892 web_ui_->CallJavascriptFunction("launchAppAfterEnable", *app_id); |
| 870 return; | 893 return; |
| 871 } | 894 } |
| 872 | 895 |
| 873 if (!extension_id_prompting_.empty()) | 896 if (!extension_id_prompting_.empty()) |
| 874 return; // Only one prompt at a time. | 897 return; // Only one prompt at a time. |
| 875 | 898 |
| 876 extension_id_prompting_ = extension_id; | 899 extension_id_prompting_ = extension_id; |
| 877 GetExtensionInstallUI()->ConfirmReEnable(this, extension); | 900 GetExtensionInstallUI()->ConfirmReEnable(this, extension); |
| 878 } | 901 } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 908 extension_service_->GetExtensionById(extension_id_prompting_, true); | 931 extension_service_->GetExtensionById(extension_id_prompting_, true); |
| 909 if (!extension) | 932 if (!extension) |
| 910 return; | 933 return; |
| 911 | 934 |
| 912 extension_service_->GrantPermissionsAndEnableExtension(extension); | 935 extension_service_->GrantPermissionsAndEnableExtension(extension); |
| 913 | 936 |
| 914 // We bounce this off the NTP so the browser can update the apps icon. | 937 // We bounce this off the NTP so the browser can update the apps icon. |
| 915 // If we don't launch the app asynchronously, then the app's disabled | 938 // If we don't launch the app asynchronously, then the app's disabled |
| 916 // icon disappears but isn't replaced by the enabled icon, making a poor | 939 // icon disappears but isn't replaced by the enabled icon, making a poor |
| 917 // visual experience. | 940 // visual experience. |
| 918 StringValue* app_id = Value::CreateStringValue(extension->id()); | 941 scoped_ptr<StringValue> app_id(Value::CreateStringValue(extension->id())); |
| 919 web_ui_->CallJavascriptFunction("launchAppAfterEnable", *app_id); | 942 web_ui_->CallJavascriptFunction("launchAppAfterEnable", *app_id); |
| 920 | 943 |
| 921 extension_id_prompting_ = ""; | 944 extension_id_prompting_ = ""; |
| 922 } | 945 } |
| 923 | 946 |
| 924 void AppLauncherHandler::InstallUIAbort(bool user_initiated) { | 947 void AppLauncherHandler::InstallUIAbort(bool user_initiated) { |
| 925 // We record the histograms here because ExtensionDialogCanceled is also | 948 // We record the histograms here because ExtensionDialogCanceled is also |
| 926 // called when the extension uninstall dialog is canceled. | 949 // called when the extension uninstall dialog is canceled. |
| 927 const Extension* extension = | 950 const Extension* extension = |
| 928 extension_service_->GetExtensionById(extension_id_prompting_, true); | 951 extension_service_->GetExtensionById(extension_id_prompting_, true); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 953 | 976 |
| 954 void AppLauncherHandler::UninstallDefaultApps() { | 977 void AppLauncherHandler::UninstallDefaultApps() { |
| 955 AppsPromo* apps_promo = extension_service_->apps_promo(); | 978 AppsPromo* apps_promo = extension_service_->apps_promo(); |
| 956 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); | 979 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); |
| 957 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); | 980 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); |
| 958 iter != app_ids.end(); ++iter) { | 981 iter != app_ids.end(); ++iter) { |
| 959 if (extension_service_->GetExtensionById(*iter, true)) | 982 if (extension_service_->GetExtensionById(*iter, true)) |
| 960 extension_service_->UninstallExtension(*iter, false, NULL); | 983 extension_service_->UninstallExtension(*iter, false, NULL); |
| 961 } | 984 } |
| 962 } | 985 } |
| OLD | NEW |