Index: ash/shelf/shelf_controller.cc |
diff --git a/ash/shelf/shelf_controller.cc b/ash/shelf/shelf_controller.cc |
index 876c5d0df2edaf39b765410901a89682415bb8aa..cc4243086e44397adf848b1abbc4dba4e9264c03 100644 |
--- a/ash/shelf/shelf_controller.cc |
+++ b/ash/shelf/shelf_controller.cc |
@@ -4,13 +4,15 @@ |
#include "ash/shelf/shelf_controller.h" |
-#include "ash/public/interfaces/shelf.mojom.h" |
+#include "ash/public/cpp/config.h" |
+#include "ash/public/cpp/remote_shelf_item_delegate.h" |
#include "ash/root_window_controller.h" |
#include "ash/session/session_controller.h" |
#include "ash/shelf/app_list_shelf_item_delegate.h" |
#include "ash/shelf/shelf.h" |
#include "ash/shell.h" |
#include "ash/wm_window.h" |
+#include "base/auto_reset.h" |
#include "base/strings/utf_string_conversions.h" |
#include "ui/base/models/simple_menu_model.h" |
#include "ui/display/display.h" |
@@ -31,11 +33,15 @@ Shelf* GetShelfForDisplay(int64_t display_id) { |
} // namespace |
ShelfController::ShelfController() { |
- // Create the app list item in the shelf. |
- AppListShelfItemDelegate::CreateAppListItemAndDelegate(&model_); |
+ // Create an AppListShelfItemDelegate for the app list item. |
+ model_.SetShelfItemDelegate(ShelfID(kAppListId), |
+ base::MakeUnique<AppListShelfItemDelegate>()); |
+ model_.AddObserver(this); |
} |
-ShelfController::~ShelfController() {} |
+ShelfController::~ShelfController() { |
+ model_.RemoveObserver(this); |
+} |
void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { |
bindings_.AddBinding(this, std::move(request)); |
@@ -76,6 +82,22 @@ void ShelfController::AddObserver( |
mojom::ShelfObserverAssociatedPtrInfo observer) { |
mojom::ShelfObserverAssociatedPtr observer_ptr; |
observer_ptr.Bind(std::move(observer)); |
+ |
+ if (Shell::GetAshConfig() == Config::MASH) { |
+ // Mash synchronizes two ShelfModel instances, owned by Ash and Chrome. |
+ // Notify Chrome of existing ShelfModel items created by Ash. |
+ for (int i = 0; i < model_.item_count(); ++i) { |
+ const ShelfItem& item = model_.items()[i]; |
+ observer_ptr->OnShelfItemAdded(i, item); |
+ ShelfItemDelegate* delegate = model_.GetShelfItemDelegate(item.id); |
+ mojom::ShelfItemDelegatePtr delegate_ptr; |
+ if (delegate) |
+ delegate_ptr = delegate->CreateInterfacePtrAndBind(); |
+ observer_ptr->OnShelfItemDelegateChanged(item.id, |
+ std::move(delegate_ptr)); |
+ } |
+ } |
+ |
observers_.AddPtr(std::move(observer_ptr)); |
} |
@@ -99,19 +121,115 @@ void ShelfController::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, |
shelf->SetAutoHideBehavior(auto_hide); |
} |
-void ShelfController::PinItem( |
- const ShelfItem& item, |
- mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { |
- NOTIMPLEMENTED(); |
+void ShelfController::AddShelfItem(int32_t index, const ShelfItem& item) { |
+ DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
+ DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
+ DCHECK_GE(model_.item_count(), index) << "Models out of sync"; |
+ base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
+ model_.AddAt(index < 0 ? model_.item_count() : index, item); |
+} |
+ |
+void ShelfController::RemoveShelfItem(const ShelfID& id) { |
+ DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
+ DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
+ const int index = model_.ItemIndexByID(id); |
+ DCHECK_GE(index, 0) << "Item not found; models out of sync"; |
+ base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
+ model_.RemoveItemAt(index); |
+} |
+ |
+void ShelfController::MoveShelfItem(const ShelfID& id, int32_t target_index) { |
+ DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
+ DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
+ const int index = model_.ItemIndexByID(id); |
+ DCHECK_GE(index, 0) << "Item not found; models out of sync"; |
+ DCHECK_NE(index, target_index) << "Item already at index; models out of sync"; |
+ DCHECK_GT(model_.item_count(), target_index) << "Models out of sync"; |
+ base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
+ model_.Move(index, target_index); |
} |
-void ShelfController::UnpinItem(const std::string& app_id) { |
- NOTIMPLEMENTED(); |
+void ShelfController::UpdateShelfItem(const ShelfItem& item) { |
+ DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
+ DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
+ const int index = model_.ItemIndexByID(item.id); |
+ DCHECK_GE(index, 0) << "Item not found; models out of sync"; |
+ base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
+ model_.Set(index, item); |
} |
-void ShelfController::SetItemImage(const std::string& app_id, |
- const SkBitmap& image) { |
- NOTIMPLEMENTED(); |
+void ShelfController::SetShelfItemDelegate( |
+ const ShelfID& id, |
+ mojom::ShelfItemDelegatePtr delegate) { |
+ DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << "Unexpected model sync"; |
+ DCHECK(!applying_remote_shelf_model_changes_) << "Unexpected model change"; |
+ base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
+ if (delegate.is_bound()) |
+ model_.SetShelfItemDelegate( |
+ id, base::MakeUnique<RemoteShelfItemDelegate>(id, std::move(delegate))); |
+ else |
+ model_.SetShelfItemDelegate(id, nullptr); |
+} |
+ |
+void ShelfController::ShelfItemAdded(int index) { |
+ if (applying_remote_shelf_model_changes_ || |
+ Shell::GetAshConfig() != Config::MASH) { |
+ return; |
+ } |
+ |
+ const ShelfItem& item = model_.items()[index]; |
+ observers_.ForAllPtrs([index, item](mojom::ShelfObserver* observer) { |
+ observer->OnShelfItemAdded(index, item); |
+ }); |
+} |
+ |
+void ShelfController::ShelfItemRemoved(int index, const ShelfItem& old_item) { |
+ if (applying_remote_shelf_model_changes_ || |
+ Shell::GetAshConfig() != Config::MASH) { |
+ return; |
+ } |
+ |
+ observers_.ForAllPtrs([index, old_item](mojom::ShelfObserver* observer) { |
+ observer->OnShelfItemRemoved(index, old_item); |
+ }); |
+} |
+ |
+void ShelfController::ShelfItemMoved(int start_index, int target_index) { |
+ if (applying_remote_shelf_model_changes_ || |
+ Shell::GetAshConfig() != Config::MASH) { |
+ return; |
+ } |
+ |
+ observers_.ForAllPtrs( |
+ [start_index, target_index](mojom::ShelfObserver* observer) { |
+ observer->OnShelfItemMoved(start_index, target_index); |
+ }); |
+} |
+ |
+void ShelfController::ShelfItemChanged(int index, const ShelfItem& old_item) { |
+ if (applying_remote_shelf_model_changes_ || |
+ Shell::GetAshConfig() != Config::MASH) { |
+ return; |
+ } |
+ |
+ const ShelfItem& item = model_.items()[index]; |
+ observers_.ForAllPtrs([index, item](mojom::ShelfObserver* observer) { |
+ observer->OnShelfItemChanged(index, item); |
+ }); |
+} |
+ |
+void ShelfController::ShelfItemDelegateChanged(const ShelfID& id, |
+ ShelfItemDelegate* delegate) { |
+ if (applying_remote_shelf_model_changes_ || |
+ Shell::GetAshConfig() != Config::MASH) { |
+ return; |
+ } |
+ |
+ observers_.ForAllPtrs([id, delegate](mojom::ShelfObserver* observer) { |
+ observer->OnShelfItemDelegateChanged( |
+ id, delegate ? delegate->CreateInterfacePtrAndBind() |
+ : mojom::ShelfItemDelegatePtr()); |
+ }); |
} |
} // namespace ash |