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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ASH_DRIVE_DRIVE_INTEGRATION_SERVICE_FACTORY_H_
#define CHROME_BROWSER_ASH_DRIVE_DRIVE_INTEGRATION_SERVICE_FACTORY_H_
#include <memory>
#include "base/functional/callback.h"
#include "base/memory/singleton.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
class Profile;
namespace drive {
class DriveIntegrationService;
// Singleton that owns all instances of DriveIntegrationService and
// associates them with Profiles.
class DriveIntegrationServiceFactory : public ProfileKeyedServiceFactory {
public:
// Factory function used by tests.
using FactoryCallback =
base::RepeatingCallback<DriveIntegrationService*(Profile* profile)>;
// Sets and resets a factory function for tests. See below for why we can't
// use BrowserContextKeyedServiceFactory::SetTestingFactory().
class ScopedFactoryForTest {
public:
explicit ScopedFactoryForTest(FactoryCallback* factory_for_test);
~ScopedFactoryForTest();
};
// Returns the DriveIntegrationService for |profile|, creating it if it is
// not yet created.
static DriveIntegrationService* GetForProfile(Profile* profile);
// Returns the DriveIntegrationService that is already associated with
// |profile|, if it is not yet created it will return NULL.
static DriveIntegrationService* FindForProfile(Profile* profile);
// Returns the DriveIntegrationServiceFactory instance.
static DriveIntegrationServiceFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<DriveIntegrationServiceFactory>;
DriveIntegrationServiceFactory();
~DriveIntegrationServiceFactory() override;
// BrowserContextKeyedServiceFactory overrides.
std::unique_ptr<KeyedService> BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const override;
// This is static so it can be set without instantiating the factory. This
// allows factory creation to be delayed until it normally happens (on profile
// creation) rather than when tests are set up. DriveIntegrationServiceFactory
// transitively depends on ChromeExtensionSystemFactory which crashes if
// created too soon (i.e. before the BrowserProcess exists).
static FactoryCallback* factory_for_test_;
};
} // namespace drive
#endif // CHROME_BROWSER_ASH_DRIVE_DRIVE_INTEGRATION_SERVICE_FACTORY_H_
|