Commit 413d67ae authored by pneubeck's avatar pneubeck Committed by Commit bot

Small clean up in PlatformKeysService.

This changes the internal function GetPlatformKeysOfExtension to always pass a PlatformKeys value to its callback.
The only function change is: If the base::Value read from StateStore has an incorrect type, it will be ignored and a new empty (but correct) value will be created.

BUG=450167

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

Cr-Commit-Position: refs/heads/master@{#314348}
parent 24f3c465
...@@ -17,7 +17,6 @@ namespace chromeos { ...@@ -17,7 +17,6 @@ namespace chromeos {
namespace { namespace {
const char kErrorInternal[] = "Internal Error.";
const char kErrorKeyNotAllowedForSigning[] = const char kErrorKeyNotAllowedForSigning[] =
"This key is not allowed for signing. Either it was used for signing " "This key is not allowed for signing. Either it was used for signing "
"before or it was not correctly generated."; "before or it was not correctly generated.";
...@@ -30,16 +29,10 @@ scoped_ptr<base::StringValue> GetPublicKeyValue( ...@@ -30,16 +29,10 @@ scoped_ptr<base::StringValue> GetPublicKeyValue(
return make_scoped_ptr(new base::StringValue(public_key_spki_der_b64)); return make_scoped_ptr(new base::StringValue(public_key_spki_der_b64));
} }
// Wraps |callback| into a void(bool) callback which forwards void RunGenerateKeyCallback(
// |public_key_spki_der| if |true| is passed to it.
void WrapGenerateKeyCallback(
const PlatformKeysService::GenerateKeyCallback& callback, const PlatformKeysService::GenerateKeyCallback& callback,
const std::string& public_key_spki_der, const std::string& public_key_spki_der) {
bool success) { callback.Run(public_key_spki_der, std::string() /* no error */);
if (success)
callback.Run(public_key_spki_der, std::string() /* no error */);
else
callback.Run(std::string() /* no public key */, kErrorInternal);
} }
// Callback used by |PlatformKeysService::Sign|. // Callback used by |PlatformKeysService::Sign|.
...@@ -118,7 +111,7 @@ void PlatformKeysService::Sign(const std::string& token_id, ...@@ -118,7 +111,7 @@ void PlatformKeysService::Sign(const std::string& token_id,
void PlatformKeysService::RegisterPublicKey( void PlatformKeysService::RegisterPublicKey(
const std::string& extension_id, const std::string& extension_id,
const std::string& public_key_spki_der, const std::string& public_key_spki_der,
const base::Callback<void(bool)>& callback) { const base::Closure& callback) {
GetPlatformKeysOfExtension( GetPlatformKeysOfExtension(
extension_id, extension_id,
base::Bind(&PlatformKeysService::RegisterPublicKeyGotPlatformKeys, base::Bind(&PlatformKeysService::RegisterPublicKeyGotPlatformKeys,
...@@ -144,12 +137,16 @@ void PlatformKeysService::GetPlatformKeysOfExtension( ...@@ -144,12 +137,16 @@ void PlatformKeysService::GetPlatformKeysOfExtension(
const std::string& extension_id, const std::string& extension_id,
const GetPlatformKeysCallback& callback) { const GetPlatformKeysCallback& callback) {
state_store_->GetExtensionValue( state_store_->GetExtensionValue(
extension_id, extension_id, kStateStorePlatformKeys,
kStateStorePlatformKeys,
base::Bind(&PlatformKeysService::GotPlatformKeysOfExtension, base::Bind(&PlatformKeysService::GotPlatformKeysOfExtension,
weak_factory_.GetWeakPtr(), weak_factory_.GetWeakPtr(), extension_id, callback));
extension_id, }
callback));
void PlatformKeysService::SetPlatformKeysOfExtension(
const std::string& extension_id,
scoped_ptr<base::ListValue> platform_keys) {
state_store_->SetExtensionValue(extension_id, kStateStorePlatformKeys,
platform_keys.Pass());
} }
void PlatformKeysService::GenerateRSAKeyCallback( void PlatformKeysService::GenerateRSAKeyCallback(
...@@ -161,22 +158,16 @@ void PlatformKeysService::GenerateRSAKeyCallback( ...@@ -161,22 +158,16 @@ void PlatformKeysService::GenerateRSAKeyCallback(
callback.Run(std::string() /* no public key */, error_message); callback.Run(std::string() /* no public key */, error_message);
return; return;
} }
base::Callback<void(bool)> wrapped_callback( base::Closure wrapped_callback(
base::Bind(&WrapGenerateKeyCallback, callback, public_key_spki_der)); base::Bind(&RunGenerateKeyCallback, callback, public_key_spki_der));
RegisterPublicKey(extension_id, public_key_spki_der, wrapped_callback); RegisterPublicKey(extension_id, public_key_spki_der, wrapped_callback);
} }
void PlatformKeysService::RegisterPublicKeyGotPlatformKeys( void PlatformKeysService::RegisterPublicKeyGotPlatformKeys(
const std::string& extension_id, const std::string& extension_id,
const std::string& public_key_spki_der, const std::string& public_key_spki_der,
const base::Callback<void(bool)>& callback, const base::Closure& callback,
scoped_ptr<base::ListValue> platform_keys) { scoped_ptr<base::ListValue> platform_keys) {
if (!platform_keys) {
LOG(ERROR) << "Error while reading the platform keys.";
callback.Run(false);
return;
}
scoped_ptr<base::StringValue> key_value( scoped_ptr<base::StringValue> key_value(
GetPublicKeyValue(public_key_spki_der)); GetPublicKeyValue(public_key_spki_der));
...@@ -184,10 +175,8 @@ void PlatformKeysService::RegisterPublicKeyGotPlatformKeys( ...@@ -184,10 +175,8 @@ void PlatformKeysService::RegisterPublicKeyGotPlatformKeys(
<< "Keys are assumed to be generated and not to be registered multiple " << "Keys are assumed to be generated and not to be registered multiple "
"times."; "times.";
platform_keys->Append(key_value.release()); platform_keys->Append(key_value.release());
SetPlatformKeysOfExtension(extension_id, platform_keys.Pass());
state_store_->SetExtensionValue( callback.Run();
extension_id, kStateStorePlatformKeys, platform_keys.Pass());
callback.Run(true);
} }
void PlatformKeysService::InvalidateKey( void PlatformKeysService::InvalidateKey(
...@@ -205,8 +194,7 @@ void PlatformKeysService::InvalidateKey( ...@@ -205,8 +194,7 @@ void PlatformKeysService::InvalidateKey(
return; return;
} }
state_store_->SetExtensionValue( SetPlatformKeysOfExtension(extension_id, platform_keys.Pass());
extension_id, kStateStorePlatformKeys, platform_keys.Pass());
callback.Run(true); callback.Run(true);
} }
...@@ -220,8 +208,11 @@ void PlatformKeysService::GotPlatformKeysOfExtension( ...@@ -220,8 +208,11 @@ void PlatformKeysService::GotPlatformKeysOfExtension(
base::ListValue* keys = NULL; base::ListValue* keys = NULL;
if (!value->GetAsList(&keys)) { if (!value->GetAsList(&keys)) {
LOG(ERROR) << "Found a value of wrong type."; LOG(ERROR) << "Found a value of wrong type.";
value.reset();
keys = new base::ListValue;
value.reset(keys);
} }
ignore_result(value.release()); ignore_result(value.release());
callback.Run(make_scoped_ptr(keys)); callback.Run(make_scoped_ptr(keys));
} }
......
...@@ -85,8 +85,8 @@ class PlatformKeysService : public KeyedService { ...@@ -85,8 +85,8 @@ class PlatformKeysService : public KeyedService {
const SignCallback& callback); const SignCallback& callback);
private: private:
typedef base::Callback<void(scoped_ptr<base::ListValue> platform_keys)> using GetPlatformKeysCallback =
GetPlatformKeysCallback; base::Callback<void(scoped_ptr<base::ListValue> platform_keys)>;
// Registers the given public key as newly generated key, which is allowed to // Registers the given public key as newly generated key, which is allowed to
// be used for signing for a single time. Afterwards, calls |callback|. If // be used for signing for a single time. Afterwards, calls |callback|. If
...@@ -94,7 +94,7 @@ class PlatformKeysService : public KeyedService { ...@@ -94,7 +94,7 @@ class PlatformKeysService : public KeyedService {
// callback. // callback.
void RegisterPublicKey(const std::string& extension_id, void RegisterPublicKey(const std::string& extension_id,
const std::string& public_key_spki_der, const std::string& public_key_spki_der,
const base::Callback<void(bool)>& callback); const base::Closure& callback);
// Gets the current validity of the given public key by reading StateStore. // Gets the current validity of the given public key by reading StateStore.
// Invalidates the key if it was found to be valid. Finally, calls |callback| // Invalidates the key if it was found to be valid. Finally, calls |callback|
...@@ -109,6 +109,11 @@ class PlatformKeysService : public KeyedService { ...@@ -109,6 +109,11 @@ class PlatformKeysService : public KeyedService {
void GetPlatformKeysOfExtension(const std::string& extension_id, void GetPlatformKeysOfExtension(const std::string& extension_id,
const GetPlatformKeysCallback& callback); const GetPlatformKeysCallback& callback);
// Writes |platform_keys| to the state store of the extension with id
// |extension_id|.
void SetPlatformKeysOfExtension(const std::string& extension_id,
scoped_ptr<base::ListValue> platform_keys);
// Callback used by |GenerateRSAKey|. // Callback used by |GenerateRSAKey|.
// If the key generation was successful, registers the generated public key // If the key generation was successful, registers the generated public key
// for the given extension. If any error occurs during key generation or // for the given extension. If any error occurs during key generation or
...@@ -125,7 +130,7 @@ class PlatformKeysService : public KeyedService { ...@@ -125,7 +130,7 @@ class PlatformKeysService : public KeyedService {
void RegisterPublicKeyGotPlatformKeys( void RegisterPublicKeyGotPlatformKeys(
const std::string& extension_id, const std::string& extension_id,
const std::string& public_key_spki_der, const std::string& public_key_spki_der,
const base::Callback<void(bool)>& callback, const base::Closure& callback,
scoped_ptr<base::ListValue> platform_keys); scoped_ptr<base::ListValue> platform_keys);
// Callback used by |ReadValidityAndInvalidateKey|. // Callback used by |ReadValidityAndInvalidateKey|.
......
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