summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.cc
blob: 49b67d047aeb1a68e661401c7c877e5fa0baf42b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"

#include <utility>

#include "base/callback.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/policy_handlers.h"
#include "components/policy/core/common/policy_map.h"
#include "components/prefs/pref_value_map.h"
#include "extensions/browser/pref_names.h"
#include "net/url_request/url_request_context_getter.h"

namespace chromeos {

DeviceLocalAccountExternalPolicyLoader::
    DeviceLocalAccountExternalPolicyLoader(policy::CloudPolicyStore* store,
                                           const base::FilePath& cache_dir)
    : store_(store),
      cache_dir_(cache_dir) {
}

bool DeviceLocalAccountExternalPolicyLoader::IsCacheRunning() const {
  return external_cache_;
}

void DeviceLocalAccountExternalPolicyLoader::StartCache(
    const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner) {
  DCHECK(!external_cache_);
  store_->AddObserver(this);
  external_cache_.reset(new ExternalCache(
      cache_dir_,
      g_browser_process->system_request_context(),
      cache_task_runner,
      this,
      true  /* always_check_updates */,
      false  /* wait_for_cache_initialization */));

  if (store_->is_initialized())
    UpdateExtensionListFromStore();
}

void DeviceLocalAccountExternalPolicyLoader::StopCache(
    const base::Closure& callback) {
  if (external_cache_) {
    external_cache_->Shutdown(callback);
    external_cache_.reset();
    store_->RemoveObserver(this);
  }

  base::DictionaryValue empty_prefs;
  OnExtensionListsUpdated(&empty_prefs);
}

void DeviceLocalAccountExternalPolicyLoader::StartLoading() {
  if (prefs_)
    LoadFinished();
}

void DeviceLocalAccountExternalPolicyLoader::OnStoreLoaded(
    policy::CloudPolicyStore* store) {
  DCHECK(external_cache_);
  DCHECK_EQ(store_, store);
  UpdateExtensionListFromStore();
}

void DeviceLocalAccountExternalPolicyLoader::OnStoreError(
    policy::CloudPolicyStore* store) {
  DCHECK(external_cache_);
  DCHECK_EQ(store_, store);
}

void DeviceLocalAccountExternalPolicyLoader::OnExtensionListsUpdated(
    const base::DictionaryValue* prefs) {
  DCHECK(external_cache_ || prefs->empty());
  prefs_.reset(prefs->DeepCopy());
  LoadFinished();
}

ExternalCache*
DeviceLocalAccountExternalPolicyLoader::GetExternalCacheForTesting() {
  return external_cache_.get();
}

DeviceLocalAccountExternalPolicyLoader::
    ~DeviceLocalAccountExternalPolicyLoader() {
  DCHECK(!external_cache_);
}

void DeviceLocalAccountExternalPolicyLoader::UpdateExtensionListFromStore() {
  scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
  const policy::PolicyMap& policy_map = store_->policy_map();
  // TODO(binjin): Use two policy handlers here after
  // ExtensionManagementPolicyHandler is introduced.
  extensions::ExtensionInstallForcelistPolicyHandler policy_handler;
  if (policy_handler.CheckPolicySettings(policy_map, NULL)) {
    PrefValueMap pref_value_map;
    policy_handler.ApplyPolicySettings(policy_map, &pref_value_map);
    const base::Value* value = NULL;
    const base::DictionaryValue* dict = NULL;
    if (pref_value_map.GetValue(extensions::pref_names::kInstallForceList,
                                &value) &&
        value->GetAsDictionary(&dict)) {
      prefs.reset(dict->DeepCopy());
    }
  }

  external_cache_->UpdateExtensionsList(std::move(prefs));
}

}  // namespace chromeos