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