Index: chrome/browser/ui/views/aura/app_list/app_list_model.cc |
diff --git a/chrome/browser/ui/views/aura/app_list/app_list_model.cc b/chrome/browser/ui/views/aura/app_list/app_list_model.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..08b7f908c18e6cfd6ed5e400360479f32927f7af |
--- /dev/null |
+++ b/chrome/browser/ui/views/aura/app_list/app_list_model.cc |
@@ -0,0 +1,133 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/views/aura/app_list/app_list_model.h" |
+ |
+#include "chrome/browser/extensions/extension_prefs.h" |
+#include "chrome/browser/extensions/extension_service.h" |
+#include "chrome/browser/prefs/pref_service.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/browser/ui/views/aura/app_list/extension_item_model.h" |
+#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
+#include "chrome/common/extensions/extension.h" |
+#include "chrome/common/pref_names.h" |
+#include "grit/generated_resources.h" |
+#include "ui/aura_shell/app_list/app_list_item_group_model.h" |
+#include "ui/base/l10n/l10n_util.h" |
+ |
+namespace { |
+ |
+// Gets page ordinal of given |extension| in its app launch page. |
+int GetExtensionPageOrdinal(const Extension* extension, |
+ ExtensionPrefs* prefs) { |
+ int page_ordinal = prefs->GetPageIndex(extension->id()); |
+ if (page_ordinal < 0) { |
+ page_ordinal = extension->id() == extension_misc::kWebStoreAppId ? |
+ 0 : prefs->GetNaturalAppPageIndex(); |
+ } |
+ |
+ return page_ordinal; |
+} |
+ |
+// Gets app launch ordinal of given extension. |
+int GetExtensionLaunchOrdinal(const Extension* extension, |
+ ExtensionPrefs* prefs) { |
+ int page_ordinal = GetExtensionPageOrdinal(extension, prefs); |
+ int app_launch_ordinal = prefs->GetAppLaunchIndex(extension->id()); |
+ if (app_launch_ordinal == -1) { |
+ // The webstore's app launch index is set to -2 to make sure it's first. |
+ app_launch_ordinal = extension->id() == extension_misc::kWebStoreAppId ? |
+ -2 : prefs->GetNextAppLaunchIndex(page_ordinal); |
+ } |
+ return app_launch_ordinal; |
+} |
+ |
+// Gets or creates group for given |extension| in |model|. The created gruop |
sky
2011/12/14 16:51:33
gruop -> group
xiyuan
2011/12/20 00:14:42
Done.
|
+// is added to |model| and owned by it. |
+aura_shell::AppListItemGroupModel* GetOrCreateGroup( |
+ const Extension* extension, |
+ ExtensionPrefs* prefs, |
+ const ListValue* app_page_names, |
+ AppListModel* model) { |
+ int page_index = GetExtensionPageOrdinal(extension, prefs); |
+ if (page_index >= model->item_count()) { |
+ for (int i = model->item_count(); i <= page_index; ++i) { |
+ std::string title; |
+ if (!app_page_names->GetString(i, &title)) |
+ title = l10n_util::GetStringUTF8(IDS_APP_DEFAULT_PAGE_NAME); |
+ |
+ aura_shell::AppListItemGroupModel* group = |
+ new aura_shell::AppListItemGroupModel(title); |
+ model->Add(group); |
+ } |
+ } |
+ |
+ return model->item_at(page_index); |
+} |
+ |
+// Binary predict to sort extension apps. Smaller launch oridnal takes |
+// precedence. |
+struct ExtensionAppPrecedes { |
+ explicit ExtensionAppPrecedes(ExtensionPrefs* prefs) |
+ : prefs(prefs) { |
+ } |
+ |
+ bool operator()(const ExtensionItemModel* a, |
+ const ExtensionItemModel* b) const { |
+ return GetExtensionLaunchOrdinal(a->extension(), prefs) < |
sky
2011/12/14 16:51:33
Is this an expensive operation? Would it be better
xiyuan
2011/12/20 00:14:42
Done. Launch ordinal is not stored in ExtensionIte
|
+ GetExtensionLaunchOrdinal(b->extension(), prefs); |
+ } |
+ |
+ ExtensionPrefs* prefs; |
+}; |
+ |
+} // namespace |
+ |
+AppListModel::AppListModel() { |
+ UpdateExtensionApps(); |
+} |
+ |
+AppListModel::~AppListModel() { |
+} |
+ |
+void AppListModel::UpdateExtensionApps() { |
sky
2011/12/14 16:51:33
This is pretty heavy for a constructor. How about
xiyuan
2011/12/20 00:14:42
Done.
|
+ Profile* profile = ProfileManager::GetDefaultProfile(); |
sky
2011/12/14 16:51:33
Seems like this should be passed in the constructo
xiyuan
2011/12/20 00:14:42
Done.
|
+ if (!profile) |
+ return; |
+ |
+ ExtensionService* service = profile->GetExtensionService(); |
+ if (!service) |
+ return; |
+ |
+ // Get extension apps. |
+ std::vector<ExtensionItemModel*> items; |
+ const ExtensionSet* extensions = service->extensions(); |
+ for (ExtensionSet::const_iterator app = extensions->begin(); |
+ app != extensions->end(); ++app) { |
+ if (AppLauncherHandler::IsAppExcludedFromList(*app)) |
+ continue; |
+ |
+ items.push_back(new ExtensionItemModel(service, *app)); |
+ } |
+ |
+ // Sort by launch ordinal. |
+ std::sort(items.begin(), items.end(), |
+ ExtensionAppPrecedes(service->extension_prefs())); |
+ |
+ // Put all items into model and group them by page ordinal. |
+ PrefService* prefs = profile->GetPrefs(); |
+ const ListValue* app_page_names = prefs->GetList(prefs::kNTPAppPageNames); |
+ for (size_t i = 0; i < items.size(); ++i) { |
+ ExtensionItemModel* item = items[i]; |
+ |
+ aura_shell::AppListItemGroupModel* group = GetOrCreateGroup( |
+ item->extension(), |
+ service->extension_prefs(), |
+ app_page_names, |
+ this); |
+ |
+ group->Add(item); |
+ } |
+} |