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/policy/device_local_account_external_data_service.h"
6
7#include <set>
8
9#include "base/bind.h"
10#include "base/bind_helpers.h"
11#include "base/files/file_path.h"
12#include "base/location.h"
13#include "base/logging.h"
14#include "base/path_service.h"
15#include "base/sequenced_task_runner.h"
16#include "chromeos/chromeos_paths.h"
17#include "components/policy/core/common/cloud/cloud_policy_store.h"
18#include "policy/policy_constants.h"
19
20namespace policy {
21
22DeviceLocalAccountExternalDataService::DeviceLocalAccountExternalDataService(
23    DeviceLocalAccountPolicyService* parent,
24    scoped_refptr<base::SequencedTaskRunner> backend_task_runner,
25    scoped_refptr<base::SequencedTaskRunner> io_task_runner)
26    : parent_(parent),
27      backend_task_runner_(backend_task_runner),
28      io_task_runner_(io_task_runner) {
29  base::FilePath cache_dir;
30  CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTERNAL_DATA,
31                         &cache_dir));
32  resource_cache_.reset(new ResourceCache(cache_dir, backend_task_runner));
33  parent_->AddObserver(this);
34}
35
36DeviceLocalAccountExternalDataService::
37    ~DeviceLocalAccountExternalDataService() {
38  parent_->RemoveObserver(this);
39#if !defined(NDEBUG)
40  for (ExternalDataManagerMap::const_iterator it =
41           external_data_managers_.begin();
42       it != external_data_managers_.end(); ++it) {
43    DCHECK(it->second->HasOneRef());
44  }
45#endif  // !defined(NDEBUG)
46  backend_task_runner_->DeleteSoon(FROM_HERE, resource_cache_.release());
47}
48
49void DeviceLocalAccountExternalDataService::OnPolicyUpdated(
50    const std::string& user_id) {
51}
52
53void DeviceLocalAccountExternalDataService::OnDeviceLocalAccountsChanged() {
54  std::set<std::string> account_ids;
55  for (ExternalDataManagerMap::iterator it = external_data_managers_.begin();
56       it != external_data_managers_.end(); ) {
57    if (it->second->HasOneRef()) {
58      external_data_managers_.erase(it++);
59    } else {
60      account_ids.insert(it->first);
61      ++it;
62    }
63  }
64  backend_task_runner_->PostTask(FROM_HERE, base::Bind(
65      &ResourceCache::PurgeOtherKeys,
66      base::Unretained(resource_cache_.get()),
67      account_ids));
68}
69
70scoped_refptr<DeviceLocalAccountExternalDataManager>
71    DeviceLocalAccountExternalDataService::GetExternalDataManager(
72        const std::string& account_id,
73        CloudPolicyStore* policy_store) {
74  scoped_refptr<DeviceLocalAccountExternalDataManager>& external_data_manager =
75      external_data_managers_[account_id];
76  if (!external_data_manager.get()) {
77    external_data_manager = new DeviceLocalAccountExternalDataManager(
78        account_id,
79        base::Bind(&GetChromePolicyDetails),
80        backend_task_runner_,
81        io_task_runner_,
82        resource_cache_.get());
83  }
84  external_data_manager->SetPolicyStore(policy_store);
85  return external_data_manager;
86}
87
88}  // namespace policy
89