12c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// Copyright (c) 2012 The Chromium Authors. All rights reserved. 22c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// Use of this source code is governed by a BSD-style license that can be 32c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// found in the LICENSE file. 42c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org 52c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org#ifndef CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_ 62c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org#define CHROME_BROWSER_POLICY_CLOUD_USER_CLOUD_POLICY_MANAGER_FACTORY_H_ 72c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org 82c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org#include <map> 92c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org 102c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org#include "base/basictypes.h" 112c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org#include "base/memory/ref_counted.h" 122c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org#include "base/memory/singleton.h" 132c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org#include "components/keyed_service/content/browser_context_keyed_base_factory.h" 142c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org 152c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.orgnamespace base { 162c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.orgclass SequencedTaskRunner; 172c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org} 182c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org 192c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.orgnamespace content { 202c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.orgclass BrowserContext; 212c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org} 222c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org 232c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.orgnamespace policy { 242c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org 252c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.orgclass UserCloudPolicyManager; 262c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org 272c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// BrowserContextKeyedBaseFactory implementation for UserCloudPolicyManager 282c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// instances that initialize per-profile cloud policy settings on the desktop 292c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// platforms. 302c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// 312c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// UserCloudPolicyManager is handled different than other 322c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// KeyedServices because it is a dependency of PrefService. 332c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// Therefore, lifetime of instances is managed by Profile, Profile startup code 342c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// invokes CreateForBrowserContext() explicitly, takes ownership, and the 352c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// instance is only deleted after PrefService destruction. 362c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// 372c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// TODO(mnissler): Remove the special lifetime management in favor of 382c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// PrefService directly depending on UserCloudPolicyManager once the former has 392c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// been converted to a KeyedService. 402c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.org// See also http://crbug.com/131843 and http://crbug.com/131844. 412c26cb18967944507a81a07ac6f1c921ebb4ab75danno@chromium.orgclass 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