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
|
// 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/dom_distiller_service.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/single_thread_task_runner.h"
#include "base/uuid.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/task_tracker.h"
#include "url/gurl.h"
namespace dom_distiller {
namespace {
ArticleEntry CreateSkeletonEntryForUrl(const GURL& url) {
ArticleEntry skeleton;
skeleton.entry_id = base::Uuid::GenerateRandomV4().AsLowercaseString();
skeleton.pages.push_back(url);
DCHECK(IsEntryValid(skeleton));
return skeleton;
}
} // namespace
DomDistillerService::DomDistillerService(
std::unique_ptr<DistillerFactory> distiller_factory,
std::unique_ptr<DistillerPageFactory> distiller_page_factory,
std::unique_ptr<DistilledPagePrefs> distilled_page_prefs,
std::unique_ptr<DistillerUIHandle> distiller_ui_handle)
: content_store_(new InMemoryContentStore(kDefaultMaxNumCachedEntries)),
distiller_factory_(std::move(distiller_factory)),
distiller_page_factory_(std::move(distiller_page_factory)),
distilled_page_prefs_(std::move(distilled_page_prefs)),
distiller_ui_handle_(std::move(distiller_ui_handle)),
weak_ptr_factory_(this) {}
DomDistillerService::~DomDistillerService() {
// There shouldn't be any tasks pending at this point.
DCHECK(tasks_.empty());
}
std::unique_ptr<DistillerPage> DomDistillerService::CreateDefaultDistillerPage(
const gfx::Size& render_view_size) {
return distiller_page_factory_->CreateDistillerPage(render_view_size);
}
std::unique_ptr<DistillerPage>
DomDistillerService::CreateDefaultDistillerPageWithHandle(
std::unique_ptr<SourcePageHandle> handle) {
return distiller_page_factory_->CreateDistillerPageWithHandle(
std::move(handle));
}
std::unique_ptr<ViewerHandle> DomDistillerService::ViewUrl(
ViewRequestDelegate* delegate,
std::unique_ptr<DistillerPage> distiller_page,
const GURL& url) {
if (!url.is_valid()) {
return nullptr;
}
TaskTracker* task_tracker = nullptr;
bool was_created = GetOrCreateTaskTrackerForUrl(url, &task_tracker);
std::unique_ptr<ViewerHandle> viewer_handle =
task_tracker->AddViewer(delegate);
// If a distiller is already running for one URL, don't start another.
if (was_created) {
task_tracker->StartDistiller(distiller_factory_.get(),
std::move(distiller_page));
task_tracker->StartBlobFetcher();
}
return viewer_handle;
}
bool DomDistillerService::GetOrCreateTaskTrackerForUrl(
const GURL& url,
TaskTracker** task_tracker) {
*task_tracker = GetTaskTrackerForUrl(url);
if (*task_tracker) {
return false;
}
ArticleEntry skeleton_entry = CreateSkeletonEntryForUrl(url);
*task_tracker = CreateTaskTracker(skeleton_entry);
return true;
}
TaskTracker* DomDistillerService::GetTaskTrackerForUrl(const GURL& url) const {
for (auto it = tasks_.begin(); it != tasks_.end(); ++it) {
if ((*it)->HasUrl(url)) {
return (*it).get();
}
}
return nullptr;
}
TaskTracker* DomDistillerService::CreateTaskTracker(const ArticleEntry& entry) {
TaskTracker::CancelCallback cancel_callback =
base::BindOnce(&DomDistillerService::CancelTask, base::Unretained(this));
tasks_.push_back(std::make_unique<TaskTracker>(
entry, std::move(cancel_callback), content_store_.get()));
return tasks_.back().get();
}
void DomDistillerService::CancelTask(TaskTracker* task) {
auto it = std::ranges::find(tasks_, task, &std::unique_ptr<TaskTracker>::get);
if (it != tasks_.end()) {
it->release();
tasks_.erase(it);
base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE,
task);
}
}
DistilledPagePrefs* DomDistillerService::GetDistilledPagePrefs() {
return distilled_page_prefs_.get();
}
DistillerUIHandle* DomDistillerService::GetDistillerUIHandle() {
return distiller_ui_handle_.get();
}
base::WeakPtr<DomDistillerService> DomDistillerService::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
bool DomDistillerService::HasTaskTrackerForTesting(const GURL& url) const {
return GetTaskTrackerForUrl(url);
}
} // namespace dom_distiller
|