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

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

Issue 2833173002: mash: Support ShelfModel access in Chrome. (Closed)
Patch Set: Address comments; fix test failures. Created 3 years, 6 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_controller.h" 5 #include "ash/shelf/shelf_controller.h"
6 6
7 #include "ash/public/interfaces/shelf.mojom.h" 7 #include "ash/public/cpp/config.h"
8 #include "ash/public/cpp/remote_shelf_item_delegate.h"
8 #include "ash/root_window_controller.h" 9 #include "ash/root_window_controller.h"
9 #include "ash/session/session_controller.h" 10 #include "ash/session/session_controller.h"
10 #include "ash/shelf/app_list_shelf_item_delegate.h" 11 #include "ash/shelf/app_list_shelf_item_delegate.h"
11 #include "ash/shelf/shelf.h" 12 #include "ash/shelf/shelf.h"
12 #include "ash/shell.h" 13 #include "ash/shell.h"
14 #include "base/auto_reset.h"
13 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
14 #include "ui/base/models/simple_menu_model.h" 16 #include "ui/base/models/simple_menu_model.h"
15 #include "ui/display/display.h" 17 #include "ui/display/display.h"
16 #include "ui/display/screen.h" 18 #include "ui/display/screen.h"
17 19
18 namespace ash { 20 namespace ash {
19 21
20 namespace { 22 namespace {
21 23
22 // Returns the Shelf instance for the display with the given |display_id|. 24 // Returns the Shelf instance for the display with the given |display_id|.
23 Shelf* GetShelfForDisplay(int64_t display_id) { 25 Shelf* GetShelfForDisplay(int64_t display_id) {
24 // The controller may be null for invalid ids or for displays being removed. 26 // The controller may be null for invalid ids or for displays being removed.
25 RootWindowController* root_window_controller = 27 RootWindowController* root_window_controller =
26 Shell::GetRootWindowControllerWithDisplayId(display_id); 28 Shell::GetRootWindowControllerWithDisplayId(display_id);
27 return root_window_controller ? root_window_controller->shelf() : nullptr; 29 return root_window_controller ? root_window_controller->shelf() : nullptr;
28 } 30 }
29 31
30 } // namespace 32 } // namespace
31 33
32 ShelfController::ShelfController() { 34 ShelfController::ShelfController() {
33 // Create the app list item in the shelf. 35 // Create an AppListShelfItemDelegate for the app list item.
34 AppListShelfItemDelegate::CreateAppListItemAndDelegate(&model_); 36 model_.SetShelfItemDelegate(ShelfID(kAppListId),
37 base::MakeUnique<AppListShelfItemDelegate>());
38 model_.AddObserver(this);
35 } 39 }
36 40
37 ShelfController::~ShelfController() {} 41 ShelfController::~ShelfController() {
42 model_.RemoveObserver(this);
43 }
38 44
39 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { 45 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) {
40 bindings_.AddBinding(this, std::move(request)); 46 bindings_.AddBinding(this, std::move(request));
41 } 47 }
42 48
43 void ShelfController::NotifyShelfInitialized(Shelf* shelf) { 49 void ShelfController::NotifyShelfInitialized(Shelf* shelf) {
44 // Notify observers, Chrome will set alignment and auto-hide from prefs. 50 // Notify observers, Chrome will set alignment and auto-hide from prefs.
45 display::Display display = 51 display::Display display =
46 display::Screen::GetScreen()->GetDisplayNearestWindow(shelf->GetWindow()); 52 display::Screen::GetScreen()->GetDisplayNearestWindow(shelf->GetWindow());
47 int64_t display_id = display.id(); 53 int64_t display_id = display.id();
(...skipping 20 matching lines...) Expand all
68 int64_t display_id = display.id(); 74 int64_t display_id = display.id();
69 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) { 75 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) {
70 observer->OnAutoHideBehaviorChanged(behavior, display_id); 76 observer->OnAutoHideBehaviorChanged(behavior, display_id);
71 }); 77 });
72 } 78 }
73 79
74 void ShelfController::AddObserver( 80 void ShelfController::AddObserver(
75 mojom::ShelfObserverAssociatedPtrInfo observer) { 81 mojom::ShelfObserverAssociatedPtrInfo observer) {
76 mojom::ShelfObserverAssociatedPtr observer_ptr; 82 mojom::ShelfObserverAssociatedPtr observer_ptr;
77 observer_ptr.Bind(std::move(observer)); 83 observer_ptr.Bind(std::move(observer));
84
85 if (Shell::GetAshConfig() == Config::MASH) {
86 // Mash synchronizes two ShelfModel instances, owned by Ash and Chrome.
87 // Notify Chrome of existing ShelfModel items created by Ash.
88 for (int i = 0; i < model_.item_count(); ++i) {
89 const ShelfItem& item = model_.items()[i];
90 observer_ptr->OnShelfItemAdded(i, item);
91 ShelfItemDelegate* delegate = model_.GetShelfItemDelegate(item.id);
92 mojom::ShelfItemDelegatePtr delegate_ptr;
93 if (delegate)
94 delegate_ptr = delegate->CreateInterfacePtrAndBind();
95 observer_ptr->OnShelfItemDelegateChanged(item.id,
96 std::move(delegate_ptr));
97 }
98 }
99
78 observers_.AddPtr(std::move(observer_ptr)); 100 observers_.AddPtr(std::move(observer_ptr));
79 } 101 }
80 102
81 void ShelfController::SetAlignment(ShelfAlignment alignment, 103 void ShelfController::SetAlignment(ShelfAlignment alignment,
82 int64_t display_id) { 104 int64_t display_id) {
83 Shelf* shelf = GetShelfForDisplay(display_id); 105 Shelf* shelf = GetShelfForDisplay(display_id);
84 // TODO(jamescook): The session state check should not be necessary, but 106 // TODO(jamescook): The session state check should not be necessary, but
85 // otherwise this wrongly tries to set the alignment on a secondary display 107 // otherwise this wrongly tries to set the alignment on a secondary display
86 // during login before the ShelfLockingManager is created. 108 // during login before the ShelfLockingManager is created.
87 if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted()) 109 if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted())
88 shelf->SetAlignment(alignment); 110 shelf->SetAlignment(alignment);
89 } 111 }
90 112
91 void ShelfController::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, 113 void ShelfController::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide,
92 int64_t display_id) { 114 int64_t display_id) {
93 Shelf* shelf = GetShelfForDisplay(display_id); 115 Shelf* shelf = GetShelfForDisplay(display_id);
94 // TODO(jamescook): The session state check should not be necessary, but 116 // TODO(jamescook): The session state check should not be necessary, but
95 // otherwise this wrongly tries to set auto-hide state on a secondary display 117 // otherwise this wrongly tries to set auto-hide state on a secondary display
96 // during login. 118 // during login.
97 if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted()) 119 if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted())
98 shelf->SetAutoHideBehavior(auto_hide); 120 shelf->SetAutoHideBehavior(auto_hide);
99 } 121 }
100 122
101 void ShelfController::PinItem( 123 void ShelfController::AddShelfItem(int32_t index, const ShelfItem& item) {
102 const ShelfItem& item, 124 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync";
103 mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { 125 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change";
104 NOTIMPLEMENTED(); 126 index = index < 0 ? model_.item_count() : index;
127 DCHECK_GT(index, 0) << "Items can not preceed the AppList";
128 DCHECK_LE(index, model_.item_count()) << "Index out of bounds";
129 index = std::min(std::max(index, 1), model_.item_count());
130 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true);
131 model_.AddAt(index, item);
105 } 132 }
106 133
107 void ShelfController::UnpinItem(const std::string& app_id) { 134 void ShelfController::RemoveShelfItem(const ShelfID& id) {
108 NOTIMPLEMENTED(); 135 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync";
136 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change";
137 const int index = model_.ItemIndexByID(id);
138 DCHECK_GE(index, 0) << "Item not found";
139 DCHECK_NE(index, 0) << "The AppList shelf item cannot be removed";
140 if (index <= 0)
141 return;
142 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true);
143 model_.RemoveItemAt(index);
109 } 144 }
110 145
111 void ShelfController::SetItemImage(const std::string& app_id, 146 void ShelfController::MoveShelfItem(const ShelfID& id, int32_t index) {
112 const SkBitmap& image) { 147 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync";
113 NOTIMPLEMENTED(); 148 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change";
149 const int current_index = model_.ItemIndexByID(id);
150 DCHECK_GE(current_index, 0) << "No item found with the given id";
151 DCHECK_NE(current_index, 0) << "The AppList shelf item cannot be moved";
152 if (current_index <= 0)
153 return;
154 DCHECK_GT(index, 0) << "Items can not preceed the AppList";
155 DCHECK_LT(index, model_.item_count()) << "Index out of bounds";
156 index = std::min(std::max(index, 1), model_.item_count() - 1);
157 DCHECK_NE(current_index, index) << "The item is already at the given index";
158 if (current_index == index)
159 return;
160 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true);
161 model_.Move(current_index, index);
162 }
163
164 void ShelfController::UpdateShelfItem(const ShelfItem& item) {
165 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync";
166 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change";
167 const int index = model_.ItemIndexByID(item.id);
168 DCHECK_GE(index, 0) << "No item found with the given id";
169 if (index < 0)
170 return;
171 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true);
172 model_.Set(index, item);
173 }
174
175 void ShelfController::SetShelfItemDelegate(
176 const ShelfID& id,
177 mojom::ShelfItemDelegatePtr delegate) {
178 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync";
179 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change";
180 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true);
181 if (delegate.is_bound())
182 model_.SetShelfItemDelegate(
183 id, base::MakeUnique<RemoteShelfItemDelegate>(id, std::move(delegate)));
184 else
185 model_.SetShelfItemDelegate(id, nullptr);
186 }
187
188 void ShelfController::ShelfItemAdded(int index) {
189 if (applying_remote_shelf_model_changes_ ||
190 Shell::GetAshConfig() != Config::MASH) {
191 return;
192 }
193
194 const ShelfItem& item = model_.items()[index];
195 observers_.ForAllPtrs([index, item](mojom::ShelfObserver* observer) {
196 observer->OnShelfItemAdded(index, item);
197 });
198 }
199
200 void ShelfController::ShelfItemRemoved(int index, const ShelfItem& old_item) {
201 if (applying_remote_shelf_model_changes_ ||
202 Shell::GetAshConfig() != Config::MASH) {
203 return;
204 }
205
206 observers_.ForAllPtrs([old_item](mojom::ShelfObserver* observer) {
207 observer->OnShelfItemRemoved(old_item.id);
208 });
209 }
210
211 void ShelfController::ShelfItemMoved(int start_index, int target_index) {
212 if (applying_remote_shelf_model_changes_ ||
213 Shell::GetAshConfig() != Config::MASH) {
214 return;
215 }
216
217 const ShelfItem& item = model_.items()[target_index];
218 observers_.ForAllPtrs([item, target_index](mojom::ShelfObserver* observer) {
219 observer->OnShelfItemMoved(item.id, target_index);
220 });
221 }
222
223 void ShelfController::ShelfItemChanged(int index, const ShelfItem& old_item) {
224 if (applying_remote_shelf_model_changes_ ||
225 Shell::GetAshConfig() != Config::MASH) {
226 return;
227 }
228
229 const ShelfItem& item = model_.items()[index];
230 observers_.ForAllPtrs([item](mojom::ShelfObserver* observer) {
231 observer->OnShelfItemUpdated(item);
232 });
233 }
234
235 void ShelfController::ShelfItemDelegateChanged(const ShelfID& id,
236 ShelfItemDelegate* delegate) {
237 if (applying_remote_shelf_model_changes_ ||
238 Shell::GetAshConfig() != Config::MASH) {
239 return;
240 }
241
242 observers_.ForAllPtrs([id, delegate](mojom::ShelfObserver* observer) {
243 observer->OnShelfItemDelegateChanged(
244 id, delegate ? delegate->CreateInterfacePtrAndBind()
245 : mojom::ShelfItemDelegatePtr());
246 });
114 } 247 }
115 248
116 } // namespace ash 249 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698