Refactored not to expose raw pointers on ProxyList class.
[chromium-blink-merge.git] / ash / shelf / shelf_item_delegate_manager.cc
blob256ee678dae595fabc35db8b86a7df27fb51b7e7
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.
5 #include "ash/shelf/shelf_item_delegate_manager.h"
7 #include "ash/shelf/shelf_item_delegate.h"
8 #include "ash/shelf/shelf_model.h"
9 #include "ash/shell.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
13 namespace ash {
15 ShelfItemDelegateManager::ShelfItemDelegateManager(ShelfModel* model)
16 : model_(model) {
17 DCHECK(model_);
18 model_->AddObserver(this);
21 ShelfItemDelegateManager::~ShelfItemDelegateManager() {
22 model_->RemoveObserver(this);
23 STLDeleteContainerPairSecondPointers(id_to_item_delegate_map_.begin(),
24 id_to_item_delegate_map_.end());
27 void ShelfItemDelegateManager::AddObserver(
28 ShelfItemDelegateManagerObserver* observer) {
29 observers_.AddObserver(observer);
32 void ShelfItemDelegateManager::RemoveObserver(
33 ShelfItemDelegateManagerObserver* observer) {
34 observers_.RemoveObserver(observer);
37 void ShelfItemDelegateManager::SetShelfItemDelegate(
38 ShelfID id,
39 scoped_ptr<ShelfItemDelegate> item_delegate) {
40 // If another ShelfItemDelegate is already registered for |id|, we assume
41 // that this request is replacing ShelfItemDelegate for |id| with
42 // |item_delegate|.
43 RemoveShelfItemDelegate(id);
45 FOR_EACH_OBSERVER(ShelfItemDelegateManagerObserver,
46 observers_,
47 OnSetShelfItemDelegate(id, item_delegate.get()));
49 id_to_item_delegate_map_[id] = item_delegate.release();
52 ShelfItemDelegate* ShelfItemDelegateManager::GetShelfItemDelegate(ShelfID id) {
53 if (model_->ItemIndexByID(id) != -1) {
54 // Each ShelfItem has to have a ShelfItemDelegate.
55 DCHECK(id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end());
56 return id_to_item_delegate_map_[id];
58 return NULL;
61 void ShelfItemDelegateManager::ShelfItemAdded(int index) {
64 void ShelfItemDelegateManager::ShelfItemRemoved(int index, ShelfID id) {
65 RemoveShelfItemDelegate(id);
66 FOR_EACH_OBSERVER(ShelfItemDelegateManagerObserver,
67 observers_,
68 OnSetShelfItemDelegate(id, nullptr));
71 void ShelfItemDelegateManager::ShelfItemMoved(int start_index,
72 int target_index) {
75 void ShelfItemDelegateManager::ShelfItemChanged(int index,
76 const ShelfItem& old_item) {
79 void ShelfItemDelegateManager::ShelfStatusChanged() {
82 void ShelfItemDelegateManager::RemoveShelfItemDelegate(ShelfID id) {
83 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) {
84 delete id_to_item_delegate_map_[id];
85 id_to_item_delegate_map_.erase(id);
89 } // namespace ash