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 #include "chrome/browser/policy/profile_policy_connector.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/bind.h"
     10 #include "base/logging.h"
     11 #include "chrome/browser/browser_process.h"
     12 #include "chrome/browser/policy/browser_policy_connector.h"
     13 #include "components/policy/core/common/cloud/cloud_policy_manager.h"
     14 #include "components/policy/core/common/configuration_policy_provider.h"
     15 #include "components/policy/core/common/forwarding_policy_provider.h"
     16 #include "components/policy/core/common/policy_service_impl.h"
     17 
     18 #if defined(OS_CHROMEOS)
     19 #include "chrome/browser/chromeos/login/user.h"
     20 #include "chrome/browser/chromeos/login/user_manager.h"
     21 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
     22 #include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
     23 #include "chrome/browser/chromeos/policy/login_profile_policy_provider.h"
     24 #endif
     25 
     26 namespace policy {
     27 
     28 ProfilePolicyConnector::ProfilePolicyConnector()
     29 #if defined(OS_CHROMEOS)
     30     : is_primary_user_(false)
     31 #endif
     32       {}
     33 
     34 ProfilePolicyConnector::~ProfilePolicyConnector() {}
     35 
     36 void ProfilePolicyConnector::Init(
     37     bool force_immediate_load,
     38 #if defined(OS_CHROMEOS)
     39     const chromeos::User* user,
     40 #endif
     41     SchemaRegistry* schema_registry,
     42     CloudPolicyManager* user_cloud_policy_manager) {
     43   // |providers| contains a list of the policy providers available for the
     44   // PolicyService of this connector, in decreasing order of priority.
     45   //
     46   // Note: all the providers appended to this vector must eventually become
     47   // initialized for every policy domain, otherwise some subsystems will never
     48   // use the policies exposed by the PolicyService!
     49   // The default ConfigurationPolicyProvider::IsInitializationComplete()
     50   // result is true, so take care if a provider overrides that.
     51   std::vector<ConfigurationPolicyProvider*> providers;
     52 
     53   BrowserPolicyConnector* connector =
     54       g_browser_process->browser_policy_connector();
     55 
     56   if (connector->GetPlatformProvider()) {
     57     forwarding_policy_provider_.reset(
     58         new ForwardingPolicyProvider(connector->GetPlatformProvider()));
     59     forwarding_policy_provider_->Init(schema_registry);
     60     providers.push_back(forwarding_policy_provider_.get());
     61   }
     62 
     63 #if defined(OS_CHROMEOS)
     64   if (connector->GetDeviceCloudPolicyManager())
     65     providers.push_back(connector->GetDeviceCloudPolicyManager());
     66 #endif
     67 
     68   if (user_cloud_policy_manager)
     69     providers.push_back(user_cloud_policy_manager);
     70 
     71 #if defined(OS_CHROMEOS)
     72   if (!user) {
     73     DCHECK(schema_registry);
     74     // This case occurs for the signin profile.
     75     special_user_policy_provider_.reset(
     76         new LoginProfilePolicyProvider(connector->GetPolicyService()));
     77     special_user_policy_provider_->Init(schema_registry);
     78   } else {
     79     // |user| should never be NULL except for the signin profile.
     80     is_primary_user_ = user == chromeos::UserManager::Get()->GetPrimaryUser();
     81     if (user->GetType() == chromeos::User::USER_TYPE_PUBLIC_ACCOUNT) {
     82       InitializeDeviceLocalAccountPolicyProvider(user->email(),
     83                                                  schema_registry);
     84     }
     85   }
     86   if (special_user_policy_provider_)
     87     providers.push_back(special_user_policy_provider_.get());
     88 #endif
     89 
     90   policy_service_.reset(new PolicyServiceImpl(providers));
     91 
     92 #if defined(OS_CHROMEOS)
     93   if (is_primary_user_) {
     94     if (user_cloud_policy_manager)
     95       connector->SetUserPolicyDelegate(user_cloud_policy_manager);
     96     else if (special_user_policy_provider_)
     97       connector->SetUserPolicyDelegate(special_user_policy_provider_.get());
     98   }
     99 #endif
    100 }
    101 
    102 void ProfilePolicyConnector::InitForTesting(scoped_ptr<PolicyService> service) {
    103   policy_service_ = service.Pass();
    104 }
    105 
    106 void ProfilePolicyConnector::Shutdown() {
    107 #if defined(OS_CHROMEOS)
    108   if (is_primary_user_)
    109     g_browser_process->browser_policy_connector()->SetUserPolicyDelegate(NULL);
    110   if (special_user_policy_provider_)
    111     special_user_policy_provider_->Shutdown();
    112 #endif
    113   if (forwarding_policy_provider_)
    114     forwarding_policy_provider_->Shutdown();
    115 }
    116 
    117 #if defined(OS_CHROMEOS)
    118 void ProfilePolicyConnector::InitializeDeviceLocalAccountPolicyProvider(
    119     const std::string& username,
    120     SchemaRegistry* schema_registry) {
    121   BrowserPolicyConnector* connector =
    122       g_browser_process->browser_policy_connector();
    123   DeviceLocalAccountPolicyService* device_local_account_policy_service =
    124       connector->GetDeviceLocalAccountPolicyService();
    125   if (!device_local_account_policy_service)
    126     return;
    127   special_user_policy_provider_.reset(new DeviceLocalAccountPolicyProvider(
    128       username, device_local_account_policy_service));
    129   special_user_policy_provider_->Init(schema_registry);
    130 }
    131 #endif
    132 
    133 }  // namespace policy
    134