James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ash/shelf/shelf_controller.h" |
| 6 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 7 | #include "ash/public/cpp/ash_pref_names.h" |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 8 | #include "ash/public/cpp/config.h" |
| 9 | #include "ash/public/cpp/remote_shelf_item_delegate.h" |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 10 | #include "ash/public/cpp/shelf_prefs.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 11 | #include "ash/root_window_controller.h" |
jamescook | 788b4fc | 2017-05-18 16:16:06 | [diff] [blame] | 12 | #include "ash/session/session_controller.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 13 | #include "ash/shelf/app_list_shelf_item_delegate.h" |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 14 | #include "ash/shelf/shelf.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 15 | #include "ash/shell.h" |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 16 | #include "ash/strings/grit/ash_strings.h" |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 17 | #include "base/auto_reset.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 19 | #include "components/pref_registry/pref_registry_syncable.h" |
| 20 | #include "components/prefs/pref_change_registrar.h" |
| 21 | #include "components/prefs/pref_registry_simple.h" |
| 22 | #include "components/prefs/pref_service.h" |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 23 | #include "ui/base/l10n/l10n_util.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 24 | #include "ui/base/models/simple_menu_model.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 25 | #include "ui/display/display.h" |
| 26 | #include "ui/display/screen.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 27 | |
| 28 | namespace ash { |
| 29 | |
| 30 | namespace { |
| 31 | |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 32 | // Returns the Shelf instance for the display with the given |display_id|. |
| 33 | Shelf* GetShelfForDisplay(int64_t display_id) { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 34 | // The controller may be null for invalid ids or for displays being removed. |
| 35 | RootWindowController* root_window_controller = |
| 36 | Shell::GetRootWindowControllerWithDisplayId(display_id); |
jamescook | aa9f49f | 2017-05-30 20:48:45 | [diff] [blame] | 37 | return root_window_controller ? root_window_controller->shelf() : nullptr; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 38 | } |
| 39 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 40 | // Set each Shelf's auto-hide behavior from the per-display pref. |
| 41 | void SetShelfAutoHideFromPrefs() { |
| 42 | // TODO(jamescook): The session state check should not be necessary, but |
| 43 | // otherwise this wrongly tries to set the alignment on a secondary display |
| 44 | // during login before the ShelfLockingManager is created. |
| 45 | SessionController* session_controller = Shell::Get()->session_controller(); |
| 46 | PrefService* prefs = Shell::Get()->GetActiveUserPrefService(); |
| 47 | if (!prefs || !session_controller->IsActiveUserSessionStarted()) |
| 48 | return; |
| 49 | |
| 50 | for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
| 51 | auto value = GetShelfAutoHideBehaviorPref(prefs, display.id()); |
| 52 | // Don't show the shelf in app mode. |
| 53 | if (session_controller->IsRunningInAppMode()) |
| 54 | value = SHELF_AUTO_HIDE_ALWAYS_HIDDEN; |
| 55 | Shelf* shelf = GetShelfForDisplay(display.id()); |
| 56 | if (shelf) |
| 57 | shelf->SetAutoHideBehavior(value); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Set each Shelf's alignment from the per-display pref. |
| 62 | void SetShelfAlignmentFromPrefs() { |
| 63 | // TODO(jamescook): The session state check should not be necessary, but |
| 64 | // otherwise this wrongly tries to set the alignment on a secondary display |
| 65 | // during login before the ShelfLockingManager is created. |
| 66 | SessionController* session_controller = Shell::Get()->session_controller(); |
| 67 | PrefService* prefs = Shell::Get()->GetActiveUserPrefService(); |
| 68 | if (!prefs || !session_controller->IsActiveUserSessionStarted()) |
| 69 | return; |
| 70 | |
| 71 | for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
| 72 | auto value = GetShelfAlignmentPref(prefs, display.id()); |
| 73 | Shelf* shelf = GetShelfForDisplay(display.id()); |
| 74 | if (shelf) |
| 75 | shelf->SetAlignment(value); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Set each Shelf's auto-hide behavior and alignment from the per-display prefs. |
| 80 | void SetShelfBehaviorsFromPrefs() { |
| 81 | SetShelfAutoHideFromPrefs(); |
| 82 | SetShelfAlignmentFromPrefs(); |
| 83 | } |
| 84 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 85 | } // namespace |
| 86 | |
| 87 | ShelfController::ShelfController() { |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 88 | // Set the delegate and title string for the app list item. |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 89 | model_.SetShelfItemDelegate(ShelfID(kAppListId), |
| 90 | base::MakeUnique<AppListShelfItemDelegate>()); |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 91 | DCHECK_EQ(0, model_.ItemIndexByID(ShelfID(kAppListId))); |
| 92 | ShelfItem item = model_.items()[0]; |
| 93 | item.title = l10n_util::GetStringUTF16(IDS_ASH_SHELF_APP_LIST_LAUNCHER_TITLE); |
| 94 | model_.Set(0, item); |
| 95 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 96 | model_.AddObserver(this); |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 97 | Shell::Get()->AddShellObserver(this); |
| 98 | Shell::Get()->window_tree_host_manager()->AddObserver(this); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 99 | } |
| 100 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 101 | ShelfController::~ShelfController() { |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 102 | Shell::Get()->window_tree_host_manager()->RemoveObserver(this); |
| 103 | Shell::Get()->RemoveShellObserver(this); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 104 | model_.RemoveObserver(this); |
| 105 | } |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 106 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 107 | // static |
| 108 | void ShelfController::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 109 | // These prefs are marked PUBLIC for use by Chrome, they're currently only |
| 110 | // needed for ChromeLauncherController::ShelfBoundsChangesProbablyWithUser |
| 111 | // and ChromeLauncherPrefsObserver. See the pref names definitions for an |
| 112 | // explanation of the synced, local, and per-display behavior of these prefs. |
| 113 | registry->RegisterStringPref( |
| 114 | prefs::kShelfAutoHideBehavior, kShelfAutoHideBehaviorNever, |
| 115 | user_prefs::PrefRegistrySyncable::SYNCABLE_PREF | PrefRegistry::PUBLIC); |
| 116 | registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal, |
| 117 | std::string(), PrefRegistry::PUBLIC); |
| 118 | registry->RegisterStringPref( |
| 119 | prefs::kShelfAlignment, kShelfAlignmentBottom, |
| 120 | user_prefs::PrefRegistrySyncable::SYNCABLE_PREF | PrefRegistry::PUBLIC); |
| 121 | registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string(), |
| 122 | PrefRegistry::PUBLIC); |
| 123 | registry->RegisterDictionaryPref(prefs::kShelfPreferences, |
| 124 | PrefRegistry::PUBLIC); |
| 125 | } |
| 126 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 127 | void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { |
| 128 | bindings_.AddBinding(this, std::move(request)); |
| 129 | } |
| 130 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 131 | void ShelfController::AddObserver( |
| 132 | mojom::ShelfObserverAssociatedPtrInfo observer) { |
| 133 | mojom::ShelfObserverAssociatedPtr observer_ptr; |
| 134 | observer_ptr.Bind(std::move(observer)); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 135 | |
| 136 | if (Shell::GetAshConfig() == Config::MASH) { |
| 137 | // Mash synchronizes two ShelfModel instances, owned by Ash and Chrome. |
| 138 | // Notify Chrome of existing ShelfModel items created by Ash. |
| 139 | for (int i = 0; i < model_.item_count(); ++i) { |
| 140 | const ShelfItem& item = model_.items()[i]; |
| 141 | observer_ptr->OnShelfItemAdded(i, item); |
| 142 | ShelfItemDelegate* delegate = model_.GetShelfItemDelegate(item.id); |
Michael Wasserman | 78b6f3e | 2017-07-20 19:51:20 | [diff] [blame] | 143 | if (delegate) { |
| 144 | observer_ptr->OnShelfItemDelegateChanged( |
| 145 | item.id, delegate->CreateInterfacePtrAndBind()); |
| 146 | } |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 150 | observers_.AddPtr(std::move(observer_ptr)); |
| 151 | } |
| 152 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 153 | void ShelfController::AddShelfItem(int32_t index, const ShelfItem& item) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 154 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 155 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 156 | index = index < 0 ? model_.item_count() : index; |
Julien Brianceau | 293917a8 | 2017-08-01 17:32:59 | [diff] [blame] | 157 | DCHECK_GT(index, 0) << " Items can not precede the AppList"; |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 158 | DCHECK_LE(index, model_.item_count()) << " Index out of bounds"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 159 | index = std::min(std::max(index, 1), model_.item_count()); |
| 160 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 161 | model_.AddAt(index, item); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 162 | } |
| 163 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 164 | void ShelfController::RemoveShelfItem(const ShelfID& id) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 165 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 166 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 167 | const int index = model_.ItemIndexByID(id); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 168 | DCHECK_GE(index, 0) << " No item found with the id: " << id; |
| 169 | DCHECK_NE(index, 0) << " The AppList shelf item cannot be removed"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 170 | if (index <= 0) |
| 171 | return; |
| 172 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 173 | model_.RemoveItemAt(index); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 174 | } |
| 175 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 176 | void ShelfController::MoveShelfItem(const ShelfID& id, int32_t index) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 177 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 178 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 179 | const int current_index = model_.ItemIndexByID(id); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 180 | DCHECK_GE(current_index, 0) << " No item found with the id: " << id; |
| 181 | DCHECK_NE(current_index, 0) << " The AppList shelf item cannot be moved"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 182 | if (current_index <= 0) |
| 183 | return; |
Julien Brianceau | 293917a8 | 2017-08-01 17:32:59 | [diff] [blame] | 184 | DCHECK_GT(index, 0) << " Items can not precede the AppList"; |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 185 | DCHECK_LT(index, model_.item_count()) << " Index out of bounds"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 186 | index = std::min(std::max(index, 1), model_.item_count() - 1); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 187 | DCHECK_NE(current_index, index) << " The item is already at the given index"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 188 | if (current_index == index) |
| 189 | return; |
| 190 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 191 | model_.Move(current_index, index); |
| 192 | } |
| 193 | |
| 194 | void ShelfController::UpdateShelfItem(const ShelfItem& item) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 195 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 196 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 197 | const int index = model_.ItemIndexByID(item.id); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 198 | DCHECK_GE(index, 0) << " No item found with the id: " << item.id; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 199 | if (index < 0) |
| 200 | return; |
| 201 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 202 | model_.Set(index, item); |
| 203 | } |
| 204 | |
| 205 | void ShelfController::SetShelfItemDelegate( |
| 206 | const ShelfID& id, |
| 207 | mojom::ShelfItemDelegatePtr delegate) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 208 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 209 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 210 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 211 | if (delegate.is_bound()) |
| 212 | model_.SetShelfItemDelegate( |
| 213 | id, base::MakeUnique<RemoteShelfItemDelegate>(id, std::move(delegate))); |
| 214 | else |
| 215 | model_.SetShelfItemDelegate(id, nullptr); |
| 216 | } |
| 217 | |
| 218 | void ShelfController::ShelfItemAdded(int index) { |
| 219 | if (applying_remote_shelf_model_changes_ || |
| 220 | Shell::GetAshConfig() != Config::MASH) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | const ShelfItem& item = model_.items()[index]; |
| 225 | observers_.ForAllPtrs([index, item](mojom::ShelfObserver* observer) { |
| 226 | observer->OnShelfItemAdded(index, item); |
| 227 | }); |
| 228 | } |
| 229 | |
| 230 | void ShelfController::ShelfItemRemoved(int index, const ShelfItem& old_item) { |
| 231 | if (applying_remote_shelf_model_changes_ || |
| 232 | Shell::GetAshConfig() != Config::MASH) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | observers_.ForAllPtrs([old_item](mojom::ShelfObserver* observer) { |
| 237 | observer->OnShelfItemRemoved(old_item.id); |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | void ShelfController::ShelfItemMoved(int start_index, int target_index) { |
| 242 | if (applying_remote_shelf_model_changes_ || |
| 243 | Shell::GetAshConfig() != Config::MASH) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | const ShelfItem& item = model_.items()[target_index]; |
| 248 | observers_.ForAllPtrs([item, target_index](mojom::ShelfObserver* observer) { |
| 249 | observer->OnShelfItemMoved(item.id, target_index); |
| 250 | }); |
| 251 | } |
| 252 | |
| 253 | void ShelfController::ShelfItemChanged(int index, const ShelfItem& old_item) { |
| 254 | if (applying_remote_shelf_model_changes_ || |
| 255 | Shell::GetAshConfig() != Config::MASH) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | const ShelfItem& item = model_.items()[index]; |
| 260 | observers_.ForAllPtrs([item](mojom::ShelfObserver* observer) { |
| 261 | observer->OnShelfItemUpdated(item); |
| 262 | }); |
| 263 | } |
| 264 | |
| 265 | void ShelfController::ShelfItemDelegateChanged(const ShelfID& id, |
| 266 | ShelfItemDelegate* delegate) { |
| 267 | if (applying_remote_shelf_model_changes_ || |
| 268 | Shell::GetAshConfig() != Config::MASH) { |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | observers_.ForAllPtrs([id, delegate](mojom::ShelfObserver* observer) { |
| 273 | observer->OnShelfItemDelegateChanged( |
| 274 | id, delegate ? delegate->CreateInterfacePtrAndBind() |
| 275 | : mojom::ShelfItemDelegatePtr()); |
| 276 | }); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 277 | } |
| 278 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 279 | void ShelfController::OnActiveUserPrefServiceChanged( |
| 280 | PrefService* pref_service) { |
| 281 | pref_change_registrar_.reset(); |
| 282 | if (!pref_service) // Null during startup, user switch and tests. |
| 283 | return; |
| 284 | SetShelfBehaviorsFromPrefs(); |
| 285 | pref_change_registrar_ = base::MakeUnique<PrefChangeRegistrar>(); |
| 286 | pref_change_registrar_->Init(pref_service); |
| 287 | pref_change_registrar_->Add(prefs::kShelfAlignmentLocal, |
| 288 | base::Bind(&SetShelfAlignmentFromPrefs)); |
| 289 | pref_change_registrar_->Add(prefs::kShelfAutoHideBehaviorLocal, |
| 290 | base::Bind(&SetShelfAutoHideFromPrefs)); |
| 291 | pref_change_registrar_->Add(prefs::kShelfPreferences, |
| 292 | base::Bind(&SetShelfBehaviorsFromPrefs)); |
| 293 | } |
| 294 | |
| 295 | void ShelfController::OnDisplayConfigurationChanged() { |
| 296 | // Set/init the shelf behaviors from preferences, in case a display was added. |
| 297 | SetShelfBehaviorsFromPrefs(); |
| 298 | } |
| 299 | |
| 300 | void ShelfController::OnWindowTreeHostReusedForDisplay( |
| 301 | AshWindowTreeHost* window_tree_host, |
| 302 | const display::Display& display) { |
| 303 | // See comment in OnWindowTreeHostsSwappedDisplays(). |
| 304 | SetShelfBehaviorsFromPrefs(); |
| 305 | } |
| 306 | |
| 307 | void ShelfController::OnWindowTreeHostsSwappedDisplays( |
| 308 | AshWindowTreeHost* host1, |
| 309 | AshWindowTreeHost* host2) { |
| 310 | // The display ids for existing shelf instances may have changed, so update |
| 311 | // the alignment and auto-hide state from prefs. See http://crbug.com/748291 |
| 312 | SetShelfBehaviorsFromPrefs(); |
| 313 | } |
| 314 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 315 | } // namespace ash |