1 // Copyright 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/chromeos/extensions/device_local_account_external_policy_loader.h"
6
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_value_map.h"
10 #include "base/values.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/extensions/policy_handlers.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/policy/core/common/policy_map.h"
15 #include "net/url_request/url_request_context_getter.h"
16
17 namespace chromeos {
18
19 DeviceLocalAccountExternalPolicyLoader::
DeviceLocalAccountExternalPolicyLoader(policy::CloudPolicyStore * store,const base::FilePath & cache_dir)20 DeviceLocalAccountExternalPolicyLoader(policy::CloudPolicyStore* store,
21 const base::FilePath& cache_dir)
22 : store_(store),
23 cache_dir_(cache_dir) {
24 }
25
IsCacheRunning() const26 bool DeviceLocalAccountExternalPolicyLoader::IsCacheRunning() const {
27 return external_cache_;
28 }
29
StartCache(const scoped_refptr<base::SequencedTaskRunner> & cache_task_runner)30 void DeviceLocalAccountExternalPolicyLoader::StartCache(
31 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner) {
32 DCHECK(!external_cache_);
33 store_->AddObserver(this);
34 external_cache_.reset(new ExternalCache(
35 cache_dir_,
36 g_browser_process->system_request_context(),
37 cache_task_runner,
38 this,
39 true /* always_check_updates */,
40 false /* wait_for_cache_initialization */));
41
42 if (store_->is_initialized())
43 UpdateExtensionListFromStore();
44 }
45
StopCache(const base::Closure & callback)46 void DeviceLocalAccountExternalPolicyLoader::StopCache(
47 const base::Closure& callback) {
48 if (external_cache_) {
49 external_cache_->Shutdown(callback);
50 external_cache_.reset();
51 store_->RemoveObserver(this);
52 }
53
54 base::DictionaryValue empty_prefs;
55 OnExtensionListsUpdated(&empty_prefs);
56 }
57
StartLoading()58 void DeviceLocalAccountExternalPolicyLoader::StartLoading() {
59 if (prefs_)
60 LoadFinished();
61 }
62
OnStoreLoaded(policy::CloudPolicyStore * store)63 void DeviceLocalAccountExternalPolicyLoader::OnStoreLoaded(
64 policy::CloudPolicyStore* store) {
65 DCHECK(external_cache_);
66 DCHECK_EQ(store_, store);
67 UpdateExtensionListFromStore();
68 }
69
OnStoreError(policy::CloudPolicyStore * store)70 void DeviceLocalAccountExternalPolicyLoader::OnStoreError(
71 policy::CloudPolicyStore* store) {
72 DCHECK(external_cache_);
73 DCHECK_EQ(store_, store);
74 }
75
OnExtensionListsUpdated(const base::DictionaryValue * prefs)76 void DeviceLocalAccountExternalPolicyLoader::OnExtensionListsUpdated(
77 const base::DictionaryValue* prefs) {
78 DCHECK(external_cache_ || prefs->empty());
79 prefs_.reset(prefs->DeepCopy());
80 LoadFinished();
81 }
82
83 DeviceLocalAccountExternalPolicyLoader::
~DeviceLocalAccountExternalPolicyLoader()84 ~DeviceLocalAccountExternalPolicyLoader() {
85 DCHECK(!external_cache_);
86 }
87
UpdateExtensionListFromStore()88 void DeviceLocalAccountExternalPolicyLoader::UpdateExtensionListFromStore() {
89 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
90 const policy::PolicyMap& policy_map = store_->policy_map();
91 extensions::ExtensionInstallForcelistPolicyHandler policy_handler;
92 if (policy_handler.CheckPolicySettings(policy_map, NULL)) {
93 PrefValueMap pref_value_map;
94 policy_handler.ApplyPolicySettings(policy_map, &pref_value_map);
95 const base::Value* value = NULL;
96 const base::DictionaryValue* dict = NULL;
97 if (pref_value_map.GetValue(prefs::kExtensionInstallForceList, &value) &&
98 value->GetAsDictionary(&dict)) {
99 prefs.reset(dict->DeepCopy());
100 }
101 }
102
103 external_cache_->UpdateExtensionsList(prefs.Pass());
104 }
105
106 } // namespace chromeos
107