18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/callback.h"
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/logging.h"
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/prefs/pref_value_map.h"
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/values.h"
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/browser_process.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/policy_handlers.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/common/pref_names.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/policy_map.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/pref_names.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "net/url_request/url_request_context_getter.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace chromeos {
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DeviceLocalAccountExternalPolicyLoader::
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    DeviceLocalAccountExternalPolicyLoader(policy::CloudPolicyStore* store,
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                           const base::FilePath& cache_dir)
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : store_(store),
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      cache_dir_(cache_dir) {
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool DeviceLocalAccountExternalPolicyLoader::IsCacheRunning() const {
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return external_cache_;
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DeviceLocalAccountExternalPolicyLoader::StartCache(
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner) {
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(!external_cache_);
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  store_->AddObserver(this);
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  external_cache_.reset(new ExternalCache(
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      cache_dir_,
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      g_browser_process->system_request_context(),
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      cache_task_runner,
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      this,
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      true  /* always_check_updates */,
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      false  /* wait_for_cache_initialization */));
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (store_->is_initialized())
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    UpdateExtensionListFromStore();
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DeviceLocalAccountExternalPolicyLoader::StopCache(
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const base::Closure& callback) {
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (external_cache_) {
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    external_cache_->Shutdown(callback);
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    external_cache_.reset();
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    store_->RemoveObserver(this);
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::DictionaryValue empty_prefs;
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  OnExtensionListsUpdated(&empty_prefs);
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DeviceLocalAccountExternalPolicyLoader::StartLoading() {
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (prefs_)
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    LoadFinished();
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DeviceLocalAccountExternalPolicyLoader::OnStoreLoaded(
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    policy::CloudPolicyStore* store) {
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(external_cache_);
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK_EQ(store_, store);
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  UpdateExtensionListFromStore();
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DeviceLocalAccountExternalPolicyLoader::OnStoreError(
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    policy::CloudPolicyStore* store) {
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(external_cache_);
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK_EQ(store_, store);
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DeviceLocalAccountExternalPolicyLoader::OnExtensionListsUpdated(
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const base::DictionaryValue* prefs) {
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(external_cache_ || prefs->empty());
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  prefs_.reset(prefs->DeepCopy());
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  LoadFinished();
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ExternalCache*
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)DeviceLocalAccountExternalPolicyLoader::GetExternalCacheForTesting() {
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return external_cache_.get();
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DeviceLocalAccountExternalPolicyLoader::
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ~DeviceLocalAccountExternalPolicyLoader() {
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(!external_cache_);
928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DeviceLocalAccountExternalPolicyLoader::UpdateExtensionListFromStore() {
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const policy::PolicyMap& policy_map = store_->policy_map();
971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // TODO(binjin): Use two policy handlers here after
981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // ExtensionManagementPolicyHandler is introduced.
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  extensions::ExtensionInstallForcelistPolicyHandler policy_handler;
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (policy_handler.CheckPolicySettings(policy_map, NULL)) {
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    PrefValueMap pref_value_map;
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    policy_handler.ApplyPolicySettings(policy_map, &pref_value_map);
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const base::Value* value = NULL;
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const base::DictionaryValue* dict = NULL;
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (pref_value_map.GetValue(extensions::pref_names::kInstallForceList,
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                &value) &&
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        value->GetAsDictionary(&dict)) {
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      prefs.reset(dict->DeepCopy());
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  external_cache_->UpdateExtensionsList(prefs.Pass());
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace chromeos
116