OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/common/shelf/shelf_item_delegate_manager.h" | |
6 | |
7 #include "ash/common/shelf/shelf_item_delegate.h" | |
8 #include "ash/common/shelf/shelf_model.h" | |
9 #include "base/logging.h" | |
10 #include "base/stl_util.h" | |
11 | |
12 namespace ash { | |
13 | |
14 ShelfItemDelegateManager::ShelfItemDelegateManager(ShelfModel* model) | |
15 : model_(model) { | |
16 DCHECK(model_); | |
17 model_->AddObserver(this); | |
18 } | |
19 | |
20 ShelfItemDelegateManager::~ShelfItemDelegateManager() { | |
21 model_->RemoveObserver(this); | |
22 STLDeleteContainerPairSecondPointers(id_to_item_delegate_map_.begin(), | |
23 id_to_item_delegate_map_.end()); | |
24 } | |
25 | |
26 void ShelfItemDelegateManager::AddObserver( | |
27 ShelfItemDelegateManagerObserver* observer) { | |
28 observers_.AddObserver(observer); | |
29 } | |
30 | |
31 void ShelfItemDelegateManager::RemoveObserver( | |
32 ShelfItemDelegateManagerObserver* observer) { | |
33 observers_.RemoveObserver(observer); | |
34 } | |
35 | |
36 void ShelfItemDelegateManager::SetShelfItemDelegate( | |
37 ShelfID id, | |
38 std::unique_ptr<ShelfItemDelegate> item_delegate) { | |
39 // If another ShelfItemDelegate is already registered for |id|, we assume | |
40 // that this request is replacing ShelfItemDelegate for |id| with | |
41 // |item_delegate|. | |
42 RemoveShelfItemDelegate(id); | |
43 | |
44 FOR_EACH_OBSERVER(ShelfItemDelegateManagerObserver, observers_, | |
45 OnSetShelfItemDelegate(id, item_delegate.get())); | |
46 | |
47 id_to_item_delegate_map_[id] = item_delegate.release(); | |
48 } | |
49 | |
50 ShelfItemDelegate* ShelfItemDelegateManager::GetShelfItemDelegate(ShelfID id) { | |
51 if (model_->ItemIndexByID(id) != -1) { | |
52 // Each ShelfItem has to have a ShelfItemDelegate. | |
53 DCHECK(id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()); | |
54 return id_to_item_delegate_map_[id]; | |
55 } | |
56 return NULL; | |
57 } | |
58 | |
59 void ShelfItemDelegateManager::ShelfItemAdded(int index) {} | |
60 | |
61 void ShelfItemDelegateManager::ShelfItemRemoved(int index, ShelfID id) { | |
62 RemoveShelfItemDelegate(id); | |
63 FOR_EACH_OBSERVER(ShelfItemDelegateManagerObserver, observers_, | |
64 OnSetShelfItemDelegate(id, nullptr)); | |
65 } | |
66 | |
67 void ShelfItemDelegateManager::ShelfItemMoved(int start_index, | |
68 int target_index) {} | |
69 | |
70 void ShelfItemDelegateManager::ShelfItemChanged(int index, | |
71 const ShelfItem& old_item) {} | |
72 | |
73 void ShelfItemDelegateManager::RemoveShelfItemDelegate(ShelfID id) { | |
74 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) { | |
75 delete id_to_item_delegate_map_[id]; | |
76 id_to_item_delegate_map_.erase(id); | |
77 } | |
78 } | |
79 | |
80 } // namespace ash | |
OLD | NEW |