Chromium Code Reviews| 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 #include "ash/shelf/shelf_model.h" | 5 #include "ash/shelf/shelf_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/public/cpp/app_launch_id.h" | |
| 9 #include "ash/public/cpp/shelf_item_delegate.h" | 10 #include "ash/public/cpp/shelf_item_delegate.h" |
| 10 #include "ash/shelf/shelf_model_observer.h" | 11 #include "ash/shelf/shelf_model_observer.h" |
| 11 | 12 |
| 12 namespace ash { | 13 namespace ash { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 int ShelfItemTypeToWeight(ShelfItemType type) { | 17 int ShelfItemTypeToWeight(ShelfItemType type) { |
| 17 switch (type) { | 18 switch (type) { |
| 18 case TYPE_APP_LIST: | 19 case TYPE_APP_LIST: |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 34 } | 35 } |
| 35 | 36 |
| 36 NOTREACHED() << "Invalid type " << type; | 37 NOTREACHED() << "Invalid type " << type; |
| 37 return 1; | 38 return 1; |
| 38 } | 39 } |
| 39 | 40 |
| 40 bool CompareByWeight(const ShelfItem& a, const ShelfItem& b) { | 41 bool CompareByWeight(const ShelfItem& a, const ShelfItem& b) { |
| 41 return ShelfItemTypeToWeight(a.type) < ShelfItemTypeToWeight(b.type); | 42 return ShelfItemTypeToWeight(a.type) < ShelfItemTypeToWeight(b.type); |
| 42 } | 43 } |
| 43 | 44 |
| 45 // Returns shelf app id. Play Store app is mapped to ARC platform host app. | |
| 46 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | |
| 47 std::string GetShelfAppIdFromArcAppId(const std::string& arc_app_id) { | |
| 48 static const char kPlayStoreAppId[] = "gpkmicpkkebkmabiaedjognfppcchdfa"; | |
| 49 static const char kArcHostAppId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; | |
| 50 return arc_app_id == kPlayStoreAppId ? kArcHostAppId : arc_app_id; | |
| 51 } | |
| 52 | |
| 44 } // namespace | 53 } // namespace |
| 45 | 54 |
| 46 ShelfModel::ShelfModel() : next_id_(1) {} | 55 ShelfModel::ShelfModel() : next_id_(1) {} |
| 47 | 56 |
| 48 ShelfModel::~ShelfModel() {} | 57 ShelfModel::~ShelfModel() {} |
| 49 | 58 |
| 59 ShelfID ShelfModel::GetShelfIDForAppID(const std::string& app_id) { | |
| 60 // Get shelf id for |app_id| and an empty |launch_id|. | |
| 61 return GetShelfIDForAppIDAndLaunchID(app_id, std::string()); | |
| 62 } | |
| 63 | |
| 64 ShelfID ShelfModel::GetShelfIDForAppIDAndLaunchID( | |
| 65 const std::string& app_id, | |
| 66 const std::string& launch_id) { | |
| 67 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | |
| 68 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id); | |
| 69 | |
| 70 if (shelf_app_id.empty()) | |
| 71 return ash::kInvalidShelfID; | |
| 72 | |
| 73 for (const ShelfItem& item : items_) { | |
| 74 // ShelfWindowWatcher handles app panel windows separately. | |
| 75 if (item.type != TYPE_APP_PANEL && | |
| 76 item.app_launch_id.app_id() == shelf_app_id && | |
| 77 item.app_launch_id.launch_id() == launch_id) { | |
| 78 return item.id; | |
| 79 } | |
| 80 } | |
| 81 return kInvalidShelfID; | |
| 82 } | |
| 83 | |
| 84 const std::string& ShelfModel::GetAppIDForShelfID(ShelfID id) { | |
| 85 ShelfItems::const_iterator item = ItemByID(id); | |
| 86 return item != items().end() ? item->app_launch_id.app_id() | |
| 87 : base::EmptyString(); | |
| 88 } | |
| 89 | |
| 90 void ShelfModel::PinAppWithID(const std::string& app_id) { | |
| 91 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | |
| 92 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id); | |
| 93 | |
| 94 // If the app is already pinned, do nothing and return. | |
| 95 if (IsAppPinned(shelf_app_id)) | |
| 96 return; | |
| 97 | |
| 98 // Convert an existing item to be pinned, or create a new pinned item. | |
| 99 const int index = ItemIndexByID(GetShelfIDForAppID(shelf_app_id)); | |
| 100 if (index >= 0) { | |
| 101 ShelfItem item = items_[index]; | |
| 102 DCHECK_EQ(item.type, TYPE_APP); | |
| 103 DCHECK(!item.pinned_by_policy); | |
| 104 item.type = TYPE_PINNED_APP; | |
| 105 Set(index, item); | |
| 106 } else if (!shelf_app_id.empty()) { | |
| 107 ash::ShelfItem item; | |
| 108 item.type = ash::TYPE_PINNED_APP; | |
| 109 item.app_launch_id = AppLaunchId(shelf_app_id); | |
| 110 Add(item); | |
| 111 } | |
| 112 } | |
|
James Cook
2017/04/19 00:23:47
It would be nice if the tests for these functions
msw
2017/04/19 20:56:06
Done, I added new tests to exercise the new ShelfM
| |
| 113 | |
| 114 bool ShelfModel::IsAppPinned(const std::string& app_id) { | |
| 115 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | |
| 116 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id); | |
| 117 | |
| 118 const int index = ItemIndexByID(GetShelfIDForAppID(shelf_app_id)); | |
| 119 return index >= 0 && (items_[index].type == TYPE_PINNED_APP || | |
| 120 items_[index].type == TYPE_BROWSER_SHORTCUT); | |
| 121 } | |
| 122 | |
| 123 void ShelfModel::UnpinAppWithID(const std::string& app_id) { | |
| 124 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | |
| 125 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id); | |
| 126 | |
| 127 // If the app is already not pinned, do nothing and return. | |
| 128 if (!IsAppPinned(shelf_app_id)) | |
| 129 return; | |
| 130 | |
| 131 // Remove the item if it is closed, or mark it as unpinned. | |
| 132 const int index = ItemIndexByID(GetShelfIDForAppID(shelf_app_id)); | |
| 133 ShelfItem item = items_[index]; | |
| 134 DCHECK_EQ(item.type, TYPE_PINNED_APP); | |
| 135 DCHECK(!item.pinned_by_policy); | |
| 136 if (item.status == ash::STATUS_CLOSED) { | |
| 137 RemoveItemAt(index); | |
| 138 } else { | |
| 139 item.type = TYPE_APP; | |
| 140 Set(index, item); | |
| 141 } | |
| 142 } | |
| 143 | |
| 50 void ShelfModel::DestroyItemDelegates() { | 144 void ShelfModel::DestroyItemDelegates() { |
| 51 // Some ShelfItemDelegates access this model in their destructors and hence | 145 // Some ShelfItemDelegates access this model in their destructors and hence |
| 52 // need early cleanup. | 146 // need early cleanup. |
| 53 id_to_item_delegate_map_.clear(); | 147 id_to_item_delegate_map_.clear(); |
| 54 } | 148 } |
| 55 | 149 |
| 56 int ShelfModel::Add(const ShelfItem& item) { | 150 int ShelfModel::Add(const ShelfItem& item) { |
| 57 return AddAt(items_.size(), item); | 151 return AddAt(items_.size(), item); |
| 58 } | 152 } |
| 59 | 153 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 static_cast<ShelfItems::difference_type>(index)); | 283 static_cast<ShelfItems::difference_type>(index)); |
| 190 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, | 284 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, |
| 191 CompareByWeight) - | 285 CompareByWeight) - |
| 192 items_.begin(), | 286 items_.begin(), |
| 193 static_cast<ShelfItems::difference_type>(index)); | 287 static_cast<ShelfItems::difference_type>(index)); |
| 194 | 288 |
| 195 return index; | 289 return index; |
| 196 } | 290 } |
| 197 | 291 |
| 198 } // namespace ash | 292 } // namespace ash |
| OLD | NEW |