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 126 127 128 129
|
// Copyright (c) 2012 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.
#ifndef CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_
#define CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_
#include <map>
#include <memory>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_base_factory.h"
namespace base {
class SequencedTaskRunner;
}
namespace content {
class BrowserContext;
}
namespace policy {
class UserCloudPolicyManager;
// BrowserContextKeyedBaseFactory implementation for UserCloudPolicyManager
// instances that initialize per-profile cloud policy settings on the desktop
// platforms.
//
// UserCloudPolicyManager is handled different than other KeyedServices because
// it is a dependency of PrefService. Therefore, lifetime of instances is
// managed by Profile, Profile startup code invokes CreateForBrowserContext()
// explicitly, takes ownership, and the instance is only deleted after
// PrefService destruction.
//
// TODO(mnissler): Remove the special lifetime management in favor of
// PrefService directly depending on UserCloudPolicyManager once the former has
// been converted to a KeyedService. See also https://crbug.com/131843 and
// https://crbug.com/131844.
class UserCloudPolicyManagerFactory : public BrowserContextKeyedBaseFactory {
public:
// Returns an instance of the UserCloudPolicyManagerFactory singleton.
static UserCloudPolicyManagerFactory* GetInstance();
// Returns the UserCloudPolicyManager instance associated with |context|.
static UserCloudPolicyManager* GetForBrowserContext(
content::BrowserContext* context);
// Creates an instance for |context|. Note that the caller is responsible for
// managing the lifetime of the instance. Subsequent calls to
// GetForBrowserContext() will return the created instance as long as it
// lives. If RegisterTestingFactory() has been called, then calls to this
// method will return null.
//
// If |force_immediate_load| is true, policy is loaded synchronously from
// UserCloudPolicyStore at startup.
//
// |background_task_runner| is used for the cloud policy store.
// |file_task_runner| is used for file operations. Currently this must be the
// FILE BrowserThread.
// |io_task_runner| is used for network IO. Currently this must be the IO
// BrowserThread.
static std::unique_ptr<UserCloudPolicyManager>
CreateForOriginalBrowserContext(
content::BrowserContext* context,
bool force_immediate_load,
const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
static UserCloudPolicyManager* RegisterForOffTheRecordBrowserContext(
content::BrowserContext* original_context,
content::BrowserContext* off_the_record_context);
typedef UserCloudPolicyManager*
(*TestingFactoryFunction)(content::BrowserContext* context);
// Allows testing code to inject UserCloudPolicyManager objects for tests.
// The factory function will be invoked for every Profile created. Because
// this class does not free the UserCloudPolicyManager objects it manages,
// it is up to the tests themselves to free the objects after the profile is
// shut down.
void RegisterTestingFactory(TestingFactoryFunction factory);
void ClearTestingFactory();
private:
class ManagerWrapper;
friend struct base::DefaultSingletonTraits<UserCloudPolicyManagerFactory>;
UserCloudPolicyManagerFactory();
~UserCloudPolicyManagerFactory() override;
// See comments for the static versions above.
UserCloudPolicyManager* GetManagerForBrowserContext(
content::BrowserContext* context);
std::unique_ptr<UserCloudPolicyManager>
CreateManagerForOriginalBrowserContext(
content::BrowserContext* context,
bool force_immediate_load,
const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
UserCloudPolicyManager* RegisterManagerForOffTheRecordBrowserContext(
content::BrowserContext* original_context,
content::BrowserContext* off_the_record_context);
// BrowserContextKeyedBaseFactory:
void BrowserContextShutdown(content::BrowserContext* context) override;
void BrowserContextDestroyed(content::BrowserContext* context) override;
void SetEmptyTestingFactory(content::BrowserContext* context) override;
bool HasTestingFactory(content::BrowserContext* context) override;
void CreateServiceNow(content::BrowserContext* context) override;
bool ServiceIsCreatedWithBrowserContext() const override;
typedef std::map<content::BrowserContext*, ManagerWrapper*> ManagerWrapperMap;
ManagerWrapperMap manager_wrappers_;
TestingFactoryFunction testing_factory_;
DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerFactory);
};
} // namespace policy
#endif // CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_
|