1 // Copyright (c) 2012 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 CHROME_BROWSER_POLICY_ASYNC_POLICY_PROVIDER_H_
6 #define CHROME_BROWSER_POLICY_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 "chrome/browser/policy/configuration_policy_provider.h"
16 class MessageLoopProxy;
21 class AsyncPolicyLoader;
24 // A policy provider that loads its policies asynchronously on a background
25 // thread. Platform-specific providers are created by passing an implementation
26 // of AsyncPolicyLoader to a new AsyncPolicyProvider.
27 class AsyncPolicyProvider : public ConfigurationPolicyProvider,
28 public base::NonThreadSafe {
30 explicit AsyncPolicyProvider(scoped_ptr<AsyncPolicyLoader> loader);
31 virtual ~AsyncPolicyProvider();
33 // ConfigurationPolicyProvider implementation.
34 virtual void Init() OVERRIDE;
35 virtual void Shutdown() OVERRIDE;
36 virtual void RefreshPolicies() OVERRIDE;
37 virtual void RegisterPolicyDomain(
38 scoped_refptr<const PolicyDomainDescriptor> descriptor) OVERRIDE;
41 // Helper for RefreshPolicies().
42 void ReloadAfterRefreshSync();
44 // Invoked with the latest bundle loaded by the |loader_|.
45 void OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle);
47 // Callback passed to the loader that it uses to pass back the current policy
48 // bundle to the provider. This is invoked on the background thread and
49 // forwards to OnLoaderReloaded() on the loop that owns the provider,
50 // if |weak_this| is still valid.
51 static void LoaderUpdateCallback(scoped_refptr<base::MessageLoopProxy> loop,
52 base::WeakPtr<AsyncPolicyProvider> weak_this,
53 scoped_ptr<PolicyBundle> bundle);
55 // The |loader_| that does the platform-specific policy loading. It lives
56 // on the background thread but is owned by |this|.
57 AsyncPolicyLoader* loader_;
59 // Used to get a WeakPtr to |this| for the update callback given to the
61 base::WeakPtrFactory<AsyncPolicyProvider> weak_factory_;
63 // Callback used to synchronize RefreshPolicies() calls with the background
64 // thread. See the implementation for the details.
65 base::CancelableClosure refresh_callback_;
67 DISALLOW_COPY_AND_ASSIGN(AsyncPolicyProvider);
72 #endif // CHROME_BROWSER_POLICY_ASYNC_POLICY_PROVIDER_H_