forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autotest_ambient_api.cc
97 lines (78 loc) · 2.82 KB
/
autotest_ambient_api.cc
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2020 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 "ash/public/cpp/autotest_ambient_api.h"
#include "ash/ambient/ambient_controller.h"
#include "ash/ambient/ui/ambient_view_delegate.h"
#include "ash/public/cpp/ambient/ambient_backend_controller.h"
#include "ash/shell.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
namespace ash {
namespace {
class PhotoTransitionAnimationObserver : public AmbientViewDelegateObserver {
public:
PhotoTransitionAnimationObserver(int num_completions,
base::TimeDelta timeout,
base::OnceClosure on_complete,
base::OnceClosure on_timeout)
: num_completions_(num_completions),
on_complete_(std::move(on_complete)),
on_timeout_(std::move(on_timeout)) {
DCHECK_GT(num_completions, 0);
DCHECK_GT(timeout, base::TimeDelta());
DCHECK(on_complete_);
DCHECK(on_timeout_);
// |base::Unretained| is safe here because this timer will be abandoned in
// the destructor.
timer_.Start(FROM_HERE, timeout,
base::BindOnce(&PhotoTransitionAnimationObserver::OnTimeout,
base::Unretained(this)));
scoped_observation_.Observe(
Shell::Get()->ambient_controller()->ambient_view_delegate());
}
PhotoTransitionAnimationObserver(const PhotoTransitionAnimationObserver&) =
delete;
PhotoTransitionAnimationObserver& operator=(
const PhotoTransitionAnimationObserver&) = delete;
~PhotoTransitionAnimationObserver() override = default;
// AmbientViewDelegateObserver:
void OnPhotoTransitionAnimationCompleted() override {
--num_completions_;
if (num_completions_ == 0) {
Cleanup();
std::move(on_complete_).Run();
delete this;
}
}
private:
void OnTimeout() {
Cleanup();
std::move(on_timeout_).Run();
delete this;
}
void Cleanup() {
timer_.AbandonAndStop();
scoped_observation_.Reset();
}
int num_completions_;
base::OnceClosure on_complete_;
base::OnceClosure on_timeout_;
base::OneShotTimer timer_;
base::ScopedObservation<AmbientViewDelegate, AmbientViewDelegateObserver>
scoped_observation_{this};
};
} // namespace
AutotestAmbientApi::AutotestAmbientApi() = default;
AutotestAmbientApi::~AutotestAmbientApi() = default;
void AutotestAmbientApi::WaitForPhotoTransitionAnimationCompleted(
int num_completions,
base::TimeDelta timeout,
base::OnceClosure on_complete,
base::OnceClosure on_timeout) {
new PhotoTransitionAnimationObserver(
num_completions, timeout, std::move(on_complete), std::move(on_timeout));
}
} // namespace ash