Commit e986e08f authored by Jeevan Shikaram's avatar Jeevan Shikaram Committed by Commit Bot

[App Management] Add null checks to shelf delegate calls.

This CL adds null checks to shelf related pointers within the shelf
delegate. This prevents null pointer errors when the App Management page
handler is created when the shelf does not exist, e.g. on the lock screen.

Bug: 1011756
Change-Id: I97d3bee4daeba9a97c89ba4a91c3f736857df964
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1858022
Commit-Queue: Jeevan Shikaram <jshikaram@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#705745}
parent 720b69cb
......@@ -17,21 +17,51 @@ using apps::mojom::OptionalBool;
AppManagementShelfDelegate::AppManagementShelfDelegate(
AppManagementPageHandler* page_handler)
: page_handler_(page_handler) {
ChromeLauncherController::instance()->shelf_model()->AddObserver(this);
auto* launcher_controller = ChromeLauncherController::instance();
if (!launcher_controller) {
return;
}
auto* shelf_model = launcher_controller->shelf_model();
if (!shelf_model) {
return;
}
shelf_model->AddObserver(this);
}
AppManagementShelfDelegate::~AppManagementShelfDelegate() {
ChromeLauncherController::instance()->shelf_model()->RemoveObserver(this);
auto* launcher_controller = ChromeLauncherController::instance();
if (!launcher_controller) {
return;
}
auto* shelf_model = launcher_controller->shelf_model();
if (!shelf_model) {
return;
}
shelf_model->RemoveObserver(this);
}
bool AppManagementShelfDelegate::IsPinned(const std::string& app_id) {
return ChromeLauncherController::instance()->IsAppPinned(app_id);
auto* launcher_controller = ChromeLauncherController::instance();
if (!launcher_controller) {
return false;
}
return launcher_controller->IsAppPinned(app_id);
}
bool AppManagementShelfDelegate::IsPolicyPinned(
const std::string& app_id) const {
auto* shelf_item =
ChromeLauncherController::instance()->GetItem(ash::ShelfID(app_id));
auto* launcher_controller = ChromeLauncherController::instance();
if (!launcher_controller) {
return false;
}
auto* shelf_item = launcher_controller->GetItem(ash::ShelfID(app_id));
// If the app does not exist on the launcher, it has not been pinned by
// policy.
return shelf_item && shelf_item->pinned_by_policy;
......@@ -39,21 +69,39 @@ bool AppManagementShelfDelegate::IsPolicyPinned(
void AppManagementShelfDelegate::SetPinned(const std::string& app_id,
OptionalBool pinned) {
auto* launcher_controller = ChromeLauncherController::instance();
if (!launcher_controller) {
return;
}
if (pinned == OptionalBool::kTrue) {
ChromeLauncherController::instance()->PinAppWithID(app_id);
launcher_controller->PinAppWithID(app_id);
} else if (pinned == OptionalBool::kFalse) {
ChromeLauncherController::instance()->UnpinAppWithID(app_id);
launcher_controller->UnpinAppWithID(app_id);
} else {
NOTREACHED();
}
}
void AppManagementShelfDelegate::ShelfItemAdded(int index) {
const std::string& app_id = ChromeLauncherController::instance()
->shelf_model()
->items()[index]
.id.app_id;
bool is_pinned = ChromeLauncherController::instance()->IsAppPinned(app_id);
auto* launcher_controller = ChromeLauncherController::instance();
if (!launcher_controller) {
return;
}
auto* shelf_model = launcher_controller->shelf_model();
if (!shelf_model) {
return;
}
if (index >= shelf_model->item_count()) {
// index out of bounds.
return;
}
const std::string& app_id = shelf_model->items()[index].id.app_id;
bool is_pinned = launcher_controller->IsAppPinned(app_id);
page_handler_->OnPinnedChanged(app_id, is_pinned);
}
......@@ -68,11 +116,23 @@ void AppManagementShelfDelegate::ShelfItemRemoved(
void AppManagementShelfDelegate::ShelfItemChanged(
int index,
const ash::ShelfItem& old_item) {
const std::string& app_id = ChromeLauncherController::instance()
->shelf_model()
->items()[index]
.id.app_id;
bool is_pinned = ChromeLauncherController::instance()->IsAppPinned(app_id);
auto* launcher_controller = ChromeLauncherController::instance();
if (!launcher_controller) {
return;
}
auto* shelf_model = launcher_controller->shelf_model();
if (!shelf_model) {
return;
}
if (index >= shelf_model->item_count()) {
// index out of bounds.
return;
}
const std::string& app_id = shelf_model->items()[index].id.app_id;
bool is_pinned = launcher_controller->IsAppPinned(app_id);
page_handler_->OnPinnedChanged(app_id, is_pinned);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment