Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(622)

Side by Side Diff: chrome/browser/ui/webui/ntp/app_launcher_handler.cc

Issue 8198003: Convert app_launch_index and page_index from int to StringOrdinal. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Addressing review comments Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/webui/ntp/app_launcher_handler.h ('k') | chrome/common/string_ordinal.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 ExtensionPrefs::LAUNCH_DEFAULT)); 153 ExtensionPrefs::LAUNCH_DEFAULT));
154 value->SetBoolean("offline_enabled", extension->offline_enabled()); 154 value->SetBoolean("offline_enabled", extension->offline_enabled());
155 value->SetBoolean("is_component", 155 value->SetBoolean("is_component",
156 extension->location() == Extension::COMPONENT); 156 extension->location() == Extension::COMPONENT);
157 value->SetBoolean("is_webstore", 157 value->SetBoolean("is_webstore",
158 extension->id() == extension_misc::kWebStoreAppId); 158 extension->id() == extension_misc::kWebStoreAppId);
159 159
160 if (notification) 160 if (notification)
161 value->Set("notification", SerializeNotification(*notification)); 161 value->Set("notification", SerializeNotification(*notification));
162 162
163 int app_launch_index = prefs->GetAppLaunchIndex(extension->id()); 163 StringOrdinal page_index = prefs->GetPageIndex(extension->id());
164 if (app_launch_index == -1) { 164 if (!page_index.IsValid()) {
165 // Make sure every app has a page index (some predate the page index).
166 // The webstore app should be on the first page.
167 page_index = extension->id() == extension_misc::kWebStoreAppId ?
168 prefs->GetFirstAppPage() : prefs->GetNaturalAppPageIndex();
169 prefs->SetPageIndex(extension->id(), page_index);
170 }
171 // We convert the page_index to an integer becuase the pages are referenced
172 // from within an array in the javascript code, which can't be easily
173 // changed to handle the StringOrdinal values, so we do the conversion here.
174 value->SetInteger("page_index", prefs->PageIndexAsInteger(page_index));
175
176 StringOrdinal app_launch_index = prefs->GetAppLaunchIndex(extension->id());
177 if (!app_launch_index.IsValid()) {
165 // Make sure every app has a launch index (some predate the launch index). 178 // Make sure every app has a launch index (some predate the launch index).
166 // The webstore's app launch index is set to -2 to make sure it's first. 179 // The webstore's app launch index is set to -2 to make sure it's first.
167 // The next time the user drags (any) app this will be set to something 180 // The next time the user drags (any) app this will be set to something
168 // sane (i.e. >= 0). 181 // sane (i.e. >= 0).
169 app_launch_index = extension->id() == extension_misc::kWebStoreAppId ? 182 app_launch_index = extension->id() == extension_misc::kWebStoreAppId ?
170 -2 : prefs->GetNextAppLaunchIndex(0); 183 prefs->CreateFirstAppLaunchIndex(page_index)
184 : prefs->GetNextAppLaunchIndex(page_index);
171 prefs->SetAppLaunchIndex(extension->id(), app_launch_index); 185 prefs->SetAppLaunchIndex(extension->id(), app_launch_index);
172 } 186 }
173 value->SetInteger("app_launch_index", app_launch_index); 187 value->SetString("app_launch_index", app_launch_index.ToString());
174
175 int page_index = prefs->GetPageIndex(extension->id());
176 if (page_index < 0) {
177 // Make sure every app has a page index (some predate the page index).
178 // The webstore app should be on the first page.
179 page_index = extension->id() == extension_misc::kWebStoreAppId ?
180 0 : prefs->GetNaturalAppPageIndex();
181 prefs->SetPageIndex(extension->id(), page_index);
182 }
183 value->SetInteger("page_index", page_index);
184 } 188 }
185 189
186 WebUIMessageHandler* AppLauncherHandler::Attach(WebUI* web_ui) { 190 WebUIMessageHandler* AppLauncherHandler::Attach(WebUI* web_ui) {
187 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, 191 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP,
188 content::Source<TabContents>(web_ui->tab_contents())); 192 content::Source<TabContents>(web_ui->tab_contents()));
189 return WebUIMessageHandler::Attach(web_ui); 193 return WebUIMessageHandler::Attach(web_ui);
190 } 194 }
191 195
192 void AppLauncherHandler::RegisterMessages() { 196 void AppLauncherHandler::RegisterMessages() {
193 web_ui_->RegisterMessageCallback("getApps", 197 web_ui_->RegisterMessageCallback("getApps",
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 AutoReset<bool> auto_reset(&ignore_changes_, true); 349 AutoReset<bool> auto_reset(&ignore_changes_, true);
346 350
347 ListValue* list = new ListValue(); 351 ListValue* list = new ListValue();
348 const ExtensionList* extensions = extension_service_->extensions(); 352 const ExtensionList* extensions = extension_service_->extensions();
349 ExtensionList::const_iterator it; 353 ExtensionList::const_iterator it;
350 for (it = extensions->begin(); it != extensions->end(); ++it) { 354 for (it = extensions->begin(); it != extensions->end(); ++it) {
351 if (!IsAppExcludedFromList(*it)) { 355 if (!IsAppExcludedFromList(*it)) {
352 DictionaryValue* app_info = GetAppInfo(*it); 356 DictionaryValue* app_info = GetAppInfo(*it);
353 list->Append(app_info); 357 list->Append(app_info);
354 } else { 358 } else {
355 // This is necessary because in some previous versions of chrome, we set a 359 // This is necessary because in some previous versions of chrome, we set a
csharp 2011/11/18 15:57:31 I'd like to move this else into the migration code
Finnur 2011/11/21 12:16:14 Not sure who this question is addressed to, but my
csharp 2011/11/21 15:14:32 The problem is that some extensions (which don't a
Finnur 2011/11/21 15:23:20 Um... that was my point. Should any extension (app
356 // page index for non-app extensions. Old profiles can persist this error, 360 // page index for non-app extensions. Old profiles can persist this error,
357 // and this fixes it. If we don't fix it, GetNaturalAppPageIndex() doesn't 361 // and this fixes it. If we don't fix it, GetNaturalAppPageIndex() doesn't
358 // work. See http://crbug.com/98325 362 // work. See http://crbug.com/98325
359 ExtensionPrefs* prefs = extension_service_->extension_prefs(); 363 ExtensionPrefs* prefs = extension_service_->extension_prefs();
360 if (prefs->GetPageIndex((*it)->id()) != -1) 364 if (prefs->GetPageIndex((*it)->id()).IsValid())
361 prefs->ClearPageIndex((*it)->id()); 365 prefs->ClearPageIndex((*it)->id());
362 } 366 }
363 } 367 }
364 368
365 extensions = extension_service_->disabled_extensions(); 369 extensions = extension_service_->disabled_extensions();
366 for (it = extensions->begin(); it != extensions->end(); ++it) { 370 for (it = extensions->begin(); it != extensions->end(); ++it) {
367 if (!IsAppExcludedFromList(*it)) { 371 if (!IsAppExcludedFromList(*it)) {
368 DictionaryValue* app_info = new DictionaryValue(); 372 DictionaryValue* app_info = new DictionaryValue();
369 CreateAppInfo(*it, 373 CreateAppInfo(*it,
370 NULL, 374 NULL,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 dictionary->SetBoolean("disableCreateAppShortcut", true); 406 dictionary->SetBoolean("disableCreateAppShortcut", true);
403 #endif 407 #endif
404 408
405 dictionary->SetBoolean( 409 dictionary->SetBoolean(
406 "showLauncher", 410 "showLauncher",
407 extension_service_->apps_promo()->ShouldShowAppLauncher( 411 extension_service_->apps_promo()->ShouldShowAppLauncher(
408 extension_service_->GetAppIds())); 412 extension_service_->GetAppIds()));
409 413
410 if (NewTabUI::NTP4Enabled()) { 414 if (NewTabUI::NTP4Enabled()) {
411 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs(); 415 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs();
412 const ListValue* app_page_names = prefs->GetList(prefs::kNTPAppPageNames); 416 const DictionaryValue* app_page_names =
413 if (!app_page_names || !app_page_names->GetSize()) { 417 prefs->GetDictionary(prefs::kNTPAppPageNames);
414 ListPrefUpdate update(prefs, prefs::kNTPAppPageNames); 418 if (!app_page_names || !app_page_names->size()) {
415 ListValue* list = update.Get(); 419 DictionaryPrefUpdate update(prefs, prefs::kNTPAppPageNames);
416 list->Set(0, Value::CreateStringValue( 420 DictionaryValue* names_dictionary = update.Get();
421 StringOrdinal new_page = StringOrdinal::CreateInitialOrdinal();
422 names_dictionary->Set(new_page.ToString(), Value::CreateStringValue(
417 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME))); 423 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME)));
418 dictionary->Set("appPageNames", 424 dictionary->Set("appPageNames",
419 static_cast<ListValue*>(list->DeepCopy())); 425 static_cast<DictionaryValue*>(
426 names_dictionary->DeepCopy()));
420 } else { 427 } else {
421 dictionary->Set("appPageNames", 428 dictionary->Set("appPageNames",
422 static_cast<ListValue*>(app_page_names->DeepCopy())); 429 static_cast<DictionaryValue*>(
430 app_page_names->DeepCopy()));
423 } 431 }
424 } 432 }
425 } 433 }
426 434
427 DictionaryValue* AppLauncherHandler::GetAppInfo(const Extension* extension) { 435 DictionaryValue* AppLauncherHandler::GetAppInfo(const Extension* extension) {
428 AppNotificationManager* notification_manager = 436 AppNotificationManager* notification_manager =
429 extension_service_->app_notification_manager(); 437 extension_service_->app_notification_manager();
430 DictionaryValue* app_info = new DictionaryValue(); 438 DictionaryValue* app_info = new DictionaryValue();
431 // CreateAppInfo can change the extension prefs. 439 // CreateAppInfo can change the extension prefs.
432 AutoReset<bool> auto_reset(&ignore_changes_, true); 440 AutoReset<bool> auto_reset(&ignore_changes_, true);
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 if (app_order->GetString(i, &value)) 706 if (app_order->GetString(i, &value))
699 extension_ids.push_back(value); 707 extension_ids.push_back(value);
700 } 708 }
701 709
702 // Don't update the page; it already knows the apps have been reordered. 710 // Don't update the page; it already knows the apps have been reordered.
703 scoped_ptr<AutoReset<bool> > auto_reset; 711 scoped_ptr<AutoReset<bool> > auto_reset;
704 if (NewTabUI::NTP4Enabled()) 712 if (NewTabUI::NTP4Enabled())
705 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); 713 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true));
706 714
707 extension_service_->extension_prefs()->SetAppDraggedByUser(dragged_app_id); 715 extension_service_->extension_prefs()->SetAppDraggedByUser(dragged_app_id);
708 extension_service_->extension_prefs()->SetAppLauncherOrder(extension_ids); 716 extension_service_->extension_prefs()->SetAppLauncherOrder(extension_ids,
717 dragged_app_id);
709 } 718 }
710 719
711 void AppLauncherHandler::HandleSetPageIndex(const ListValue* args) { 720 void AppLauncherHandler::HandleSetPageIndex(const ListValue* args) {
712 std::string extension_id; 721 std::string extension_id;
713 double page_index; 722 double page_index_raw;
714 CHECK(args->GetString(0, &extension_id)); 723 CHECK(args->GetString(0, &extension_id));
715 CHECK(args->GetDouble(1, &page_index)); 724 CHECK(args->GetDouble(1, &page_index_raw));
725 StringOrdinal page_index =
726 extension_service_->extension_prefs()->PageIndexAsStringOrdinal(
727 static_cast<size_t>(page_index_raw));
716 728
717 // Don't update the page; it already knows the apps have been reordered. 729 // Don't update the page; it already knows the apps have been reordered.
718 scoped_ptr<AutoReset<bool> > auto_reset; 730 scoped_ptr<AutoReset<bool> > auto_reset;
719 if (NewTabUI::NTP4Enabled()) 731 if (NewTabUI::NTP4Enabled())
720 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true)); 732 auto_reset.reset(new AutoReset<bool>(&ignore_changes_, true));
721 733
722 extension_service_->extension_prefs()->SetPageIndex(extension_id, 734 extension_service_->extension_prefs()->SetPageIndex(extension_id,
723 static_cast<int>(page_index)); 735 page_index);
724 } 736 }
725 737
726 void AppLauncherHandler::HandlePromoSeen(const ListValue* args) { 738 void AppLauncherHandler::HandlePromoSeen(const ListValue* args) {
727 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, 739 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram,
728 extension_misc::PROMO_SEEN, 740 extension_misc::PROMO_SEEN,
729 extension_misc::PROMO_BUCKET_BOUNDARY); 741 extension_misc::PROMO_BUCKET_BOUNDARY);
730 } 742 }
731 743
732 void AppLauncherHandler::HandleSaveAppPageName(const ListValue* args) { 744 void AppLauncherHandler::HandleSaveAppPageName(const ListValue* args) {
733 string16 name; 745 string16 name;
734 CHECK(args->GetString(0, &name)); 746 CHECK(args->GetString(0, &name));
735 747
736 double page_index; 748 double page_index;
737 CHECK(args->GetDouble(1, &page_index)); 749 CHECK(args->GetDouble(1, &page_index));
750 std::string page_index_str = base::IntToString(static_cast<int>(page_index));
738 751
739 AutoReset<bool> auto_reset(&ignore_changes_, true); 752 AutoReset<bool> auto_reset(&ignore_changes_, true);
740 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs(); 753 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs();
741 ListPrefUpdate update(prefs, prefs::kNTPAppPageNames); 754 DictionaryPrefUpdate update(prefs, prefs::kNTPAppPageNames);
742 ListValue* list = update.Get(); 755 DictionaryValue* dictionary = update.Get();
743 list->Set(static_cast<size_t>(page_index), Value::CreateStringValue(name)); 756 dictionary->Set(page_index_str, Value::CreateStringValue(name));
744 } 757 }
745 758
746 void AppLauncherHandler::HandleGenerateAppForLink(const ListValue* args) { 759 void AppLauncherHandler::HandleGenerateAppForLink(const ListValue* args) {
747 std::string url; 760 std::string url;
748 CHECK(args->GetString(0, &url)); 761 CHECK(args->GetString(0, &url));
749 GURL launch_url(url); 762 GURL launch_url(url);
750 763
751 string16 title; 764 string16 title;
752 CHECK(args->GetString(1, &title)); 765 CHECK(args->GetString(1, &title));
753 766
754 double page_index; 767 double page_index;
755 CHECK(args->GetDouble(2, &page_index)); 768 CHECK(args->GetDouble(2, &page_index));
769 StringOrdinal page_index_str =
770 extension_service_->extension_prefs()->PageIndexAsStringOrdinal(
771 static_cast<int>(page_index));
756 772
757 Profile* profile = Profile::FromWebUI(web_ui_); 773 Profile* profile = Profile::FromWebUI(web_ui_);
758 FaviconService* favicon_service = 774 FaviconService* favicon_service =
759 profile->GetFaviconService(Profile::EXPLICIT_ACCESS); 775 profile->GetFaviconService(Profile::EXPLICIT_ACCESS);
760 if (!favicon_service) { 776 if (!favicon_service) {
761 LOG(ERROR) << "No favicon service"; 777 LOG(ERROR) << "No favicon service";
762 return; 778 return;
763 } 779 }
764 780
765 scoped_ptr<AppInstallInfo> install_info(new AppInstallInfo()); 781 scoped_ptr<AppInstallInfo> install_info(new AppInstallInfo());
766 install_info->is_bookmark_app = true; 782 install_info->is_bookmark_app = true;
767 install_info->title = title; 783 install_info->title = title;
768 install_info->app_url = launch_url; 784 install_info->app_url = launch_url;
769 install_info->page_index = static_cast<int>(page_index); 785 install_info->page_index = page_index_str;
770 786
771 FaviconService::Handle h = favicon_service->GetFaviconForURL( 787 FaviconService::Handle h = favicon_service->GetFaviconForURL(
772 launch_url, history::FAVICON, &favicon_consumer_, 788 launch_url, history::FAVICON, &favicon_consumer_,
773 base::Bind(&AppLauncherHandler::OnFaviconForApp, base::Unretained(this))); 789 base::Bind(&AppLauncherHandler::OnFaviconForApp, base::Unretained(this)));
774 favicon_consumer_.SetClientData(favicon_service, h, install_info.release()); 790 favicon_consumer_.SetClientData(favicon_service, h, install_info.release());
775 } 791 }
776 792
777 void AppLauncherHandler::HandleRecordAppLaunchByURL( 793 void AppLauncherHandler::HandleRecordAppLaunchByURL(
778 const base::ListValue* args) { 794 const base::ListValue* args) {
779 std::string url; 795 std::string url;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 return; 852 return;
837 853
838 StringValue app_id(highlight_app_id_); 854 StringValue app_id(highlight_app_id_);
839 web_ui_->CallJavascriptFunction("ntp4.setAppToBeHighlighted", app_id); 855 web_ui_->CallJavascriptFunction("ntp4.setAppToBeHighlighted", app_id);
840 highlight_app_id_.clear(); 856 highlight_app_id_.clear();
841 } 857 }
842 858
843 // static 859 // static
844 void AppLauncherHandler::RegisterUserPrefs(PrefService* pref_service) { 860 void AppLauncherHandler::RegisterUserPrefs(PrefService* pref_service) {
845 // TODO(csilv): We will want this to be a syncable preference instead. 861 // TODO(csilv): We will want this to be a syncable preference instead.
846 pref_service->RegisterListPref(prefs::kNTPAppPageNames, 862 pref_service->RegisterDictionaryPref(prefs::kNTPAppPageNames,
847 PrefService::UNSYNCABLE_PREF); 863 PrefService::UNSYNCABLE_PREF);
848 } 864 }
849 865
850 // statiic 866 // static
851 void AppLauncherHandler::RecordWebStoreLaunch(bool promo_active) { 867 void AppLauncherHandler::RecordWebStoreLaunch(bool promo_active) {
852 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppLaunchHistogram, 868 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppLaunchHistogram,
853 extension_misc::APP_LAUNCH_NTP_WEBSTORE, 869 extension_misc::APP_LAUNCH_NTP_WEBSTORE,
854 extension_misc::APP_LAUNCH_BUCKET_BOUNDARY); 870 extension_misc::APP_LAUNCH_BUCKET_BOUNDARY);
855 871
856 if (!promo_active) return; 872 if (!promo_active) return;
857 873
858 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, 874 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram,
859 extension_misc::PROMO_LAUNCH_WEB_STORE, 875 extension_misc::PROMO_LAUNCH_WEB_STORE,
860 extension_misc::PROMO_BUCKET_BOUNDARY); 876 extension_misc::PROMO_BUCKET_BOUNDARY);
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
995 1011
996 void AppLauncherHandler::UninstallDefaultApps() { 1012 void AppLauncherHandler::UninstallDefaultApps() {
997 AppsPromo* apps_promo = extension_service_->apps_promo(); 1013 AppsPromo* apps_promo = extension_service_->apps_promo();
998 const ExtensionIdSet& app_ids = apps_promo->old_default_apps(); 1014 const ExtensionIdSet& app_ids = apps_promo->old_default_apps();
999 for (ExtensionIdSet::const_iterator iter = app_ids.begin(); 1015 for (ExtensionIdSet::const_iterator iter = app_ids.begin();
1000 iter != app_ids.end(); ++iter) { 1016 iter != app_ids.end(); ++iter) {
1001 if (extension_service_->GetExtensionById(*iter, true)) 1017 if (extension_service_->GetExtensionById(*iter, true))
1002 extension_service_->UninstallExtension(*iter, false, NULL); 1018 extension_service_->UninstallExtension(*iter, false, NULL);
1003 } 1019 }
1004 } 1020 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/ntp/app_launcher_handler.h ('k') | chrome/common/string_ordinal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698