Index: chrome/browser/policy/user_policy_key.cc |
diff --git a/chrome/browser/policy/user_policy_key.cc b/chrome/browser/policy/user_policy_key.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..45e5d7b6a51fcb7a42305d3a381417d07447c9e8 |
--- /dev/null |
+++ b/chrome/browser/policy/user_policy_key.cc |
@@ -0,0 +1,73 @@ |
+// Copyright (c) 2013 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/user_policy_key.h" |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/callback.h" |
+#include "base/file_util.h" |
+#include "base/logging.h" |
+#include "base/message_loop.h" |
+#include "base/stl_util.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace policy { |
+ |
+namespace { |
+ |
+const int kKeySizeLimit = 16 * 1024; |
+ |
+void LoadKey(const FilePath& path, std::vector<uint8>* key) { |
+ if (!file_util::PathExists(path)) { |
+ VLOG(1) << "No key at " << path.value(); |
+ return; |
+ } |
+ |
+ int64 size; |
+ if (!file_util::GetFileSize(path, &size)) { |
+ LOG(ERROR) << "Could not get size of " << path.value(); |
+ } else if (size == 0 || size > kKeySizeLimit) { |
+ LOG(ERROR) << "Key at " << path.value() << " has bad size " << size; |
+ } else { |
+ key->resize(size); |
+ int read_size = file_util::ReadFile( |
+ path, reinterpret_cast<char*>(vector_as_array(key)), size); |
+ if (read_size != size) { |
+ LOG(ERROR) << "Failed to read key at " << path.value(); |
+ key->clear(); |
+ } |
+ } |
+} |
+ |
+} // namespace |
+ |
+UserPolicyKey::UserPolicyKey(const FilePath& path) |
+ : path_(path), |
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} |
+ |
+UserPolicyKey::~UserPolicyKey() { |
+ DCHECK(CalledOnValidThread()); |
+} |
+ |
+void UserPolicyKey::Load(const base::Closure& callback) { |
+ DCHECK(CalledOnValidThread()); |
+ std::vector<uint8>* key = new std::vector<uint8>(); |
+ content::BrowserThread::PostBlockingPoolTaskAndReply( |
+ FROM_HERE, |
+ base::Bind(&LoadKey, path_, key), |
+ base::Bind(&UserPolicyKey::OnKeyLoaded, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ base::Owned(key), |
+ callback)); |
+} |
+ |
+void UserPolicyKey::OnKeyLoaded(std::vector<uint8>* key, |
+ const base::Closure& callback) { |
+ DCHECK(CalledOnValidThread()); |
+ key_.swap(*key); |
+ MessageLoop::current()->PostTask(FROM_HERE, callback); |
+} |
+ |
+} // namespace policy |