summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/asynchronous_policy_provider.h
blob: 26e85e8ab2eba1f2ac2db4b2fb2c276182327581 (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
73
74
75
// 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.

#ifndef CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_PROVIDER_H_
#define CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_PROVIDER_H_
#pragma once

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "chrome/browser/policy/configuration_policy_provider.h"

namespace policy {

class AsynchronousPolicyLoader;
class PolicyBundle;
class PolicyMap;

// Policy provider that loads policy asynchronously. Providers should subclass
// from this class if loading the policy requires disk access or must for some
// other reason be performed on the file thread. The actual logic for loading
// policy is handled by a delegate passed at construction time.
class AsynchronousPolicyProvider
    : public ConfigurationPolicyProvider,
      public base::NonThreadSafe {
 public:
  // Must be implemented by subclasses of the asynchronous policy provider to
  // provide the implementation details of how policy is loaded.
  class Delegate {
   public:
    virtual ~Delegate() {}

    // Load policy from the delegate's source, and return a PolicyMap. Ownership
    // is transferred to the caller.
    virtual PolicyMap* Load() = 0;
  };

  // Assumes ownership of |loader|.
  AsynchronousPolicyProvider(
      const PolicyDefinitionList* policy_list,
      scoped_refptr<AsynchronousPolicyLoader> loader);
  virtual ~AsynchronousPolicyProvider();

  // ConfigurationPolicyProvider implementation.
  virtual void RefreshPolicies() OVERRIDE;

 private:
  // Used to trigger a Reload on |loader| on the FILE thread.
  static void PostReloadOnFileThread(AsynchronousPolicyLoader* loader);

  // Used to notify UI that a reload task has been submitted.
  void OnReloadPosted();

  // Callback from the loader. This is invoked whenever the loader has completed
  // a reload of the policies.
  void OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle);

  // The loader object used internally.
  scoped_refptr<AsynchronousPolicyLoader> loader_;

  // Number of refreshes requested whose reload is still pending. Used to only
  // fire notifications when all pending refreshes are done.
  int pending_refreshes_;

  // Used to post tasks to self on UI.
  base::WeakPtrFactory<AsynchronousPolicyProvider> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyProvider);
};

}  // namespace policy

#endif  // CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_PROVIDER_H_