Home | History | Annotate | Download | only in cloud
      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 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
      6 
      7 #include "base/files/file_path.h"
      8 #include "base/logging.h"
      9 #include "base/message_loop/message_loop_proxy.h"
     10 #include "base/sequenced_task_runner.h"
     11 #include "chrome/browser/policy/schema_registry_service.h"
     12 #include "chrome/browser/policy/schema_registry_service_factory.h"
     13 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
     14 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     15 #include "components/policy/core/common/cloud/cloud_external_data_manager.h"
     16 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
     17 #include "components/policy/core/common/cloud/user_cloud_policy_store.h"
     18 #include "content/public/browser/browser_context.h"
     19 
     20 namespace policy {
     21 
     22 namespace {
     23 
     24 // Directory inside the profile directory where policy-related resources are
     25 // stored.
     26 const base::FilePath::CharType kPolicy[] = FILE_PATH_LITERAL("Policy");
     27 
     28 // Directory under kPolicy, in the user's profile dir, where policy for
     29 // components is cached.
     30 const base::FilePath::CharType kComponentsDir[] =
     31     FILE_PATH_LITERAL("Components");
     32 
     33 }  // namespace
     34 
     35 // A BrowserContextKeyedService that wraps a UserCloudPolicyManager.
     36 class UserCloudPolicyManagerFactory::ManagerWrapper
     37     : public BrowserContextKeyedService {
     38  public:
     39   explicit ManagerWrapper(UserCloudPolicyManager* manager)
     40       : manager_(manager) {}
     41   virtual ~ManagerWrapper() {}
     42 
     43   virtual void Shutdown() OVERRIDE {
     44     manager_->Shutdown();
     45   }
     46 
     47   UserCloudPolicyManager* manager() { return manager_; }
     48 
     49  private:
     50   UserCloudPolicyManager* manager_;
     51 
     52   DISALLOW_COPY_AND_ASSIGN(ManagerWrapper);
     53 };
     54 
     55 // static
     56 UserCloudPolicyManagerFactory* UserCloudPolicyManagerFactory::GetInstance() {
     57   return Singleton<UserCloudPolicyManagerFactory>::get();
     58 }
     59 
     60 // static
     61 UserCloudPolicyManager* UserCloudPolicyManagerFactory::GetForBrowserContext(
     62     content::BrowserContext* context) {
     63   return GetInstance()->GetManagerForBrowserContext(context);
     64 }
     65 
     66 // static
     67 scoped_ptr<UserCloudPolicyManager>
     68 UserCloudPolicyManagerFactory::CreateForOriginalBrowserContext(
     69     content::BrowserContext* context,
     70     bool force_immediate_load,
     71     const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
     72     const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
     73     const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) {
     74   return GetInstance()->CreateManagerForOriginalBrowserContext(
     75       context,
     76       force_immediate_load,
     77       background_task_runner,
     78       file_task_runner,
     79       io_task_runner);
     80 }
     81 
     82 // static
     83 UserCloudPolicyManager*
     84 UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
     85     content::BrowserContext* original_context,
     86     content::BrowserContext* off_the_record_context) {
     87   return GetInstance()->RegisterManagerForOffTheRecordBrowserContext(
     88       original_context, off_the_record_context);
     89 }
     90 
     91 void UserCloudPolicyManagerFactory::RegisterForTesting(
     92     content::BrowserContext* context,
     93     UserCloudPolicyManager* manager) {
     94   ManagerWrapper*& manager_wrapper = manager_wrappers_[context];
     95   delete manager_wrapper;
     96   manager_wrapper = new ManagerWrapper(manager);
     97 }
     98 
     99 UserCloudPolicyManagerFactory::UserCloudPolicyManagerFactory()
    100     : BrowserContextKeyedBaseFactory(
    101         "UserCloudPolicyManager",
    102         BrowserContextDependencyManager::GetInstance()) {
    103   DependsOn(SchemaRegistryServiceFactory::GetInstance());
    104 }
    105 
    106 UserCloudPolicyManagerFactory::~UserCloudPolicyManagerFactory() {
    107   DCHECK(manager_wrappers_.empty());
    108 }
    109 
    110 UserCloudPolicyManager*
    111 UserCloudPolicyManagerFactory::GetManagerForBrowserContext(
    112     content::BrowserContext* context) {
    113   // In case |context| is an incognito Profile/Context, |manager_wrappers_|
    114   // will have a matching entry pointing to the manager of the original context.
    115   ManagerWrapperMap::const_iterator it = manager_wrappers_.find(context);
    116   return it != manager_wrappers_.end() ? it->second->manager() : NULL;
    117 }
    118 
    119 scoped_ptr<UserCloudPolicyManager>
    120 UserCloudPolicyManagerFactory::CreateManagerForOriginalBrowserContext(
    121     content::BrowserContext* context,
    122     bool force_immediate_load,
    123     const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
    124     const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
    125     const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) {
    126   DCHECK(!context->IsOffTheRecord());
    127 
    128   scoped_ptr<UserCloudPolicyStore> store(
    129       UserCloudPolicyStore::Create(context->GetPath(), background_task_runner));
    130   if (force_immediate_load)
    131     store->LoadImmediately();
    132 
    133   const base::FilePath component_policy_cache_dir =
    134       context->GetPath().Append(kPolicy).Append(kComponentsDir);
    135 
    136   scoped_ptr<UserCloudPolicyManager> manager(
    137       new UserCloudPolicyManager(store.Pass(),
    138                                  component_policy_cache_dir,
    139                                  scoped_ptr<CloudExternalDataManager>(),
    140                                  base::MessageLoopProxy::current(),
    141                                  file_task_runner,
    142                                  io_task_runner));
    143   manager->Init(SchemaRegistryServiceFactory::GetForContext(context));
    144   manager_wrappers_[context] = new ManagerWrapper(manager.get());
    145   return manager.Pass();
    146 }
    147 
    148 UserCloudPolicyManager*
    149 UserCloudPolicyManagerFactory::RegisterManagerForOffTheRecordBrowserContext(
    150     content::BrowserContext* original_context,
    151     content::BrowserContext* off_the_record_context) {
    152   // Register the UserCloudPolicyManager of the original context for the
    153   // respective incognito context. See also GetManagerForBrowserContext.
    154   UserCloudPolicyManager* manager =
    155       GetManagerForBrowserContext(original_context);
    156   manager_wrappers_[off_the_record_context] = new ManagerWrapper(manager);
    157   return manager;
    158 }
    159 
    160 void UserCloudPolicyManagerFactory::BrowserContextShutdown(
    161     content::BrowserContext* context) {
    162   if (context->IsOffTheRecord())
    163     return;
    164   ManagerWrapperMap::iterator it = manager_wrappers_.find(context);
    165   // E.g. for a TestingProfile there might not be a manager created.
    166   if (it != manager_wrappers_.end())
    167     it->second->Shutdown();
    168 }
    169 
    170 void UserCloudPolicyManagerFactory::BrowserContextDestroyed(
    171     content::BrowserContext* context) {
    172   ManagerWrapperMap::iterator it = manager_wrappers_.find(context);
    173   if (it != manager_wrappers_.end()) {
    174     // The manager is not owned by the factory, so it's not deleted here.
    175     delete it->second;
    176     manager_wrappers_.erase(it);
    177   }
    178 }
    179 
    180 void UserCloudPolicyManagerFactory::SetEmptyTestingFactory(
    181     content::BrowserContext* context) {}
    182 
    183 void UserCloudPolicyManagerFactory::CreateServiceNow(
    184     content::BrowserContext* context) {}
    185 
    186 }  // namespace policy
    187