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.
5 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
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 "components/policy/core/common/policy_map.h"
14 #include "extensions/browser/pref_names.h"
15 #include "net/url_request/url_request_context_getter.h"
19 DeviceLocalAccountExternalPolicyLoader::
20 DeviceLocalAccountExternalPolicyLoader(policy::CloudPolicyStore
* store
,
21 const base::FilePath
& cache_dir
)
23 cache_dir_(cache_dir
) {
26 bool DeviceLocalAccountExternalPolicyLoader::IsCacheRunning() const {
27 return external_cache_
;
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(
36 g_browser_process
->system_request_context(),
39 true /* always_check_updates */,
40 false /* wait_for_cache_initialization */));
42 if (store_
->is_initialized())
43 UpdateExtensionListFromStore();
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);
54 base::DictionaryValue empty_prefs
;
55 OnExtensionListsUpdated(&empty_prefs
);
58 void DeviceLocalAccountExternalPolicyLoader::StartLoading() {
63 void DeviceLocalAccountExternalPolicyLoader::OnStoreLoaded(
64 policy::CloudPolicyStore
* store
) {
65 DCHECK(external_cache_
);
66 DCHECK_EQ(store_
, store
);
67 UpdateExtensionListFromStore();
70 void DeviceLocalAccountExternalPolicyLoader::OnStoreError(
71 policy::CloudPolicyStore
* store
) {
72 DCHECK(external_cache_
);
73 DCHECK_EQ(store_
, store
);
76 void DeviceLocalAccountExternalPolicyLoader::OnExtensionListsUpdated(
77 const base::DictionaryValue
* prefs
) {
78 DCHECK(external_cache_
|| prefs
->empty());
79 prefs_
.reset(prefs
->DeepCopy());
84 DeviceLocalAccountExternalPolicyLoader::GetExternalCacheForTesting() {
85 return external_cache_
.get();
88 DeviceLocalAccountExternalPolicyLoader::
89 ~DeviceLocalAccountExternalPolicyLoader() {
90 DCHECK(!external_cache_
);
93 void DeviceLocalAccountExternalPolicyLoader::UpdateExtensionListFromStore() {
94 scoped_ptr
<base::DictionaryValue
> prefs(new base::DictionaryValue
);
95 const policy::PolicyMap
& policy_map
= store_
->policy_map();
96 // TODO(binjin): Use two policy handlers here after
97 // ExtensionManagementPolicyHandler is introduced.
98 extensions::ExtensionInstallForcelistPolicyHandler policy_handler
;
99 if (policy_handler
.CheckPolicySettings(policy_map
, NULL
)) {
100 PrefValueMap pref_value_map
;
101 policy_handler
.ApplyPolicySettings(policy_map
, &pref_value_map
);
102 const base::Value
* value
= NULL
;
103 const base::DictionaryValue
* dict
= NULL
;
104 if (pref_value_map
.GetValue(extensions::pref_names::kInstallForceList
,
106 value
->GetAsDictionary(&dict
)) {
107 prefs
.reset(dict
->DeepCopy());
111 external_cache_
->UpdateExtensionsList(prefs
.Pass());
114 } // namespace chromeos