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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 ExtensionPrefs::LAUNCH_DEFAULT)); | 160 ExtensionPrefs::LAUNCH_DEFAULT)); |
161 value->SetBoolean("offline_enabled", extension->offline_enabled()); | 161 value->SetBoolean("offline_enabled", extension->offline_enabled()); |
162 value->SetBoolean("is_component", | 162 value->SetBoolean("is_component", |
163 extension->location() == Extension::COMPONENT); | 163 extension->location() == Extension::COMPONENT); |
164 value->SetBoolean("is_webstore", | 164 value->SetBoolean("is_webstore", |
165 extension->id() == extension_misc::kWebStoreAppId); | 165 extension->id() == extension_misc::kWebStoreAppId); |
166 | 166 |
167 if (notification) | 167 if (notification) |
168 value->Set("notification", SerializeNotification(*notification)); | 168 value->Set("notification", SerializeNotification(*notification)); |
169 | 169 |
170 int app_launch_index = prefs->GetAppLaunchIndex(extension->id()); | 170 StringOrdinal page_index = prefs->GetPageIndex(extension->id()); |
171 if (app_launch_index == -1) { | 171 if (!page_index.IsValid()) { |
172 // Make sure every app has a page index (some predate the page index). | |
173 // The webstore app should be on the first page. | |
174 page_index = extension->id() == extension_misc::kWebStoreAppId ? | |
175 prefs->GetFirstAppPage() : prefs->GetNaturalAppPageIndex(); | |
176 prefs->SetPageIndex(extension->id(), page_index); | |
177 } | |
178 // We convert the page_index to an integer becuase the pages are referenced | |
179 // from within an array in the javascript code, which can't be easily | |
180 // changed to handle the StringOrdinal values, so we do the conversion here. | |
181 value->SetInteger("page_index", prefs->PageIndexAsInteger(page_index)); | |
182 | |
183 StringOrdinal app_launch_index = prefs->GetAppLaunchIndex(extension->id()); | |
184 if (!app_launch_index.IsValid()) { | |
172 // Make sure every app has a launch index (some predate the launch index). | 185 // Make sure every app has a launch index (some predate the launch index). |
173 // The webstore's app launch index is set to -2 to make sure it's first. | 186 // The webstore's app launch index is set to -2 to make sure it's first. |
174 // The next time the user drags (any) app this will be set to something | 187 // The next time the user drags (any) app this will be set to something |
175 // sane (i.e. >= 0). | 188 // sane (i.e. >= 0). |
176 app_launch_index = extension->id() == extension_misc::kWebStoreAppId ? | 189 app_launch_index = extension->id() == extension_misc::kWebStoreAppId ? |
177 -2 : prefs->GetNextAppLaunchIndex(0); | 190 prefs->CreateFirstAppLaunchIndex(page_index) |
191 : prefs->GetNextAppLaunchIndex(page_index); | |
Finnur
2011/11/17 14:59:25
nit: operator (:) should be at the end of previous
csharp
2011/11/17 19:51:58
Done.
Finnur
2011/11/18 09:35:15
Did you? I don't see the change...
On 2011/11/17
csharp
2011/11/18 15:57:31
Done.
| |
178 prefs->SetAppLaunchIndex(extension->id(), app_launch_index); | 192 prefs->SetAppLaunchIndex(extension->id(), app_launch_index); |
179 } | 193 } |
180 value->SetInteger("app_launch_index", app_launch_index); | 194 value->SetString("app_launch_index", app_launch_index.ToString()); |
181 | |
182 int page_index = prefs->GetPageIndex(extension->id()); | |
183 if (page_index < 0) { | |
184 // Make sure every app has a page index (some predate the page index). | |
185 // The webstore app should be on the first page. | |
186 page_index = extension->id() == extension_misc::kWebStoreAppId ? | |
187 0 : prefs->GetNaturalAppPageIndex(); | |
188 prefs->SetPageIndex(extension->id(), page_index); | |
189 } | |
190 value->SetInteger("page_index", page_index); | |
191 } | 195 } |
192 | 196 |
193 // TODO(estade): remove this. We record app launches via js calls rather than | 197 // TODO(estade): remove this. We record app launches via js calls rather than |
194 // pings for ntp4. | 198 // pings for ntp4. |
195 // static | 199 // static |
196 bool AppLauncherHandler::HandlePing(Profile* profile, const std::string& path) { | 200 bool AppLauncherHandler::HandlePing(Profile* profile, const std::string& path) { |
197 std::vector<std::string> params; | 201 std::vector<std::string> params; |
198 base::SplitString(path, '+', ¶ms); | 202 base::SplitString(path, '+', ¶ms); |
199 | 203 |
200 // Check if the user launched an app from the most visited or recently | 204 // Check if the user launched an app from the most visited or recently |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
400 for (it = extensions->begin(); it != extensions->end(); ++it) { | 404 for (it = extensions->begin(); it != extensions->end(); ++it) { |
401 if (!IsAppExcludedFromList(*it)) { | 405 if (!IsAppExcludedFromList(*it)) { |
402 DictionaryValue* app_info = GetAppInfo(*it); | 406 DictionaryValue* app_info = GetAppInfo(*it); |
403 list->Append(app_info); | 407 list->Append(app_info); |
404 } else { | 408 } else { |
405 // This is necessary because in some previous versions of chrome, we set a | 409 // This is necessary because in some previous versions of chrome, we set a |
406 // page index for non-app extensions. Old profiles can persist this error, | 410 // page index for non-app extensions. Old profiles can persist this error, |
407 // and this fixes it. If we don't fix it, GetNaturalAppPageIndex() doesn't | 411 // and this fixes it. If we don't fix it, GetNaturalAppPageIndex() doesn't |
408 // work. See http://crbug.com/98325 | 412 // work. See http://crbug.com/98325 |
409 ExtensionPrefs* prefs = extension_service_->extension_prefs(); | 413 ExtensionPrefs* prefs = extension_service_->extension_prefs(); |
410 if (prefs->GetPageIndex((*it)->id()) != -1) | 414 if (prefs->GetPageIndex((*it)->id()).IsValid()) |
411 prefs->ClearPageIndex((*it)->id()); | 415 prefs->ClearPageIndex((*it)->id()); |
412 } | 416 } |
413 } | 417 } |
414 | 418 |
415 extensions = extension_service_->disabled_extensions(); | 419 extensions = extension_service_->disabled_extensions(); |
416 for (it = extensions->begin(); it != extensions->end(); ++it) { | 420 for (it = extensions->begin(); it != extensions->end(); ++it) { |
417 if (!IsAppExcludedFromList(*it)) { | 421 if (!IsAppExcludedFromList(*it)) { |
418 DictionaryValue* app_info = new DictionaryValue(); | 422 DictionaryValue* app_info = new DictionaryValue(); |
419 CreateAppInfo(*it, | 423 CreateAppInfo(*it, |
420 NULL, | 424 NULL, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
452 dictionary->SetBoolean("disableCreateAppShortcut", true); | 456 dictionary->SetBoolean("disableCreateAppShortcut", true); |
453 #endif | 457 #endif |
454 | 458 |
455 dictionary->SetBoolean( | 459 dictionary->SetBoolean( |
456 "showLauncher", | 460 "showLauncher", |
457 extension_service_->apps_promo()->ShouldShowAppLauncher( | 461 extension_service_->apps_promo()->ShouldShowAppLauncher( |
458 extension_service_->GetAppIds())); | 462 extension_service_->GetAppIds())); |
459 | 463 |
460 if (NewTabUI::NTP4Enabled()) { | 464 if (NewTabUI::NTP4Enabled()) { |
461 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs(); | 465 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs(); |
462 const ListValue* app_page_names = prefs->GetList(prefs::kNTPAppPageNames); | 466 const DictionaryValue* app_page_names = |
463 if (!app_page_names || !app_page_names->GetSize()) { | 467 prefs->GetDictionary(prefs::kNTPAppPageNames); |
464 ListPrefUpdate update(prefs, prefs::kNTPAppPageNames); | 468 if (!app_page_names || !app_page_names->size()) { |
465 ListValue* list = update.Get(); | 469 DictionaryPrefUpdate update(prefs, prefs::kNTPAppPageNames); |
466 list->Set(0, Value::CreateStringValue( | 470 DictionaryValue* names_dictionary = update.Get(); |
471 StringOrdinal new_page = StringOrdinal::CreateValidOrdinal(); | |
472 names_dictionary->Set(new_page.ToString(), Value::CreateStringValue( | |
467 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME))); | 473 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME))); |
468 dictionary->Set("appPageNames", | 474 dictionary->Set("appPageNames", |
469 static_cast<ListValue*>(list->DeepCopy())); | 475 static_cast<DictionaryValue*>( |
476 names_dictionary->DeepCopy())); | |
470 } else { | 477 } else { |
471 dictionary->Set("appPageNames", | 478 dictionary->Set("appPageNames", |
472 static_cast<ListValue*>(app_page_names->DeepCopy())); | 479 static_cast<DictionaryValue*>( |
480 app_page_names->DeepCopy())); | |
473 } | 481 } |
474 } | 482 } |
475 } | 483 } |
476 | 484 |
477 DictionaryValue* AppLauncherHandler::GetAppInfo(const Extension* extension) { | 485 DictionaryValue* AppLauncherHandler::GetAppInfo(const Extension* extension) { |
478 AppNotificationManager* notification_manager = | 486 AppNotificationManager* notification_manager = |
479 extension_service_->app_notification_manager(); | 487 extension_service_->app_notification_manager(); |
480 DictionaryValue* app_info = new DictionaryValue(); | 488 DictionaryValue* app_info = new DictionaryValue(); |
481 // CreateAppInfo can change the extension prefs. | 489 // CreateAppInfo can change the extension prefs. |
482 AutoReset<bool> auto_reset(&ignore_changes_, true); | 490 AutoReset<bool> auto_reset(&ignore_changes_, true); |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
746 if (app_order->GetString(i, &value)) | 754 if (app_order->GetString(i, &value)) |
747 extension_ids.push_back(value); | 755 extension_ids.push_back(value); |
748 } | 756 } |
749 | 757 |
750 // Don't update the page; it already knows the apps have been reordered. | 758 // Don't update the page; it already knows the apps have been reordered. |
751 scoped_ptr<AutoReset<bool> > auto_reset; | 759 scoped_ptr<AutoReset<bool> > auto_reset; |
752 if (NewTabUI::NTP4Enabled()) | 760 if (NewTabUI::NTP4Enabled()) |
753 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); | 761 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); |
754 | 762 |
755 extension_service_->extension_prefs()->SetAppDraggedByUser(dragged_app_id); | 763 extension_service_->extension_prefs()->SetAppDraggedByUser(dragged_app_id); |
756 extension_service_->extension_prefs()->SetAppLauncherOrder(extension_ids); | 764 extension_service_->extension_prefs()->SetAppLauncherOrder(extension_ids, |
765 dragged_app_id); | |
757 } | 766 } |
758 | 767 |
759 void AppLauncherHandler::HandleSetPageIndex(const ListValue* args) { | 768 void AppLauncherHandler::HandleSetPageIndex(const ListValue* args) { |
760 std::string extension_id; | 769 std::string extension_id; |
761 double page_index; | 770 double page_index_raw; |
762 CHECK(args->GetString(0, &extension_id)); | 771 CHECK(args->GetString(0, &extension_id)); |
763 CHECK(args->GetDouble(1, &page_index)); | 772 CHECK(args->GetDouble(1, &page_index_raw)); |
773 StringOrdinal page_index = | |
774 extension_service_->extension_prefs()->PageIndexAsStringOrdinal( | |
775 static_cast<size_t>(page_index_raw)); | |
764 | 776 |
765 // Don't update the page; it already knows the apps have been reordered. | 777 // Don't update the page; it already knows the apps have been reordered. |
766 scoped_ptr<AutoReset<bool> > auto_reset; | 778 scoped_ptr<AutoReset<bool> > auto_reset; |
767 if (NewTabUI::NTP4Enabled()) | 779 if (NewTabUI::NTP4Enabled()) |
768 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); | 780 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); |
769 | 781 |
770 extension_service_->extension_prefs()->SetPageIndex(extension_id, | 782 extension_service_->extension_prefs()->SetPageIndex(extension_id, |
771 static_cast<int>(page_index)); | 783 page_index); |
772 } | 784 } |
773 | 785 |
774 void AppLauncherHandler::HandlePromoSeen(const ListValue* args) { | 786 void AppLauncherHandler::HandlePromoSeen(const ListValue* args) { |
775 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, | 787 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, |
776 extension_misc::PROMO_SEEN, | 788 extension_misc::PROMO_SEEN, |
777 extension_misc::PROMO_BUCKET_BOUNDARY); | 789 extension_misc::PROMO_BUCKET_BOUNDARY); |
778 } | 790 } |
779 | 791 |
780 void AppLauncherHandler::HandleSaveAppPageName(const ListValue* args) { | 792 void AppLauncherHandler::HandleSaveAppPageName(const ListValue* args) { |
781 string16 name; | 793 string16 name; |
782 CHECK(args->GetString(0, &name)); | 794 CHECK(args->GetString(0, &name)); |
783 | 795 |
784 double page_index; | 796 double page_index; |
785 CHECK(args->GetDouble(1, &page_index)); | 797 CHECK(args->GetDouble(1, &page_index)); |
798 std::string page_index_str = base::IntToString(static_cast<int>(page_index)); | |
786 | 799 |
787 AutoReset<bool> auto_reset(&ignore_changes_, true); | 800 AutoReset<bool> auto_reset(&ignore_changes_, true); |
788 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs(); | 801 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs(); |
789 ListPrefUpdate update(prefs, prefs::kNTPAppPageNames); | 802 DictionaryPrefUpdate update(prefs, prefs::kNTPAppPageNames); |
790 ListValue* list = update.Get(); | 803 DictionaryValue* dictionary = update.Get(); |
791 list->Set(static_cast<size_t>(page_index), Value::CreateStringValue(name)); | 804 dictionary->Set(page_index_str, Value::CreateStringValue(name)); |
792 } | 805 } |
793 | 806 |
794 void AppLauncherHandler::HandleGenerateAppForLink(const ListValue* args) { | 807 void AppLauncherHandler::HandleGenerateAppForLink(const ListValue* args) { |
795 std::string url; | 808 std::string url; |
796 CHECK(args->GetString(0, &url)); | 809 CHECK(args->GetString(0, &url)); |
797 GURL launch_url(url); | 810 GURL launch_url(url); |
798 | 811 |
799 string16 title; | 812 string16 title; |
800 CHECK(args->GetString(1, &title)); | 813 CHECK(args->GetString(1, &title)); |
801 | 814 |
802 double page_index; | 815 double page_index; |
803 CHECK(args->GetDouble(2, &page_index)); | 816 CHECK(args->GetDouble(2, &page_index)); |
817 StringOrdinal page_index_str = | |
818 extension_service_->extension_prefs()->PageIndexAsStringOrdinal( | |
819 static_cast<int>(page_index)); | |
804 | 820 |
805 Profile* profile = Profile::FromWebUI(web_ui_); | 821 Profile* profile = Profile::FromWebUI(web_ui_); |
806 FaviconService* favicon_service = | 822 FaviconService* favicon_service = |
807 profile->GetFaviconService(Profile::EXPLICIT_ACCESS); | 823 profile->GetFaviconService(Profile::EXPLICIT_ACCESS); |
808 if (!favicon_service) { | 824 if (!favicon_service) { |
809 LOG(ERROR) << "No favicon service"; | 825 LOG(ERROR) << "No favicon service"; |
810 return; | 826 return; |
811 } | 827 } |
812 | 828 |
813 scoped_ptr<AppInstallInfo> install_info(new AppInstallInfo()); | 829 scoped_ptr<AppInstallInfo> install_info(new AppInstallInfo()); |
814 install_info->is_bookmark_app = true; | 830 install_info->is_bookmark_app = true; |
815 install_info->title = title; | 831 install_info->title = title; |
816 install_info->app_url = launch_url; | 832 install_info->app_url = launch_url; |
817 install_info->page_index = static_cast<int>(page_index); | 833 install_info->page_index = page_index_str; |
818 | 834 |
819 FaviconService::Handle h = favicon_service->GetFaviconForURL( | 835 FaviconService::Handle h = favicon_service->GetFaviconForURL( |
820 launch_url, history::FAVICON, &favicon_consumer_, | 836 launch_url, history::FAVICON, &favicon_consumer_, |
821 base::Bind(&AppLauncherHandler::OnFaviconForApp, base::Unretained(this))); | 837 base::Bind(&AppLauncherHandler::OnFaviconForApp, base::Unretained(this))); |
822 favicon_consumer_.SetClientData(favicon_service, h, install_info.release()); | 838 favicon_consumer_.SetClientData(favicon_service, h, install_info.release()); |
823 } | 839 } |
824 | 840 |
825 void AppLauncherHandler::HandleRecordAppLaunchByURL( | 841 void AppLauncherHandler::HandleRecordAppLaunchByURL( |
826 const base::ListValue* args) { | 842 const base::ListValue* args) { |
827 std::string url; | 843 std::string url; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
884 return; | 900 return; |
885 | 901 |
886 StringValue app_id(highlight_app_id_); | 902 StringValue app_id(highlight_app_id_); |
887 web_ui_->CallJavascriptFunction("ntp4.setAppToBeHighlighted", app_id); | 903 web_ui_->CallJavascriptFunction("ntp4.setAppToBeHighlighted", app_id); |
888 highlight_app_id_.clear(); | 904 highlight_app_id_.clear(); |
889 } | 905 } |
890 | 906 |
891 // static | 907 // static |
892 void AppLauncherHandler::RegisterUserPrefs(PrefService* pref_service) { | 908 void AppLauncherHandler::RegisterUserPrefs(PrefService* pref_service) { |
893 // TODO(csilv): We will want this to be a syncable preference instead. | 909 // TODO(csilv): We will want this to be a syncable preference instead. |
894 pref_service->RegisterListPref(prefs::kNTPAppPageNames, | 910 pref_service->RegisterDictionaryPref(prefs::kNTPAppPageNames, |
895 PrefService::UNSYNCABLE_PREF); | 911 PrefService::UNSYNCABLE_PREF); |
896 } | 912 } |
897 | 913 |
898 // statiic | 914 // static |
899 void AppLauncherHandler::RecordWebStoreLaunch(bool promo_active) { | 915 void AppLauncherHandler::RecordWebStoreLaunch(bool promo_active) { |
900 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppLaunchHistogram, | 916 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppLaunchHistogram, |
901 extension_misc::APP_LAUNCH_NTP_WEBSTORE, | 917 extension_misc::APP_LAUNCH_NTP_WEBSTORE, |
902 extension_misc::APP_LAUNCH_BUCKET_BOUNDARY); | 918 extension_misc::APP_LAUNCH_BUCKET_BOUNDARY); |
903 | 919 |
904 if (!promo_active) return; | 920 if (!promo_active) return; |
905 | 921 |
906 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, | 922 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, |
907 extension_misc::PROMO_LAUNCH_WEB_STORE, | 923 extension_misc::PROMO_LAUNCH_WEB_STORE, |
908 extension_misc::PROMO_BUCKET_BOUNDARY); | 924 extension_misc::PROMO_BUCKET_BOUNDARY); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1049 | 1065 |
1050 void AppLauncherHandler::UninstallDefaultApps() { | 1066 void AppLauncherHandler::UninstallDefaultApps() { |
1051 AppsPromo* apps_promo = extension_service_->apps_promo(); | 1067 AppsPromo* apps_promo = extension_service_->apps_promo(); |
1052 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); | 1068 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); |
1053 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); | 1069 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); |
1054 iter != app_ids.end(); ++iter) { | 1070 iter != app_ids.end(); ++iter) { |
1055 if (extension_service_->GetExtensionById(*iter, true)) | 1071 if (extension_service_->GetExtensionById(*iter, true)) |
1056 extension_service_->UninstallExtension(*iter, false, NULL); | 1072 extension_service_->UninstallExtension(*iter, false, NULL); |
1057 } | 1073 } |
1058 } | 1074 } |
OLD | NEW |