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