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#ifndef COMPONENTS_POLICY_CORE_COMMON_ASYNC_POLICY_PROVIDER_H_
6#define COMPONENTS_POLICY_CORE_COMMON_ASYNC_POLICY_PROVIDER_H_
7
8#include "base/cancelable_callback.h"
9#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "base/threading/non_thread_safe.h"
13#include "components/policy/core/common/configuration_policy_provider.h"
14#include "components/policy/policy_export.h"
15
16namespace base {
17class MessageLoopProxy;
18}
19
20namespace policy {
21
22class AsyncPolicyLoader;
23class PolicyBundle;
24class SchemaRegistry;
25
26// A policy provider that loads its policies asynchronously on a background
27// thread. Platform-specific providers are created by passing an implementation
28// of AsyncPolicyLoader to a new AsyncPolicyProvider.
29class POLICY_EXPORT AsyncPolicyProvider : public ConfigurationPolicyProvider,
30                                          public base::NonThreadSafe {
31 public:
32  // The AsyncPolicyProvider does a synchronous load in its constructor, and
33  // therefore it needs the |registry| at construction time. The same |registry|
34  // should be passed later to Init().
35  AsyncPolicyProvider(SchemaRegistry* registry,
36                      scoped_ptr<AsyncPolicyLoader> loader);
37  virtual ~AsyncPolicyProvider();
38
39  // ConfigurationPolicyProvider implementation.
40  virtual void Init(SchemaRegistry* registry) OVERRIDE;
41  virtual void Shutdown() OVERRIDE;
42  virtual void RefreshPolicies() OVERRIDE;
43
44 private:
45  // Helper for RefreshPolicies().
46  void ReloadAfterRefreshSync();
47
48  // Invoked with the latest bundle loaded by the |loader_|.
49  void OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle);
50
51  // Callback passed to the loader that it uses to pass back the current policy
52  // bundle to the provider. This is invoked on the background thread and
53  // forwards to OnLoaderReloaded() on the loop that owns the provider,
54  // if |weak_this| is still valid.
55  static void LoaderUpdateCallback(scoped_refptr<base::MessageLoopProxy> loop,
56                                   base::WeakPtr<AsyncPolicyProvider> weak_this,
57                                   scoped_ptr<PolicyBundle> bundle);
58
59  // The |loader_| that does the platform-specific policy loading. It lives
60  // on the background thread but is owned by |this|.
61  AsyncPolicyLoader* loader_;
62
63  // Callback used to synchronize RefreshPolicies() calls with the background
64  // thread. See the implementation for the details.
65  base::CancelableClosure refresh_callback_;
66
67  // Used to get a WeakPtr to |this| for the update callback given to the
68  // loader.
69  base::WeakPtrFactory<AsyncPolicyProvider> weak_factory_;
70
71  DISALLOW_COPY_AND_ASSIGN(AsyncPolicyProvider);
72};
73
74}  // namespace policy
75
76#endif  // COMPONENTS_POLICY_CORE_COMMON_ASYNC_POLICY_PROVIDER_H_
77