sync: Introduce CryptographerProvider interface

Creates a CryptographerProvider interface that can be used to expose the
syncable::Directory's Cryptographer without exposing the directory
itself.  Since the directory's cryptographer is protected by a lock,
this requires the creation of several helper classes to manage the
acquistion of the lock.

This CL includes the interface, an implementationthat exposes the
directory's cryptographer (DirectoryCryptographerProvider) and
a SimpleCryptographerProvider that wraps a non-owned Cryptographer for
testing purposes.

These interfaces will be used to implement and test encryption of
non-blocking sync types.

BUG=351005

Review URL: https://codereview.chromium.org/413833002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285777 0039d316-1c4b-4281-b951-d872f2087c98
parent 9f3f286a
// Copyright 2014 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 "sync/engine/cryptographer_provider.h"
namespace syncer {
CryptographerProvider::CryptographerProvider() {
}
CryptographerProvider::~CryptographerProvider() {
}
ScopedCryptographerRef::ScopedCryptographerRef() {
}
ScopedCryptographerRef::~ScopedCryptographerRef() {
}
bool ScopedCryptographerRef::Initialize(ScopedCryptographerInternal* impl) {
scoped_cryptographer_internal_.reset(impl);
return IsValid();
}
bool ScopedCryptographerRef::IsValid() const {
return !!Get();
}
Cryptographer* ScopedCryptographerRef::Get() const {
if (!scoped_cryptographer_internal_)
return NULL;
return scoped_cryptographer_internal_->Get();
}
ScopedCryptographerInternal::ScopedCryptographerInternal() {
}
ScopedCryptographerInternal::~ScopedCryptographerInternal() {
}
} // namespace syncer
// Copyright 2014 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 SYNC_ENGINE_CRYPTOGRAPHER_PROVIDER_H_
#define SYNC_ENGINE_CRYPTOGRAPHER_PROVIDER_H_
#include "base/memory/scoped_ptr.h"
#include "sync/base/sync_export.h"
namespace syncer {
class Cryptographer;
class ScopedCryptographerRef;
class ScopedCryptographerInternal;
// An interface for providing clients with a ScopedCryptographerRef.
//
// Used to expose the syncable::Directory's cryptographer to clients that
// would otherwise not have access to the Directory.
class SYNC_EXPORT_PRIVATE CryptographerProvider {
public:
CryptographerProvider();
virtual ~CryptographerProvider();
virtual bool InitScopedCryptographerRef(ScopedCryptographerRef* scoped) = 0;
};
// A concrete class representing a reference to a cryptographer.
//
// Access to the cryptographer is lost when this class goes out of scope.
class SYNC_EXPORT_PRIVATE ScopedCryptographerRef {
public:
ScopedCryptographerRef();
~ScopedCryptographerRef();
bool Initialize(ScopedCryptographerInternal* impl);
bool IsValid() const;
Cryptographer* Get() const;
private:
scoped_ptr<ScopedCryptographerInternal> scoped_cryptographer_internal_;
DISALLOW_COPY_AND_ASSIGN(ScopedCryptographerRef);
};
// An interface class used in the implementation of the ScopedCryptographerRef.
//
// We use this class to allow different implementations of
// CryptographerProvider to implement InitScopedCryptographer in different
// ways. The ScopedCryptographerRef itself must be stack-allocated, so it
// can't vary depending on which kind of CryptographerProvider is used to
// intialize it.
class SYNC_EXPORT_PRIVATE ScopedCryptographerInternal {
public:
ScopedCryptographerInternal();
virtual ~ScopedCryptographerInternal();
virtual Cryptographer* Get() const = 0;
};
} // namespace syncer
#endif // SYNC_ENGINE_CRYPTOGRAPHER_PROVIDER_H_
// Copyright 2014 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 "sync/engine/directory_cryptographer_provider.h"
#include "sync/syncable/directory.h"
#include "sync/syncable/syncable_read_transaction.h"
namespace syncer {
DirectoryCryptographerProvider::DirectoryCryptographerProvider(
syncable::Directory* dir)
: dir_(dir) {
}
DirectoryCryptographerProvider::~DirectoryCryptographerProvider() {
}
bool DirectoryCryptographerProvider::InitScopedCryptographerRef(
ScopedCryptographerRef* scoped) {
scoped->Initialize(new ScopedDirectoryCryptographerInternal(dir_));
return scoped->IsValid();
}
ScopedDirectoryCryptographerInternal::ScopedDirectoryCryptographerInternal(
syncable::Directory* dir)
: dir_(dir), trans_(FROM_HERE, dir) {
}
ScopedDirectoryCryptographerInternal::~ScopedDirectoryCryptographerInternal() {
}
Cryptographer* ScopedDirectoryCryptographerInternal::Get() const {
return dir_->GetCryptographer(&trans_);
}
} // namespace syncer
// Copyright 2014 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 SYNC_ENGINE_DIRECTORY_CRYPTOGRAPHER_PROVIDER_H_
#define SYNC_ENGINE_DIRECTORY_CRYPTOGRAPHER_PROVIDER_H_
#include "sync/engine/cryptographer_provider.h"
#include "sync/syncable/syncable_read_transaction.h"
namespace syncer {
namespace syncable {
class Directory;
}
// Provides access to the Directory's cryptographer through the
// CryptographerProvider interface.
class DirectoryCryptographerProvider : public CryptographerProvider {
public:
DirectoryCryptographerProvider(syncable::Directory* dir);
virtual ~DirectoryCryptographerProvider();
virtual bool InitScopedCryptographerRef(
ScopedCryptographerRef* scoped) OVERRIDE;
private:
syncable::Directory* dir_;
};
// An implementation detail of the DirectoryCryptographerProvider. Contains
// the ReadTransaction used to safely access the Cryptographer.
class ScopedDirectoryCryptographerInternal
: public ScopedCryptographerInternal {
public:
ScopedDirectoryCryptographerInternal(syncable::Directory* dir);
virtual ~ScopedDirectoryCryptographerInternal();
virtual Cryptographer* Get() const OVERRIDE;
private:
syncable::Directory* dir_;
syncable::ReadTransaction trans_;
DISALLOW_COPY_AND_ASSIGN(ScopedDirectoryCryptographerInternal);
};
} // namespace syncer
#endif // SYNC_ENGINE_DIRECTORY_CRYPTOGRAPHER_PROVIDER_H_
......@@ -48,10 +48,14 @@
'engine/conflict_resolver.h',
'engine/conflict_util.cc',
'engine/conflict_util.h',
'engine/cryptographer_provider.cc',
'engine/cryptographer_provider.h',
'engine/directory_commit_contribution.cc',
'engine/directory_commit_contribution.h',
'engine/directory_commit_contributor.cc',
'engine/directory_commit_contributor.h',
'engine/directory_cryptographer_provider.cc',
'engine/directory_cryptographer_provider.h',
'engine/directory_update_handler.cc',
'engine/directory_update_handler.h',
'engine/entity_tracker.cc',
......
......@@ -50,6 +50,8 @@
'test/engine/mock_nudge_handler.h',
'test/engine/mock_update_handler.cc',
'test/engine/mock_update_handler.h',
'test/engine/simple_cryptographer_provider.cc',
'test/engine/simple_cryptographer_provider.h',
'test/engine/single_type_mock_server.cc',
'test/engine/single_type_mock_server.h',
'test/engine/test_directory_setter_upper.cc',
......
// Copyright 2014 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 "sync/test/engine/simple_cryptographer_provider.h"
namespace syncer {
SimpleCryptographerProvider::SimpleCryptographerProvider(
Cryptographer* cryptographer)
: cryptographer_(cryptographer) {
}
SimpleCryptographerProvider::~SimpleCryptographerProvider() {
}
bool SimpleCryptographerProvider::InitScopedCryptographerRef(
ScopedCryptographerRef* scoped) {
scoped->Initialize(new SimpleScopedCryptographerInternal(cryptographer_));
return scoped->IsValid();
}
SimpleScopedCryptographerInternal::SimpleScopedCryptographerInternal(
Cryptographer* cryptographer)
: cryptographer_(cryptographer) {
}
SimpleScopedCryptographerInternal::~SimpleScopedCryptographerInternal() {
}
Cryptographer* SimpleScopedCryptographerInternal::Get() const {
return cryptographer_;
}
} // namespace syncer
// Copyright 2014 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 SYNC_TEST_ENGINE_SIMPLE_CRYPTOGRAPHER_PROVIDER_H_
#define SYNC_TEST_ENGINE_SIMPLE_CRYPTOGRAPHER_PROVIDER_H_
#include "sync/engine/cryptographer_provider.h"
#include "sync/util/cryptographer.h"
namespace syncer {
// A trivial cryptographer provider. Exposes the given Cryptographer through
// the CryptographerProvider interface. Does not take ownership of the
// |cryptographer|.
class SimpleCryptographerProvider : public CryptographerProvider {
public:
explicit SimpleCryptographerProvider(Cryptographer* cryptographer);
virtual ~SimpleCryptographerProvider();
// Implementation of CryptographerProvider.
virtual bool InitScopedCryptographerRef(
ScopedCryptographerRef* scoped) OVERRIDE;
private:
Cryptographer* cryptographer_;
};
class SimpleScopedCryptographerInternal : public ScopedCryptographerInternal {
public:
explicit SimpleScopedCryptographerInternal(Cryptographer* cryptographer);
virtual ~SimpleScopedCryptographerInternal();
// Implementation of ScopedCryptographerInternal.
virtual Cryptographer* Get() const OVERRIDE;
private:
Cryptographer* cryptographer_;
};
} // namespace syncer
#endif // SYNC_TEST_ENGINE_SIMPLE_CRYPTOGRAPHER_PROVIDER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment