Commit 28579b0f authored by Jesse Schettler's avatar Jesse Schettler Committed by Chromium LUCI CQ

scanning: Enable ScanService in Guest Mode

Ensure the ScanService can be created in Guest Mode so that users in
Guest Mode can use the Scan app.

Bug: 1059779
Change-Id: I7d596b16f7516c0bf241c933e19be546561dc9d5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2586265Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Commit-Queue: Jesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835905}
parent 5b17a8f1
......@@ -3881,6 +3881,7 @@ source_set("unit_tests") {
"scanning/fake_lorgnette_scanner_manager.h",
"scanning/lorgnette_scanner_manager_unittest.cc",
"scanning/lorgnette_scanner_manager_util_unittest.cc",
"scanning/scan_service_factory_unittest.cc",
"scanning/scan_service_unittest.cc",
"scanning/scanning_paths_provider_impl_unittest.cc",
"scanning/scanning_type_converters_unittest.cc",
......
......@@ -10,6 +10,7 @@
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/chromeos/scanning/lorgnette_scanner_manager_factory.h"
#include "chrome/browser/chromeos/scanning/scan_service.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"
......@@ -29,17 +30,9 @@ ScanServiceFactory* ScanServiceFactory::GetInstance() {
return base::Singleton<ScanServiceFactory>::get();
}
ScanServiceFactory::ScanServiceFactory()
: BrowserContextKeyedServiceFactory(
"ScanService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(LorgnetteScannerManagerFactory::GetInstance());
}
ScanServiceFactory::~ScanServiceFactory() = default;
KeyedService* ScanServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
// static
KeyedService* ScanServiceFactory::BuildInstanceFor(
content::BrowserContext* context) {
// Prevent an instance of ScanService from being created on the lock screen.
Profile* profile = Profile::FromBrowserContext(context);
if (ProfileHelper::IsLockScreenAppProfile(profile) ||
......@@ -59,6 +52,25 @@ KeyedService* ScanServiceFactory::BuildServiceInstanceFor(
: base::FilePath());
}
ScanServiceFactory::ScanServiceFactory()
: BrowserContextKeyedServiceFactory(
"ScanService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(LorgnetteScannerManagerFactory::GetInstance());
}
ScanServiceFactory::~ScanServiceFactory() = default;
KeyedService* ScanServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return BuildInstanceFor(context);
}
content::BrowserContext* ScanServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
bool ScanServiceFactory::ServiceIsCreatedWithBrowserContext() const {
return true;
}
......
......@@ -25,6 +25,7 @@ class ScanServiceFactory : public BrowserContextKeyedServiceFactory {
public:
static ScanService* GetForBrowserContext(content::BrowserContext* context);
static ScanServiceFactory* GetInstance();
static KeyedService* BuildInstanceFor(content::BrowserContext* context);
private:
friend struct base::DefaultSingletonTraits<ScanServiceFactory>;
......@@ -38,6 +39,8 @@ class ScanServiceFactory : 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/scanning/scan_service_factory.h"
#include <memory>
#include <string>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "chrome/browser/chromeos/scanning/fake_lorgnette_scanner_manager.h"
#include "chrome/browser/chromeos/scanning/lorgnette_scanner_manager_factory.h"
#include "chrome/browser/chromeos/scanning/scan_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace {
// Creates a new LorgnetteScannerManager for the given `context`.
std::unique_ptr<KeyedService> BuildLorgnetteScannerManager(
content::BrowserContext* context) {
return std::make_unique<chromeos::FakeLorgnetteScannerManager>();
}
// Creates a new ScanService for the given `context`.
std::unique_ptr<KeyedService> BuildScanService(
content::BrowserContext* context) {
return std::unique_ptr<KeyedService>(static_cast<KeyedService*>(
ScanServiceFactory::BuildInstanceFor(context)));
}
// Creates a Profile based on the provided `file_path` and sets the required
// testing factories for that Profile.
std::unique_ptr<Profile> CreateProfile(const std::string& file_path) {
TestingProfile::Builder builder;
if (!file_path.empty())
builder.SetPath(base::FilePath(file_path));
std::unique_ptr<Profile> profile = builder.Build();
LorgnetteScannerManagerFactory::GetInstance()->SetTestingFactory(
profile.get(), base::BindRepeating(&BuildLorgnetteScannerManager));
ScanServiceFactory::GetInstance()->SetTestingFactory(
profile.get(), base::BindRepeating(&BuildScanService));
return profile;
}
} // namespace
// Test that the ScanService can be created with the original profile.
TEST(ScanServiceFactoryTest, OriginalProfileHasService) {
content::BrowserTaskEnvironment task_environment;
std::unique_ptr<Profile> profile = CreateProfile("");
EXPECT_NE(nullptr, ScanServiceFactory::GetForBrowserContext(profile.get()));
}
// Test that the ScanService can be created with an off-the-record profile.
TEST(ScanServiceFactoryTest, OffTheRecordProfileHasService) {
content::BrowserTaskEnvironment task_environment;
std::unique_ptr<Profile> profile = CreateProfile("");
EXPECT_NE(nullptr, ScanServiceFactory::GetForBrowserContext(
profile->GetPrimaryOTRProfile()));
}
// Test that the ScanService cannot be created with a signin profile.
TEST(ScanServiceFactoryTest, SigninProfileNoService) {
content::BrowserTaskEnvironment task_environment;
std::unique_ptr<Profile> signin_profile =
CreateProfile(chrome::kInitialProfile);
EXPECT_EQ(nullptr,
ScanServiceFactory::GetForBrowserContext(signin_profile.get()));
}
// Test that the ScanService cannot be created on the lock screen.
TEST(ScanServiceFactoryTest, LockScreenProfileNoService) {
content::BrowserTaskEnvironment task_environment;
std::unique_ptr<Profile> lockscreen_profile =
CreateProfile(chrome::kLockScreenAppProfile);
EXPECT_EQ(nullptr,
ScanServiceFactory::GetForBrowserContext(lockscreen_profile.get()));
}
} // 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