Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / gcm_driver / crypto / gcm_encryption_provider.cc
blob2b775e4c8ee56b0d6e2d1029e63736f7282bff5f
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.
5 #include "components/gcm_driver/crypto/gcm_encryption_provider.h"
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"
12 namespace gcm {
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");
18 GCMEncryptionProvider::GCMEncryptionProvider()
19 : weak_ptr_factory_(this) {
22 GCMEncryptionProvider::~GCMEncryptionProvider() {
25 void GCMEncryptionProvider::Init(
26 const base::FilePath& store_path,
27 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) {
28 DCHECK(!key_store_);
30 base::FilePath encryption_store_path = store_path;
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);
37 key_store_.reset(
38 new GCMKeyStore(encryption_store_path, blocking_task_runner));
41 void GCMEncryptionProvider::GetPublicKey(const std::string& app_id,
42 const PublicKeyCallback& callback) {
43 DCHECK(key_store_);
44 key_store_->GetKeys(
45 app_id, base::Bind(&GCMEncryptionProvider::DidGetPublicKey,
46 weak_ptr_factory_.GetWeakPtr(), app_id, callback));
49 void GCMEncryptionProvider::DidGetPublicKey(const std::string& app_id,
50 const PublicKeyCallback& callback,
51 const KeyPair& pair) {
52 if (!pair.IsInitialized()) {
53 key_store_->CreateKeys(
54 app_id, base::Bind(&GCMEncryptionProvider::DidCreatePublicKey,
55 weak_ptr_factory_.GetWeakPtr(), callback));
56 return;
59 DCHECK_EQ(KeyPair::ECDH_CURVE_25519, pair.type());
60 callback.Run(pair.public_key());
63 void GCMEncryptionProvider::DidCreatePublicKey(
64 const PublicKeyCallback& callback,
65 const KeyPair& pair) {
66 if (!pair.IsInitialized()) {
67 callback.Run(std::string());
68 return;
71 DCHECK_EQ(KeyPair::ECDH_CURVE_25519, pair.type());
72 callback.Run(pair.public_key());
75 } // namespace gcm