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() : shelf_model_observer_binding_(this) { |
34 // Create the app list item in the shelf. | 36 // Create the app list item in the shelf. |
35 AppListShelfItemDelegate::CreateAppListItemAndDelegate(&model_); | 37 AppListShelfItemDelegate::CreateAppListItemAndDelegate(&model_); |
| 38 model_.AddObserver(this); |
36 } | 39 } |
37 | 40 |
38 ShelfController::~ShelfController() {} | 41 ShelfController::~ShelfController() { |
| 42 model_.RemoveObserver(this); |
| 43 } |
39 | 44 |
40 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { | 45 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { |
41 bindings_.AddBinding(this, std::move(request)); | 46 bindings_.AddBinding(this, std::move(request)); |
42 } | 47 } |
43 | 48 |
44 void ShelfController::NotifyShelfInitialized(Shelf* shelf) { | 49 void ShelfController::NotifyShelfInitialized(Shelf* shelf) { |
45 // Notify observers, Chrome will set alignment and auto-hide from prefs. | 50 // Notify observers, Chrome will set alignment and auto-hide from prefs. |
46 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); | 51 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
47 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { | 52 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { |
48 observer->OnShelfInitialized(display_id); | 53 observer->OnShelfInitialized(display_id); |
(...skipping 17 matching lines...) Expand all Loading... |
66 }); | 71 }); |
67 } | 72 } |
68 | 73 |
69 void ShelfController::AddObserver( | 74 void ShelfController::AddObserver( |
70 mojom::ShelfObserverAssociatedPtrInfo observer) { | 75 mojom::ShelfObserverAssociatedPtrInfo observer) { |
71 mojom::ShelfObserverAssociatedPtr observer_ptr; | 76 mojom::ShelfObserverAssociatedPtr observer_ptr; |
72 observer_ptr.Bind(std::move(observer)); | 77 observer_ptr.Bind(std::move(observer)); |
73 observers_.AddPtr(std::move(observer_ptr)); | 78 observers_.AddPtr(std::move(observer_ptr)); |
74 } | 79 } |
75 | 80 |
| 81 void ShelfController::LinkShelfModels(mojom::ShelfModelObserverPtr observer, |
| 82 LinkShelfModelsCallback callback) { |
| 83 // Notify Ash of all the existing local (Chrome) items and delegates. |
| 84 std::move(callback).Run( |
| 85 shelf_model_observer_binding_.CreateInterfacePtrAndBind(), |
| 86 model_.items()); |
| 87 for (const ShelfItem& item : model_.items()) { |
| 88 ShelfItemDelegate* delegate = model_.GetShelfItemDelegate(item.id); |
| 89 mojom::ShelfItemDelegatePtr delegate_ptr; |
| 90 if (delegate) |
| 91 delegate_ptr = delegate->CreateInterfacePtrAndBind(); |
| 92 observer->OnShelfItemDelegateChanged(item.id, std::move(delegate_ptr)); |
| 93 } |
| 94 model_observers_.AddPtr(std::move(observer)); |
| 95 } |
| 96 |
76 void ShelfController::SetAlignment(ShelfAlignment alignment, | 97 void ShelfController::SetAlignment(ShelfAlignment alignment, |
77 int64_t display_id) { | 98 int64_t display_id) { |
78 Shelf* shelf = GetShelfForDisplay(display_id); | 99 Shelf* shelf = GetShelfForDisplay(display_id); |
79 // TODO(jamescook): The session state check should not be necessary, but | 100 // TODO(jamescook): The session state check should not be necessary, but |
80 // otherwise this wrongly tries to set the alignment on a secondary display | 101 // otherwise this wrongly tries to set the alignment on a secondary display |
81 // during login before the ShelfLockingManager is created. | 102 // during login before the ShelfLockingManager is created. |
82 if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted()) | 103 if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted()) |
83 shelf->SetAlignment(alignment); | 104 shelf->SetAlignment(alignment); |
84 } | 105 } |
85 | 106 |
86 void ShelfController::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, | 107 void ShelfController::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, |
87 int64_t display_id) { | 108 int64_t display_id) { |
88 Shelf* shelf = GetShelfForDisplay(display_id); | 109 Shelf* shelf = GetShelfForDisplay(display_id); |
89 // TODO(jamescook): The session state check should not be necessary, but | 110 // TODO(jamescook): The session state check should not be necessary, but |
90 // otherwise this wrongly tries to set auto-hide state on a secondary display | 111 // otherwise this wrongly tries to set auto-hide state on a secondary display |
91 // during login. | 112 // during login. |
92 if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted()) | 113 if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted()) |
93 shelf->SetAutoHideBehavior(auto_hide); | 114 shelf->SetAutoHideBehavior(auto_hide); |
94 } | 115 } |
95 | 116 |
96 void ShelfController::PinItem( | 117 void ShelfController::OnShelfItemAdded(int32_t index, const ShelfItem& item) { |
97 const ShelfItem& item, | 118 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
98 mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { | 119 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
99 NOTIMPLEMENTED(); | 120 DCHECK_GE(model_.item_count(), index) << "Models out of sync"; |
| 121 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 122 model_.AddAt(index, item); |
100 } | 123 } |
101 | 124 |
102 void ShelfController::UnpinItem(const std::string& app_id) { | 125 void ShelfController::OnShelfItemRemoved(int32_t index, const ShelfItem& item) { |
103 NOTIMPLEMENTED(); | 126 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
| 127 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
| 128 DCHECK_GT(model_.item_count(), index) << "Models out of sync"; |
| 129 DCHECK_EQ(model_.items()[index].id, item.id) << "Models out of sync"; |
| 130 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 131 model_.RemoveItemAt(index); |
104 } | 132 } |
105 | 133 |
106 void ShelfController::SetItemImage(const std::string& app_id, | 134 void ShelfController::OnShelfItemMoved(int32_t start_index, |
107 const SkBitmap& image) { | 135 int32_t target_index) { |
108 NOTIMPLEMENTED(); | 136 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
| 137 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
| 138 DCHECK_GT(model_.item_count(), start_index) << "Models out of sync"; |
| 139 DCHECK_GT(model_.item_count(), target_index) << "Models out of sync"; |
| 140 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 141 model_.Move(start_index, target_index); |
| 142 } |
| 143 |
| 144 void ShelfController::OnShelfItemChanged(int32_t index, const ShelfItem& item) { |
| 145 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
| 146 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
| 147 DCHECK_GT(model_.item_count(), index) << "Models out of sync"; |
| 148 DCHECK_EQ(model_.items()[index].id, item.id) << "Models out of sync"; |
| 149 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 150 model_.Set(index, item); |
| 151 } |
| 152 |
| 153 void ShelfController::OnShelfItemDelegateChanged( |
| 154 const ShelfID& id, |
| 155 mojom::ShelfItemDelegatePtr delegate) { |
| 156 DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
| 157 DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
| 158 base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 159 if (delegate.is_bound()) |
| 160 model_.SetShelfItemDelegate( |
| 161 id, base::MakeUnique<RemoteShelfItemDelegate>(id, std::move(delegate))); |
| 162 else |
| 163 model_.SetShelfItemDelegate(id, nullptr); |
| 164 } |
| 165 |
| 166 void ShelfController::ShelfItemAdded(int index) { |
| 167 if (applying_remote_shelf_model_changes_) |
| 168 return; |
| 169 |
| 170 const ShelfItem& item = model_.items()[index]; |
| 171 model_observers_.ForAllPtrs( |
| 172 [index, item](mojom::ShelfModelObserver* observer) { |
| 173 observer->OnShelfItemAdded(index, item); |
| 174 }); |
| 175 } |
| 176 |
| 177 void ShelfController::ShelfItemRemoved(int index, const ShelfItem& old_item) { |
| 178 if (applying_remote_shelf_model_changes_) |
| 179 return; |
| 180 |
| 181 model_observers_.ForAllPtrs( |
| 182 [index, old_item](mojom::ShelfModelObserver* observer) { |
| 183 observer->OnShelfItemRemoved(index, old_item); |
| 184 }); |
| 185 } |
| 186 |
| 187 void ShelfController::ShelfItemMoved(int start_index, int target_index) { |
| 188 if (applying_remote_shelf_model_changes_) |
| 189 return; |
| 190 |
| 191 model_observers_.ForAllPtrs( |
| 192 [start_index, target_index](mojom::ShelfModelObserver* observer) { |
| 193 observer->OnShelfItemMoved(start_index, target_index); |
| 194 }); |
| 195 } |
| 196 |
| 197 void ShelfController::ShelfItemChanged(int index, const ShelfItem& old_item) { |
| 198 if (applying_remote_shelf_model_changes_) |
| 199 return; |
| 200 |
| 201 const ShelfItem& item = model_.items()[index]; |
| 202 model_observers_.ForAllPtrs( |
| 203 [index, item](mojom::ShelfModelObserver* observer) { |
| 204 observer->OnShelfItemChanged(index, item); |
| 205 }); |
| 206 } |
| 207 |
| 208 void ShelfController::ShelfItemDelegateChanged(const ShelfID& id, |
| 209 ShelfItemDelegate* delegate) { |
| 210 if (applying_remote_shelf_model_changes_) |
| 211 return; |
| 212 |
| 213 model_observers_.ForAllPtrs( |
| 214 [id, delegate](mojom::ShelfModelObserver* observer) { |
| 215 observer->OnShelfItemDelegateChanged( |
| 216 id, delegate ? delegate->CreateInterfacePtrAndBind() |
| 217 : mojom::ShelfItemDelegatePtr()); |
| 218 }); |
109 } | 219 } |
110 | 220 |
111 } // namespace ash | 221 } // namespace ash |
OLD | NEW |