Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / app_list_item_list.h
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef UI_APP_LIST_APP_LIST_ITEM_LIST_H_
6 #define UI_APP_LIST_APP_LIST_ITEM_LIST_H_
7
8 #include <string>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/observer_list.h"
13 #include "sync/api/string_ordinal.h"
14 #include "ui/app_list/app_list_export.h"
15 #include "ui/app_list/app_list_item_list_observer.h"
16
17 namespace app_list {
18
19 class AppListItem;
20
21 // Class to manage items in the app list. Used both by AppListModel and
22 // AppListFolderItem. Manages the position ordinal of items in the list, and
23 // notifies observers when items in the list are added / deleted / moved.
24 class APP_LIST_EXPORT AppListItemList {
25  public:
26   AppListItemList();
27   virtual ~AppListItemList();
28
29   void AddObserver(AppListItemListObserver* observer);
30   void RemoveObserver(AppListItemListObserver* observer);
31
32   // Finds item matching |id|. NOTE: Requires a linear search.
33   // Generally this should not be used directly, AppListModel::FindItem
34   // should be used instead.
35   AppListItem* FindItem(const std::string& id);
36
37   // Finds the |index| of the the item matching |id| in |app_list_items_|.
38   // Returns true if the matching item is found.
39   // Note: Requires a linear search.
40   bool FindItemIndex(const std::string& id, size_t* index);
41
42   // Moves item at |from_index| to |to_index|.
43   // Triggers observers_.OnListItemMoved().
44   void MoveItem(size_t from_index, size_t to_index);
45
46   // Sets the position of |item| which is expected to be a member of
47   // |app_list_items_| and sorts the list accordingly. If |new_position| is
48   // invalid, move the item to the end of the list.
49   void SetItemPosition(AppListItem* item, syncer::StringOrdinal new_position);
50
51   AppListItem* item_at(size_t index) {
52     DCHECK_LT(index, app_list_items_.size());
53     return app_list_items_[index];
54   }
55   const AppListItem* item_at(size_t index) const {
56     DCHECK_LT(index, app_list_items_.size());
57     return app_list_items_[index];
58   }
59   size_t item_count() const { return app_list_items_.size(); }
60
61  private:
62   friend class AppListItemListTest;
63   friend class AppListModel;
64
65   // Returns a unique, valid StringOrdinal immediately before |position| or at
66   // the end of the list if |position| is invalid.
67   syncer::StringOrdinal CreatePositionBefore(
68       const syncer::StringOrdinal& position);
69
70   // Adds |item| to the end of |app_list_items_|. Takes ownership of |item|.
71   // Triggers observers_.OnListItemAdded(). Returns a pointer to the added item
72   // that is safe to use (e.g. after releasing a scoped ptr).
73   AppListItem* AddItem(scoped_ptr<AppListItem> item_ptr);
74
75   // Finds item matching |id| in |app_list_items_| (linear search) and deletes
76   // it. Triggers observers_.OnListItemRemoved() after removing the item from
77   // the list and before deleting it.
78   void DeleteItem(const std::string& id);
79
80   // Removes the item with matching |id| in |app_list_items_| without deleting
81   // it. Returns a scoped pointer containing the removed item.
82   scoped_ptr<AppListItem> RemoveItem(const std::string& id);
83
84   // Removes the item at |index| from |app_list_items_| without deleting it.
85   // Returns a scoped pointer containing the removed item.
86   scoped_ptr<AppListItem> RemoveItemAt(size_t index);
87
88   // Deletes item at |index| and signals observers.
89   void DeleteItemAt(size_t index);
90
91   // If |item|->position() is not a valid ordinal, sets |item|->position()
92   // to a valid ordinal after the last item in the list.
93   void EnsureValidItemPosition(AppListItem* item);
94
95   // Returns the index at which to insert an item in |app_list_items_| based on
96   // |position| (which must be valid) and |id| (if the positions are equal).
97   size_t GetItemSortOrderIndex(const syncer::StringOrdinal& position,
98                                const std::string& id);
99
100   // Fixes the position of the item at |index| when the position matches the
101   // previous item's position. |index| must be > 0.
102   void FixItemPosition(size_t index);
103
104   ScopedVector<AppListItem> app_list_items_;
105   ObserverList<AppListItemListObserver, true> observers_;
106
107   DISALLOW_COPY_AND_ASSIGN(AppListItemList);
108 };
109
110 }  // namespace app_list
111
112 #endif  // UI_APP_LIST_APP_LIST_ITEM_LIST_H_