Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / components / policy / core / common / async_policy_provider.h
blob63e33865e9c2c061ed9f7422d9fef30525b8f1ee
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.
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_ASYNC_POLICY_PROVIDER_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_ASYNC_POLICY_PROVIDER_H_
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"
16 namespace base {
17 class MessageLoopProxy;
20 namespace policy {
22 class AsyncPolicyLoader;
23 class PolicyBundle;
24 class SchemaRegistry;
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.
29 class 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 ~AsyncPolicyProvider() override;
39 // ConfigurationPolicyProvider implementation.
40 void Init(SchemaRegistry* registry) override;
41 void Shutdown() override;
42 void RefreshPolicies() override;
44 private:
45 // Helper for RefreshPolicies().
46 void ReloadAfterRefreshSync();
48 // Invoked with the latest bundle loaded by the |loader_|.
49 void OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle);
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);
59 // The |loader_| that does the platform-specific policy loading. It lives
60 // on the background thread but is owned by |this|.
61 scoped_ptr<AsyncPolicyLoader> loader_;
63 // Callback used to synchronize RefreshPolicies() calls with the background
64 // thread. See the implementation for the details.
65 base::CancelableClosure refresh_callback_;
67 // Used to get a WeakPtr to |this| for the update callback given to the
68 // loader.
69 base::WeakPtrFactory<AsyncPolicyProvider> weak_factory_;
71 DISALLOW_COPY_AND_ASSIGN(AsyncPolicyProvider);
74 } // namespace policy
76 #endif // COMPONENTS_POLICY_CORE_COMMON_ASYNC_POLICY_PROVIDER_H_