1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_ 6#define CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_ 7 8#include <map> 9 10#include "base/basictypes.h" 11#include "base/memory/ref_counted.h" 12#include "base/memory/singleton.h" 13#include "components/keyed_service/content/browser_context_keyed_base_factory.h" 14 15namespace base { 16class SequencedTaskRunner; 17} 18 19namespace content { 20class BrowserContext; 21} 22 23namespace policy { 24 25class UserCloudPolicyManager; 26 27// BrowserContextKeyedBaseFactory implementation for UserCloudPolicyManager 28// instances that initialize per-profile cloud policy settings on the desktop 29// platforms. 30// 31// UserCloudPolicyManager is handled different than other 32// KeyedServices because it is a dependency of PrefService. 33// Therefore, lifetime of instances is managed by Profile, Profile startup code 34// invokes CreateForBrowserContext() explicitly, takes ownership, and the 35// instance is only deleted after PrefService destruction. 36// 37// TODO(mnissler): Remove the special lifetime management in favor of 38// PrefService directly depending on UserCloudPolicyManager once the former has 39// been converted to a KeyedService. 40// See also http://crbug.com/131843 and http://crbug.com/131844. 41class UserCloudPolicyManagerFactory : public BrowserContextKeyedBaseFactory { 42 public: 43 // Returns an instance of the UserCloudPolicyManagerFactory singleton. 44 static UserCloudPolicyManagerFactory* GetInstance(); 45 46 // Returns the UserCloudPolicyManager instance associated with |context|. 47 static UserCloudPolicyManager* GetForBrowserContext( 48 content::BrowserContext* context); 49 50 // Creates an instance for |context|. Note that the caller is responsible for 51 // managing the lifetime of the instance. Subsequent calls to 52 // GetForBrowserContext() will return the created instance as long as it 53 // lives. If RegisterTestingFactory() has been called, then calls to 54 // this method will return null. 55 // 56 // If |force_immediate_load| is true, policy is loaded synchronously from 57 // UserCloudPolicyStore at startup. 58 // 59 // |background_task_runner| is used for the cloud policy store. 60 // |file_task_runner| is used for file operations. Currently this must be the 61 // FILE BrowserThread. 62 // |io_task_runner| is used for network IO. Currently this must be the IO 63 // BrowserThread. 64 static scoped_ptr<UserCloudPolicyManager> CreateForOriginalBrowserContext( 65 content::BrowserContext* context, 66 bool force_immediate_load, 67 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, 68 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner, 69 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner); 70 71 static UserCloudPolicyManager* RegisterForOffTheRecordBrowserContext( 72 content::BrowserContext* original_context, 73 content::BrowserContext* off_the_record_context); 74 75 typedef UserCloudPolicyManager* 76 (*TestingFactoryFunction)(content::BrowserContext* context); 77 78 // Allows testing code to inject UserCloudPolicyManager objects for tests. 79 // The factory function will be invoked for every Profile created. Because 80 // this class does not free the UserCloudPolicyManager objects it manages, 81 // it is up to the tests themselves to free the objects after the profile is 82 // shut down. 83 void RegisterTestingFactory(TestingFactoryFunction factory); 84 void ClearTestingFactory(); 85 86 private: 87 class ManagerWrapper; 88 friend struct DefaultSingletonTraits<UserCloudPolicyManagerFactory>; 89 90 UserCloudPolicyManagerFactory(); 91 virtual ~UserCloudPolicyManagerFactory(); 92 93 // See comments for the static versions above. 94 UserCloudPolicyManager* GetManagerForBrowserContext( 95 content::BrowserContext* context); 96 97 scoped_ptr<UserCloudPolicyManager> CreateManagerForOriginalBrowserContext( 98 content::BrowserContext* context, 99 bool force_immediate_load, 100 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, 101 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner, 102 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner); 103 104 UserCloudPolicyManager* RegisterManagerForOffTheRecordBrowserContext( 105 content::BrowserContext* original_context, 106 content::BrowserContext* off_the_record_context); 107 108 // BrowserContextKeyedBaseFactory: 109 virtual void BrowserContextShutdown( 110 content::BrowserContext* context) OVERRIDE; 111 virtual void BrowserContextDestroyed( 112 content::BrowserContext* context) OVERRIDE; 113 virtual void SetEmptyTestingFactory( 114 content::BrowserContext* context) OVERRIDE; 115 virtual bool HasTestingFactory(content::BrowserContext* context) OVERRIDE; 116 virtual void CreateServiceNow(content::BrowserContext* context) OVERRIDE; 117 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE; 118 119 120 typedef std::map<content::BrowserContext*, ManagerWrapper*> ManagerWrapperMap; 121 122 ManagerWrapperMap manager_wrappers_; 123 TestingFactoryFunction testing_factory_; 124 125 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerFactory); 126}; 127 128} // namespace policy 129 130#endif // CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_ 131