summaryrefslogtreecommitdiff
path: root/chromium/components/feature_engagement_tracker/internal/init_aware_model.cc
blob: c514a705f05459e7d91a84bbe5dd7efb41b3f659 (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
62
63
64
65
// 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/feature_engagement_tracker/internal/init_aware_model.h"

#include "base/bind.h"

namespace feature_engagement_tracker {

InitAwareModel::InitAwareModel(std::unique_ptr<Model> model)
    : model_(std::move(model)),
      initialization_complete_(false),
      weak_ptr_factory_(this) {
  DCHECK(model_);
}

InitAwareModel::~InitAwareModel() = default;

void InitAwareModel::Initialize(const OnModelInitializationFinished& callback,
                                uint32_t current_day) {
  model_->Initialize(base::Bind(&InitAwareModel::OnInitializeComplete,
                                weak_ptr_factory_.GetWeakPtr(), callback),
                     current_day);
}

bool InitAwareModel::IsReady() const {
  return model_->IsReady();
}

const Event* InitAwareModel::GetEvent(const std::string& event_name) const {
  return model_->GetEvent(event_name);
}

void InitAwareModel::IncrementEvent(const std::string& event_name,
                                    uint32_t current_day) {
  if (IsReady()) {
    model_->IncrementEvent(event_name, current_day);
    return;
  }

  if (initialization_complete_)
    return;

  queued_events_.push_back(std::tie(event_name, current_day));
}

void InitAwareModel::OnInitializeComplete(
    const OnModelInitializationFinished& callback,
    bool success) {
  initialization_complete_ = true;
  if (success) {
    for (auto& event : queued_events_)
      model_->IncrementEvent(std::get<0>(event), std::get<1>(event));
  }
  queued_events_.clear();

  callback.Run(success);
}

size_t InitAwareModel::GetQueuedEventCountForTesting() {
  return queued_events_.size();
}

}  // namespace feature_engagement_tracker