| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 static_cast<extension_misc::AppLaunchBucket>(bucket_num); | 67 static_cast<extension_misc::AppLaunchBucket>(bucket_num); |
| 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 } | 79 } |
| 79 | 80 |
| 80 AppLauncherHandler::~AppLauncherHandler() {} | 81 AppLauncherHandler::~AppLauncherHandler() {} |
| 81 | 82 |
| 82 // Serializes |notification| into a new DictionaryValue which the caller then | 83 // Serializes |notification| into a new DictionaryValue which the caller then |
| 83 // owns. | 84 // owns. |
| 84 static DictionaryValue* SerializeNotification( | 85 static DictionaryValue* SerializeNotification( |
| 85 const AppNotification& notification) { | 86 const AppNotification& notification) { |
| 86 DictionaryValue* dictionary = new DictionaryValue(); | 87 DictionaryValue* dictionary = new DictionaryValue(); |
| 87 dictionary->SetString("title", notification.title); | 88 dictionary->SetString("title", notification.title); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 const std::string& id = *Details<const std::string>(details).ptr(); | 254 const std::string& id = *Details<const std::string>(details).ptr(); |
| 254 const AppNotification* notification = | 255 const AppNotification* notification = |
| 255 extension_service_->app_notification_manager()->GetLast(id); | 256 extension_service_->app_notification_manager()->GetLast(id); |
| 256 ListValue args; | 257 ListValue args; |
| 257 args.Append(new StringValue(id)); | 258 args.Append(new StringValue(id)); |
| 258 if (notification) | 259 if (notification) |
| 259 args.Append(SerializeNotification(*notification)); | 260 args.Append(SerializeNotification(*notification)); |
| 260 web_ui_->CallJavascriptFunction("appNotificationChanged", args); | 261 web_ui_->CallJavascriptFunction("appNotificationChanged", args); |
| 261 break; | 262 break; |
| 262 } | 263 } |
| 263 case chrome::NOTIFICATION_EXTENSION_LOADED: | 264 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| 264 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | 265 const Extension* extension = Details<const Extension>(details).ptr(); |
| 265 const Extension* extension = | |
| 266 type == chrome::NOTIFICATION_EXTENSION_LOADED ? | |
| 267 Details<const Extension>(details).ptr() : | |
| 268 Details<UnloadedExtensionInfo>(details)->extension; | |
| 269 if (!extension->is_app()) | 266 if (!extension->is_app()) |
| 267 return; |
| 268 |
| 269 if (!NewTabUI::NTP4Enabled()) { |
| 270 HandleGetApps(NULL); |
| 270 break; | 271 break; |
| 272 } |
| 271 | 273 |
| 272 if (NewTabUI::NTP4Enabled()) { | 274 scoped_ptr<DictionaryValue> app_info(GetAppInfo(extension)); |
| 273 scoped_ptr<DictionaryValue> app_info(GetAppInfo(extension)); | 275 if (app_info.get()) { |
| 274 if (app_info.get()) { | 276 ExtensionPrefs* prefs = extension_service_->extension_prefs(); |
| 275 std::string function = | 277 scoped_ptr<base::FundamentalValue> highlight(Value::CreateBooleanValue( |
| 276 type == chrome::NOTIFICATION_EXTENSION_LOADED ? | 278 prefs->IsFromBookmark(extension->id()) && |
| 277 "ntp4.appAdded" : "ntp4.appRemoved"; | 279 attempted_bookmark_app_install_)); |
| 278 web_ui_->CallJavascriptFunction(function, *app_info); | 280 attempted_bookmark_app_install_ = false; |
| 279 } | 281 web_ui_->CallJavascriptFunction("ntp4.appAdded", *app_info, *highlight); |
| 280 } else if (web_ui_->tab_contents()) { | |
| 281 HandleGetApps(NULL); | |
| 282 } | 282 } |
| 283 | 283 |
| 284 break; | 284 break; |
| 285 } | 285 } |
| 286 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 287 const Extension* extension = |
| 288 Details<UnloadedExtensionInfo>(details)->extension; |
| 289 if (!extension->is_app()) |
| 290 return; |
| 291 |
| 292 if (!NewTabUI::NTP4Enabled()) { |
| 293 HandleGetApps(NULL); |
| 294 break; |
| 295 } |
| 296 |
| 297 scoped_ptr<DictionaryValue> app_info(GetAppInfo(extension)); |
| 298 if (app_info.get()) |
| 299 web_ui_->CallJavascriptFunction("ntp4.appRemoved", *app_info); |
| 300 break; |
| 301 } |
| 286 case chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED: | 302 case chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED: |
| 287 // The promo may not load until a couple seconds after the first NTP view, | 303 // The promo may not load until a couple seconds after the first NTP view, |
| 288 // so we listen for the load notification and notify the NTP when ready. | 304 // so we listen for the load notification and notify the NTP when ready. |
| 289 case chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED: | 305 case chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED: |
| 290 if (web_ui_->tab_contents()) | 306 if (web_ui_->tab_contents()) |
| 291 HandleGetApps(NULL); | 307 HandleGetApps(NULL); |
| 292 break; | 308 break; |
| 293 case chrome::NOTIFICATION_PREF_CHANGED: { | 309 case chrome::NOTIFICATION_PREF_CHANGED: { |
| 294 if (!web_ui_->tab_contents()) | 310 if (!web_ui_->tab_contents()) |
| 295 break; | 311 break; |
| 296 // Handle app page renames. | 312 // Handle app page renames. |
| 297 std::string* pref_name = Details<std::string>(details).ptr(); | 313 std::string* pref_name = Details<std::string>(details).ptr(); |
| 298 if (*pref_name == prefs::kNTPAppPageNames) { | 314 if (*pref_name == prefs::kNTPAppPageNames) { |
| 299 // TODO(estade): this doesn't need to regenerate the entire page. | 315 // TODO(estade): this doesn't need to regenerate the entire page. |
| 300 HandleGetApps(NULL); | 316 HandleGetApps(NULL); |
| 301 } else { | 317 } else { |
| 302 // Default prefs change handling. | 318 // Default prefs change handling. |
| 303 DictionaryValue dictionary; | 319 DictionaryValue dictionary; |
| 304 FillAppDictionary(&dictionary); | 320 FillAppDictionary(&dictionary); |
| 305 web_ui_->CallJavascriptFunction("appsPrefChangeCallback", dictionary); | 321 web_ui_->CallJavascriptFunction("appsPrefChangeCallback", dictionary); |
| 306 } | 322 } |
| 307 break; | 323 break; |
| 308 } | 324 } |
| 325 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { |
| 326 attempted_bookmark_app_install_ = false; |
| 327 break; |
| 328 } |
| 309 default: | 329 default: |
| 310 NOTREACHED(); | 330 NOTREACHED(); |
| 311 } | 331 } |
| 312 } | 332 } |
| 313 | 333 |
| 314 void AppLauncherHandler::FillAppDictionary(DictionaryValue* dictionary) { | 334 void AppLauncherHandler::FillAppDictionary(DictionaryValue* dictionary) { |
| 315 ListValue* list = new ListValue(); | 335 ListValue* list = new ListValue(); |
| 316 const ExtensionList* extensions = extension_service_->extensions(); | 336 const ExtensionList* extensions = extension_service_->extensions(); |
| 317 ExtensionList::const_iterator it; | 337 ExtensionList::const_iterator it; |
| 318 for (it = extensions->begin(); it != extensions->end(); ++it) { | 338 for (it = extensions->begin(); it != extensions->end(); ++it) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 registrar_.Add(this, chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED, | 459 registrar_.Add(this, chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED, |
| 440 NotificationService::AllSources()); | 460 NotificationService::AllSources()); |
| 441 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 461 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 442 NotificationService::AllSources()); | 462 NotificationService::AllSources()); |
| 443 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 463 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 444 NotificationService::AllSources()); | 464 NotificationService::AllSources()); |
| 445 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, | 465 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, |
| 446 NotificationService::AllSources()); | 466 NotificationService::AllSources()); |
| 447 registrar_.Add(this, chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED, | 467 registrar_.Add(this, chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED, |
| 448 NotificationService::AllSources()); | 468 NotificationService::AllSources()); |
| 469 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| 470 NotificationService::AllSources()); |
| 449 } | 471 } |
| 450 if (pref_change_registrar_.IsEmpty()) { | 472 if (pref_change_registrar_.IsEmpty()) { |
| 451 pref_change_registrar_.Init( | 473 pref_change_registrar_.Init( |
| 452 extension_service_->extension_prefs()->pref_service()); | 474 extension_service_->extension_prefs()->pref_service()); |
| 453 pref_change_registrar_.Add(ExtensionPrefs::kExtensionsPref, this); | 475 pref_change_registrar_.Add(ExtensionPrefs::kExtensionsPref, this); |
| 454 pref_change_registrar_.Add(prefs::kNTPAppPageNames, this); | 476 pref_change_registrar_.Add(prefs::kNTPAppPageNames, this); |
| 455 } | 477 } |
| 456 } | 478 } |
| 457 | 479 |
| 458 void AppLauncherHandler::HandleLaunchApp(const ListValue* args) { | 480 void AppLauncherHandler::HandleLaunchApp(const ListValue* args) { |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 web_app->icons[0].width = web_app->icons[0].data.width(); | 772 web_app->icons[0].width = web_app->icons[0].data.width(); |
| 751 web_app->icons[0].height = web_app->icons[0].data.height(); | 773 web_app->icons[0].height = web_app->icons[0].data.height(); |
| 752 } else { | 774 } else { |
| 753 web_app->icons.clear(); | 775 web_app->icons.clear(); |
| 754 } | 776 } |
| 755 | 777 |
| 756 scoped_refptr<CrxInstaller> installer( | 778 scoped_refptr<CrxInstaller> installer( |
| 757 extension_service_->MakeCrxInstaller(NULL)); | 779 extension_service_->MakeCrxInstaller(NULL)); |
| 758 installer->set_page_index(install_info->page_index); | 780 installer->set_page_index(install_info->page_index); |
| 759 installer->InstallWebApp(*web_app); | 781 installer->InstallWebApp(*web_app); |
| 782 attempted_bookmark_app_install_ = true; |
| 760 } | 783 } |
| 761 | 784 |
| 762 // static | 785 // static |
| 763 void AppLauncherHandler::RegisterUserPrefs(PrefService* pref_service) { | 786 void AppLauncherHandler::RegisterUserPrefs(PrefService* pref_service) { |
| 764 // TODO(csilv): We will want this to be a syncable preference instead. | 787 // TODO(csilv): We will want this to be a syncable preference instead. |
| 765 pref_service->RegisterListPref(prefs::kNTPAppPageNames, | 788 pref_service->RegisterListPref(prefs::kNTPAppPageNames, |
| 766 PrefService::UNSYNCABLE_PREF); | 789 PrefService::UNSYNCABLE_PREF); |
| 767 } | 790 } |
| 768 | 791 |
| 769 // statiic | 792 // statiic |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 917 | 940 |
| 918 void AppLauncherHandler::UninstallDefaultApps() { | 941 void AppLauncherHandler::UninstallDefaultApps() { |
| 919 AppsPromo* apps_promo = extension_service_->apps_promo(); | 942 AppsPromo* apps_promo = extension_service_->apps_promo(); |
| 920 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); | 943 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); |
| 921 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); | 944 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); |
| 922 iter != app_ids.end(); ++iter) { | 945 iter != app_ids.end(); ++iter) { |
| 923 if (extension_service_->GetExtensionById(*iter, true)) | 946 if (extension_service_->GetExtensionById(*iter, true)) |
| 924 extension_service_->UninstallExtension(*iter, false, NULL); | 947 extension_service_->UninstallExtension(*iter, false, NULL); |
| 925 } | 948 } |
| 926 } | 949 } |
| OLD | NEW |