diff options
author | mnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-24 18:03:24 +0000 |
---|---|---|
committer | mnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-24 18:03:24 +0000 |
commit | ec13ca612aa5f6fed4b33b17802d8141c182b5e9 (patch) | |
tree | fbda87303c35d3e56f7e02f44af338f668152ad6 /chrome | |
parent | 81a6b0b57290b80a5cc292b1ee293feda5123c66 (diff) | |
download | chromium_src-ec13ca612aa5f6fed4b33b17802d8141c182b5e9.zip chromium_src-ec13ca612aa5f6fed4b33b17802d8141c182b5e9.tar.gz chromium_src-ec13ca612aa5f6fed4b33b17802d8141c182b5e9.tar.bz2 |
Separate user policy disk cache into separate file.
This is in preparation to also use it for the new CrOS user policy
implementation.
BUG=87384
TEST=unit tests
Review URL: http://codereview.chromium.org/7238024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90385 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/policy/user_policy_cache.cc | 119 | ||||
-rw-r--r-- | chrome/browser/policy/user_policy_cache.h | 15 | ||||
-rw-r--r-- | chrome/browser/policy/user_policy_disk_cache.cc | 97 | ||||
-rw-r--r-- | chrome/browser/policy/user_policy_disk_cache.h | 68 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 |
5 files changed, 179 insertions, 122 deletions
diff --git a/chrome/browser/policy/user_policy_cache.cc b/chrome/browser/policy/user_policy_cache.cc index 6d0a53e..d0484f5 100644 --- a/chrome/browser/policy/user_policy_cache.cc +++ b/chrome/browser/policy/user_policy_cache.cc @@ -8,16 +8,13 @@ #include <string> #include "base/basictypes.h" -#include "base/file_util.h" #include "base/logging.h" -#include "base/task.h" #include "base/values.h" #include "chrome/browser/policy/configuration_policy_pref_store.h" #include "chrome/browser/policy/policy_map.h" #include "chrome/browser/policy/proto/cloud_policy.pb.h" #include "chrome/browser/policy/proto/device_management_local.pb.h" #include "chrome/browser/policy/proto/old_generic_format.pb.h" -#include "content/browser/browser_thread.h" #include "policy/configuration_policy_type.h" namespace policy { @@ -28,122 +25,10 @@ namespace policy { void DecodePolicy(const em::CloudPolicySettings& policy, PolicyMap* mandatory, PolicyMap* recommended); -// Handles the on-disk cache file used by UserPolicyCache. This class handles -// the necessary thread switching and may outlive the associated UserPolicyCache -// instance. -class UserPolicyCache::DiskCache - : public base::RefCountedThreadSafe<UserPolicyCache::DiskCache> { - public: - DiskCache(const base::WeakPtr<UserPolicyCache>& cache, - const FilePath& backing_file_path); - - // Starts reading the policy cache from disk. Passes the read policy - // information back to the hosting UserPolicyCache after a successful cache - // load through UserPolicyCache::OnDiskCacheLoaded(). - void Load(); - - // Triggers a write operation to the disk cache on the FILE thread. - void Store(const em::CachedCloudPolicyResponse& policy); - - private: - // Tries to load the cache file on the FILE thread. - void LoadOnFileThread(); - - // Passes back the successfully read policy to the cache on the UI thread. - void FinishLoadOnUIThread(const em::CachedCloudPolicyResponse& policy); - - // Saves a policy blob on the FILE thread. - void StoreOnFileThread(const em::CachedCloudPolicyResponse& policy); - - base::WeakPtr<UserPolicyCache> cache_; - const FilePath backing_file_path_; - - DISALLOW_COPY_AND_ASSIGN(DiskCache); -}; - -UserPolicyCache::DiskCache::DiskCache( - const base::WeakPtr<UserPolicyCache>& cache, - const FilePath& backing_file_path) - : cache_(cache), - backing_file_path_(backing_file_path) {} - -void UserPolicyCache::DiskCache::Load() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - BrowserThread::PostTask( - BrowserThread::FILE, FROM_HERE, - NewRunnableMethod(this, &DiskCache::LoadOnFileThread)); -} - -void UserPolicyCache::DiskCache::Store( - const em::CachedCloudPolicyResponse& policy) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - BrowserThread::PostTask( - BrowserThread::FILE, FROM_HERE, - NewRunnableMethod(this, &DiskCache::StoreOnFileThread, policy)); -} - -void UserPolicyCache::DiskCache::LoadOnFileThread() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - if (!file_util::PathExists(backing_file_path_)) - return; - - // Read the protobuf from the file. - std::string data; - if (!file_util::ReadFileToString(backing_file_path_, &data)) { - LOG(WARNING) << "Failed to read policy data from " - << backing_file_path_.value(); - return; - } - - // Decode it. - em::CachedCloudPolicyResponse cached_response; - if (!cached_response.ParseFromArray(data.c_str(), data.size())) { - LOG(WARNING) << "Failed to parse policy data read from " - << backing_file_path_.value(); - return; - } - - BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, - NewRunnableMethod(this, - &DiskCache::FinishLoadOnUIThread, - cached_response)); -} - -void UserPolicyCache::DiskCache::FinishLoadOnUIThread( - const em::CachedCloudPolicyResponse& policy) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (cache_.get()) - cache_->OnDiskCacheLoaded(policy); -} - - -void UserPolicyCache::DiskCache::StoreOnFileThread( - const em::CachedCloudPolicyResponse& policy) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - std::string data; - if (!policy.SerializeToString(&data)) { - LOG(WARNING) << "Failed to serialize policy data"; - return; - } - - if (!file_util::CreateDirectory(backing_file_path_.DirName())) { - LOG(WARNING) << "Failed to create directory " - << backing_file_path_.DirName().value(); - return; - } - - int size = data.size(); - if (file_util::WriteFile(backing_file_path_, data.c_str(), size) != size) { - LOG(WARNING) << "Failed to write " << backing_file_path_.value(); - return; - } -} - UserPolicyCache::UserPolicyCache(const FilePath& backing_file_path) : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { - disk_cache_ = new DiskCache(weak_ptr_factory_.GetWeakPtr(), - backing_file_path); + disk_cache_ = new UserPolicyDiskCache(weak_ptr_factory_.GetWeakPtr(), + backing_file_path); } UserPolicyCache::~UserPolicyCache() { diff --git a/chrome/browser/policy/user_policy_cache.h b/chrome/browser/policy/user_policy_cache.h index 45eb3d0..307530c 100644 --- a/chrome/browser/policy/user_policy_cache.h +++ b/chrome/browser/policy/user_policy_cache.h @@ -12,6 +12,9 @@ #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" #include "chrome/browser/policy/cloud_policy_cache_base.h" +#include "chrome/browser/policy/user_policy_disk_cache.h" + +namespace em = enterprise_management; namespace enterprise_management { class CachedCloudPolicyResponse; @@ -24,7 +27,8 @@ namespace policy { // CloudPolicyCacheBase implementation that persists policy information // into the file specified by the c'tor parameter |backing_file_path|. -class UserPolicyCache : public CloudPolicyCacheBase { +class UserPolicyCache : public CloudPolicyCacheBase, + public UserPolicyDiskCache::Delegate { public: explicit UserPolicyCache(const FilePath& backing_file_path); virtual ~UserPolicyCache(); @@ -37,8 +41,9 @@ class UserPolicyCache : public CloudPolicyCacheBase { private: class DiskCache; - // Invoked by DiskCache after the file cache has been read successfully. - void OnDiskCacheLoaded(const em::CachedCloudPolicyResponse& cached_response); + // UserPolicyDiskCache::Delegate implementation: + virtual void OnDiskCacheLoaded( + const em::CachedCloudPolicyResponse& cached_response) OVERRIDE; // CloudPolicyCacheBase implementation: virtual bool DecodePolicyData(const em::PolicyData& policy_data, @@ -63,10 +68,10 @@ class UserPolicyCache : public CloudPolicyCacheBase { // </Old-style policy support> // Manages the cache file. - scoped_refptr<DiskCache> disk_cache_; + scoped_refptr<UserPolicyDiskCache> disk_cache_; // Used for constructing the weak ptr passed to |disk_cache_|. - base::WeakPtrFactory<UserPolicyCache> weak_ptr_factory_; + base::WeakPtrFactory<UserPolicyDiskCache::Delegate> weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(UserPolicyCache); }; diff --git a/chrome/browser/policy/user_policy_disk_cache.cc b/chrome/browser/policy/user_policy_disk_cache.cc new file mode 100644 index 0000000..08c2ba7 --- /dev/null +++ b/chrome/browser/policy/user_policy_disk_cache.cc @@ -0,0 +1,97 @@ +// 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 "base/file_util.h" +#include "base/logging.h" +#include "base/task.h" +#include "chrome/browser/policy/proto/device_management_local.pb.h" +#include "chrome/browser/policy/user_policy_disk_cache.h" +#include "content/browser/browser_thread.h" + +namespace policy { + +UserPolicyDiskCache::Delegate::~Delegate() {} + +UserPolicyDiskCache::UserPolicyDiskCache( + const base::WeakPtr<Delegate>& delegate, + const FilePath& backing_file_path) + : delegate_(delegate), + backing_file_path_(backing_file_path) {} + +void UserPolicyDiskCache::Load() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + BrowserThread::PostTask( + BrowserThread::FILE, FROM_HERE, + NewRunnableMethod(this, &UserPolicyDiskCache::LoadOnFileThread)); +} + +void UserPolicyDiskCache::Store( + const em::CachedCloudPolicyResponse& policy) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + BrowserThread::PostTask( + BrowserThread::FILE, FROM_HERE, + NewRunnableMethod(this, &UserPolicyDiskCache::StoreOnFileThread, policy)); +} + +UserPolicyDiskCache::~UserPolicyDiskCache() {} + +void UserPolicyDiskCache::LoadOnFileThread() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); + if (!file_util::PathExists(backing_file_path_)) + return; + + // Read the protobuf from the file. + std::string data; + if (!file_util::ReadFileToString(backing_file_path_, &data)) { + LOG(WARNING) << "Failed to read policy data from " + << backing_file_path_.value(); + return; + } + + // Decode it. + em::CachedCloudPolicyResponse cached_response; + if (!cached_response.ParseFromArray(data.c_str(), data.size())) { + LOG(WARNING) << "Failed to parse policy data read from " + << backing_file_path_.value(); + return; + } + + BrowserThread::PostTask( + BrowserThread::UI, FROM_HERE, + NewRunnableMethod(this, + &UserPolicyDiskCache::FinishLoadOnUIThread, + cached_response)); +} + +void UserPolicyDiskCache::FinishLoadOnUIThread( + const em::CachedCloudPolicyResponse& policy) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + if (delegate_.get()) + delegate_->OnDiskCacheLoaded(policy); +} + + +void UserPolicyDiskCache::StoreOnFileThread( + const em::CachedCloudPolicyResponse& policy) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); + std::string data; + if (!policy.SerializeToString(&data)) { + LOG(WARNING) << "Failed to serialize policy data"; + return; + } + + if (!file_util::CreateDirectory(backing_file_path_.DirName())) { + LOG(WARNING) << "Failed to create directory " + << backing_file_path_.DirName().value(); + return; + } + + int size = data.size(); + if (file_util::WriteFile(backing_file_path_, data.c_str(), size) != size) { + LOG(WARNING) << "Failed to write " << backing_file_path_.value(); + return; + } +} + +} // namespace policy diff --git a/chrome/browser/policy/user_policy_disk_cache.h b/chrome/browser/policy/user_policy_disk_cache.h new file mode 100644 index 0000000..555524c --- /dev/null +++ b/chrome/browser/policy/user_policy_disk_cache.h @@ -0,0 +1,68 @@ +// 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_USER_POLICY_DISK_CACHE_H_ +#define CHROME_BROWSER_POLICY_USER_POLICY_DISK_CACHE_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/file_path.h" +#include "base/memory/ref_counted.h" +#include "base/memory/weak_ptr.h" + +namespace em = enterprise_management; + +namespace enterprise_management { +class CachedCloudPolicyResponse; +} + +namespace policy { + +// Handles the on-disk cache file used by UserPolicyCache. This class handles +// the necessary thread switching and may outlive the associated UserPolicyCache +// instance. +class UserPolicyDiskCache + : public base::RefCountedThreadSafe<UserPolicyDiskCache> { + public: + // Delegate interface for observing loads operations. + class Delegate { + public: + virtual ~Delegate(); + virtual void OnDiskCacheLoaded( + const em::CachedCloudPolicyResponse& policy) = 0; + }; + + UserPolicyDiskCache(const base::WeakPtr<Delegate>& delegate, + const FilePath& backing_file_path); + + // Starts reading the policy cache from disk. Passes the read policy + // information back to the hosting UserPolicyCache after a successful cache + // load through UserPolicyCache::OnDiskCacheLoaded(). + void Load(); + + // Triggers a write operation to the disk cache on the FILE thread. + void Store(const em::CachedCloudPolicyResponse& policy); + + private: + friend class base::RefCountedThreadSafe<UserPolicyDiskCache>; + ~UserPolicyDiskCache(); + + // Tries to load the cache file on the FILE thread. + void LoadOnFileThread(); + + // Passes back the successfully read policy to the cache on the UI thread. + void FinishLoadOnUIThread(const em::CachedCloudPolicyResponse& policy); + + // Saves a policy blob on the FILE thread. + void StoreOnFileThread(const em::CachedCloudPolicyResponse& policy); + + base::WeakPtr<Delegate> delegate_; + const FilePath backing_file_path_; + + DISALLOW_COPY_AND_ASSIGN(UserPolicyDiskCache); +}; + +} // namespace policy + +#endif // CHROME_BROWSER_POLICY_USER_POLICY_DISK_CACHE_H_ diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index c6a77c2..b873b8e 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1630,6 +1630,8 @@ 'browser/policy/profile_policy_connector_factory.h', 'browser/policy/user_policy_cache.cc', 'browser/policy/user_policy_cache.h', + 'browser/policy/user_policy_disk_cache.cc', + 'browser/policy/user_policy_disk_cache.h', 'browser/policy/user_policy_identity_strategy.cc', 'browser/policy/user_policy_identity_strategy.h', 'browser/policy/user_policy_token_cache.cc', |