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
|
// Copyright 2023 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/extensions/api/reading_list/reading_list_api.h"
#include "base/containers/flat_set.h"
#include "base/location.h"
#include "base/time/time.h"
#include "chrome/browser/extensions/api/reading_list/reading_list_api_constants.h"
#include "chrome/browser/extensions/api/reading_list/reading_list_util.h"
#include "chrome/browser/reading_list/reading_list_model_factory.h"
#include "chrome/common/extensions/api/reading_list.h"
#include "components/reading_list/core/reading_list_entry.h"
#include "components/reading_list/core/reading_list_model.h"
#include "components/reading_list/core/reading_list_model_observer.h"
#include "extensions/browser/extension_function.h"
#include "url/gurl.h"
namespace extensions {
//////////////////////////////////////////////////////////////////////////////
/////////////////////// ReadingListAddEntryFunction //////////////////////////
//////////////////////////////////////////////////////////////////////////////
ReadingListAddEntryFunction::ReadingListAddEntryFunction() = default;
ReadingListAddEntryFunction::~ReadingListAddEntryFunction() = default;
ExtensionFunction::ResponseAction ReadingListAddEntryFunction::Run() {
auto params = api::reading_list::AddEntry::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
title_ = std::move(params->entry.title);
url_ = GURL(params->entry.url);
has_been_read_ = params->entry.has_been_read;
if (!url_.is_valid()) {
return RespondNow(Error(reading_list_api_constants::kInvalidURLError));
}
reading_list_model_ =
ReadingListModelFactory::GetForBrowserContext(browser_context());
if (!reading_list_model_->loaded()) {
reading_list_observation_.Observe(reading_list_model_);
AddRef();
return RespondLater();
}
auto response = AddEntryToReadingList();
return RespondNow(std::move(response));
}
void ReadingListAddEntryFunction::ReadingListModelLoaded(
const ReadingListModel* model) {
reading_list_observation_.Reset();
auto response = AddEntryToReadingList();
Respond(std::move(response));
Release(); // Balanced in Run().
}
ExtensionFunction::ResponseValue
ReadingListAddEntryFunction::AddEntryToReadingList() {
if (!reading_list_model_->IsUrlSupported(url_)) {
return Error(reading_list_api_constants::kNotSupportedURLError);
}
if (reading_list_model_->GetEntryByURL(url_)) {
return Error(reading_list_api_constants::kDuplicateURLError);
}
reading_list_model_->AddOrReplaceEntry(
url_, title_, reading_list::EntrySource::ADDED_VIA_EXTENSION,
/*estimated_read_time=*/base::TimeDelta());
reading_list_model_->SetReadStatusIfExists(url_, has_been_read_);
return NoArguments();
}
//////////////////////////////////////////////////////////////////////////////
///////////////////// ReadingListRemoveEntryFunction /////////////////////////
//////////////////////////////////////////////////////////////////////////////
ReadingListRemoveEntryFunction::ReadingListRemoveEntryFunction() = default;
ReadingListRemoveEntryFunction::~ReadingListRemoveEntryFunction() = default;
ExtensionFunction::ResponseAction ReadingListRemoveEntryFunction::Run() {
auto params = api::reading_list::RemoveEntry::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
url_ = GURL(params->info.url);
if (!url_.is_valid()) {
return RespondNow(Error(reading_list_api_constants::kInvalidURLError));
}
reading_list_model_ =
ReadingListModelFactory::GetForBrowserContext(browser_context());
if (!reading_list_model_->loaded()) {
reading_list_observation_.Observe(reading_list_model_);
AddRef();
return RespondLater();
}
auto response = RemoveEntryFromReadingList();
return RespondNow(std::move(response));
}
void ReadingListRemoveEntryFunction::ReadingListModelLoaded(
const ReadingListModel* model) {
reading_list_observation_.Reset();
auto response = RemoveEntryFromReadingList();
Respond(std::move(response));
Release(); // Balanced in Run().
}
ExtensionFunction::ResponseValue
ReadingListRemoveEntryFunction::RemoveEntryFromReadingList() {
if (!reading_list_model_->IsUrlSupported(url_)) {
return Error(reading_list_api_constants::kNotSupportedURLError);
}
if (!reading_list_model_->GetEntryByURL(url_)) {
return Error(reading_list_api_constants::kURLNotFoundError);
}
reading_list_model_->RemoveEntryByURL(url_, FROM_HERE);
return NoArguments();
}
//////////////////////////////////////////////////////////////////////////////
///////////////////// ReadingListUpdateEntryFunction /////////////////////////
//////////////////////////////////////////////////////////////////////////////
ReadingListUpdateEntryFunction::ReadingListUpdateEntryFunction() = default;
ReadingListUpdateEntryFunction::~ReadingListUpdateEntryFunction() = default;
ExtensionFunction::ResponseAction ReadingListUpdateEntryFunction::Run() {
auto params = api::reading_list::UpdateEntry::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
title_ = params->info.title;
has_been_read_ = params->info.has_been_read;
if (!title_.has_value() && !has_been_read_.has_value()) {
return RespondNow(Error(reading_list_api_constants::kNoUpdateProvided));
}
url_ = GURL(params->info.url);
if (!url_.is_valid()) {
return RespondNow(Error(reading_list_api_constants::kInvalidURLError));
}
reading_list_model_ =
ReadingListModelFactory::GetForBrowserContext(browser_context());
if (!reading_list_model_->loaded()) {
reading_list_observation_.Observe(reading_list_model_);
AddRef();
return RespondLater();
}
auto response = UpdateEntriesInTheReadingList();
return RespondNow(std::move(response));
}
void ReadingListUpdateEntryFunction::ReadingListModelLoaded(
const ReadingListModel* model) {
reading_list_observation_.Reset();
auto response = UpdateEntriesInTheReadingList();
Respond(std::move(response));
Release(); // Balanced in Run().
}
ExtensionFunction::ResponseValue
ReadingListUpdateEntryFunction::UpdateEntriesInTheReadingList() {
if (!reading_list_model_->IsUrlSupported(url_)) {
return Error(reading_list_api_constants::kNotSupportedURLError);
}
if (!reading_list_model_->GetEntryByURL(url_)) {
return Error(reading_list_api_constants::kURLNotFoundError);
}
if (title_.has_value()) {
reading_list_model_->SetEntryTitleIfExists(url_, title_.value());
}
if (has_been_read_.has_value()) {
reading_list_model_->SetReadStatusIfExists(url_, has_been_read_.value());
}
return NoArguments();
}
//////////////////////////////////////////////////////////////////////////////
///////////////////////// ReadingListQueryFunction ///////////////////////////
//////////////////////////////////////////////////////////////////////////////
ReadingListQueryFunction::ReadingListQueryFunction() = default;
ReadingListQueryFunction::~ReadingListQueryFunction() = default;
ExtensionFunction::ResponseAction ReadingListQueryFunction::Run() {
auto params = api::reading_list::Query::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
if (params->info.url.has_value()) {
url_ = GURL(params->info.url.value());
if (!url_->is_valid()) {
return RespondNow(Error(reading_list_api_constants::kInvalidURLError));
}
}
title_ = params->info.title;
has_been_read_ = params->info.has_been_read;
reading_list_model_ =
ReadingListModelFactory::GetForBrowserContext(browser_context());
if (!reading_list_model_->loaded()) {
reading_list_observation_.Observe(reading_list_model_);
AddRef();
return RespondLater();
}
auto response = MatchEntries();
return RespondNow(std::move(response));
}
void ReadingListQueryFunction::ReadingListModelLoaded(
const ReadingListModel* model) {
reading_list_observation_.Reset();
auto response = MatchEntries();
Respond(std::move(response));
Release(); // Balanced in Run().
}
ExtensionFunction::ResponseValue ReadingListQueryFunction::MatchEntries() {
if (url_.has_value() && !reading_list_model_->IsUrlSupported(url_.value())) {
return Error(reading_list_api_constants::kNotSupportedURLError);
}
base::flat_set<GURL> urls = reading_list_model_->GetKeys();
std::vector<api::reading_list::ReadingListEntry> matching_entries;
for (const auto& url : urls) {
scoped_refptr<const ReadingListEntry> entry =
reading_list_model_->GetEntryByURL(url);
if (url_.has_value() && entry->URL() != url_) {
continue;
}
if (title_.has_value() && entry->Title() != title_) {
continue;
}
if (has_been_read_.has_value() && entry->IsRead() != has_been_read_) {
continue;
}
matching_entries.emplace_back(reading_list_util::ParseEntry(*entry));
}
return ArgumentList(
api::reading_list::Query::Results::Create(std::move(matching_entries)));
}
} // namespace extensions
|