OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 #include "chrome/browser/policy/user_policy_key.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/stl_util.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 |
| 16 namespace policy { |
| 17 |
| 18 namespace { |
| 19 |
| 20 const int kKeySizeLimit = 16 * 1024; |
| 21 |
| 22 void LoadKey(const FilePath& path, std::vector<uint8>* key) { |
| 23 if (!file_util::PathExists(path)) { |
| 24 VLOG(1) << "No key at " << path.value(); |
| 25 return; |
| 26 } |
| 27 |
| 28 int64 size; |
| 29 if (!file_util::GetFileSize(path, &size)) { |
| 30 LOG(ERROR) << "Could not get size of " << path.value(); |
| 31 } else if (size == 0 || size > kKeySizeLimit) { |
| 32 LOG(ERROR) << "Key at " << path.value() << " has bad size " << size; |
| 33 } else { |
| 34 key->resize(size); |
| 35 int read_size = file_util::ReadFile( |
| 36 path, reinterpret_cast<char*>(vector_as_array(key)), size); |
| 37 if (read_size != size) { |
| 38 LOG(ERROR) << "Failed to read key at " << path.value(); |
| 39 key->clear(); |
| 40 } |
| 41 } |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 UserPolicyKey::UserPolicyKey(const FilePath& path) |
| 47 : path_(path), |
| 48 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} |
| 49 |
| 50 UserPolicyKey::~UserPolicyKey() { |
| 51 DCHECK(CalledOnValidThread()); |
| 52 } |
| 53 |
| 54 void UserPolicyKey::Load(const base::Closure& callback) { |
| 55 DCHECK(CalledOnValidThread()); |
| 56 std::vector<uint8>* key = new std::vector<uint8>(); |
| 57 content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 58 FROM_HERE, |
| 59 base::Bind(&LoadKey, path_, key), |
| 60 base::Bind(&UserPolicyKey::OnKeyLoaded, |
| 61 weak_ptr_factory_.GetWeakPtr(), |
| 62 base::Owned(key), |
| 63 callback)); |
| 64 } |
| 65 |
| 66 void UserPolicyKey::OnKeyLoaded(std::vector<uint8>* key, |
| 67 const base::Closure& callback) { |
| 68 DCHECK(CalledOnValidThread()); |
| 69 key_.swap(*key); |
| 70 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 71 } |
| 72 |
| 73 } // namespace policy |
OLD | NEW |