Commit 1a66612d authored by Gavin Williams's avatar Gavin Williams Committed by Commit Bot

Add GetBrowserContextToUse override to PrintManagementFactory

In guest mode print jobs did not appear in print management app. This
prevented users being able to cancel in-progress jobs.

Overriding GetBrowserContextToUse allows using an OTR profile so that
jobs appear in the app.

Added function BuildInstanceFor for creating PrintManagement service in
tests.

Bug: 1128690
Change-Id: I837afd57711505e7bf8ab93f9aa38ed16dc5d3d0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2412222
Commit-Queue: Gavin Williams <gavinwill@chromium.org>
Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810812}
parent ccedfa2a
......@@ -3561,6 +3561,7 @@ source_set("unit_tests") {
"printing/ppd_resolution_state_unittest.cc",
"printing/ppd_resolution_tracker_unittest.cc",
"printing/print_management/print_job_info_mojom_conversions_unittest.cc",
"printing/print_management/printing_manager_factory_unittest.cc",
"printing/print_management/printing_manager_unittest.cc",
"printing/print_servers_provider_unittest.cc",
"printing/printer_detector_test_util.h",
......
......@@ -9,6 +9,7 @@
#include "chrome/browser/chromeos/printing/print_management/printing_manager.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
......@@ -47,8 +48,9 @@ PrintingManagerFactory::PrintingManagerFactory()
PrintingManagerFactory::~PrintingManagerFactory() = default;
KeyedService* PrintingManagerFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
// static
KeyedService* PrintingManagerFactory::BuildInstanceFor(
content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
// We do not want an instance of PrintingManager on the lock screen. The
......@@ -66,6 +68,16 @@ KeyedService* PrintingManagerFactory::BuildServiceInstanceFor(
profile->GetPrefs());
}
KeyedService* PrintingManagerFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return BuildInstanceFor(static_cast<Profile*>(context));
}
content::BrowserContext* PrintingManagerFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
bool PrintingManagerFactory::ServiceIsCreatedWithBrowserContext() const {
return true;
}
......
......@@ -22,6 +22,7 @@ class PrintingManagerFactory : public BrowserContextKeyedServiceFactory {
public:
static PrintingManager* GetForProfile(Profile* profile);
static PrintingManagerFactory* GetInstance();
static KeyedService* BuildInstanceFor(content::BrowserContext* profile);
// Register the delete print job history preferences with the |registry|.
static void RegisterProfilePrefs(PrefRegistrySimple* registry);
......@@ -38,6 +39,8 @@ class PrintingManagerFactory : public BrowserContextKeyedServiceFactory {
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
bool ServiceIsCreatedWithBrowserContext() const override;
bool ServiceIsNULLWhileTesting() const override;
};
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/printing/print_management/printing_manager_factory.h"
#include "chrome/browser/chromeos/printing/print_management/printing_manager.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "components/history/core/browser/history_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace printing {
namespace print_management {
std::unique_ptr<KeyedService> BuildTestHistoryService(
content::BrowserContext* context) {
return std::make_unique<history::HistoryService>();
}
std::unique_ptr<KeyedService> BuildPrintingManager(
content::BrowserContext* context) {
return std::unique_ptr<KeyedService>(
PrintingManagerFactory::BuildInstanceFor(context));
}
std::unique_ptr<Profile> CreateProfile(std::string file_path) {
TestingProfile::Builder builder;
if (!file_path.empty()) {
builder.SetPath(base::FilePath(FILE_PATH_LITERAL(file_path)));
}
std::unique_ptr<Profile> profile = builder.Build();
HistoryServiceFactory::GetInstance()->SetTestingFactory(
profile.get(), base::BindRepeating(&BuildTestHistoryService));
PrintingManagerFactory::GetInstance()->SetTestingFactory(
profile.get(), base::BindRepeating(&BuildPrintingManager));
return profile;
}
TEST(PrintingManagerFactoryTest, OriginalProfileHasService) {
content::BrowserTaskEnvironment task_environment;
std::unique_ptr<Profile> profile = CreateProfile("");
EXPECT_NE(nullptr, PrintingManagerFactory::GetForProfile(profile.get()));
}
TEST(PrintingManagerFactoryTest, OffTheRecordProfileHasService) {
content::BrowserTaskEnvironment task_environment;
std::unique_ptr<Profile> profile = CreateProfile("");
EXPECT_NE(nullptr, PrintingManagerFactory::GetForProfile(
profile->GetPrimaryOTRProfile()));
}
TEST(PrintingManagerFactoryTest, SigninProfileNoService) {
content::BrowserTaskEnvironment task_environment;
std::unique_ptr<Profile> signin_profile =
CreateProfile(chrome::kInitialProfile);
EXPECT_EQ(nullptr,
PrintingManagerFactory::GetForProfile(signin_profile.get()));
}
TEST(PrintingManagerFactoryTest, LockScreenProfileNoService) {
content::BrowserTaskEnvironment task_environment;
std::unique_ptr<Profile> lockscreen_profile =
CreateProfile(chrome::kLockScreenAppProfile);
EXPECT_EQ(nullptr,
PrintingManagerFactory::GetForProfile(lockscreen_profile.get()));
}
} // namespace print_management
} // namespace printing
} // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment