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
|
// Copyright 2016 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/offline_pages/request_coordinator_factory.h"
#include <memory>
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/offline_pages/android/background_scheduler_bridge.h"
#include "chrome/browser/offline_pages/android/load_termination_listener_impl.h"
#include "chrome/browser/offline_pages/background_loader_offliner.h"
#include "chrome/browser/offline_pages/offline_page_model_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "chrome/common/chrome_constants.h"
#include "components/offline_pages/core/background/offliner.h"
#include "components/offline_pages/core/background/offliner_policy.h"
#include "components/offline_pages/core/background/request_coordinator.h"
#include "components/offline_pages/core/background/request_queue.h"
#include "components/offline_pages/core/background/request_queue_store.h"
#include "components/offline_pages/core/background/scheduler.h"
#include "components/offline_pages/core/offline_page_feature.h"
#include "content/public/browser/web_contents.h"
namespace network {
class NetworkQualityTracker;
}
namespace offline_pages {
namespace {
class ActiveTabInfo : public RequestCoordinator::ActiveTabInfo {
public:
explicit ActiveTabInfo(Profile* profile) : profile_(profile) {}
~ActiveTabInfo() override = default;
bool DoesActiveTabMatch(const GURL& url) override {
// Loop through to find the active tab and report whether the URL matches.
for (const TabModel* model : TabModelList::models()) {
if (model->GetProfile() == profile_) {
content::WebContents* contents = model->GetActiveWebContents();
// Check visibility to make sure Chrome is in the foreground.
if (contents &&
contents->GetVisibility() == content::Visibility::VISIBLE) {
if (contents->GetVisibleURL() == url)
return true;
if (contents->GetLastCommittedURL() == url)
return true;
}
}
}
return false;
}
private:
raw_ptr<Profile> profile_;
};
} // namespace
RequestCoordinatorFactory::RequestCoordinatorFactory()
: ProfileKeyedServiceFactory(
"OfflineRequestCoordinator",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kOriginalOnly)
// TODO(crbug.com/40257657): Check if this service is needed in
// Guest mode.
.WithGuest(ProfileSelection::kOriginalOnly)
.Build()) {
// Depends on OfflinePageModelFactory in SimpleDependencyManager.
}
// static
RequestCoordinatorFactory* RequestCoordinatorFactory::GetInstance() {
static base::NoDestructor<RequestCoordinatorFactory> instance;
return instance.get();
}
// static
RequestCoordinator* RequestCoordinatorFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<RequestCoordinator*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
std::unique_ptr<KeyedService>
RequestCoordinatorFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy());
std::unique_ptr<Offliner> offliner;
OfflinePageModel* model =
OfflinePageModelFactory::GetInstance()->GetForBrowserContext(context);
std::unique_ptr<LoadTerminationListenerImpl> load_termination_listener =
std::make_unique<LoadTerminationListenerImpl>();
offliner = std::make_unique<BackgroundLoaderOffliner>(
context, policy.get(), model, std::move(load_termination_listener));
scoped_refptr<base::SequencedTaskRunner> background_task_runner =
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT});
Profile* profile = Profile::FromBrowserContext(context);
base::FilePath queue_store_path =
profile->GetPath().Append(chrome::kOfflinePageRequestQueueDirname);
std::unique_ptr<RequestQueueStore> queue_store(
new RequestQueueStore(background_task_runner, queue_store_path));
std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(queue_store)));
std::unique_ptr<Scheduler>
scheduler(new android::BackgroundSchedulerBridge());
network::NetworkQualityTracker* network_quality_tracker =
g_browser_process->network_quality_tracker();
return std::make_unique<RequestCoordinator>(
std::move(policy), std::move(offliner), std::move(queue),
std::move(scheduler), network_quality_tracker,
std::make_unique<ActiveTabInfo>(profile));
}
} // namespace offline_pages
|