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"
17 class SingleThreadTaskRunner
;
22 class AsyncPolicyLoader
;
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
{
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
;
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 runner that owns the provider,
54 // if |weak_this| is still valid.
55 static void LoaderUpdateCallback(
56 scoped_refptr
<base::SingleThreadTaskRunner
> runner
,
57 base::WeakPtr
<AsyncPolicyProvider
> weak_this
,
58 scoped_ptr
<PolicyBundle
> bundle
);
60 // The |loader_| that does the platform-specific policy loading. It lives
61 // on the background thread but is owned by |this|.
62 scoped_ptr
<AsyncPolicyLoader
> loader_
;
64 // Callback used to synchronize RefreshPolicies() calls with the background
65 // thread. See the implementation for the details.
66 base::CancelableClosure refresh_callback_
;
68 // Used to get a WeakPtr to |this| for the update callback given to the
70 base::WeakPtrFactory
<AsyncPolicyProvider
> weak_factory_
;
72 DISALLOW_COPY_AND_ASSIGN(AsyncPolicyProvider
);
77 #endif // COMPONENTS_POLICY_CORE_COMMON_ASYNC_POLICY_PROVIDER_H_