Home | History | Annotate | Download | only in policy
      1 // Copyright (c) 2013 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_PROFILE_POLICY_CONNECTOR_FACTORY_H_
      6 #define CHROME_BROWSER_POLICY_PROFILE_POLICY_CONNECTOR_FACTORY_H_
      7 
      8 #include <list>
      9 #include <map>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "components/keyed_service/content/browser_context_keyed_base_factory.h"
     14 
     15 template <typename T>
     16 struct DefaultSingletonTraits;
     17 
     18 class Profile;
     19 
     20 namespace base {
     21 class SequencedTaskRunner;
     22 }
     23 
     24 namespace content {
     25 class BrowserContext;
     26 }
     27 
     28 namespace policy {
     29 
     30 class ConfigurationPolicyProvider;
     31 class ProfilePolicyConnector;
     32 
     33 // Creates ProfilePolicyConnectors for Profiles, which manage the common
     34 // policy providers and other policy components.
     35 // TODO(joaodasilva): convert this class to a proper PKS once the PrefService,
     36 // which depends on this class, becomes a PKS too.
     37 class ProfilePolicyConnectorFactory : public BrowserContextKeyedBaseFactory {
     38  public:
     39   // Returns the ProfilePolicyConnectorFactory singleton.
     40   static ProfilePolicyConnectorFactory* GetInstance();
     41 
     42   // Returns the ProfilePolicyConnector associated with |profile|. This is only
     43   // valid before |profile| is shut down.
     44   static ProfilePolicyConnector* GetForProfile(Profile* profile);
     45 
     46   // Creates a new ProfilePolicyConnector for |profile|, which must be managed
     47   // by the caller. Subsequent calls to GetForProfile() will return the instance
     48   // created, as long as it lives.
     49   // If |force_immediate_load| is true then policy is loaded synchronously on
     50   // startup.
     51   static scoped_ptr<ProfilePolicyConnector> CreateForProfile(
     52       Profile* profile,
     53       bool force_immediate_load);
     54 
     55   // Overrides the |connector| for the given |profile|; use only in tests.
     56   // Once this class becomes a proper PKS then it can reuse the testing
     57   // methods of BrowserContextKeyedServiceFactory.
     58   void SetServiceForTesting(Profile* profile,
     59                             ProfilePolicyConnector* connector);
     60 
     61   // The next Profile to call CreateForProfile() will get a PolicyService
     62   // with |provider| as its sole policy provider. This can be called multiple
     63   // times to override the policy providers for more than 1 Profile.
     64   void PushProviderForTesting(ConfigurationPolicyProvider* provider);
     65 
     66  private:
     67   friend struct DefaultSingletonTraits<ProfilePolicyConnectorFactory>;
     68 
     69   ProfilePolicyConnectorFactory();
     70   virtual ~ProfilePolicyConnectorFactory();
     71 
     72   ProfilePolicyConnector* GetForProfileInternal(Profile* profile);
     73 
     74   scoped_ptr<ProfilePolicyConnector> CreateForProfileInternal(
     75       Profile* profile,
     76       bool force_immediate_load);
     77 
     78   // BrowserContextKeyedBaseFactory:
     79   virtual void BrowserContextShutdown(
     80       content::BrowserContext* context) OVERRIDE;
     81   virtual void BrowserContextDestroyed(
     82       content::BrowserContext* context) OVERRIDE;
     83   virtual void SetEmptyTestingFactory(
     84       content::BrowserContext* context) OVERRIDE;
     85   virtual bool HasTestingFactory(content::BrowserContext* context) OVERRIDE;
     86   virtual void CreateServiceNow(content::BrowserContext* context) OVERRIDE;
     87 
     88   typedef std::map<Profile*, ProfilePolicyConnector*> ConnectorMap;
     89   ConnectorMap connectors_;
     90   std::list<ConfigurationPolicyProvider*> test_providers_;
     91 
     92   DISALLOW_COPY_AND_ASSIGN(ProfilePolicyConnectorFactory);
     93 };
     94 
     95 }  // namespace policy
     96 
     97 #endif  // CHROME_BROWSER_POLICY_PROFILE_POLICY_CONNECTOR_FACTORY_H_
     98