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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
// 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 "components/ntp_tiles/custom_links_manager_impl.h"
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/auto_reset.h"
#include "base/functional/bind.h"
#include "components/ntp_tiles/constants.h"
#include "components/ntp_tiles/deleted_tile_type.h"
#include "components/ntp_tiles/metrics.h"
#include "components/ntp_tiles/most_visited_sites.h"
#include "components/ntp_tiles/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
namespace ntp_tiles {
CustomLinksManagerImpl::CustomLinksManagerImpl(
PrefService* prefs,
history::HistoryService* history_service)
: prefs_(prefs), store_(prefs) {
DCHECK(prefs);
if (history_service) {
history_service_observation_.Observe(history_service);
}
if (IsInitialized()) {
current_links_ = store_.RetrieveLinks();
RemoveCustomLinksForPreinstalledApps();
}
base::RepeatingClosure callback =
base::BindRepeating(&CustomLinksManagerImpl::OnPreferenceChanged,
weak_ptr_factory_.GetWeakPtr());
pref_change_registrar_.Init(prefs_);
pref_change_registrar_.Add(prefs::kCustomLinksInitialized, callback);
pref_change_registrar_.Add(prefs::kCustomLinksList, callback);
}
CustomLinksManagerImpl::~CustomLinksManagerImpl() = default;
bool CustomLinksManagerImpl::Initialize(const NTPTilesVector& tiles) {
if (IsInitialized()) {
return false;
}
for (const NTPTile& tile : tiles) {
current_links_.emplace_back(Link{tile.url, tile.title, true});
}
{
base::AutoReset<bool> auto_reset(&updating_preferences_, true);
prefs_->SetBoolean(prefs::kCustomLinksInitialized, true);
}
StoreLinks();
return true;
}
void CustomLinksManagerImpl::Uninitialize() {
{
base::AutoReset<bool> auto_reset(&updating_preferences_, true);
prefs_->SetBoolean(prefs::kCustomLinksInitialized, false);
}
ClearLinks();
}
bool CustomLinksManagerImpl::IsInitialized() const {
return prefs_->GetBoolean(prefs::kCustomLinksInitialized);
}
const std::vector<CustomLinksManager::Link>& CustomLinksManagerImpl::GetLinks()
const {
return current_links_;
}
bool CustomLinksManagerImpl::AddLinkTo(const GURL& url,
const std::u16string& title,
size_t pos) {
if (!IsInitialized() || !url.is_valid() ||
current_links_.size() == ntp_tiles::kMaxNumCustomLinks) {
return false;
}
if (FindLinkWithUrl(url) != current_links_.end()) {
return false;
}
pos = std::min(pos, current_links_.size());
previous_links_ = current_links_;
current_links_.insert(current_links_.begin() + pos, Link{url, title, false});
StoreLinks();
return true;
}
bool CustomLinksManagerImpl::AddLink(const GURL& url,
const std::u16string& title) {
return AddLinkTo(url, title, current_links_.size());
}
bool CustomLinksManagerImpl::UpdateLink(const GURL& url,
const GURL& new_url,
const std::u16string& new_title) {
if (!IsInitialized() || !url.is_valid() ||
(new_url.is_empty() && new_title.empty())) {
return false;
}
// Do not update if |new_url| is invalid or already exists in the list.
if (!new_url.is_empty() &&
(!new_url.is_valid() ||
FindLinkWithUrl(new_url) != current_links_.end())) {
return false;
}
auto it = FindLinkWithUrl(url);
if (it == current_links_.end()) {
return false;
}
// At this point, we will be modifying at least one of the values.
previous_links_ = current_links_;
if (!new_url.is_empty()) {
it->url = new_url;
}
if (!new_title.empty()) {
it->title = new_title;
}
it->is_most_visited = false;
StoreLinks();
return true;
}
bool CustomLinksManagerImpl::ReorderLink(const GURL& url, size_t new_pos) {
if (!IsInitialized() || !url.is_valid() || new_pos < 0 ||
new_pos >= current_links_.size()) {
return false;
}
auto curr_it = FindLinkWithUrl(url);
if (curr_it == current_links_.end()) {
return false;
}
auto new_it = current_links_.begin() + new_pos;
if (new_it == curr_it) {
return false;
}
previous_links_ = current_links_;
// If the new position is to the left of the current position, left rotate the
// range [new_pos, curr_pos] until the link is first.
if (new_it < curr_it) {
std::rotate(new_it, curr_it, curr_it + 1);
}
// If the new position is to the right, we only need to left rotate the range
// [curr_pos, new_pos] once so that the link is last.
else {
std::rotate(curr_it, curr_it + 1, new_it + 1);
}
StoreLinks();
return true;
}
bool CustomLinksManagerImpl::DeleteLink(const GURL& url) {
if (!IsInitialized() || !url.is_valid()) {
return false;
}
auto it = FindLinkWithUrl(url);
if (it == current_links_.end()) {
return false;
}
previous_links_ = current_links_;
current_links_.erase(it);
StoreLinks();
return true;
}
bool CustomLinksManagerImpl::UndoAction() {
if (!IsInitialized() || !previous_links_.has_value()) {
return false;
}
// Replace the current links with the previous state.
current_links_ = *previous_links_;
previous_links_ = std::nullopt;
StoreLinks();
return true;
}
void CustomLinksManagerImpl::ClearLinks() {
{
base::AutoReset<bool> auto_reset(&updating_preferences_, true);
store_.ClearLinks();
}
current_links_.clear();
previous_links_ = std::nullopt;
}
void CustomLinksManagerImpl::StoreLinks() {
base::AutoReset<bool> auto_reset(&updating_preferences_, true);
store_.StoreLinks(current_links_);
}
void CustomLinksManagerImpl::RemoveCustomLinksForPreinstalledApps() {
if (!prefs_->GetBoolean(prefs::kCustomLinksForPreinstalledAppsRemoved)) {
bool default_app_links_deleted = false;
for (const Link& link : current_links_) {
if (MostVisitedSites::IsNtpTileFromPreinstalledApp(link.url) &&
MostVisitedSites::WasNtpAppMigratedToWebApp(prefs_, link.url)) {
DeleteLink(link.url);
default_app_links_deleted = true;
}
}
if (default_app_links_deleted) {
metrics::RecordsMigratedDefaultAppDeleted(DeletedTileType::kCustomLink);
prefs_->SetBoolean(prefs::kCustomLinksForPreinstalledAppsRemoved, true);
}
}
}
std::vector<CustomLinksManager::Link>::iterator
CustomLinksManagerImpl::FindLinkWithUrl(const GURL& url) {
return std::ranges::find(current_links_, url, &Link::url);
}
base::CallbackListSubscription
CustomLinksManagerImpl::RegisterCallbackForOnChanged(
base::RepeatingClosure callback) {
return closure_list_.Add(callback);
}
// history::HistoryServiceObserver implementation.
void CustomLinksManagerImpl::OnHistoryDeletions(
history::HistoryService* history_service,
const history::DeletionInfo& deletion_info) {
// We don't care about expired entries.
if (!IsInitialized() || deletion_info.is_from_expiration()) {
return;
}
size_t initial_size = current_links_.size();
if (deletion_info.IsAllHistory()) {
std::erase_if(current_links_,
[](auto& link) { return link.is_most_visited; });
} else {
for (const history::URLRow& row : deletion_info.deleted_rows()) {
auto it = FindLinkWithUrl(row.url());
if (it != current_links_.end() && it->is_most_visited) {
current_links_.erase(it);
}
}
}
StoreLinks();
previous_links_ = std::nullopt;
// Alert MostVisitedSites that some links have been deleted.
if (initial_size != current_links_.size()) {
closure_list_.Notify();
}
}
void CustomLinksManagerImpl::HistoryServiceBeingDeleted(
history::HistoryService* history_service) {
DCHECK(history_service_observation_.IsObserving());
history_service_observation_.Reset();
}
void CustomLinksManagerImpl::OnPreferenceChanged() {
if (updating_preferences_) {
return;
}
if (IsInitialized()) {
current_links_ = store_.RetrieveLinks();
} else {
current_links_.clear();
}
previous_links_ = std::nullopt;
closure_list_.Notify();
}
// static
void CustomLinksManagerImpl::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* user_prefs) {
user_prefs->RegisterBooleanPref(
prefs::kCustomLinksInitialized, false,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
user_prefs->RegisterBooleanPref(prefs::kCustomLinksForPreinstalledAppsRemoved,
false);
CustomLinksStore::RegisterProfilePrefs(user_prefs);
}
} // namespace ntp_tiles
|