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