Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: ash/shelf/shelf_model.cc

Issue 2791463002: mash: Remove ShelfDelegate; move functions to ShelfModel. (Closed)
Patch Set: Address comment. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/shelf/shelf_model.h ('k') | ash/shelf/shelf_model_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859
61 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id);
62
63 if (shelf_app_id.empty())
64 return ash::kInvalidShelfID;
65
66 for (const ShelfItem& item : items_) {
67 // ShelfWindowWatcher handles app panel windows separately.
68 if (item.type != TYPE_APP_PANEL &&
69 item.app_launch_id.app_id() == shelf_app_id) {
70 return item.id;
71 }
72 }
73 return kInvalidShelfID;
74 }
75
76 ShelfID ShelfModel::GetShelfIDForAppIDAndLaunchID(
77 const std::string& app_id,
78 const std::string& launch_id) {
79 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859
80 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id);
81
82 if (shelf_app_id.empty())
83 return ash::kInvalidShelfID;
84
85 for (const ShelfItem& item : items_) {
86 // ShelfWindowWatcher handles app panel windows separately.
87 if (item.type != TYPE_APP_PANEL &&
88 item.app_launch_id.app_id() == shelf_app_id &&
89 item.app_launch_id.launch_id() == launch_id) {
90 return item.id;
91 }
92 }
93 return kInvalidShelfID;
94 }
95
96 const std::string& ShelfModel::GetAppIDForShelfID(ShelfID id) {
97 ShelfItems::const_iterator item = ItemByID(id);
98 return item != items().end() ? item->app_launch_id.app_id()
99 : base::EmptyString();
100 }
101
102 void ShelfModel::PinAppWithID(const std::string& app_id) {
103 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859
104 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id);
105
106 // If the app is already pinned, do nothing and return.
107 if (IsAppPinned(shelf_app_id))
108 return;
109
110 // Convert an existing item to be pinned, or create a new pinned item.
111 const int index = ItemIndexByID(GetShelfIDForAppID(shelf_app_id));
112 if (index >= 0) {
113 ShelfItem item = items_[index];
114 DCHECK_EQ(item.type, TYPE_APP);
115 DCHECK(!item.pinned_by_policy);
116 item.type = TYPE_PINNED_APP;
117 Set(index, item);
118 } else if (!shelf_app_id.empty()) {
119 ash::ShelfItem item;
120 item.type = ash::TYPE_PINNED_APP;
121 item.app_launch_id = AppLaunchId(shelf_app_id);
122 Add(item);
123 }
124 }
125
126 bool ShelfModel::IsAppPinned(const std::string& app_id) {
127 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859
128 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id);
129
130 const int index = ItemIndexByID(GetShelfIDForAppID(shelf_app_id));
131 return index >= 0 && (items_[index].type == TYPE_PINNED_APP ||
132 items_[index].type == TYPE_BROWSER_SHORTCUT);
133 }
134
135 void ShelfModel::UnpinAppWithID(const std::string& app_id) {
136 // TODO(khmel): Fix this Arc application id mapping. See http://b/31703859
137 const std::string shelf_app_id = GetShelfAppIdFromArcAppId(app_id);
138
139 // If the app is already not pinned, do nothing and return.
140 if (!IsAppPinned(shelf_app_id))
141 return;
142
143 // Remove the item if it is closed, or mark it as unpinned.
144 const int index = ItemIndexByID(GetShelfIDForAppID(shelf_app_id));
145 ShelfItem item = items_[index];
146 DCHECK_EQ(item.type, TYPE_PINNED_APP);
147 DCHECK(!item.pinned_by_policy);
148 if (item.status == ash::STATUS_CLOSED) {
149 RemoveItemAt(index);
150 } else {
151 item.type = TYPE_APP;
152 Set(index, item);
153 }
154 }
155
50 void ShelfModel::DestroyItemDelegates() { 156 void ShelfModel::DestroyItemDelegates() {
51 // Some ShelfItemDelegates access this model in their destructors and hence 157 // Some ShelfItemDelegates access this model in their destructors and hence
52 // need early cleanup. 158 // need early cleanup.
53 id_to_item_delegate_map_.clear(); 159 id_to_item_delegate_map_.clear();
54 } 160 }
55 161
56 int ShelfModel::Add(const ShelfItem& item) { 162 int ShelfModel::Add(const ShelfItem& item) {
57 return AddAt(items_.size(), item); 163 return AddAt(items_.size(), item);
58 } 164 }
59 165
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 static_cast<ShelfItems::difference_type>(index)); 295 static_cast<ShelfItems::difference_type>(index));
190 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, 296 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy,
191 CompareByWeight) - 297 CompareByWeight) -
192 items_.begin(), 298 items_.begin(),
193 static_cast<ShelfItems::difference_type>(index)); 299 static_cast<ShelfItems::difference_type>(index));
194 300
195 return index; 301 return index;
196 } 302 }
197 303
198 } // namespace ash 304 } // namespace ash
OLDNEW
« no previous file with comments | « ash/shelf/shelf_model.h ('k') | ash/shelf/shelf_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698