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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
// Copyright 2022 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/ash/holding_space/holding_space_suggestions_delegate.h"
#include <algorithm>
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/holding_space/holding_space_file.h"
#include "base/containers/adapters.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/file_suggest/file_suggest_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
namespace ash {
namespace {
// Returns the holding space item type that matches a given suggestion type.
HoldingSpaceItem::Type GetItemTypeFromSuggestionType(
FileSuggestionType suggestion_type) {
switch (suggestion_type) {
case FileSuggestionType::kDriveFile:
return HoldingSpaceItem::Type::kDriveSuggestion;
case FileSuggestionType::kLocalFile:
return HoldingSpaceItem::Type::kLocalSuggestion;
}
}
// Returns whether `item` represents a pinned file that also exists as a
// suggested file in `suggestions_by_type`.
bool ItemIsPinnedSuggestion(
const HoldingSpaceItem* item,
std::map<HoldingSpaceItem::Type, std::vector<base::FilePath>>&
suggestions_by_type) {
if (item->type() != HoldingSpaceItem::Type::kPinnedFile) {
return false;
}
for (const auto& [_, suggested_file_paths] : suggestions_by_type) {
if (base::Contains(suggested_file_paths, item->file().file_path)) {
return true;
}
}
return false;
}
} // namespace
HoldingSpaceSuggestionsDelegate::HoldingSpaceSuggestionsDelegate(
HoldingSpaceKeyedService* service,
HoldingSpaceModel* model)
: HoldingSpaceKeyedServiceDelegate(service, model) {
DCHECK(features::IsHoldingSpaceSuggestionsEnabled());
}
HoldingSpaceSuggestionsDelegate::~HoldingSpaceSuggestionsDelegate() = default;
void HoldingSpaceSuggestionsDelegate::RefreshSuggestions() {
MaybeFetchSuggestions(FileSuggestionType::kDriveFile);
MaybeFetchSuggestions(FileSuggestionType::kLocalFile);
}
void HoldingSpaceSuggestionsDelegate::RemoveSuggestions(
const std::vector<base::FilePath>& absolute_file_paths) {
FileSuggestKeyedServiceFactory::GetInstance()
->GetService(profile())
->RemoveSuggestionsAndNotify(absolute_file_paths);
}
void HoldingSpaceSuggestionsDelegate::OnHoldingSpaceItemsAdded(
const std::vector<const HoldingSpaceItem*>& items) {
if (std::ranges::any_of(items, [&](const HoldingSpaceItem* item) {
return item->IsInitialized() &&
ItemIsPinnedSuggestion(item, suggestions_by_type_);
})) {
// Update suggestions asynchronously to avoid updating suggestions along
// with other model updates.
MaybeScheduleUpdateSuggestionsInModel();
}
}
void HoldingSpaceSuggestionsDelegate::OnHoldingSpaceItemsRemoved(
const std::vector<const HoldingSpaceItem*>& items) {
if (std::ranges::any_of(items, [&](const HoldingSpaceItem* item) {
return item->IsInitialized() &&
ItemIsPinnedSuggestion(item, suggestions_by_type_);
})) {
// Update suggestions asynchronously to avoid updating suggestions along
// with other model updates.
MaybeScheduleUpdateSuggestionsInModel();
}
}
void HoldingSpaceSuggestionsDelegate::OnHoldingSpaceItemInitialized(
const HoldingSpaceItem* item) {
if (ItemIsPinnedSuggestion(item, suggestions_by_type_)) {
// Update suggestions asynchronously to avoid updating suggestions along
// with other model updates.
MaybeScheduleUpdateSuggestionsInModel();
}
}
void HoldingSpaceSuggestionsDelegate::OnPersistenceRestored() {
// Initialize `suggestions_by_type_` with the restored suggestions. The model
// items are iterated reversely so that the suggestions of the same category
// in `suggestions_by_type_` follow the relevance order.
DCHECK(suggestions_by_type_.empty());
for (const auto& item : base::Reversed(model()->items())) {
// Skip if `item` is not a suggestion.
if (HoldingSpaceItem::IsSuggestionType(item->type())) {
suggestions_by_type_[item->type()].push_back(item->file().file_path);
}
}
file_suggest_service_observation_.Observe(
FileSuggestKeyedServiceFactory::GetInstance()->GetService(profile()));
MaybeFetchSuggestions(FileSuggestionType::kDriveFile);
MaybeFetchSuggestions(FileSuggestionType::kLocalFile);
}
void HoldingSpaceSuggestionsDelegate::OnFileSuggestionUpdated(
FileSuggestionType type) {
MaybeFetchSuggestions(type);
}
void HoldingSpaceSuggestionsDelegate::MaybeFetchSuggestions(
FileSuggestionType type) {
// A data query on `type` has been sent so it is unnecessary to send a request
// again. Return early.
if (base::Contains(pending_fetches_, type)) {
return;
}
// Mark that the query for suggestions of `type` has been sent.
pending_fetches_.insert(type);
FileSuggestKeyedServiceFactory::GetInstance()
->GetService(profile())
->GetSuggestFileData(
type,
base::BindOnce(&HoldingSpaceSuggestionsDelegate::OnSuggestionsFetched,
weak_factory_.GetWeakPtr(), type));
}
void HoldingSpaceSuggestionsDelegate::MaybeScheduleUpdateSuggestionsInModel() {
// Return early if the task of updating model suggestions has been scheduled.
if (suggestion_update_timer_.IsRunning()) {
return;
}
suggestion_update_timer_.Start(
FROM_HERE, /*delay=*/base::TimeDelta(),
base::BindOnce(&HoldingSpaceSuggestionsDelegate::UpdateSuggestionsInModel,
weak_factory_.GetWeakPtr()));
}
void HoldingSpaceSuggestionsDelegate::OnSuggestionsFetched(
FileSuggestionType type,
const std::optional<std::vector<FileSuggestData>>& suggestions) {
// Mark that the suggestions of `type` have been fetched.
size_t deleted_size = pending_fetches_.erase(type);
DCHECK_EQ(1u, deleted_size);
if (!suggestions) {
return;
}
// Extract file paths from `suggestions`.
std::vector<base::FilePath> updated_suggestions(suggestions->size());
std::ranges::transform(*suggestions, updated_suggestions.begin(),
&FileSuggestData::file_path);
// No-op if `updated_suggestions` are unchanged.
const HoldingSpaceItem::Type item_type = GetItemTypeFromSuggestionType(type);
if (auto it = suggestions_by_type_.find(item_type);
it != suggestions_by_type_.end() && it->second == updated_suggestions) {
return;
}
// Update cache and model.
suggestions_by_type_[item_type] = std::move(updated_suggestions);
UpdateSuggestionsInModel();
}
void HoldingSpaceSuggestionsDelegate::UpdateSuggestionsInModel() {
std::vector<std::pair<HoldingSpaceItem::Type, base::FilePath>>
suggestion_items;
base::FilePath downloads_folder =
file_manager::util::GetDownloadsFolderForProfile(profile());
for (const auto& [type, suggested_file_paths] : suggestions_by_type_) {
for (const auto& file_path : suggested_file_paths) {
if (file_path != downloads_folder &&
!model()->ContainsItem(HoldingSpaceItem::Type::kPinnedFile,
file_path)) {
suggestion_items.emplace_back(type, file_path);
}
}
}
service()->SetSuggestions(suggestion_items);
}
} // namespace ash
|