summaryrefslogtreecommitdiff
path: root/chromium/media/blink/learning_experiment_helper.cc
blob: e34303845c6ef6c2ae681cb476a6e4bdd0303b8f (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
// Copyright 2019 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 "media/blink/learning_experiment_helper.h"

namespace media {

using learning::FeatureDictionary;
using learning::FeatureVector;
using learning::LearningTask;
using learning::LearningTaskController;
using learning::TargetValue;

LearningExperimentHelper::LearningExperimentHelper(
    std::unique_ptr<LearningTaskController> controller)
    : controller_(std::move(controller)) {}

LearningExperimentHelper::~LearningExperimentHelper() {
  CancelObservationIfNeeded();
}

void LearningExperimentHelper::BeginObservation(
    const FeatureDictionary& dictionary) {
  if (!controller_)
    return;

  CancelObservationIfNeeded();

  // Get the features that our task needs.
  FeatureVector features;
  dictionary.Lookup(controller_->GetLearningTask(), &features);

  observation_id_ = base::UnguessableToken::Create();
  controller_->BeginObservation(observation_id_, features);
}

void LearningExperimentHelper::CompleteObservationIfNeeded(
    const TargetValue& target) {
  if (!observation_id_)
    return;

  controller_->CompleteObservation(observation_id_, target);
  observation_id_ = base::UnguessableToken::Null();
}

void LearningExperimentHelper::CancelObservationIfNeeded() {
  if (!observation_id_)
    return;

  controller_->CancelObservation(observation_id_);
  observation_id_ = base::UnguessableToken::Null();
}

}  // namespace media