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
|
// Copyright 2013 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/dom_distiller/core/task_tracker.h"
#include <stddef.h>
#include <memory>
#include <utility>
#include "base/auto_reset.h"
#include "base/location.h"
#include "base/observer_list.h"
#include "base/task/single_thread_task_runner.h"
#include "components/dom_distiller/core/distilled_content_store.h"
#include "components/dom_distiller/core/proto/distilled_article.pb.h"
#include "components/dom_distiller/core/proto/distilled_page.pb.h"
namespace dom_distiller {
ViewerHandle::ViewerHandle(CancelCallback callback)
: cancel_callback_(std::move(callback)) {}
ViewerHandle::~ViewerHandle() {
if (!cancel_callback_.is_null()) {
std::move(cancel_callback_).Run();
}
}
TaskTracker::TaskTracker(const ArticleEntry& entry,
CancelCallback callback,
DistilledContentStore* content_store)
: cancel_callback_(std::move(callback)),
content_store_(content_store),
blob_fetcher_running_(false),
entry_(entry),
distilled_article_(),
content_ready_(false),
destruction_allowed_(true) {}
TaskTracker::~TaskTracker() {
DCHECK(destruction_allowed_);
DCHECK(viewers_.empty());
}
void TaskTracker::StartDistiller(
DistillerFactory* factory,
std::unique_ptr<DistillerPage> distiller_page) {
if (distiller_) {
return;
}
if (entry_.pages.empty()) {
return;
}
GURL url(entry_.pages[0]);
DCHECK(url.is_valid());
distiller_ = factory->CreateDistiller();
distiller_->DistillPage(
url, std::move(distiller_page),
base::BindOnce(&TaskTracker::OnDistillerFinished,
weak_ptr_factory_.GetWeakPtr()),
base::BindRepeating(&TaskTracker::OnArticleDistillationUpdated,
weak_ptr_factory_.GetWeakPtr()));
}
void TaskTracker::StartBlobFetcher() {
if (content_store_) {
blob_fetcher_running_ = true;
content_store_->LoadContent(entry_,
base::BindOnce(&TaskTracker::OnBlobFetched,
weak_ptr_factory_.GetWeakPtr()));
}
}
void TaskTracker::AddSaveCallback(SaveCallback callback) {
DCHECK(!callback.is_null());
save_callbacks_.push_back(std::move(callback));
if (content_ready_) {
// Distillation for this task has already completed, and so it can be
// immediately saved.
ScheduleSaveCallbacks(true);
}
}
std::unique_ptr<ViewerHandle> TaskTracker::AddViewer(
ViewRequestDelegate* delegate) {
viewers_.AddObserver(delegate);
if (content_ready_) {
// Distillation for this task has already completed, and so the delegate can
// be immediately told of the result.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&TaskTracker::NotifyViewer,
weak_ptr_factory_.GetWeakPtr(), delegate));
}
return std::make_unique<ViewerHandle>(base::BindOnce(
&TaskTracker::RemoveViewer, weak_ptr_factory_.GetWeakPtr(), delegate));
}
const std::string& TaskTracker::GetEntryId() const {
return entry_.entry_id;
}
bool TaskTracker::HasEntryId(const std::string& entry_id) const {
return entry_.entry_id == entry_id;
}
bool TaskTracker::HasUrl(const GURL& url) const {
for (const GURL& page : entry_.pages) {
if (page == url) {
return true;
}
}
return false;
}
void TaskTracker::RemoveViewer(ViewRequestDelegate* delegate) {
viewers_.RemoveObserver(delegate);
if (viewers_.empty()) {
MaybeCancel();
}
}
void TaskTracker::MaybeCancel() {
if (!save_callbacks_.empty() || !viewers_.empty()) {
// There's still work to be done.
return;
}
CancelPendingSources();
base::AutoReset<bool> dont_delete_this_in_callback(&destruction_allowed_,
false);
std::move(cancel_callback_).Run(this);
}
void TaskTracker::CancelSaveCallbacks() {
ScheduleSaveCallbacks(false);
}
void TaskTracker::ScheduleSaveCallbacks(bool distillation_succeeded) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&TaskTracker::DoSaveCallbacks,
weak_ptr_factory_.GetWeakPtr(), distillation_succeeded));
}
void TaskTracker::OnDistillerFinished(
std::unique_ptr<DistilledArticleProto> distilled_article) {
if (content_ready_) {
return;
}
DistilledArticleReady(std::move(distilled_article));
if (content_ready_) {
AddDistilledContentToStore(*distilled_article_);
}
// 'distiller_ != null' is used as a signal that distillation is in progress,
// so it needs to be released so that we know distillation is done.
base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(
FROM_HERE, distiller_.release());
ContentSourceFinished();
}
void TaskTracker::CancelPendingSources() {
if (distiller_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(
FROM_HERE, distiller_.release());
}
}
void TaskTracker::OnBlobFetched(
bool success,
std::unique_ptr<DistilledArticleProto> distilled_article) {
blob_fetcher_running_ = false;
if (content_ready_) {
return;
}
DistilledArticleReady(std::move(distilled_article));
ContentSourceFinished();
}
bool TaskTracker::IsAnySourceRunning() const {
return distiller_ || blob_fetcher_running_;
}
void TaskTracker::ContentSourceFinished() {
if (content_ready_) {
CancelPendingSources();
} else if (!IsAnySourceRunning()) {
distilled_article_ = std::make_unique<DistilledArticleProto>();
NotifyViewersAndCallbacks();
}
}
void TaskTracker::DistilledArticleReady(
std::unique_ptr<DistilledArticleProto> distilled_article) {
DCHECK(!content_ready_);
if (distilled_article->pages().empty()) {
return;
}
content_ready_ = true;
distilled_article_ = std::move(distilled_article);
entry_.title = distilled_article_->title();
entry_.pages.clear();
for (const auto& page : distilled_article_->pages()) {
entry_.pages.push_back(GURL(page.url()));
}
NotifyViewersAndCallbacks();
}
void TaskTracker::NotifyViewersAndCallbacks() {
for (auto& viewer : viewers_) {
NotifyViewer(&viewer);
}
// Already inside a callback run SaveCallbacks directly.
DoSaveCallbacks(content_ready_);
}
void TaskTracker::NotifyViewer(ViewRequestDelegate* delegate) {
delegate->OnArticleReady(distilled_article_.get());
}
void TaskTracker::DoSaveCallbacks(bool success) {
if (!save_callbacks_.empty()) {
for (auto& callback : save_callbacks_)
std::move(callback).Run(entry_, distilled_article_.get(), success);
save_callbacks_.clear();
MaybeCancel();
}
}
void TaskTracker::OnArticleDistillationUpdated(
const ArticleDistillationUpdate& article_update) {
for (auto& viewer : viewers_) {
viewer.OnArticleUpdated(article_update);
}
}
void TaskTracker::AddDistilledContentToStore(
const DistilledArticleProto& content) {
if (content_store_) {
content_store_->SaveContent(entry_, content,
DistilledContentStore::SaveCallback());
}
}
} // namespace dom_distiller
|