summaryrefslogtreecommitdiff
path: root/chromium/components/cryptauth/fake_secure_channel.cc
blob: 03956aadc48792899f68a7b95a09f17809d9b95e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2017 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/cryptauth/fake_secure_channel.h"

#include "base/logging.h"

namespace cryptauth {

FakeSecureChannel::SentMessage::SentMessage(const std::string& feature,
                                            const std::string& payload)
    : feature(feature), payload(payload) {}

FakeSecureChannel::FakeSecureChannel(std::unique_ptr<Connection> connection,
                                     CryptAuthService* cryptauth_service)
    : SecureChannel(std::move(connection), cryptauth_service) {}

FakeSecureChannel::~FakeSecureChannel() {}

void FakeSecureChannel::ChangeStatus(const Status& new_status) {
  Status old_status = status_;
  status_ = new_status;

  // Copy to prevent channel from being removed during handler.
  std::vector<Observer*> observers_copy = observers_;
  for (auto* observer : observers_copy) {
    observer->OnSecureChannelStatusChanged(this, old_status, status_);
  }
}

void FakeSecureChannel::ReceiveMessage(const std::string& feature,
                                       const std::string& payload) {
  // Copy to prevent channel from being removed during handler.
  std::vector<Observer*> observers_copy = observers_;
  for (auto* observer : observers_copy) {
    observer->OnMessageReceived(this, feature, payload);
  }
}

void FakeSecureChannel::Initialize() {}

void FakeSecureChannel::SendMessage(const std::string& feature,
                                    const std::string& payload) {
  sent_messages_.push_back(SentMessage(feature, payload));
}

void FakeSecureChannel::Disconnect() {
  ChangeStatus(Status::DISCONNECTED);
}

void FakeSecureChannel::AddObserver(Observer* observer) {
  observers_.push_back(observer);
}

void FakeSecureChannel::RemoveObserver(Observer* observer) {
  observers_.erase(std::find(observers_.begin(), observers_.end(), observer),
                   observers_.end());
}

}  // namespace cryptauth