summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/asynchronous_policy_provider.cc
blob: 01a647c5ab7b9d0dd11f4c76b1c59b187039dcec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/asynchronous_policy_provider.h"

#include "base/bind.h"
#include "chrome/browser/policy/asynchronous_policy_loader.h"
#include "chrome/browser/policy/policy_map.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace policy {

AsynchronousPolicyProvider::AsynchronousPolicyProvider(
    const PolicyDefinitionList* policy_list,
    scoped_refptr<AsynchronousPolicyLoader> loader)
    : ConfigurationPolicyProvider(policy_list),
      loader_(loader),
      pending_refreshes_(0),
      ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
  loader_->Init(
      base::Bind(&AsynchronousPolicyProvider::OnLoaderReloaded,
                 base::Unretained(this)));
}

AsynchronousPolicyProvider::~AsynchronousPolicyProvider() {
  DCHECK(CalledOnValidThread());
  // |loader_| won't invoke its callback anymore after Stop(), therefore
  // Unretained(this) is safe in the ctor.
  loader_->Stop();
}

bool AsynchronousPolicyProvider::ProvideInternal(PolicyMap* map) {
  DCHECK(CalledOnValidThread());
  map->CopyFrom(loader_->policy());
  return true;
}

void AsynchronousPolicyProvider::RefreshPolicies() {
  DCHECK(CalledOnValidThread());
  pending_refreshes_++;
  BrowserThread::PostTaskAndReply(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&AsynchronousPolicyProvider::PostReloadOnFileThread,
                 loader_),
      base::Bind(&AsynchronousPolicyProvider::OnReloadPosted,
                 weak_ptr_factory_.GetWeakPtr()));
}

// static
void AsynchronousPolicyProvider::PostReloadOnFileThread(
    AsynchronousPolicyLoader* loader) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&AsynchronousPolicyLoader::Reload, loader, true));
}

void AsynchronousPolicyProvider::OnReloadPosted() {
  DCHECK(CalledOnValidThread());
  pending_refreshes_--;
}

void AsynchronousPolicyProvider::OnLoaderReloaded() {
  DCHECK(CalledOnValidThread());
  if (pending_refreshes_ == 0)
    NotifyPolicyUpdated();
}

}  // namespace policy