Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / local_discovery / privetv3_crypto_provider.cc
blob819df06be3103254bcc6ef47b93faefba6faf113
1 // Copyright 2014 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 "chrome/browser/local_discovery/privetv3_crypto_provider.h"
7 #include "base/logging.h"
9 namespace local_discovery {
11 namespace {
13 // A stub session type used for development/debugging.
14 const char kAuthMethodEmpty[] = "empty";
16 const char kHandshakeStateComplete[] = "complete";
17 const char kStubVerificationCode[] = "SAMPLE";
20 class PrivetV3CryptoProviderEmpty : public PrivetV3CryptoProvider {
21 public:
22 PrivetV3CryptoProviderEmpty();
23 ~PrivetV3CryptoProviderEmpty() override;
25 // PrivetV3CryptoProvider implementation.
26 HandshakeState GetState() override;
27 std::string GetAuthMethod() override;
28 HandshakeState GetNextStep(int* step, std::string* package) override;
29 HandshakeState SetStepResponse(int step,
30 const std::string& state,
31 const std::string& package) override;
32 std::string GetVerificationCode() override;
33 HandshakeState AcceptVerificationCode() override;
34 bool EncryptData(const std::string& input, std::string* output) override;
36 private:
37 HandshakeState state_;
40 scoped_ptr<PrivetV3CryptoProvider> Create(
41 const std::vector<std::string>& available_auth_methods) {
42 for (size_t i = 0; i < available_auth_methods.size(); i++) {
43 if (available_auth_methods[i] == kAuthMethodEmpty) {
44 return scoped_ptr<PrivetV3CryptoProvider>(
45 new PrivetV3CryptoProviderEmpty());
49 return scoped_ptr<PrivetV3CryptoProvider>();
52 PrivetV3CryptoProviderEmpty::PrivetV3CryptoProviderEmpty()
53 : state_(IN_PROGRESS) {
56 PrivetV3CryptoProviderEmpty::~PrivetV3CryptoProviderEmpty() {
59 PrivetV3CryptoProvider::HandshakeState PrivetV3CryptoProviderEmpty::GetState() {
60 return state_;
63 std::string PrivetV3CryptoProviderEmpty::GetAuthMethod() {
64 return kAuthMethodEmpty;
67 PrivetV3CryptoProvider::HandshakeState PrivetV3CryptoProviderEmpty::GetNextStep(
68 int* step,
69 std::string* package) {
70 DCHECK(state_ == IN_PROGRESS);
71 *step = 0;
72 package->clear();
73 state_ = AWAITING_RESPONSE;
75 return state_;
78 PrivetV3CryptoProvider::HandshakeState
79 PrivetV3CryptoProviderEmpty::SetStepResponse(int step,
80 const std::string& state,
81 const std::string& package) {
82 DCHECK(state_ == AWAITING_RESPONSE);
84 bool success =
85 (step == 0 && package.empty() && state == kHandshakeStateComplete);
87 if (success) {
88 state_ = AWAITING_USER_VERIFICATION;
89 } else {
90 state_ = HANDSHAKE_ERROR;
93 return state_;
96 std::string PrivetV3CryptoProviderEmpty::GetVerificationCode() {
97 DCHECK(state_ == AWAITING_USER_VERIFICATION);
98 return kStubVerificationCode;
101 PrivetV3CryptoProvider::HandshakeState
102 PrivetV3CryptoProviderEmpty::AcceptVerificationCode() {
103 DCHECK(state_ == AWAITING_USER_VERIFICATION);
104 return (state_ = HANDSHAKE_COMPLETE);
107 bool PrivetV3CryptoProviderEmpty::EncryptData(const std::string& input,
108 std::string* output) {
109 *output = input;
110 return true;
113 } // namespace local_discovery