1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/app_management/app_management_shelf_delegate_chromeos.h"
#include "ash/public/cpp/shelf_item.h"
#include "ash/public/cpp/shelf_model.h"
#include "ash/public/cpp/shelf_types.h"
#include "base/containers/contains.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/shelf/app_shortcut_shelf_item_controller.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller_util.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_prefs.h"
#include "chrome/browser/ui/ash/shelf/shelf_controller_helper.h"
#include "chrome/browser/ui/webui/app_management/app_management_page_handler_chromeos.h"
AppManagementShelfDelegate::AppManagementShelfDelegate(
AppManagementPageHandlerChromeOs* page_handler,
Profile* profile)
: page_handler_(page_handler),
shelf_controller_helper_(
std::make_unique<ShelfControllerHelper>(profile)) {
auto* shelf_controller = ChromeShelfController::instance();
if (!shelf_controller) {
return;
}
auto* shelf_model = shelf_controller->shelf_model();
if (!shelf_model) {
return;
}
shelf_model->AddObserver(this);
}
AppManagementShelfDelegate::~AppManagementShelfDelegate() {
auto* shelf_controller = ChromeShelfController::instance();
if (!shelf_controller) {
return;
}
auto* shelf_model = shelf_controller->shelf_model();
if (!shelf_model) {
return;
}
shelf_model->RemoveObserver(this);
}
bool AppManagementShelfDelegate::IsPinned(const std::string& app_id) {
auto* shelf_controller = ChromeShelfController::instance();
if (!shelf_controller) {
return false;
}
return shelf_controller->IsAppPinned(app_id);
}
bool AppManagementShelfDelegate::IsPolicyPinned(
const std::string& app_id) const {
auto* shelf_controller = ChromeShelfController::instance();
if (!shelf_controller) {
return false;
}
auto* shelf_item = shelf_controller->GetItem(ash::ShelfID(app_id));
if (shelf_item) {
return shelf_item->pinned_by_policy;
}
// The app doesn't exist on the shelf - check launcher prefs instead.
std::vector<std::string> policy_pinned_apps =
ChromeShelfPrefs::GetAppsPinnedByPolicy(
shelf_controller_helper_->profile());
return base::Contains(policy_pinned_apps, app_id);
}
void AppManagementShelfDelegate::SetPinned(const std::string& app_id,
bool pinned) {
auto* shelf_controller = ChromeShelfController::instance();
if (!shelf_controller) {
return;
}
if (pinned) {
PinAppWithIDToShelf(app_id);
} else {
UnpinAppWithIDFromShelf(app_id);
}
}
void AppManagementShelfDelegate::ShelfItemAdded(int index) {
auto* shelf_controller = ChromeShelfController::instance();
if (!shelf_controller) {
return;
}
auto* shelf_model = shelf_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 = shelf_controller->IsAppPinned(app_id);
page_handler_->OnPinnedChanged(app_id, is_pinned);
}
void AppManagementShelfDelegate::ShelfItemRemoved(
int index,
const ash::ShelfItem& old_item) {
// If the app has been removed from the shelf model, it is not longer pinned.
page_handler_->OnPinnedChanged(old_item.id.app_id, false);
}
void AppManagementShelfDelegate::ShelfItemChanged(
int index,
const ash::ShelfItem& old_item) {
auto* shelf_controller = ChromeShelfController::instance();
if (!shelf_controller) {
return;
}
auto* shelf_model = shelf_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 = shelf_controller->IsAppPinned(app_id);
page_handler_->OnPinnedChanged(app_id, is_pinned);
}
|