| 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/shelf_item_delegate.h" | 9 #include "ash/public/cpp/shelf_item_delegate.h" |
| 10 #include "ash/shelf/shelf_model_observer.h" | 10 #include "ash/shelf/shelf_model_observer.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 static const char kArcHostAppId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; | 48 static const char kArcHostAppId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; |
| 49 return arc_app_id == kPlayStoreAppId ? kArcHostAppId : arc_app_id; | 49 return arc_app_id == kPlayStoreAppId ? kArcHostAppId : arc_app_id; |
| 50 } | 50 } |
| 51 | 51 |
| 52 } // namespace | 52 } // namespace |
| 53 | 53 |
| 54 ShelfModel::ShelfModel() = default; | 54 ShelfModel::ShelfModel() = default; |
| 55 | 55 |
| 56 ShelfModel::~ShelfModel() = default; | 56 ShelfModel::~ShelfModel() = default; |
| 57 | 57 |
| 58 ShelfID ShelfModel::GetShelfIDForAppID(const std::string& app_id) { | |
| 59 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | |
| 60 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id); | |
| 61 | |
| 62 if (shelf_app_id.empty()) | |
| 63 return ShelfID(); | |
| 64 | |
| 65 for (const ShelfItem& item : items_) { | |
| 66 // ShelfWindowWatcher handles app panel windows separately. | |
| 67 if (item.type != TYPE_APP_PANEL && item.id.app_id == shelf_app_id) | |
| 68 return item.id; | |
| 69 } | |
| 70 return ShelfID(); | |
| 71 } | |
| 72 | |
| 73 ShelfID ShelfModel::GetShelfIDForAppIDAndLaunchID( | |
| 74 const std::string& app_id, | |
| 75 const std::string& launch_id) { | |
| 76 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | |
| 77 const ShelfID id = ShelfID(GetShelfAppIdFromArcAppId(app_id), launch_id); | |
| 78 | |
| 79 if (id.IsNull()) | |
| 80 return ShelfID(); | |
| 81 | |
| 82 for (const ShelfItem& item : items_) { | |
| 83 // ShelfWindowWatcher handles app panel windows separately. | |
| 84 if (item.type != TYPE_APP_PANEL && item.id == id) | |
| 85 return item.id; | |
| 86 } | |
| 87 return ShelfID(); | |
| 88 } | |
| 89 | |
| 90 const std::string& ShelfModel::GetAppIDForShelfID(const ShelfID& id) { | |
| 91 ShelfItems::const_iterator item = ItemByID(id); | |
| 92 return item != items().end() ? item->id.app_id : base::EmptyString(); | |
| 93 } | |
| 94 | |
| 95 void ShelfModel::PinAppWithID(const std::string& app_id) { | 58 void ShelfModel::PinAppWithID(const std::string& app_id) { |
| 96 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | 59 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 |
| 97 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id); | 60 const ShelfID shelf_id(GetShelfAppIdFromArcAppId(app_id)); |
| 98 | 61 |
| 99 // If the app is already pinned, do nothing and return. | 62 // If the app is already pinned, do nothing and return. |
| 100 if (IsAppPinned(shelf_app_id)) | 63 if (IsAppPinned(shelf_id.app_id)) |
| 101 return; | 64 return; |
| 102 | 65 |
| 103 // Convert an existing item to be pinned, or create a new pinned item. | 66 // Convert an existing item to be pinned, or create a new pinned item. |
| 104 const int index = ItemIndexByID(GetShelfIDForAppID(shelf_app_id)); | 67 const int index = ItemIndexByID(shelf_id); |
| 105 if (index >= 0) { | 68 if (index >= 0) { |
| 106 ShelfItem item = items_[index]; | 69 ShelfItem item = items_[index]; |
| 107 DCHECK_EQ(item.type, TYPE_APP); | 70 DCHECK_EQ(item.type, TYPE_APP); |
| 108 DCHECK(!item.pinned_by_policy); | 71 DCHECK(!item.pinned_by_policy); |
| 109 item.type = TYPE_PINNED_APP; | 72 item.type = TYPE_PINNED_APP; |
| 110 Set(index, item); | 73 Set(index, item); |
| 111 } else if (!shelf_app_id.empty()) { | 74 } else if (!shelf_id.IsNull()) { |
| 112 ShelfItem item; | 75 ShelfItem item; |
| 113 item.type = TYPE_PINNED_APP; | 76 item.type = TYPE_PINNED_APP; |
| 114 item.id = ShelfID(shelf_app_id); | 77 item.id = shelf_id; |
| 115 Add(item); | 78 Add(item); |
| 116 } | 79 } |
| 117 } | 80 } |
| 118 | 81 |
| 119 bool ShelfModel::IsAppPinned(const std::string& app_id) { | 82 bool ShelfModel::IsAppPinned(const std::string& app_id) { |
| 120 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | 83 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 |
| 121 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id); | 84 const ShelfID shelf_id(GetShelfAppIdFromArcAppId(app_id)); |
| 122 | 85 |
| 123 const int index = ItemIndexByID(GetShelfIDForAppID(shelf_app_id)); | 86 const int index = ItemIndexByID(shelf_id); |
| 124 return index >= 0 && (items_[index].type == TYPE_PINNED_APP || | 87 return index >= 0 && (items_[index].type == TYPE_PINNED_APP || |
| 125 items_[index].type == TYPE_BROWSER_SHORTCUT); | 88 items_[index].type == TYPE_BROWSER_SHORTCUT); |
| 126 } | 89 } |
| 127 | 90 |
| 128 void ShelfModel::UnpinAppWithID(const std::string& app_id) { | 91 void ShelfModel::UnpinAppWithID(const std::string& app_id) { |
| 129 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 | 92 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 |
| 130 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id); | 93 const ShelfID shelf_id(GetShelfAppIdFromArcAppId(app_id)); |
| 131 | 94 |
| 132 // If the app is already not pinned, do nothing and return. | 95 // If the app is already not pinned, do nothing and return. |
| 133 if (!IsAppPinned(shelf_app_id)) | 96 if (!IsAppPinned(shelf_id.app_id)) |
| 134 return; | 97 return; |
| 135 | 98 |
| 136 // Remove the item if it is closed, or mark it as unpinned. | 99 // Remove the item if it is closed, or mark it as unpinned. |
| 137 const int index = ItemIndexByID(GetShelfIDForAppID(shelf_app_id)); | 100 const int index = ItemIndexByID(shelf_id); |
| 138 ShelfItem item = items_[index]; | 101 ShelfItem item = items_[index]; |
| 139 DCHECK_EQ(item.type, TYPE_PINNED_APP); | 102 DCHECK_EQ(item.type, TYPE_PINNED_APP); |
| 140 DCHECK(!item.pinned_by_policy); | 103 DCHECK(!item.pinned_by_policy); |
| 141 if (item.status == STATUS_CLOSED) { | 104 if (item.status == STATUS_CLOSED) { |
| 142 RemoveItemAt(index); | 105 RemoveItemAt(index); |
| 143 } else { | 106 } else { |
| 144 item.type = TYPE_APP; | 107 item.type = TYPE_APP; |
| 145 Set(index, item); | 108 Set(index, item); |
| 146 } | 109 } |
| 147 } | 110 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 // TODO(skuhne): Moving this into the Move function breaks lots of unit | 174 // TODO(skuhne): Moving this into the Move function breaks lots of unit |
| 212 // tests. So several functions were already using this incorrectly. | 175 // tests. So several functions were already using this incorrectly. |
| 213 // That needs to be cleaned up. | 176 // That needs to be cleaned up. |
| 214 if (index < new_index) | 177 if (index < new_index) |
| 215 new_index--; | 178 new_index--; |
| 216 | 179 |
| 217 Move(index, new_index); | 180 Move(index, new_index); |
| 218 } | 181 } |
| 219 } | 182 } |
| 220 | 183 |
| 221 int ShelfModel::ItemIndexByID(const ShelfID& id) const { | 184 int ShelfModel::ItemIndexByID(const ShelfID& shelf_id) const { |
| 185 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 |
| 186 ShelfID id(GetShelfAppIdFromArcAppId(shelf_id.app_id), shelf_id.launch_id); |
| 187 |
| 222 ShelfItems::const_iterator i = ItemByID(id); | 188 ShelfItems::const_iterator i = ItemByID(id); |
| 223 return i == items_.end() ? -1 : static_cast<int>(i - items_.begin()); | 189 return i == items_.end() ? -1 : static_cast<int>(i - items_.begin()); |
| 224 } | 190 } |
| 225 | 191 |
| 226 int ShelfModel::GetItemIndexForType(ShelfItemType type) { | 192 int ShelfModel::GetItemIndexForType(ShelfItemType type) { |
| 227 for (size_t i = 0; i < items_.size(); ++i) { | 193 for (size_t i = 0; i < items_.size(); ++i) { |
| 228 if (items_[i].type == type) | 194 if (items_[i].type == type) |
| 229 return i; | 195 return i; |
| 230 } | 196 } |
| 231 return -1; | 197 return -1; |
| 232 } | 198 } |
| 233 | 199 |
| 234 ShelfItems::const_iterator ShelfModel::ItemByID(const ShelfID& id) const { | 200 ShelfItems::const_iterator ShelfModel::ItemByID(const ShelfID& shelf_id) const { |
| 201 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 |
| 202 ShelfID id(GetShelfAppIdFromArcAppId(shelf_id.app_id), shelf_id.launch_id); |
| 203 |
| 235 for (ShelfItems::const_iterator i = items_.begin(); i != items_.end(); ++i) { | 204 for (ShelfItems::const_iterator i = items_.begin(); i != items_.end(); ++i) { |
| 236 if (i->id == id) | 205 if (i->id == id) |
| 237 return i; | 206 return i; |
| 238 } | 207 } |
| 239 return items_.end(); | 208 return items_.end(); |
| 240 } | 209 } |
| 241 | 210 |
| 242 int ShelfModel::FirstRunningAppIndex() const { | 211 int ShelfModel::FirstRunningAppIndex() const { |
| 243 ShelfItem weight_dummy; | 212 ShelfItem weight_dummy; |
| 244 weight_dummy.type = TYPE_APP; | 213 weight_dummy.type = TYPE_APP; |
| 245 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 214 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
| 246 CompareByWeight) - | 215 CompareByWeight) - |
| 247 items_.begin(); | 216 items_.begin(); |
| 248 } | 217 } |
| 249 | 218 |
| 250 int ShelfModel::FirstPanelIndex() const { | 219 int ShelfModel::FirstPanelIndex() const { |
| 251 ShelfItem weight_dummy; | 220 ShelfItem weight_dummy; |
| 252 weight_dummy.type = TYPE_APP_PANEL; | 221 weight_dummy.type = TYPE_APP_PANEL; |
| 253 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 222 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
| 254 CompareByWeight) - | 223 CompareByWeight) - |
| 255 items_.begin(); | 224 items_.begin(); |
| 256 } | 225 } |
| 257 | 226 |
| 258 void ShelfModel::SetShelfItemDelegate( | 227 void ShelfModel::SetShelfItemDelegate( |
| 259 const ShelfID& id, | 228 const ShelfID& shelf_id, |
| 260 std::unique_ptr<ShelfItemDelegate> item_delegate) { | 229 std::unique_ptr<ShelfItemDelegate> item_delegate) { |
| 230 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 |
| 231 ShelfID id(GetShelfAppIdFromArcAppId(shelf_id.app_id), shelf_id.launch_id); |
| 232 |
| 261 if (item_delegate) | 233 if (item_delegate) |
| 262 item_delegate->set_shelf_id(id); | 234 item_delegate->set_shelf_id(id); |
| 263 // This assignment replaces any ShelfItemDelegate already registered for |id|. | 235 // This assignment replaces any ShelfItemDelegate already registered for |id|. |
| 264 id_to_item_delegate_map_[id] = std::move(item_delegate); | 236 id_to_item_delegate_map_[id] = std::move(item_delegate); |
| 265 } | 237 } |
| 266 | 238 |
| 267 ShelfItemDelegate* ShelfModel::GetShelfItemDelegate(const ShelfID& id) { | 239 ShelfItemDelegate* ShelfModel::GetShelfItemDelegate(const ShelfID& shelf_id) { |
| 240 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859 |
| 241 ShelfID id(GetShelfAppIdFromArcAppId(shelf_id.app_id), shelf_id.launch_id); |
| 242 |
| 268 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) | 243 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) |
| 269 return id_to_item_delegate_map_[id].get(); | 244 return id_to_item_delegate_map_[id].get(); |
| 270 return nullptr; | 245 return nullptr; |
| 271 } | 246 } |
| 272 | 247 |
| 273 void ShelfModel::AddObserver(ShelfModelObserver* observer) { | 248 void ShelfModel::AddObserver(ShelfModelObserver* observer) { |
| 274 observers_.AddObserver(observer); | 249 observers_.AddObserver(observer); |
| 275 } | 250 } |
| 276 | 251 |
| 277 void ShelfModel::RemoveObserver(ShelfModelObserver* observer) { | 252 void ShelfModel::RemoveObserver(ShelfModelObserver* observer) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 290 static_cast<ShelfItems::difference_type>(index)); | 265 static_cast<ShelfItems::difference_type>(index)); |
| 291 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, | 266 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, |
| 292 CompareByWeight) - | 267 CompareByWeight) - |
| 293 items_.begin(), | 268 items_.begin(), |
| 294 static_cast<ShelfItems::difference_type>(index)); | 269 static_cast<ShelfItems::difference_type>(index)); |
| 295 | 270 |
| 296 return index; | 271 return index; |
| 297 } | 272 } |
| 298 | 273 |
| 299 } // namespace ash | 274 } // namespace ash |
| OLD | NEW |