This source file includes following definitions.
- weak_factory_
- Init
- Shutdown
- RefreshPolicies
- ReloadAfterRefreshSync
- OnLoaderReloaded
- LoaderUpdateCallback
#include "components/policy/core/common/async_policy_provider.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/sequenced_task_runner.h"
#include "components/policy/core/common/async_policy_loader.h"
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/schema_registry.h"
namespace policy {
AsyncPolicyProvider::AsyncPolicyProvider(
SchemaRegistry* registry,
scoped_ptr<AsyncPolicyLoader> loader)
: loader_(loader.release()),
weak_factory_(this) {
OnLoaderReloaded(loader_->InitialLoad(registry->schema_map()));
}
AsyncPolicyProvider::~AsyncPolicyProvider() {
DCHECK(CalledOnValidThread());
DCHECK(!loader_);
}
void AsyncPolicyProvider::Init(SchemaRegistry* registry) {
DCHECK(CalledOnValidThread());
ConfigurationPolicyProvider::Init(registry);
if (!loader_)
return;
AsyncPolicyLoader::UpdateCallback callback =
base::Bind(&AsyncPolicyProvider::LoaderUpdateCallback,
base::MessageLoopProxy::current(),
weak_factory_.GetWeakPtr());
bool post = loader_->task_runner()->PostTask(
FROM_HERE,
base::Bind(&AsyncPolicyLoader::Init,
base::Unretained(loader_),
callback));
DCHECK(post) << "AsyncPolicyProvider::Init() called with threads not running";
}
void AsyncPolicyProvider::Shutdown() {
DCHECK(CalledOnValidThread());
if (!loader_->task_runner()->DeleteSoon(FROM_HERE, loader_)) {
delete loader_;
}
loader_ = NULL;
ConfigurationPolicyProvider::Shutdown();
}
void AsyncPolicyProvider::RefreshPolicies() {
DCHECK(CalledOnValidThread());
if (!loader_)
return;
refresh_callback_.Reset(
base::Bind(&AsyncPolicyProvider::ReloadAfterRefreshSync,
weak_factory_.GetWeakPtr()));
loader_->task_runner()->PostTaskAndReply(
FROM_HERE,
base::Bind(base::DoNothing),
refresh_callback_.callback());
}
void AsyncPolicyProvider::ReloadAfterRefreshSync() {
DCHECK(CalledOnValidThread());
DCHECK(!refresh_callback_.IsCancelled());
refresh_callback_.Cancel();
if (!loader_)
return;
loader_->task_runner()->PostTask(
FROM_HERE,
base::Bind(&AsyncPolicyLoader::RefreshPolicies,
base::Unretained(loader_),
schema_map()));
}
void AsyncPolicyProvider::OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle) {
DCHECK(CalledOnValidThread());
if (refresh_callback_.IsCancelled() && loader_)
UpdatePolicy(bundle.Pass());
}
void AsyncPolicyProvider::LoaderUpdateCallback(
scoped_refptr<base::MessageLoopProxy> loop,
base::WeakPtr<AsyncPolicyProvider> weak_this,
scoped_ptr<PolicyBundle> bundle) {
loop->PostTask(FROM_HERE,
base::Bind(&AsyncPolicyProvider::OnLoaderReloaded,
weak_this,
base::Passed(&bundle)));
}
}