| // Copyright 2022 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #ifndef SRC_DEVELOPER_FORENSICS_FEEDBACK_ANNOTATIONS_UI_STATE_PROVIDER_H_ | |
| #define SRC_DEVELOPER_FORENSICS_FEEDBACK_ANNOTATIONS_UI_STATE_PROVIDER_H_ | |
| #include <fidl/fuchsia.ui.activity/cpp/fidl.h> | |
| #include <lib/async/cpp/task.h> | |
| #include <lib/async/dispatcher.h> | |
| #include <lib/fidl/cpp/client.h> | |
| #include <lib/fidl/cpp/wire/channel.h> | |
| #include <lib/fit/function.h> | |
| #include <lib/sys/cpp/service_directory.h> | |
| #include <lib/zx/time.h> | |
| #include <memory> | |
| #include <optional> | |
| #include <set> | |
| #include <string> | |
| #include <string_view> | |
| #include <variant> | |
| #include "src/developer/forensics/feedback/annotations/provider.h" | |
| #include "src/developer/forensics/feedback/annotations/types.h" | |
| #include "src/lib/backoff/backoff.h" | |
| #include "src/lib/timekeeper/clock.h" | |
| namespace forensics::feedback { | |
| // Caches the UI activity state and dynamically computes the duration since the last state change | |
| class UIStateProvider : public fidl::Server<fuchsia_ui_activity::Listener>, | |
| public fidl::AsyncEventHandler<fuchsia_ui_activity::Provider>, | |
| public CachedAsyncAnnotationProvider, | |
| public DynamicSyncAnnotationProvider { | |
| public: | |
| UIStateProvider(async_dispatcher_t* dispatcher, std::shared_ptr<sys::ServiceDirectory> services, | |
| std::unique_ptr<timekeeper::Clock> clock, | |
| std::unique_ptr<backoff::Backoff> backoff); | |
| static std::set<std::string> GetAnnotationKeys(); | |
| std::set<std::string> GetKeys() const override; | |
| // Returns the duration since the last state change | |
| Annotations Get() override; | |
| // Sets the most recent UI activity state with |callback| | |
| void GetOnUpdate(::fit::function<void(Annotations)> callback) override; | |
| // |fidl::Server<fuchsia_ui_activity::Listener>| | |
| void OnStateChanged(OnStateChangedRequest& request, | |
| OnStateChangedCompleter::Sync& completer) override; | |
| // |fidl::AsyncEventHandler<fuchsia_ui_activity::Provider>| | |
| void on_fidl_error(fidl::UnbindInfo error) override; | |
| private: | |
| void OnListenerClosed(fidl::UnbindInfo info); | |
| void OnDisconnect(zx_status_t status, std::string_view interface_name); | |
| void StartListening(); | |
| async_dispatcher_t* dispatcher_; | |
| const std::shared_ptr<sys::ServiceDirectory> services_; | |
| std::unique_ptr<timekeeper::Clock> clock_; | |
| std::unique_ptr<backoff::Backoff> backoff_; | |
| std::optional<ErrorOrString> current_state_; | |
| std::variant<std::monostate, Error, zx::time_monotonic> last_transition_time_; | |
| ::fit::function<void(Annotations)> on_update_; | |
| fidl::Client<fuchsia_ui_activity::Provider> provider_; | |
| std::optional<fidl::ServerBinding<fuchsia_ui_activity::Listener>> binding_; | |
| async::TaskClosureMethod<UIStateProvider, &UIStateProvider::StartListening> reconnect_task_{this}; | |
| }; | |
| } // namespace forensics::feedback | |
| #endif // SRC_DEVELOPER_FORENSICS_FEEDBACK_ANNOTATIONS_UI_STATE_PROVIDER_H_ |