| 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" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" | |
| 13 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 14 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 15 #include "base/string_split.h" | 14 #include "base/string_split.h" |
| 16 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 17 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 18 #include "base/values.h" | 17 #include "base/values.h" |
| 19 #include "chrome/browser/extensions/apps_promo.h" | 18 #include "chrome/browser/extensions/apps_promo.h" |
| 20 #include "chrome/browser/extensions/crx_installer.h" | 19 #include "chrome/browser/extensions/crx_installer.h" |
| 21 #include "chrome/browser/extensions/extension_prefs.h" | 20 #include "chrome/browser/extensions/extension_prefs.h" |
| 22 #include "chrome/browser/extensions/extension_service.h" | 21 #include "chrome/browser/extensions/extension_service.h" |
| 23 #include "chrome/browser/platform_util.h" | 22 #include "chrome/browser/platform_util.h" |
| 24 #include "chrome/browser/prefs/pref_service.h" | 23 #include "chrome/browser/prefs/pref_service.h" |
| 25 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 24 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 28 #include "chrome/browser/ui/browser_list.h" | 27 #include "chrome/browser/ui/browser_list.h" |
| 29 #include "chrome/browser/ui/browser_window.h" | 28 #include "chrome/browser/ui/browser_window.h" |
| 30 #include "chrome/browser/ui/webui/extension_icon_source.h" | 29 #include "chrome/browser/ui/webui/extension_icon_source.h" |
| 30 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 31 #include "chrome/browser/ui/webui/ntp/shown_sections_handler.h" | 31 #include "chrome/browser/ui/webui/ntp/shown_sections_handler.h" |
| 32 #include "chrome/common/chrome_notification_types.h" | 32 #include "chrome/common/chrome_notification_types.h" |
| 33 #include "chrome/common/chrome_switches.h" | |
| 34 #include "chrome/common/extensions/extension.h" | 33 #include "chrome/common/extensions/extension.h" |
| 35 #include "chrome/common/extensions/extension_constants.h" | 34 #include "chrome/common/extensions/extension_constants.h" |
| 36 #include "chrome/common/extensions/extension_icon_set.h" | 35 #include "chrome/common/extensions/extension_icon_set.h" |
| 37 #include "chrome/common/extensions/extension_resource.h" | 36 #include "chrome/common/extensions/extension_resource.h" |
| 38 #include "chrome/common/favicon_url.h" | 37 #include "chrome/common/favicon_url.h" |
| 39 #include "chrome/common/pref_names.h" | 38 #include "chrome/common/pref_names.h" |
| 40 #include "chrome/common/url_constants.h" | 39 #include "chrome/common/url_constants.h" |
| 41 #include "chrome/common/web_apps.h" | 40 #include "chrome/common/web_apps.h" |
| 42 #include "content/browser/disposition_utils.h" | 41 #include "content/browser/disposition_utils.h" |
| 43 #include "content/browser/tab_contents/tab_contents.h" | 42 #include "content/browser/tab_contents/tab_contents.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 } | 93 } |
| 95 return dictionary; | 94 return dictionary; |
| 96 } | 95 } |
| 97 | 96 |
| 98 // static | 97 // static |
| 99 bool AppLauncherHandler::IsAppExcludedFromList(const Extension* extension) { | 98 bool AppLauncherHandler::IsAppExcludedFromList(const Extension* extension) { |
| 100 // Don't include the WebStore and the Cloud Print app. | 99 // Don't include the WebStore and the Cloud Print app. |
| 101 // The WebStore launcher gets special treatment in ntp/apps.js. | 100 // The WebStore launcher gets special treatment in ntp/apps.js. |
| 102 // The Cloud Print app should never be displayed in the NTP. | 101 // The Cloud Print app should never be displayed in the NTP. |
| 103 bool ntp3 = | 102 bool ntp3 = |
| 104 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4); | 103 !NewTabUI::Ntp4Enabled(); |
| 105 if (!extension->is_app() || | 104 if (!extension->is_app() || |
| 106 (ntp3 && extension->id() == extension_misc::kWebStoreAppId) || | 105 (ntp3 && extension->id() == extension_misc::kWebStoreAppId) || |
| 107 (extension->id() == extension_misc::kCloudPrintAppId)) { | 106 (extension->id() == extension_misc::kCloudPrintAppId)) { |
| 108 return true; | 107 return true; |
| 109 } | 108 } |
| 110 return false; | 109 return false; |
| 111 } | 110 } |
| 112 | 111 |
| 113 void AppLauncherHandler::CreateAppInfo(const Extension* extension, | 112 void AppLauncherHandler::CreateAppInfo(const Extension* extension, |
| 114 const AppNotification* notification, | 113 const AppNotification* notification, |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 } | 259 } |
| 261 case chrome::NOTIFICATION_EXTENSION_LOADED: | 260 case chrome::NOTIFICATION_EXTENSION_LOADED: |
| 262 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | 261 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 263 const Extension* extension = | 262 const Extension* extension = |
| 264 type == chrome::NOTIFICATION_EXTENSION_LOADED ? | 263 type == chrome::NOTIFICATION_EXTENSION_LOADED ? |
| 265 Details<const Extension>(details).ptr() : | 264 Details<const Extension>(details).ptr() : |
| 266 Details<UnloadedExtensionInfo>(details)->extension; | 265 Details<UnloadedExtensionInfo>(details)->extension; |
| 267 if (!extension->is_app()) | 266 if (!extension->is_app()) |
| 268 break; | 267 break; |
| 269 | 268 |
| 270 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) { | 269 if (NewTabUI::Ntp4Enabled()) { |
| 271 scoped_ptr<DictionaryValue> app_info(GetAppInfo(extension)); | 270 scoped_ptr<DictionaryValue> app_info(GetAppInfo(extension)); |
| 272 if (app_info.get()) { | 271 if (app_info.get()) { |
| 273 std::string function = | 272 std::string function = |
| 274 type == chrome::NOTIFICATION_EXTENSION_LOADED ? | 273 type == chrome::NOTIFICATION_EXTENSION_LOADED ? |
| 275 "ntp4.appAdded" : "ntp4.appRemoved"; | 274 "ntp4.appAdded" : "ntp4.appRemoved"; |
| 276 web_ui_->CallJavascriptFunction(function, *app_info); | 275 web_ui_->CallJavascriptFunction(function, *app_info); |
| 277 } | 276 } |
| 278 } else if (web_ui_->tab_contents()) { | 277 } else if (web_ui_->tab_contents()) { |
| 279 HandleGetApps(NULL); | 278 HandleGetApps(NULL); |
| 280 } | 279 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 // Making shortcut does not make sense on ChromeOS because it does not have | 357 // Making shortcut does not make sense on ChromeOS because it does not have |
| 359 // a desktop. | 358 // a desktop. |
| 360 dictionary->SetBoolean("disableCreateAppShortcut", true); | 359 dictionary->SetBoolean("disableCreateAppShortcut", true); |
| 361 #endif | 360 #endif |
| 362 | 361 |
| 363 dictionary->SetBoolean( | 362 dictionary->SetBoolean( |
| 364 "showLauncher", | 363 "showLauncher", |
| 365 extension_service_->apps_promo()->ShouldShowAppLauncher( | 364 extension_service_->apps_promo()->ShouldShowAppLauncher( |
| 366 extension_service_->GetAppIds())); | 365 extension_service_->GetAppIds())); |
| 367 | 366 |
| 368 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) { | 367 if (NewTabUI::Ntp4Enabled()) { |
| 369 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); | 368 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); |
| 370 const ListValue* app_page_names = prefs->GetList(prefs::kNTPAppPageNames); | 369 const ListValue* app_page_names = prefs->GetList(prefs::kNTPAppPageNames); |
| 371 if (app_page_names && app_page_names->GetSize()) { | 370 if (app_page_names && app_page_names->GetSize()) { |
| 372 dictionary->Set("appPageNames", | 371 dictionary->Set("appPageNames", |
| 373 static_cast<ListValue*>(app_page_names->DeepCopy())); | 372 static_cast<ListValue*>(app_page_names->DeepCopy())); |
| 374 } | 373 } |
| 375 } | 374 } |
| 376 } | 375 } |
| 377 | 376 |
| 378 DictionaryValue* AppLauncherHandler::GetAppInfo(const Extension* extension) { | 377 DictionaryValue* AppLauncherHandler::GetAppInfo(const Extension* extension) { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 // If the user pressed special keys when clicking, override the saved | 491 // If the user pressed special keys when clicking, override the saved |
| 493 // preference for launch container. | 492 // preference for launch container. |
| 494 bool middle_button = (button == 1.0); | 493 bool middle_button = (button == 1.0); |
| 495 WindowOpenDisposition disposition = | 494 WindowOpenDisposition disposition = |
| 496 disposition_utils::DispositionFromClick(middle_button, alt_key, | 495 disposition_utils::DispositionFromClick(middle_button, alt_key, |
| 497 ctrl_key, meta_key, shift_key); | 496 ctrl_key, meta_key, shift_key); |
| 498 | 497 |
| 499 if (extension_id != extension_misc::kWebStoreAppId) { | 498 if (extension_id != extension_misc::kWebStoreAppId) { |
| 500 RecordAppLaunchByID(promo_active_, launch_bucket); | 499 RecordAppLaunchByID(promo_active_, launch_bucket); |
| 501 extension_service_->apps_promo()->ExpireDefaultApps(); | 500 extension_service_->apps_promo()->ExpireDefaultApps(); |
| 502 } else if (CommandLine::ForCurrentProcess()->HasSwitch( | 501 } else if (NewTabUI::Ntp4Enabled()) { |
| 503 switches::kNewTabPage4)) { | |
| 504 RecordWebStoreLaunch(promo_active_); | 502 RecordWebStoreLaunch(promo_active_); |
| 505 } | 503 } |
| 506 | 504 |
| 507 if (disposition == NEW_FOREGROUND_TAB || disposition == NEW_BACKGROUND_TAB) { | 505 if (disposition == NEW_FOREGROUND_TAB || disposition == NEW_BACKGROUND_TAB) { |
| 508 // TODO(jamescook): Proper support for background tabs. | 506 // TODO(jamescook): Proper support for background tabs. |
| 509 Browser::OpenApplication( | 507 Browser::OpenApplication( |
| 510 profile, extension, extension_misc::LAUNCH_TAB, disposition); | 508 profile, extension, extension_misc::LAUNCH_TAB, disposition); |
| 511 } else if (disposition == NEW_WINDOW) { | 509 } else if (disposition == NEW_WINDOW) { |
| 512 // Force a new window open. | 510 // Force a new window open. |
| 513 Browser::OpenApplication( | 511 Browser::OpenApplication( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 541 double launch_type; | 539 double launch_type; |
| 542 CHECK(args->GetString(0, &extension_id)); | 540 CHECK(args->GetString(0, &extension_id)); |
| 543 CHECK(args->GetDouble(1, &launch_type)); | 541 CHECK(args->GetDouble(1, &launch_type)); |
| 544 | 542 |
| 545 const Extension* extension = | 543 const Extension* extension = |
| 546 extension_service_->GetExtensionById(extension_id, true); | 544 extension_service_->GetExtensionById(extension_id, true); |
| 547 CHECK(extension); | 545 CHECK(extension); |
| 548 | 546 |
| 549 // Don't update the page; it already knows about the launch type change. | 547 // Don't update the page; it already knows about the launch type change. |
| 550 scoped_ptr<AutoReset<bool> > auto_reset; | 548 scoped_ptr<AutoReset<bool> > auto_reset; |
| 551 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) | 549 if (NewTabUI::Ntp4Enabled()) |
| 552 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); | 550 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); |
| 553 | 551 |
| 554 extension_service_->extension_prefs()->SetLaunchType( | 552 extension_service_->extension_prefs()->SetLaunchType( |
| 555 extension_id, | 553 extension_id, |
| 556 static_cast<ExtensionPrefs::LaunchType>( | 554 static_cast<ExtensionPrefs::LaunchType>( |
| 557 static_cast<int>(launch_type))); | 555 static_cast<int>(launch_type))); |
| 558 } | 556 } |
| 559 | 557 |
| 560 void AppLauncherHandler::HandleUninstallApp(const ListValue* args) { | 558 void AppLauncherHandler::HandleUninstallApp(const ListValue* args) { |
| 561 std::string extension_id; | 559 std::string extension_id; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 572 return; | 570 return; |
| 573 } | 571 } |
| 574 if (!extension_id_prompting_.empty()) | 572 if (!extension_id_prompting_.empty()) |
| 575 return; // Only one prompt at a time. | 573 return; // Only one prompt at a time. |
| 576 | 574 |
| 577 extension_id_prompting_ = extension_id; | 575 extension_id_prompting_ = extension_id; |
| 578 | 576 |
| 579 bool dont_confirm = false; | 577 bool dont_confirm = false; |
| 580 if (args->GetBoolean(1, &dont_confirm) && dont_confirm) { | 578 if (args->GetBoolean(1, &dont_confirm) && dont_confirm) { |
| 581 scoped_ptr<AutoReset<bool> > auto_reset; | 579 scoped_ptr<AutoReset<bool> > auto_reset; |
| 582 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) | 580 if (NewTabUI::Ntp4Enabled()) |
| 583 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); | 581 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); |
| 584 ExtensionDialogAccepted(); | 582 ExtensionDialogAccepted(); |
| 585 } else { | 583 } else { |
| 586 GetExtensionUninstallDialog()->ConfirmUninstall(this, extension); | 584 GetExtensionUninstallDialog()->ConfirmUninstall(this, extension); |
| 587 } | 585 } |
| 588 } | 586 } |
| 589 | 587 |
| 590 void AppLauncherHandler::HandleHideAppsPromo(const ListValue* args) { | 588 void AppLauncherHandler::HandleHideAppsPromo(const ListValue* args) { |
| 591 // If the user has intentionally hidden the promotion, we'll uninstall all the | 589 // If the user has intentionally hidden the promotion, we'll uninstall all the |
| 592 // default apps (we know the user hasn't installed any apps on their own at | 590 // default apps (we know the user hasn't installed any apps on their own at |
| 593 // this point, or the promotion wouldn't have been shown). | 591 // this point, or the promotion wouldn't have been shown). |
| 594 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) { | 592 if (NewTabUI::Ntp4Enabled()) { |
| 595 UninstallDefaultApps(); | 593 UninstallDefaultApps(); |
| 596 extension_service_->apps_promo()->HidePromo(); | 594 extension_service_->apps_promo()->HidePromo(); |
| 597 } else { | 595 } else { |
| 598 // TODO(estade): remove all this. NTP3 uninstalled all the default apps then | 596 // TODO(estade): remove all this. NTP3 uninstalled all the default apps then |
| 599 // refreshed the entire NTP, we don't have to jump through these hoops for | 597 // refreshed the entire NTP, we don't have to jump through these hoops for |
| 600 // NTP4 because each app uninstall is handled separately without reloading | 598 // NTP4 because each app uninstall is handled separately without reloading |
| 601 // the entire page. | 599 // the entire page. |
| 602 ignore_changes_ = true; | 600 ignore_changes_ = true; |
| 603 UninstallDefaultApps(); | 601 UninstallDefaultApps(); |
| 604 extension_service_->apps_promo()->HidePromo(); | 602 extension_service_->apps_promo()->HidePromo(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 635 | 633 |
| 636 std::vector<std::string> extension_ids; | 634 std::vector<std::string> extension_ids; |
| 637 for (size_t i = 0; i < app_order->GetSize(); ++i) { | 635 for (size_t i = 0; i < app_order->GetSize(); ++i) { |
| 638 std::string value; | 636 std::string value; |
| 639 if (app_order->GetString(i, &value)) | 637 if (app_order->GetString(i, &value)) |
| 640 extension_ids.push_back(value); | 638 extension_ids.push_back(value); |
| 641 } | 639 } |
| 642 | 640 |
| 643 // Don't update the page; it already knows the apps have been reordered. | 641 // Don't update the page; it already knows the apps have been reordered. |
| 644 scoped_ptr<AutoReset<bool> > auto_reset; | 642 scoped_ptr<AutoReset<bool> > auto_reset; |
| 645 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) | 643 if (NewTabUI::Ntp4Enabled()) |
| 646 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); | 644 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); |
| 647 | 645 |
| 648 extension_service_->extension_prefs()->SetAppDraggedByUser(dragged_app_id); | 646 extension_service_->extension_prefs()->SetAppDraggedByUser(dragged_app_id); |
| 649 extension_service_->extension_prefs()->SetAppLauncherOrder(extension_ids); | 647 extension_service_->extension_prefs()->SetAppLauncherOrder(extension_ids); |
| 650 } | 648 } |
| 651 | 649 |
| 652 void AppLauncherHandler::HandleSetPageIndex(const ListValue* args) { | 650 void AppLauncherHandler::HandleSetPageIndex(const ListValue* args) { |
| 653 std::string extension_id; | 651 std::string extension_id; |
| 654 double page_index; | 652 double page_index; |
| 655 CHECK(args->GetString(0, &extension_id)); | 653 CHECK(args->GetString(0, &extension_id)); |
| 656 CHECK(args->GetDouble(1, &page_index)); | 654 CHECK(args->GetDouble(1, &page_index)); |
| 657 | 655 |
| 658 // Don't update the page; it already knows the apps have been reordered. | 656 // Don't update the page; it already knows the apps have been reordered. |
| 659 scoped_ptr<AutoReset<bool> > auto_reset; | 657 scoped_ptr<AutoReset<bool> > auto_reset; |
| 660 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) | 658 if (NewTabUI::Ntp4Enabled()) |
| 661 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); | 659 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); |
| 662 | 660 |
| 663 extension_service_->extension_prefs()->SetPageIndex(extension_id, | 661 extension_service_->extension_prefs()->SetPageIndex(extension_id, |
| 664 static_cast<int>(page_index)); | 662 static_cast<int>(page_index)); |
| 665 } | 663 } |
| 666 | 664 |
| 667 void AppLauncherHandler::HandlePromoSeen(const ListValue* args) { | 665 void AppLauncherHandler::HandlePromoSeen(const ListValue* args) { |
| 668 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, | 666 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, |
| 669 extension_misc::PROMO_SEEN, | 667 extension_misc::PROMO_SEEN, |
| 670 extension_misc::PROMO_BUCKET_BOUNDARY); | 668 extension_misc::PROMO_BUCKET_BOUNDARY); |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 | 908 |
| 911 void AppLauncherHandler::UninstallDefaultApps() { | 909 void AppLauncherHandler::UninstallDefaultApps() { |
| 912 AppsPromo* apps_promo = extension_service_->apps_promo(); | 910 AppsPromo* apps_promo = extension_service_->apps_promo(); |
| 913 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); | 911 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); |
| 914 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); | 912 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); |
| 915 iter != app_ids.end(); ++iter) { | 913 iter != app_ids.end(); ++iter) { |
| 916 if (extension_service_->GetExtensionById(*iter, true)) | 914 if (extension_service_->GetExtensionById(*iter, true)) |
| 917 extension_service_->UninstallExtension(*iter, false, NULL); | 915 extension_service_->UninstallExtension(*iter, false, NULL); |
| 918 } | 916 } |
| 919 } | 917 } |
| OLD | NEW |