OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/gcm_driver/crypto/gcm_encryption_provider.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "components/gcm_driver/crypto/gcm_key_store.h" |
| 10 #include "components/gcm_driver/crypto/proto/gcm_encryption_data.pb.h" |
| 11 |
| 12 namespace gcm { |
| 13 |
| 14 // Directory in the GCM Store in which the encryption database will be stored. |
| 15 const base::FilePath::CharType kEncryptionDirectoryName[] = |
| 16 FILE_PATH_LITERAL("Encryption"); |
| 17 |
| 18 GCMEncryptionProvider::GCMEncryptionProvider() |
| 19 : weak_ptr_factory_(this) { |
| 20 } |
| 21 |
| 22 GCMEncryptionProvider::~GCMEncryptionProvider() { |
| 23 } |
| 24 |
| 25 void GCMEncryptionProvider::Init( |
| 26 const base::FilePath& store_path, |
| 27 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) { |
| 28 DCHECK(!key_store_); |
| 29 |
| 30 base::FilePath encryption_store_path = store_path; |
| 31 |
| 32 // |store_path| can be empty in tests, which means that the database should |
| 33 // be created in memory rather than on-disk. |
| 34 if (!store_path.empty()) |
| 35 encryption_store_path = store_path.Append(kEncryptionDirectoryName); |
| 36 |
| 37 key_store_ = new GCMKeyStore(encryption_store_path, blocking_task_runner); |
| 38 } |
| 39 |
| 40 void GCMEncryptionProvider::GetPublicKey(const std::string& app_id, |
| 41 const PublicKeyCallback& callback) { |
| 42 DCHECK(key_store_); |
| 43 key_store_->GetKeys( |
| 44 app_id, base::Bind(&GCMEncryptionProvider::DidGetPublicKey, |
| 45 weak_ptr_factory_.GetWeakPtr(), app_id, callback)); |
| 46 } |
| 47 |
| 48 void GCMEncryptionProvider::DidGetPublicKey(const std::string& app_id, |
| 49 const PublicKeyCallback& callback, |
| 50 const KeyPair& pair) { |
| 51 if (!pair.IsInitialized()) { |
| 52 key_store_->CreateKeys( |
| 53 app_id, base::Bind(&GCMEncryptionProvider::DidCreatePublicKey, |
| 54 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 55 return; |
| 56 } |
| 57 |
| 58 DCHECK_EQ(KeyPair::ECDH_CURVE_25519, pair.type()); |
| 59 callback.Run(pair.public_key()); |
| 60 } |
| 61 |
| 62 void GCMEncryptionProvider::DidCreatePublicKey( |
| 63 const PublicKeyCallback& callback, |
| 64 const KeyPair& pair) { |
| 65 if (!pair.IsInitialized()) { |
| 66 callback.Run(std::string()); |
| 67 return; |
| 68 } |
| 69 |
| 70 DCHECK_EQ(KeyPair::ECDH_CURVE_25519, pair.type()); |
| 71 callback.Run(pair.public_key()); |
| 72 } |
| 73 |
| 74 } // namespace gcm |
OLD | NEW |