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..b5ad2db147006bf09e62aeb1fcb22fa21df8c034 |
--- /dev/null |
+++ b/chrome/browser/ui/views/aura/app_list/app_list_model.cc |
@@ -0,0 +1,123 @@ |
+// 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/app/chrome_command_ids.h" |
+#include "chrome/browser/extensions/extension_service.h" |
+#include "chrome/browser/prefs/pref_service.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_list.h" |
+#include "chrome/browser/ui/views/aura/app_list/browser_command_item.h" |
+#include "chrome/browser/ui/views/aura/app_list/extension_item.h" |
+#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
+#include "chrome/common/pref_names.h" |
+#include "grit/generated_resources.h" |
+#include "grit/theme_resources.h" |
+#include "ui/aura_shell/app_list/app_list_item_group_model.h" |
+#include "ui/base/l10n/l10n_util.h" |
+ |
+namespace { |
+ |
+// Gets or creates group for given |extension| in |model|. The created group |
+// is added to |model| and owned by it. |
+aura_shell::AppListItemGroupModel* GetOrCreateGroup( |
+ int page_index, |
+ const ListValue* app_page_names, |
+ AppListModel* model) { |
+ 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 ordinal takes |
+// precedence. |
+bool ExtensionAppPrecedes(const ExtensionItem* a, const ExtensionItem* b) { |
+ return a->launch_ordinal().LessThan(b->launch_ordinal()); |
+} |
+ |
+} // namespace |
+ |
+AppListModel::AppListModel(Profile* profile) |
+ : profile_(profile) { |
+} |
+ |
+AppListModel::~AppListModel() { |
+} |
+ |
+void AppListModel::Init() { |
+ GetExtensionApps(); |
+ GetBrowserCommands(); |
+} |
+ |
+void AppListModel::GetExtensionApps() { |
+ DCHECK(profile_); |
+ ExtensionService* service = profile_->GetExtensionService(); |
+ if (!service) |
+ return; |
+ |
+ // Get extension apps. |
+ std::vector<ExtensionItem*> 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 ExtensionItem(profile_, *app)); |
+ } |
+ |
+ // Sort by launch ordinal. |
+ std::sort(items.begin(), items.end(), &ExtensionAppPrecedes); |
+ |
+ // 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) { |
+ ExtensionItem* item = items[i]; |
+ |
+ aura_shell::AppListItemGroupModel* group = GetOrCreateGroup( |
+ item->page_index(), |
+ app_page_names, |
+ this); |
+ |
+ group->AddItem(item); |
+ } |
+} |
+ |
+void AppListModel::GetBrowserCommands() { |
+ Browser* browser = BrowserList::GetLastActiveWithProfile(profile_); |
+ if (!browser) |
+ return; |
+ |
+ // Uses the first group to put browser commands |
+ aura_shell::AppListItemGroupModel* group = NULL; |
+ if (item_count() == 0) { |
+ group = new aura_shell::AppListItemGroupModel(""); |
+ Add(group); |
+ } else { |
+ group = item_at(0); |
+ } |
+ |
+ group->AddItem(new BrowserCommandItem(browser, |
+ IDC_NEW_INCOGNITO_WINDOW, |
+ IDS_APP_LIST_INCOGNITO, |
+ IDR_APP_LIST_INCOGNITO)); |
+ group->AddItem(new BrowserCommandItem(browser, |
+ IDC_OPTIONS, |
+ IDS_APP_LIST_SETTINGS, |
+ IDR_APP_LIST_SETTINGS)); |
+} |
+ |