Chromium Code Reviews| Index: components/password_manager/content/public/type_converters.cc |
| diff --git a/components/password_manager/content/public/type_converters.cc b/components/password_manager/content/public/type_converters.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a69bec98193c7437b326bac5f839dca10685a87e |
| --- /dev/null |
| +++ b/components/password_manager/content/public/type_converters.cc |
| @@ -0,0 +1,128 @@ |
| +// Copyright 2016 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 "components/password_manager/content/public/type_converters.h" |
| + |
| +#include "base/logging.h" |
| +#include "components/password_manager/core/common/credential_manager_types.h" |
| +#include "mojo/common/common_type_converters.h" |
| +#include "mojo/common/url_type_converters.h" |
| +#include "third_party/WebKit/public/platform/WebCredential.h" |
| +#include "third_party/WebKit/public/platform/WebFederatedCredential.h" |
| +#include "third_party/WebKit/public/platform/WebPasswordCredential.h" |
| + |
| +using namespace password_manager; |
| + |
| +namespace mojo { |
| + |
| +namespace { |
| + |
| +mojom::CredentialType CMCredentialTypeToMojo(CredentialType type) { |
| + switch (type) { |
| + case CredentialType::CREDENTIAL_TYPE_EMPTY: |
| + return mojom::CredentialType::EMPTY; |
| + case CredentialType::CREDENTIAL_TYPE_PASSWORD: |
| + return mojom::CredentialType::PASSWORD; |
| + case CredentialType::CREDENTIAL_TYPE_FEDERATED: |
| + return mojom::CredentialType::FEDERATED; |
| + default: |
|
vabr (Chromium)
2016/03/09 16:26:29
Could you leave out the default clause completely?
leonhsl(Using Gerrit)
2016/03/10 06:42:41
Done. Thanks a lot for the knowledge sharing! Will
|
| + NOTREACHED(); |
| + } |
| + return mojom::CredentialType::EMPTY; |
|
vabr (Chromium)
2016/03/09 16:26:29
nit: Put a NOTREACHED() between the current lines
leonhsl(Using Gerrit)
2016/03/10 06:42:41
Done.
|
| +} |
| + |
| +CredentialType MojoCredentialTypeToCM(mojom::CredentialType type) { |
| + switch (type) { |
| + case mojom::CredentialType::EMPTY: |
| + return CredentialType::CREDENTIAL_TYPE_EMPTY; |
| + case mojom::CredentialType::PASSWORD: |
| + return CredentialType::CREDENTIAL_TYPE_PASSWORD; |
| + case mojom::CredentialType::FEDERATED: |
| + return CredentialType::CREDENTIAL_TYPE_FEDERATED; |
| + default: |
| + NOTREACHED(); |
| + } |
| + return CredentialType::CREDENTIAL_TYPE_EMPTY; |
| +} |
| + |
| +} // namespace |
| + |
| +mojom::CredentialInfoPtr |
| +TypeConverter<mojom::CredentialInfoPtr, CredentialInfo>::Convert( |
| + const CredentialInfo& input) { |
| + mojom::CredentialInfoPtr output(mojom::CredentialInfo::New()); |
| + output->type = CMCredentialTypeToMojo(input.type); |
| + output->id = mojo::String::From(input.id); |
| + output->name = mojo::String::From(input.name); |
| + output->icon = mojo::String::From(input.icon); |
| + output->password = mojo::String::From(input.password); |
| + output->federation = input.federation; |
| + |
| + return output; |
| +} |
| + |
| +CredentialInfo TypeConverter<CredentialInfo, mojom::CredentialInfoPtr>::Convert( |
| + const mojom::CredentialInfoPtr& input) { |
| + CredentialInfo output; |
| + output.type = MojoCredentialTypeToCM(input->type); |
| + output.id = input->id.To<base::string16>(); |
| + output.name = input->name.To<base::string16>(); |
| + output.icon = input->icon.To<GURL>(); |
| + output.password = input->password.To<base::string16>(); |
| + output.federation = input->federation; |
| + |
| + return output; |
| +} |
| + |
| +mojom::CredentialInfoPtr |
| +TypeConverter<mojom::CredentialInfoPtr, blink::WebCredential>::Convert( |
| + const blink::WebCredential& input) { |
| + mojom::CredentialInfoPtr output(mojom::CredentialInfo::New()); |
| + |
| + if (input.isPasswordCredential()) { |
| + // blink::WebPasswordCredential |
| + output->type = mojom::CredentialType::PASSWORD; |
| + output->password = mojo::String::From(base::string16( |
| + static_cast<const blink::WebPasswordCredential&>(input).password())); |
| + } else { |
| + DCHECK(input.isFederatedCredential()); |
| + // blink::WebFederatedCredential |
| + output->type = mojom::CredentialType::FEDERATED; |
| + output->federation = |
| + static_cast<const blink::WebFederatedCredential&>(input).provider(); |
| + } |
| + output->id = mojo::String::From(base::string16(input.id())); |
| + output->name = mojo::String::From(base::string16(input.name())); |
| + output->icon = mojo::String::From(GURL(input.iconURL())); |
| + |
| + return output; |
| +} |
| + |
| +BlinkWebCredentialPtr |
| +TypeConverter<BlinkWebCredentialPtr, mojom::CredentialInfoPtr>::Convert( |
| + const mojom::CredentialInfoPtr& input) { |
| + BlinkWebCredentialPtr output = nullptr; |
|
vabr (Chromium)
2016/03/09 16:26:29
nit: scoped_ptr is automatically null on creation,
leonhsl(Using Gerrit)
2016/03/10 06:42:42
Done.
|
| + |
| + switch (input->type) { |
| + case mojom::CredentialType::PASSWORD: |
| + output.reset(new blink::WebPasswordCredential( |
| + input->id.To<base::string16>(), input->password.To<base::string16>(), |
| + input->name.To<base::string16>(), input->icon.To<GURL>())); |
| + break; |
| + case mojom::CredentialType::FEDERATED: |
| + output.reset(new blink::WebFederatedCredential( |
| + input->id.To<base::string16>(), input->federation, |
| + input->name.To<base::string16>(), input->icon.To<GURL>())); |
| + break; |
| + case mojom::CredentialType::EMPTY: |
| + // Intentionally empty, return nullptr. |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| + return output; |
| +} |
| + |
| +} // namespace mojo |