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/browser_context_keyed_service/browser_context_keyed_base_factory.h" 14 15 namespace base { 16 class SequencedTaskRunner; 17 } 18 19 namespace content { 20 class BrowserContext; 21 } 22 23 namespace policy { 24 25 class 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 // BrowserContextKeyedServices 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 BrowserContextKeyedService. 40 // See also http://crbug.com/131843 and http://crbug.com/131844. 41 class 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. 54 // 55 // If |force_immediate_load| is true, policy is loaded synchronously from 56 // UserCloudPolicyStore at startup. 57 // 58 // |background_task_runner| is used for the cloud policy store. 59 // |file_task_runner| is used for file operations. Currently this must be the 60 // FILE BrowserThread. 61 // |io_task_runner| is used for network IO. Currently this must be the IO 62 // BrowserThread. 63 static scoped_ptr<UserCloudPolicyManager> CreateForOriginalBrowserContext( 64 content::BrowserContext* context, 65 bool force_immediate_load, 66 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, 67 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner, 68 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner); 69 70 static UserCloudPolicyManager* RegisterForOffTheRecordBrowserContext( 71 content::BrowserContext* original_context, 72 content::BrowserContext* off_the_record_context); 73 74 void RegisterForTesting(content::BrowserContext* context, 75 UserCloudPolicyManager* manager); 76 77 private: 78 class ManagerWrapper; 79 friend struct DefaultSingletonTraits<UserCloudPolicyManagerFactory>; 80 81 UserCloudPolicyManagerFactory(); 82 virtual ~UserCloudPolicyManagerFactory(); 83 84 // See comments for the static versions above. 85 UserCloudPolicyManager* GetManagerForBrowserContext( 86 content::BrowserContext* context); 87 88 scoped_ptr<UserCloudPolicyManager> CreateManagerForOriginalBrowserContext( 89 content::BrowserContext* context, 90 bool force_immediate_load, 91 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, 92 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner, 93 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner); 94 95 UserCloudPolicyManager* RegisterManagerForOffTheRecordBrowserContext( 96 content::BrowserContext* original_context, 97 content::BrowserContext* off_the_record_context); 98 99 // BrowserContextKeyedBaseFactory: 100 virtual void BrowserContextShutdown( 101 content::BrowserContext* context) OVERRIDE; 102 virtual void BrowserContextDestroyed( 103 content::BrowserContext* context) OVERRIDE; 104 virtual void SetEmptyTestingFactory( 105 content::BrowserContext* context) OVERRIDE; 106 virtual void CreateServiceNow(content::BrowserContext* context) OVERRIDE; 107 108 typedef std::map<content::BrowserContext*, ManagerWrapper*> ManagerWrapperMap; 109 110 ManagerWrapperMap manager_wrappers_; 111 112 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerFactory); 113 }; 114 115 } // namespace policy 116 117 #endif // CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_ 118