diff options
author | joaodasilva@chromium.org <joaodasilva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-06 17:30:29 +0000 |
---|---|---|
committer | joaodasilva@chromium.org <joaodasilva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-06 17:30:29 +0000 |
commit | bbfacc89f9f8eb332c9193d272249592d226ee8e (patch) | |
tree | 60cc824ac61febbe77b9f4fa2744a486a2560701 /chrome/browser/policy | |
parent | e617cd7ae2ac3ddf95778feef1c2da005070c9a2 (diff) | |
download | chromium_src-bbfacc89f9f8eb332c9193d272249592d226ee8e.zip chromium_src-bbfacc89f9f8eb332c9193d272249592d226ee8e.tar.gz chromium_src-bbfacc89f9f8eb332c9193d272249592d226ee8e.tar.bz2 |
Delete old policy code after AsyncPolicyLoader refactor.
BUG=130918
TEST=nothing breaks
Review URL: https://chromiumcodereview.appspot.com/10500013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@140784 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/policy')
13 files changed, 0 insertions, 1117 deletions
diff --git a/chrome/browser/policy/asynchronous_policy_loader.cc b/chrome/browser/policy/asynchronous_policy_loader.cc deleted file mode 100644 index 104003a..0000000 --- a/chrome/browser/policy/asynchronous_policy_loader.cc +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/policy/asynchronous_policy_loader.h" - -#include "base/bind.h" -#include "base/bind_helpers.h" -#include "base/message_loop.h" -#include "chrome/browser/policy/policy_bundle.h" -#include "content/public/browser/browser_thread.h" - -using content::BrowserThread; - -namespace policy { - -AsynchronousPolicyLoader::AsynchronousPolicyLoader( - AsynchronousPolicyProvider::Delegate* delegate, - int reload_interval_minutes) - : delegate_(delegate), - ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), - reload_interval_(base::TimeDelta::FromMinutes(reload_interval_minutes)), - origin_loop_(MessageLoop::current()), - stopped_(false) {} - -void AsynchronousPolicyLoader::Init(const UpdateCallback& callback) { - update_callback_ = callback; - - // Load initial policy synchronously at startup. - scoped_ptr<PolicyBundle> policy(delegate_->Load()); - if (policy.get()) - UpdatePolicy(policy.Pass()); - - // Initialization can happen early when the file thread is not yet available, - // but the subclass of the loader must do some of their initialization on the - // file thread. Posting to the file thread directly before it is initialized - // will cause the task to be forgotten. Instead, post a task to the ui thread - // to delay the remainder of initialization until threading is fully - // initialized. - BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, - base::Bind(&AsynchronousPolicyLoader::InitAfterFileThreadAvailable, - this)); -} - -void AsynchronousPolicyLoader::Stop() { - if (!stopped_) { - stopped_ = true; - BrowserThread::PostTask( - BrowserThread::FILE, FROM_HERE, - base::Bind(&AsynchronousPolicyLoader::StopOnFileThread, this)); - } -} - -AsynchronousPolicyLoader::~AsynchronousPolicyLoader() { -} - -void AsynchronousPolicyLoader::Reload(bool force) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - if (delegate_.get()) - PostUpdatePolicyTask(delegate_->Load().Pass()); -} - -void AsynchronousPolicyLoader::CancelReloadTask() { - weak_ptr_factory_.InvalidateWeakPtrs(); -} - -void AsynchronousPolicyLoader::ScheduleReloadTask( - const base::TimeDelta& delay) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - - CancelReloadTask(); - - BrowserThread::PostDelayedTask( - BrowserThread::FILE, FROM_HERE, - base::Bind(&AsynchronousPolicyLoader::ReloadFromTask, - weak_ptr_factory_.GetWeakPtr()), - delay); -} - -void AsynchronousPolicyLoader::ScheduleFallbackReloadTask() { - // As a safeguard in case that the load delegate failed to timely notice a - // change in policy, schedule a reload task that'll make us recheck after a - // reasonable interval. - ScheduleReloadTask(reload_interval_); -} - -void AsynchronousPolicyLoader::ReloadFromTask() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - Reload(false); -} - -void AsynchronousPolicyLoader::InitOnFileThread() { -} - -void AsynchronousPolicyLoader::StopOnFileThread() { - delegate_.reset(); - CancelReloadTask(); -} - -void AsynchronousPolicyLoader::PostUpdatePolicyTask( - scoped_ptr<PolicyBundle> bundle) { - origin_loop_->PostTask( - FROM_HERE, - base::Bind(&AsynchronousPolicyLoader::UpdatePolicy, - this, base::Passed(&bundle))); -} - -void AsynchronousPolicyLoader::UpdatePolicy(scoped_ptr<PolicyBundle> bundle) { - if (!stopped_) - update_callback_.Run(bundle.Pass()); -} - -void AsynchronousPolicyLoader::InitAfterFileThreadAvailable() { - if (!stopped_) { - BrowserThread::PostTask( - BrowserThread::FILE, FROM_HERE, - base::Bind(&AsynchronousPolicyLoader::InitOnFileThread, this)); - } -} - -} // namespace policy diff --git a/chrome/browser/policy/asynchronous_policy_loader.h b/chrome/browser/policy/asynchronous_policy_loader.h deleted file mode 100644 index ee62cdc..0000000 --- a/chrome/browser/policy/asynchronous_policy_loader.h +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_LOADER_H_ -#define CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_LOADER_H_ -#pragma once - -#include "base/callback.h" -#include "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" -#include "base/memory/weak_ptr.h" -#include "base/time.h" -#include "chrome/browser/policy/asynchronous_policy_provider.h" - -class MessageLoop; - -namespace policy { - -class PolicyBundle; - -// Used by the implementation of asynchronous policy provider to manage the -// tasks on the FILE thread that do the heavy lifting of loading policies. -class AsynchronousPolicyLoader - : public base::RefCountedThreadSafe<AsynchronousPolicyLoader> { - public: - // The type of the callback passed to Init(). - typedef base::Callback<void(scoped_ptr<PolicyBundle>)> UpdateCallback; - - AsynchronousPolicyLoader(AsynchronousPolicyProvider::Delegate* delegate, - int reload_interval_minutes); - - // Triggers initial policy load, and installs |callback| as the callback to - // invoke on policy updates. |callback| takes ownership of the passed - // PolicyBundle, which contains all the policies that were loaded. - virtual void Init(const UpdateCallback& callback); - - // Reloads policy, sending notification of changes if necessary. Must be - // called on the FILE thread. When |force| is true, the loader should do an - // immediate full reload. - virtual void Reload(bool force); - - // Stops any pending reload tasks. Updates callbacks won't be performed - // anymore once the loader is stopped. - virtual void Stop(); - - protected: - // AsynchronousPolicyLoader objects should only be deleted by - // RefCountedThreadSafe. - friend class base::RefCountedThreadSafe<AsynchronousPolicyLoader>; - virtual ~AsynchronousPolicyLoader(); - - // Schedules a call to UpdatePolicy on |origin_loop_|. - void PostUpdatePolicyTask(scoped_ptr<PolicyBundle> bundle); - - AsynchronousPolicyProvider::Delegate* delegate() { - return delegate_.get(); - } - - // Performs start operations that must be performed on the FILE thread. - virtual void InitOnFileThread(); - - // Performs stop operations that must be performed on the FILE thread. - virtual void StopOnFileThread(); - - // Schedules a reload task to run when |delay| expires. Must be called on the - // FILE thread. - void ScheduleReloadTask(const base::TimeDelta& delay); - - // Schedules a reload task to run after the number of minutes specified - // in |reload_interval_minutes_|. Must be called on the FILE thread. - void ScheduleFallbackReloadTask(); - - void CancelReloadTask(); - - // Invoked from the reload task on the FILE thread. - void ReloadFromTask(); - - private: - friend class AsynchronousPolicyLoaderTest; - - // Finishes loader initialization after the threading system has been fully - // intialized. - void InitAfterFileThreadAvailable(); - - // Invokes the |update_callback_| with a new PolicyBundle that maps - // the chrome namespace to |policy|. Must be called on |origin_loop_| so that - // it's safe to invoke |update_callback_|. - void UpdatePolicy(scoped_ptr<PolicyBundle> policy); - - // Provides the low-level mechanics for loading policy. - scoped_ptr<AsynchronousPolicyProvider::Delegate> delegate_; - - // Used to create and invalidate WeakPtrs on the FILE thread. These are only - // used to post reload tasks that can be cancelled. - base::WeakPtrFactory<AsynchronousPolicyLoader> weak_ptr_factory_; - - // The interval at which a policy reload will be triggered as a fallback. - const base::TimeDelta reload_interval_; - - // The message loop on which this object was constructed. Recorded so that - // it's possible to call back into the non thread safe provider to fire the - // notification. - MessageLoop* origin_loop_; - - // True if Stop has been called. - bool stopped_; - - // Callback to invoke on policy updates. - UpdateCallback update_callback_; - - DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyLoader); -}; - -} // namespace policy - -#endif // CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_LOADER_H_ diff --git a/chrome/browser/policy/asynchronous_policy_loader_unittest.cc b/chrome/browser/policy/asynchronous_policy_loader_unittest.cc deleted file mode 100644 index 8828735..0000000 --- a/chrome/browser/policy/asynchronous_policy_loader_unittest.cc +++ /dev/null @@ -1,151 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/bind.h" -#include "base/memory/scoped_ptr.h" -#include "base/values.h" -#include "chrome/browser/policy/asynchronous_policy_loader.h" -#include "chrome/browser/policy/asynchronous_policy_provider.h" -#include "chrome/browser/policy/asynchronous_policy_test_base.h" -#include "chrome/browser/policy/mock_configuration_policy_provider.h" -#include "chrome/browser/policy/policy_bundle.h" -#include "chrome/browser/policy/policy_map.h" -#include "testing/gmock/include/gmock/gmock.h" - -using ::testing::InSequence; -using ::testing::Mock; -using ::testing::Return; -using ::testing::_; - -namespace policy { - -namespace { - -void AddSequencedTestPolicy(PolicyBundle* bundle, int* number) { - bundle->Get(POLICY_DOMAIN_CHROME, "") - .Set("id", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateIntegerValue(++(*number))); -} - -} // namespace - -class AsynchronousPolicyLoaderTest : public AsynchronousPolicyTestBase { - public: - AsynchronousPolicyLoaderTest() {} - virtual ~AsynchronousPolicyLoaderTest() {} - - virtual void SetUp() { - AsynchronousPolicyTestBase::SetUp(); - update_callback_ = base::Bind(&AsynchronousPolicyLoaderTest::UpdateCallback, - base::Unretained(this)); - } - - protected: - AsynchronousPolicyLoader::UpdateCallback update_callback_; - scoped_ptr<PolicyBundle> bundle_; - - private: - void UpdateCallback(scoped_ptr<PolicyBundle> bundle) { - bundle_.swap(bundle); - } - - DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyLoaderTest); -}; - -TEST_F(AsynchronousPolicyLoaderTest, InitialLoad) { - PolicyBundle template_bundle; - template_bundle.Get(POLICY_DOMAIN_CHROME, "") - .Set("test", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateIntegerValue(123)); - template_bundle.Get(POLICY_DOMAIN_EXTENSIONS, "extension-id") - .Set("test", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateIntegerValue(123)); - ProviderDelegateMock* delegate = new ProviderDelegateMock(); - EXPECT_CALL(*delegate, MockLoad()).WillOnce(Return(&template_bundle)); - scoped_refptr<AsynchronousPolicyLoader> loader = - new AsynchronousPolicyLoader(delegate, 10); - EXPECT_FALSE(bundle_.get()); - loader->Init(update_callback_); - ASSERT_TRUE(bundle_.get()); - EXPECT_TRUE(bundle_->Equals(template_bundle)); -} - -// Verify that the fallback policy requests are made. -TEST_F(AsynchronousPolicyLoaderTest, InitialLoadWithFallback) { - int policy_number = 0; - PolicyBundle bundle0; - AddSequencedTestPolicy(&bundle0, &policy_number); - PolicyBundle bundle1; - AddSequencedTestPolicy(&bundle1, &policy_number); - InSequence s; - ProviderDelegateMock* delegate = new ProviderDelegateMock(); - EXPECT_CALL(*delegate, MockLoad()).WillOnce(Return(&bundle0)); - EXPECT_CALL(*delegate, MockLoad()).WillOnce(Return(&bundle1)); - scoped_refptr<AsynchronousPolicyLoader> loader = - new AsynchronousPolicyLoader(delegate, 10); - loader->Init(update_callback_); - loop_.RunAllPending(); - loader->Reload(true); - loop_.RunAllPending(); - - const PolicyMap& chrome_policy = - bundle_->Get(POLICY_DOMAIN_CHROME, std::string()); - base::FundamentalValue expected(policy_number); - EXPECT_TRUE(base::Value::Equals(&expected, chrome_policy.GetValue("id"))); - EXPECT_EQ(1U, chrome_policy.size()); -} - -// Ensure that calling stop on the loader stops subsequent reloads from -// happening. -TEST_F(AsynchronousPolicyLoaderTest, Stop) { - PolicyBundle bundle; - ProviderDelegateMock* delegate = new ProviderDelegateMock(); - ON_CALL(*delegate, MockLoad()).WillByDefault(Return(&bundle)); - EXPECT_CALL(*delegate, MockLoad()).Times(1); - scoped_refptr<AsynchronousPolicyLoader> loader = - new AsynchronousPolicyLoader(delegate, 10); - loader->Init(update_callback_); - loop_.RunAllPending(); - loader->Stop(); - loop_.RunAllPending(); - loader->Reload(true); - loop_.RunAllPending(); -} - -// Verifies that the provider is notified upon policy reload. -TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) { - InSequence s; - MockConfigurationPolicyObserver observer; - int policy_number = 0; - - PolicyBundle bundle; - AddSequencedTestPolicy(&bundle, &policy_number); - ProviderDelegateMock* delegate = new ProviderDelegateMock(); - EXPECT_CALL(*delegate, MockLoad()).WillOnce(Return(&bundle)); - - scoped_refptr<AsynchronousPolicyLoader> loader = - new AsynchronousPolicyLoader(delegate, 10); - AsynchronousPolicyProvider provider(NULL, loader); - // |registrar| must be declared last so that it is destroyed first. - ConfigurationPolicyObserverRegistrar registrar; - registrar.Init(&provider, &observer); - Mock::VerifyAndClearExpectations(delegate); - - EXPECT_CALL(*delegate, MockLoad()).WillOnce(Return(&bundle)); - EXPECT_CALL(observer, OnUpdatePolicy(_)).Times(1); - provider.RefreshPolicies(); - loop_.RunAllPending(); - Mock::VerifyAndClearExpectations(delegate); - Mock::VerifyAndClearExpectations(&observer); - - AddSequencedTestPolicy(&bundle, &policy_number); - EXPECT_CALL(*delegate, MockLoad()).WillOnce(Return(&bundle)); - EXPECT_CALL(observer, OnUpdatePolicy(_)).Times(1); - provider.RefreshPolicies(); - loop_.RunAllPending(); - Mock::VerifyAndClearExpectations(delegate); - Mock::VerifyAndClearExpectations(&observer); -} - -} // namespace policy diff --git a/chrome/browser/policy/asynchronous_policy_provider.cc b/chrome/browser/policy/asynchronous_policy_provider.cc deleted file mode 100644 index 911a251a..0000000 --- a/chrome/browser/policy/asynchronous_policy_provider.cc +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/policy/asynchronous_policy_provider.h" - -#include "base/bind.h" -#include "chrome/browser/policy/asynchronous_policy_loader.h" -#include "chrome/browser/policy/policy_bundle.h" -#include "content/public/browser/browser_thread.h" - -using content::BrowserThread; - -namespace policy { - -AsynchronousPolicyProvider::AsynchronousPolicyProvider( - const PolicyDefinitionList* policy_list, - scoped_refptr<AsynchronousPolicyLoader> loader) - : ConfigurationPolicyProvider(policy_list), - loader_(loader), - pending_refreshes_(0), - ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { - loader_->Init( - base::Bind(&AsynchronousPolicyProvider::OnLoaderReloaded, - base::Unretained(this))); -} - -AsynchronousPolicyProvider::~AsynchronousPolicyProvider() { - DCHECK(CalledOnValidThread()); - // |loader_| won't invoke its callback anymore after Stop(), therefore - // Unretained(this) is safe in the ctor. - loader_->Stop(); -} - -void AsynchronousPolicyProvider::RefreshPolicies() { - DCHECK(CalledOnValidThread()); - pending_refreshes_++; - BrowserThread::PostTaskAndReply( - BrowserThread::FILE, FROM_HERE, - base::Bind(&AsynchronousPolicyProvider::PostReloadOnFileThread, - loader_), - base::Bind(&AsynchronousPolicyProvider::OnReloadPosted, - weak_ptr_factory_.GetWeakPtr())); -} - -// static -void AsynchronousPolicyProvider::PostReloadOnFileThread( - AsynchronousPolicyLoader* loader) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - BrowserThread::PostTask( - BrowserThread::FILE, FROM_HERE, - base::Bind(&AsynchronousPolicyLoader::Reload, loader, true)); -} - -void AsynchronousPolicyProvider::OnReloadPosted() { - DCHECK(CalledOnValidThread()); - pending_refreshes_--; -} - -void AsynchronousPolicyProvider::OnLoaderReloaded( - scoped_ptr<PolicyBundle> bundle) { - DCHECK(CalledOnValidThread()); - if (pending_refreshes_ == 0) - UpdatePolicy(bundle.Pass()); -} - -} // namespace policy diff --git a/chrome/browser/policy/asynchronous_policy_provider.h b/chrome/browser/policy/asynchronous_policy_provider.h deleted file mode 100644 index 000b8a6..0000000 --- a/chrome/browser/policy/asynchronous_policy_provider.h +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_PROVIDER_H_ -#define CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_PROVIDER_H_ -#pragma once - -#include "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" -#include "base/memory/weak_ptr.h" -#include "base/threading/non_thread_safe.h" -#include "chrome/browser/policy/configuration_policy_provider.h" - -namespace policy { - -class AsynchronousPolicyLoader; -class PolicyBundle; - -// Policy provider that loads policy asynchronously. Providers should subclass -// from this class if loading the policy requires disk access or must for some -// other reason be performed on the file thread. The actual logic for loading -// policy is handled by a delegate passed at construction time. -class AsynchronousPolicyProvider - : public ConfigurationPolicyProvider, - public base::NonThreadSafe { - public: - // Must be implemented by subclasses of the asynchronous policy provider to - // provide the implementation details of how policy is loaded. - class Delegate { - public: - virtual ~Delegate() {} - - // Load policy from the delegate's source, and return a PolicyBundle. - virtual scoped_ptr<PolicyBundle> Load() = 0; - }; - - // Assumes ownership of |loader|. - AsynchronousPolicyProvider( - const PolicyDefinitionList* policy_list, - scoped_refptr<AsynchronousPolicyLoader> loader); - virtual ~AsynchronousPolicyProvider(); - - // ConfigurationPolicyProvider implementation. - virtual void RefreshPolicies() OVERRIDE; - - private: - // Used to trigger a Reload on |loader| on the FILE thread. - static void PostReloadOnFileThread(AsynchronousPolicyLoader* loader); - - // Used to notify UI that a reload task has been submitted. - void OnReloadPosted(); - - // Callback from the loader. This is invoked whenever the loader has completed - // a reload of the policies. - void OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle); - - // The loader object used internally. - scoped_refptr<AsynchronousPolicyLoader> loader_; - - // Number of refreshes requested whose reload is still pending. Used to only - // fire notifications when all pending refreshes are done. - int pending_refreshes_; - - // Used to post tasks to self on UI. - base::WeakPtrFactory<AsynchronousPolicyProvider> weak_ptr_factory_; - - DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyProvider); -}; - -} // namespace policy - -#endif // CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_PROVIDER_H_ diff --git a/chrome/browser/policy/asynchronous_policy_provider_unittest.cc b/chrome/browser/policy/asynchronous_policy_provider_unittest.cc deleted file mode 100644 index 65230bb..0000000 --- a/chrome/browser/policy/asynchronous_policy_provider_unittest.cc +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/values.h" -#include "chrome/browser/policy/asynchronous_policy_loader.h" -#include "chrome/browser/policy/asynchronous_policy_provider.h" -#include "chrome/browser/policy/asynchronous_policy_test_base.h" -#include "chrome/browser/policy/configuration_policy_pref_store.h" -#include "chrome/browser/policy/configuration_policy_provider.h" -#include "chrome/browser/policy/mock_configuration_policy_provider.h" -#include "chrome/browser/policy/policy_map.h" -#include "policy/policy_constants.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -using ::testing::InSequence; -using ::testing::Return; -using ::testing::_; - -namespace policy { - -// Creating the provider should provide initial policy. -TEST_F(AsynchronousPolicyTestBase, Provide) { - InSequence s; - PolicyBundle bundle; - bundle.Get(POLICY_DOMAIN_CHROME, "") - .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateBooleanValue(true)); - ProviderDelegateMock* delegate = new ProviderDelegateMock(); - EXPECT_CALL(*delegate, MockLoad()).WillOnce(Return(&bundle)); - AsynchronousPolicyProvider provider( - GetChromePolicyDefinitionList(), - new AsynchronousPolicyLoader(delegate, 10)); - EXPECT_TRUE(provider.policies().Equals(bundle)); -} - -// Trigger a refresh manually and ensure that policy gets reloaded. -TEST_F(AsynchronousPolicyTestBase, ProvideAfterRefresh) { - InSequence s; - PolicyBundle original_policies; - original_policies.Get(POLICY_DOMAIN_CHROME, "") - .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateBooleanValue(true)); - ProviderDelegateMock* delegate = new ProviderDelegateMock(); - EXPECT_CALL(*delegate, MockLoad()).WillOnce(Return(&original_policies)); - PolicyBundle refresh_policies; - refresh_policies.Get(POLICY_DOMAIN_CHROME, "") - .Set(key::kJavascriptEnabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateBooleanValue(true)); - EXPECT_CALL(*delegate, MockLoad()).WillOnce(Return(&refresh_policies)); - AsynchronousPolicyLoader* loader = new AsynchronousPolicyLoader(delegate, 10); - AsynchronousPolicyProvider provider(GetChromePolicyDefinitionList(), loader); - // The original policies have been loaded. - EXPECT_TRUE(provider.policies().Equals(original_policies)); - - MockConfigurationPolicyObserver observer; - ConfigurationPolicyObserverRegistrar registrar; - registrar.Init(&provider, &observer); - EXPECT_CALL(observer, OnUpdatePolicy(&provider)).Times(1); - provider.RefreshPolicies(); - loop_.RunAllPending(); - // The refreshed policies are now provided. - EXPECT_TRUE(provider.policies().Equals(refresh_policies)); -} - -} // namespace policy diff --git a/chrome/browser/policy/asynchronous_policy_test_base.cc b/chrome/browser/policy/asynchronous_policy_test_base.cc deleted file mode 100644 index 68887fe..0000000 --- a/chrome/browser/policy/asynchronous_policy_test_base.cc +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/policy/asynchronous_policy_test_base.h" - -using content::BrowserThread; - -namespace policy { - -ProviderDelegateMock::ProviderDelegateMock() - : AsynchronousPolicyProvider::Delegate() {} - -ProviderDelegateMock::~ProviderDelegateMock() {} - -AsynchronousPolicyTestBase::AsynchronousPolicyTestBase() - : ui_thread_(BrowserThread::UI, &loop_), - file_thread_(BrowserThread::FILE, &loop_) {} - -AsynchronousPolicyTestBase::~AsynchronousPolicyTestBase() {} - -void AsynchronousPolicyTestBase::TearDown() { - loop_.RunAllPending(); -} - -} // namespace policy diff --git a/chrome/browser/policy/asynchronous_policy_test_base.h b/chrome/browser/policy/asynchronous_policy_test_base.h deleted file mode 100644 index 9694fd0..0000000 --- a/chrome/browser/policy/asynchronous_policy_test_base.h +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_TEST_BASE_H_ -#define CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_TEST_BASE_H_ -#pragma once - -#include "base/memory/scoped_ptr.h" -#include "base/message_loop.h" -#include "chrome/browser/policy/asynchronous_policy_provider.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/test/test_browser_thread.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace policy { - -class PolicyBundle; - -// A delegate for testing that can feed arbitrary information to the loader. -class ProviderDelegateMock : public AsynchronousPolicyProvider::Delegate { - public: - ProviderDelegateMock(); - virtual ~ProviderDelegateMock(); - - // Load() returns a scoped_ptr<PolicyBundle> but it can't be mocked because - // scoped_ptr is moveable but not copyable. This override forwards the - // call to MockLoad() which returns a PolicyBundle*, and returns a copy - // wrapped in a passed scoped_ptr. - virtual scoped_ptr<PolicyBundle> Load() OVERRIDE { - scoped_ptr<PolicyBundle> bundle; - PolicyBundle* loaded = MockLoad(); - if (loaded) { - bundle.reset(new PolicyBundle()); - bundle->CopyFrom(*loaded); - } - return bundle.Pass(); - } - - MOCK_METHOD0(MockLoad, PolicyBundle*()); - - private: - DISALLOW_COPY_AND_ASSIGN(ProviderDelegateMock); -}; - -class AsynchronousPolicyTestBase : public testing::Test { - public: - AsynchronousPolicyTestBase(); - virtual ~AsynchronousPolicyTestBase(); - - // testing::Test: - virtual void TearDown() OVERRIDE; - - protected: - // Create an actual IO loop (needed by FilePathWatcher). - MessageLoopForIO loop_; - - private: - content::TestBrowserThread ui_thread_; - content::TestBrowserThread file_thread_; - - DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyTestBase); -}; - -} // namespace policy - -#endif // CHROME_BROWSER_POLICY_ASYNCHRONOUS_POLICY_TEST_BASE_H_ diff --git a/chrome/browser/policy/file_based_policy_loader.cc b/chrome/browser/policy/file_based_policy_loader.cc deleted file mode 100644 index c0430f7..0000000 --- a/chrome/browser/policy/file_based_policy_loader.cc +++ /dev/null @@ -1,156 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/policy/file_based_policy_loader.h" - -#include "base/files/file_path_watcher.h" -#include "base/memory/ref_counted.h" -#include "chrome/browser/policy/policy_bundle.h" -#include "content/public/browser/browser_thread.h" - -using ::base::files::FilePathWatcher; -using content::BrowserThread; - -namespace { - -// Amount of time we wait for the files on disk to settle before trying to load -// them. This alleviates the problem of reading partially written files and -// makes it possible to batch quasi-simultaneous changes. -const int kSettleIntervalSeconds = 5; - -// The time interval for rechecking policy. This is our fallback in case the -// delegate never reports a change to the ReloadObserver. -const int kReloadIntervalMinutes = 15; - -} // namespace - -namespace policy { - -FileBasedPolicyLoader::FileBasedPolicyLoader( - FileBasedPolicyProvider::ProviderDelegate* provider_delegate) - : AsynchronousPolicyLoader(provider_delegate, - kReloadIntervalMinutes), - config_file_path_(provider_delegate->config_file_path()), - settle_interval_(base::TimeDelta::FromSeconds(kSettleIntervalSeconds)) { -} - -FileBasedPolicyLoader::~FileBasedPolicyLoader() {} - -class FileBasedPolicyWatcherDelegate : public FilePathWatcher::Delegate { - public: - explicit FileBasedPolicyWatcherDelegate( - scoped_refptr<FileBasedPolicyLoader> loader) - : loader_(loader) {} - - // FilePathWatcher::Delegate implementation: - virtual void OnFilePathChanged(const FilePath& path) OVERRIDE { - loader_->OnFilePathChanged(path); - } - - virtual void OnFilePathError(const FilePath& path) OVERRIDE { - loader_->OnFilePathError(path); - } - - private: - virtual ~FileBasedPolicyWatcherDelegate() {} - - scoped_refptr<FileBasedPolicyLoader> loader_; - DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyWatcherDelegate); -}; - -void FileBasedPolicyLoader::OnFilePathChanged( - const FilePath& path) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - Reload(false); -} - -void FileBasedPolicyLoader::OnFilePathError(const FilePath& path) { - LOG(ERROR) << "FilePathWatcher on " << path.value() - << " failed."; -} - -void FileBasedPolicyLoader::Reload(bool force) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - - if (!delegate()) { - scoped_ptr<PolicyBundle> empty_bundle; - PostUpdatePolicyTask(empty_bundle.Pass()); - return; - } - - // Check the directory time in order to see whether a reload is required. - base::TimeDelta delay; - base::Time now = base::Time::Now(); - if (!force && !IsSafeToReloadPolicy(now, &delay)) { - ScheduleReloadTask(delay); - return; - } - - // Load the policy definitions. - scoped_ptr<PolicyBundle> bundle(delegate()->Load()); - - // Check again in case the directory has changed while reading it. - if (!force && !IsSafeToReloadPolicy(now, &delay)) { - ScheduleReloadTask(delay); - return; - } - - PostUpdatePolicyTask(bundle.Pass()); - - ScheduleFallbackReloadTask(); -} - -void FileBasedPolicyLoader::InitOnFileThread() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - watcher_.reset(new FilePathWatcher); - const FilePath& path = config_file_path(); - if (!path.empty() && - !watcher_->Watch(path, new FileBasedPolicyWatcherDelegate(this))) { - OnFilePathError(path); - } - - // There might have been changes to the directory in the time between - // construction of the loader and initialization of the watcher. Call reload - // to detect if that is the case. - Reload(false); - - ScheduleFallbackReloadTask(); -} - -void FileBasedPolicyLoader::StopOnFileThread() { - watcher_.reset(); - AsynchronousPolicyLoader::StopOnFileThread(); -} - -bool FileBasedPolicyLoader::IsSafeToReloadPolicy( - const base::Time& now, - base::TimeDelta* delay) { - DCHECK(delay); - - // A null modification time indicates there's no data. - FileBasedPolicyProvider::ProviderDelegate* provider_delegate = - static_cast<FileBasedPolicyProvider::ProviderDelegate*>(delegate()); - base::Time last_modification(provider_delegate->GetLastModification()); - if (last_modification.is_null()) - return true; - - // If there was a change since the last recorded modification, wait some more. - if (last_modification != last_modification_file_) { - last_modification_file_ = last_modification; - last_modification_clock_ = now; - *delay = settle_interval_; - return false; - } - - // Check whether the settle interval has elapsed. - base::TimeDelta age = now - last_modification_clock_; - if (age < settle_interval_) { - *delay = settle_interval_ - age; - return false; - } - - return true; -} - -} // namespace policy diff --git a/chrome/browser/policy/file_based_policy_loader.h b/chrome/browser/policy/file_based_policy_loader.h deleted file mode 100644 index e82a26e..0000000 --- a/chrome/browser/policy/file_based_policy_loader.h +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_POLICY_FILE_BASED_POLICY_LOADER_H_ -#define CHROME_BROWSER_POLICY_FILE_BASED_POLICY_LOADER_H_ -#pragma once - -#include "base/compiler_specific.h" -#include "base/file_path.h" -#include "base/memory/scoped_ptr.h" -#include "base/time.h" -#include "chrome/browser/policy/asynchronous_policy_loader.h" -#include "chrome/browser/policy/file_based_policy_provider.h" - -namespace base { -namespace files { - -class FilePathWatcher; - -} // namespace files -} // namespace base - -namespace policy { - -// A customized asynchronous policy loader that handles loading policy from a -// file using a FilePathWatcher. The loader creates a fallback task to load -// policy periodically in case the watcher fails and retries policy loads when -// the watched file is in flux. -class FileBasedPolicyLoader : public AsynchronousPolicyLoader { - public: - explicit FileBasedPolicyLoader( - FileBasedPolicyProvider::ProviderDelegate* provider_delegate); - - // AsynchronousPolicyLoader overrides: - virtual void Reload(bool force) OVERRIDE; - - void OnFilePathChanged(const FilePath& path); - void OnFilePathError(const FilePath& path); - - protected: - // FileBasedPolicyLoader objects should only be deleted by - // RefCountedThreadSafe. - friend class base::RefCountedThreadSafe<AsynchronousPolicyLoader>; - virtual ~FileBasedPolicyLoader(); - - const FilePath& config_file_path() { return config_file_path_; } - - // AsynchronousPolicyLoader overrides: - - // Creates the file path watcher and configures it to watch - // |config_file_path_|. Must be called on the file thread. - virtual void InitOnFileThread() OVERRIDE; - virtual void StopOnFileThread() OVERRIDE; - - private: - // Checks whether policy information is safe to read. If not, returns false - // and then delays until it is considered safe to reload in |delay|. - // Must be called on the file thread. - bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay); - - // The path at which we look for configuration files. - const FilePath config_file_path_; - - // Managed with a scoped_ptr rather than being declared as an inline member to - // decouple the watcher's life cycle from the loader's. This decoupling makes - // it possible to destroy the watcher before the loader's destructor is called - // (e.g. during Stop), since |watcher_| internally holds a reference to the - // loader and keeps it alive. - scoped_ptr<base::files::FilePathWatcher> watcher_; - - // Settle interval. - const base::TimeDelta settle_interval_; - - // Records last known modification timestamp of |config_file_path_|. - base::Time last_modification_file_; - - // The wall clock time at which the last modification timestamp was - // recorded. It's better to not assume the file notification time and the - // wall clock times come from the same source, just in case there is some - // non-local filesystem involved. - base::Time last_modification_clock_; - - DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyLoader); -}; - -} // namespace policy - -#endif // CHROME_BROWSER_POLICY_FILE_BASED_POLICY_LOADER_H_ diff --git a/chrome/browser/policy/file_based_policy_provider.cc b/chrome/browser/policy/file_based_policy_provider.cc deleted file mode 100644 index b03d851..0000000 --- a/chrome/browser/policy/file_based_policy_provider.cc +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/policy/file_based_policy_provider.h" - -#include "chrome/browser/policy/file_based_policy_loader.h" - -namespace policy { - -FileBasedPolicyProvider::ProviderDelegate::ProviderDelegate( - const FilePath& config_file_path) - : config_file_path_(config_file_path) {} - -FileBasedPolicyProvider::ProviderDelegate::~ProviderDelegate() {} - -FileBasedPolicyProvider::FileBasedPolicyProvider( - const PolicyDefinitionList* policy_list, - FileBasedPolicyProvider::ProviderDelegate* delegate) - : AsynchronousPolicyProvider( - policy_list, - new FileBasedPolicyLoader(delegate)) {} - -} // namespace policy diff --git a/chrome/browser/policy/file_based_policy_provider.h b/chrome/browser/policy/file_based_policy_provider.h deleted file mode 100644 index dbc8f59..0000000 --- a/chrome/browser/policy/file_based_policy_provider.h +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_ -#define CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_ -#pragma once - -#include "base/file_path.h" -#include "base/time.h" -#include "chrome/browser/policy/asynchronous_policy_provider.h" - -namespace policy { - -// File based policy provider that coordinates watching and reloading policy -// information from the configuration path. Actual logic for loading policy -// information is handled by a delegate passed at construction time. -class FileBasedPolicyProvider : public AsynchronousPolicyProvider { - public: - - // Delegate interface for actual policy loading from the system. - class ProviderDelegate : public AsynchronousPolicyProvider::Delegate { - public: - explicit ProviderDelegate(const FilePath& config_file_path); - virtual ~ProviderDelegate(); - - // AsynchronousPolicyProvider::Delegate implementation: - virtual scoped_ptr<PolicyBundle> Load() = 0; - - // Gets the last modification timestamp for the policy information from the - // filesystem. Returns base::Time() if the information is not present, in - // which case Load() should return an empty dictionary. - virtual base::Time GetLastModification() = 0; - - const FilePath& config_file_path() { return config_file_path_; } - - private: - const FilePath config_file_path_; - - DISALLOW_COPY_AND_ASSIGN(ProviderDelegate); - }; - - // Assumes ownership of |delegate|. - FileBasedPolicyProvider(const PolicyDefinitionList* policy_list, - ProviderDelegate* delegate); - virtual ~FileBasedPolicyProvider() {} - - private: - DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyProvider); -}; - -} // namespace policy - -#endif // CHROME_BROWSER_POLICY_FILE_BASED_POLICY_PROVIDER_H_ diff --git a/chrome/browser/policy/file_based_policy_provider_unittest.cc b/chrome/browser/policy/file_based_policy_provider_unittest.cc deleted file mode 100644 index 8699f68..0000000 --- a/chrome/browser/policy/file_based_policy_provider_unittest.cc +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/values.h" -#include "chrome/browser/policy/asynchronous_policy_loader.h" -#include "chrome/browser/policy/asynchronous_policy_test_base.h" -#include "chrome/browser/policy/configuration_policy_pref_store.h" -#include "chrome/browser/policy/configuration_policy_provider.h" -#include "chrome/browser/policy/file_based_policy_provider.h" -#include "chrome/browser/policy/mock_configuration_policy_provider.h" -#include "chrome/browser/policy/policy_bundle.h" -#include "chrome/browser/policy/policy_map.h" -#include "policy/policy_constants.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -using testing::InSequence; -using testing::Return; -using testing::_; - -namespace policy { - -class FileBasedPolicyProviderDelegateMock - : public FileBasedPolicyProvider::ProviderDelegate { - public: - FileBasedPolicyProviderDelegateMock() - : FileBasedPolicyProvider::ProviderDelegate(FilePath()) {} - - // Load() returns a scoped_ptr<PolicyBundle> but it can't be mocked because - // scoped_ptr is moveable but not copyable. This override forwards the - // call to MockLoad() which returns a PolicyBundle*, and returns a copy - // wrapped in a passed scoped_ptr. - virtual scoped_ptr<PolicyBundle> Load() OVERRIDE { - scoped_ptr<PolicyBundle> bundle; - PolicyBundle* loaded = MockLoad(); - if (loaded) { - bundle.reset(new PolicyBundle()); - bundle->CopyFrom(*loaded); - } - return bundle.Pass(); - } - - MOCK_METHOD0(MockLoad, PolicyBundle*()); - MOCK_METHOD0(GetLastModification, base::Time()); -}; - -TEST_F(AsynchronousPolicyTestBase, ProviderInit) { - base::Time last_modified; - FileBasedPolicyProviderDelegateMock* provider_delegate = - new FileBasedPolicyProviderDelegateMock(); - EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( - Return(last_modified)); - InSequence s; - PolicyBundle empty_bundle; - EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&empty_bundle)); - PolicyBundle bundle; - bundle.Get(POLICY_DOMAIN_CHROME, "") - .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateBooleanValue(true)); - // A second call to Load gets triggered during the provider's construction - // when the file watcher is initialized, since this file may have changed - // between the initial load and creating watcher. - EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&bundle)); - FileBasedPolicyProvider provider(GetChromePolicyDefinitionList(), - provider_delegate); - loop_.RunAllPending(); - EXPECT_TRUE(provider.policies().Equals(bundle)); -} - -TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) { - base::Time last_modified; - FileBasedPolicyProviderDelegateMock* provider_delegate = - new FileBasedPolicyProviderDelegateMock(); - EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly( - Return(last_modified)); - InSequence s; - PolicyBundle empty_bundle; - EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&empty_bundle)); - FileBasedPolicyProvider file_based_provider(GetChromePolicyDefinitionList(), - provider_delegate); - // A second call to Load gets triggered during the provider's construction - // when the file watcher is initialized, since this file may have changed - // between the initial load and creating watcher. - EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&empty_bundle)); - loop_.RunAllPending(); - // A third and final call to Load is made by the explicit Reload. This - // should be the one that provides the current policy. - PolicyBundle bundle; - bundle.Get(POLICY_DOMAIN_CHROME, "") - .Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - base::Value::CreateBooleanValue(true)); - EXPECT_CALL(*provider_delegate, MockLoad()).WillOnce(Return(&bundle)); - MockConfigurationPolicyObserver observer; - ConfigurationPolicyObserverRegistrar registrar; - registrar.Init(&file_based_provider, &observer); - EXPECT_CALL(observer, OnUpdatePolicy(&file_based_provider)).Times(1); - file_based_provider.RefreshPolicies(); - loop_.RunAllPending(); - EXPECT_TRUE(file_based_provider.policies().Equals(bundle)); -} - -} // namespace policy |