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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/page_action/page_action_observer.h"
#include <memory>
#include <optional>
#include <string>
#include "base/scoped_observation.h"
#include "chrome/browser/ui/views/page_action/page_action_controller.h"
#include "chrome/browser/ui/views/page_action/page_action_model.h"
#include "chrome/browser/ui/views/page_action/page_action_model_observer.h"
namespace {
page_actions::PageActionState ModelToState(
actions::ActionId action_id,
const page_actions::PageActionModelInterface& model) {
page_actions::PageActionState state;
state.action_id = action_id;
state.showing = model.GetVisible();
state.chip_showing = model.GetShowSuggestionChip() && model.GetVisible();
state.tooltip = model.GetVisible()
? std::make_optional(model.GetTooltipText())
: std::nullopt;
state.label = model.GetShowSuggestionChip() && model.GetVisible()
? std::make_optional(model.GetText())
: std::nullopt;
return state;
}
} // namespace
namespace page_actions {
PageActionState::PageActionState() = default;
PageActionState::~PageActionState() = default;
PageActionState::PageActionState(const PageActionState&) = default;
PageActionState& PageActionState::operator=(const PageActionState&) = default;
bool PageActionState::operator==(const PageActionState& other) const {
return action_id == other.action_id && showing == other.showing &&
chip_showing == other.chip_showing && label == other.label &&
tooltip == other.tooltip;
}
// The internal implementation of `PageActionObserver`, that observes a
// `PageActionModel` and distills it into a public-facing type.
class PageActionObserverImpl : PageActionModelObserver {
public:
PageActionObserverImpl(PageActionObserver& base, actions::ActionId action_id);
~PageActionObserverImpl() override;
PageActionObserverImpl(const PageActionObserverImpl&) = delete;
PageActionObserverImpl& operator=(const PageActionObserverImpl&) = delete;
void RegisterAsPageActionObserver(PageActionController& controller);
// PageActionModelObserver
void OnPageActionModelChanged(const PageActionModelInterface& model) override;
void OnPageActionModelWillBeDeleted(
const PageActionModelInterface& model) override;
const PageActionState& page_action() const { return page_action_; }
private:
const raw_ref<PageActionObserver> base_;
base::ScopedObservation<PageActionModelInterface, PageActionModelObserver>
observation_{this};
const actions::ActionId action_id_;
PageActionState page_action_;
};
PageActionObserverImpl::PageActionObserverImpl(PageActionObserver& base,
actions::ActionId action_id)
: base_(base), action_id_(action_id) {}
PageActionObserverImpl::~PageActionObserverImpl() = default;
void PageActionObserverImpl::RegisterAsPageActionObserver(
PageActionController& controller) {
controller.AddObserver(action_id_, observation_);
page_action_ = ModelToState(action_id_, *observation_.GetSource());
}
void PageActionObserverImpl::OnPageActionModelChanged(
const PageActionModelInterface& model) {
PageActionState new_page_action_state = ModelToState(action_id_, model);
if (new_page_action_state.showing && !page_action_.showing) {
base_->OnPageActionIconShown(new_page_action_state);
} else if (!new_page_action_state.showing && page_action_.showing) {
base_->OnPageActionIconHidden(new_page_action_state);
}
if (new_page_action_state.chip_showing && !page_action_.chip_showing) {
base_->OnPageActionChipShown(new_page_action_state);
} else if (!new_page_action_state.chip_showing && page_action_.chip_showing) {
base_->OnPageActionChipHidden(new_page_action_state);
}
page_action_ = new_page_action_state;
}
void PageActionObserverImpl::OnPageActionModelWillBeDeleted(
const PageActionModelInterface& model) {
observation_.Reset();
}
PageActionObserver::PageActionObserver(actions::ActionId action_id)
: action_id_(action_id) {}
PageActionObserver::~PageActionObserver() = default;
void PageActionObserver::RegisterAsPageActionObserver(
PageActionController& controller) {
observer_impl_ = std::make_unique<PageActionObserverImpl>(*this, action_id_);
observer_impl_->RegisterAsPageActionObserver(controller);
}
const PageActionState& PageActionObserver::GetCurrentPageActionState() const {
return observer_impl_->page_action();
}
} // namespace page_actions
|