OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef ASH_COMMON_SHELF_SHELF_MODEL_H_ | 5 #ifndef ASH_COMMON_SHELF_SHELF_MODEL_H_ |
6 #define ASH_COMMON_SHELF_SHELF_MODEL_H_ | 6 #define ASH_COMMON_SHELF_SHELF_MODEL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "ash/ash_export.h" | 11 #include "ash/ash_export.h" |
12 #include "ash/common/shelf/shelf_item_types.h" | 12 #include "ash/common/shelf/shelf_item_types.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 | 15 |
16 namespace ash { | 16 namespace ash { |
17 | 17 |
18 class ShelfItemDelegate; | 18 class ShelfItemDelegate; |
19 class ShelfModelObserver; | 19 class ShelfModelObserver; |
20 | 20 |
21 // Model used for shelf items. Owns ShelfItemDelegates, but does not create | 21 // Model used for shelf items. Owns ShelfItemDelegates but does not create them. |
22 // them. | |
23 class ASH_EXPORT ShelfModel { | 22 class ASH_EXPORT ShelfModel { |
24 public: | 23 public: |
25 enum Status { | |
26 STATUS_NORMAL, | |
27 // A status that indicates apps are syncing/loading. | |
28 STATUS_LOADING, | |
29 }; | |
30 | |
31 ShelfModel(); | 24 ShelfModel(); |
32 ~ShelfModel(); | 25 ~ShelfModel(); |
33 | 26 |
34 // Cleans up the ShelfItemDelegates. | 27 // Cleans up the ShelfItemDelegates. |
35 void DestroyItemDelegates(); | 28 void DestroyItemDelegates(); |
36 | 29 |
37 // Adds a new item to the model. Returns the resulting index. | 30 // Adds a new item to the model. Returns the resulting index. |
38 int Add(const ShelfItem& item); | 31 int Add(const ShelfItem& item); |
39 | 32 |
40 // Adds the item. |index| is the requested insertion index, which may be | 33 // Adds the item. |index| is the requested insertion index, which may be |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 // Returns a reserved id which will not be used by the |ShelfModel|. | 68 // Returns a reserved id which will not be used by the |ShelfModel|. |
76 ShelfID reserve_external_id() { return next_id_++; } | 69 ShelfID reserve_external_id() { return next_id_++; } |
77 | 70 |
78 // Returns an iterator into items() for the item with the specified id, or | 71 // Returns an iterator into items() for the item with the specified id, or |
79 // items().end() if there is no item with the specified id. | 72 // items().end() if there is no item with the specified id. |
80 ShelfItems::const_iterator ItemByID(ShelfID id) const; | 73 ShelfItems::const_iterator ItemByID(ShelfID id) const; |
81 | 74 |
82 const ShelfItems& items() const { return items_; } | 75 const ShelfItems& items() const { return items_; } |
83 int item_count() const { return static_cast<int>(items_.size()); } | 76 int item_count() const { return static_cast<int>(items_.size()); } |
84 | 77 |
85 void set_status(Status status) { status_ = status; } | |
86 Status status() const { return status_; } | |
87 | |
88 // Set |item_delegate| for |id| and takes ownership. | 78 // Set |item_delegate| for |id| and takes ownership. |
89 void SetShelfItemDelegate(ShelfID id, | 79 void SetShelfItemDelegate(ShelfID id, |
90 std::unique_ptr<ShelfItemDelegate> item_delegate); | 80 std::unique_ptr<ShelfItemDelegate> item_delegate); |
91 | 81 |
92 // Returns ShelfItemDelegate for |id|, or null if none exists. | 82 // Returns ShelfItemDelegate for |id|, or null if none exists. |
93 ShelfItemDelegate* GetShelfItemDelegate(ShelfID id); | 83 ShelfItemDelegate* GetShelfItemDelegate(ShelfID id); |
94 | 84 |
95 void AddObserver(ShelfModelObserver* observer); | 85 void AddObserver(ShelfModelObserver* observer); |
96 void RemoveObserver(ShelfModelObserver* observer); | 86 void RemoveObserver(ShelfModelObserver* observer); |
97 | 87 |
98 private: | 88 private: |
99 // Makes sure |index| is in line with the type-based order of items. If that | 89 // Makes sure |index| is in line with the type-based order of items. If that |
100 // is not the case, adjusts index by shifting it to the valid range and | 90 // is not the case, adjusts index by shifting it to the valid range and |
101 // returns the new value. | 91 // returns the new value. |
102 int ValidateInsertionIndex(ShelfItemType type, int index) const; | 92 int ValidateInsertionIndex(ShelfItemType type, int index) const; |
103 | 93 |
104 // Remove and destroy ShelfItemDelegate for |id|. | 94 // Remove and destroy ShelfItemDelegate for |id|. |
105 void RemoveShelfItemDelegate(ShelfID id); | 95 void RemoveShelfItemDelegate(ShelfID id); |
106 | 96 |
107 // ID assigned to the next item. | 97 // ID assigned to the next item. |
108 ShelfID next_id_; | 98 ShelfID next_id_; |
109 | 99 |
110 ShelfItems items_; | 100 ShelfItems items_; |
111 Status status_; | |
112 base::ObserverList<ShelfModelObserver> observers_; | 101 base::ObserverList<ShelfModelObserver> observers_; |
113 | 102 |
114 std::map<ShelfID, std::unique_ptr<ShelfItemDelegate>> | 103 std::map<ShelfID, std::unique_ptr<ShelfItemDelegate>> |
115 id_to_item_delegate_map_; | 104 id_to_item_delegate_map_; |
116 | 105 |
117 DISALLOW_COPY_AND_ASSIGN(ShelfModel); | 106 DISALLOW_COPY_AND_ASSIGN(ShelfModel); |
118 }; | 107 }; |
119 | 108 |
120 } // namespace ash | 109 } // namespace ash |
121 | 110 |
122 #endif // ASH_COMMON_SHELF_SHELF_MODEL_H_ | 111 #endif // ASH_COMMON_SHELF_SHELF_MODEL_H_ |
OLD | NEW |