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
|
// Copyright 2019 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/download/simple_download_manager_coordinator_factory.h"
#include "base/no_destructor.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/transition_manager/full_browser_transition_manager.h"
#include "components/download/public/common/simple_download_manager_coordinator.h"
#include "components/keyed_service/core/simple_dependency_manager.h"
#include "components/keyed_service/core/simple_factory_key.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/download_manager.h"
namespace {
void DownloadUrl(std::unique_ptr<download::DownloadUrlParameters> parameters,
Profile* profile) {
content::DownloadManager* manager = profile->GetDownloadManager();
DCHECK(manager);
manager->DownloadUrl(std::move(parameters));
}
void DownloadUrlWithDownloadManager(
SimpleFactoryKey* key,
std::unique_ptr<download::DownloadUrlParameters> parameters) {
FullBrowserTransitionManager::Get()->RegisterCallbackOnProfileCreation(
key, base::BindOnce(&DownloadUrl, std::move(parameters)));
}
} // namespace
// static
SimpleDownloadManagerCoordinatorFactory*
SimpleDownloadManagerCoordinatorFactory::GetInstance() {
static base::NoDestructor<SimpleDownloadManagerCoordinatorFactory> instance;
return instance.get();
}
// static
download::SimpleDownloadManagerCoordinator*
SimpleDownloadManagerCoordinatorFactory::GetForKey(SimpleFactoryKey* key) {
return static_cast<download::SimpleDownloadManagerCoordinator*>(
GetInstance()->GetServiceForKey(key, true));
}
SimpleDownloadManagerCoordinatorFactory::
SimpleDownloadManagerCoordinatorFactory()
: SimpleKeyedServiceFactory("SimpleDownloadManagerCoordinator",
SimpleDependencyManager::GetInstance()) {}
SimpleDownloadManagerCoordinatorFactory::
~SimpleDownloadManagerCoordinatorFactory() = default;
std::unique_ptr<KeyedService>
SimpleDownloadManagerCoordinatorFactory::BuildServiceInstanceFor(
SimpleFactoryKey* key) const {
// Use unretained is safe as the key is associated with the callback.
return std::make_unique<download::SimpleDownloadManagerCoordinator>(
base::BindRepeating(&DownloadUrlWithDownloadManager,
base::Unretained(key)));
}
SimpleFactoryKey* SimpleDownloadManagerCoordinatorFactory::GetKeyToUse(
SimpleFactoryKey* key) const {
return key;
}
|