| Index: ash/action_handler/action_handler_state_controller.cc
|
| diff --git a/ash/action_handler/action_handler_state_controller.cc b/ash/action_handler/action_handler_state_controller.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..118dc0dea827cf1c460987c98667e7f485be68c6
|
| --- /dev/null
|
| +++ b/ash/action_handler/action_handler_state_controller.cc
|
| @@ -0,0 +1,87 @@
|
| +// 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 "ash/action_handler/action_handler_state_controller.h"
|
| +
|
| +#include <utility>
|
| +
|
| +#include "base/logging.h"
|
| +
|
| +namespace ash {
|
| +
|
| +ActionHandlerStateController::ActionHandlerStateController() {}
|
| +
|
| +ActionHandlerStateController::~ActionHandlerStateController() {}
|
| +
|
| +void ActionHandlerStateController::AddObserver(
|
| + ActionHandlerStateObserver* observer) {
|
| + observers_.AddObserver(observer);
|
| +}
|
| +
|
| +void ActionHandlerStateController::RemoveObserver(
|
| + ActionHandlerStateObserver* observer) {
|
| + observers_.RemoveObserver(observer);
|
| +}
|
| +
|
| +void ActionHandlerStateController::BindRequest(
|
| + mojom::ActionHandlerStateControllerRequest request) {
|
| + bindings_.AddBinding(this, std::move(request));
|
| +}
|
| +
|
| +mojom::ActionHandlerState ActionHandlerStateController::GetState(
|
| + mojom::ActionHandlerAction action) {
|
| + DCHECK_EQ(mojom::ActionHandlerAction::kNewNote, action);
|
| +
|
| + if (!action_handler_)
|
| + return mojom::ActionHandlerState::kNotSupported;
|
| + return new_note_state_;
|
| +}
|
| +
|
| +void ActionHandlerStateController::SetActionHandler(
|
| + mojom::ActionHandlerPtr action_handler) {
|
| + mojom::ActionHandlerState old_effective_new_note_state =
|
| + GetState(mojom::ActionHandlerAction::kNewNote);
|
| +
|
| + action_handler_ = std::move(action_handler);
|
| +
|
| + // Setting action handler value can change effective state - notify observers
|
| + // if that was the case.
|
| + if (GetState(mojom::ActionHandlerAction::kNewNote) !=
|
| + old_effective_new_note_state) {
|
| + NotifyActionStateChanged(mojom::ActionHandlerAction::kNewNote);
|
| + }
|
| +}
|
| +
|
| +void ActionHandlerStateController::UpdateActionState(
|
| + mojom::ActionHandlerAction action,
|
| + mojom::ActionHandlerState state) {
|
| + if (action != mojom::ActionHandlerAction::kNewNote) {
|
| + LOG(ERROR) << "Unexpected action " << action;
|
| + return;
|
| + }
|
| +
|
| + if (state == new_note_state_)
|
| + return;
|
| +
|
| + new_note_state_ = state;
|
| +
|
| + if (action_handler_)
|
| + NotifyActionStateChanged(action);
|
| +}
|
| +
|
| +void ActionHandlerStateController::RequestHandleAction(
|
| + mojom::ActionHandlerAction action) {
|
| + if (!action_handler_)
|
| + return;
|
| +
|
| + action_handler_->RequestHandleAction(action);
|
| +}
|
| +
|
| +void ActionHandlerStateController::NotifyActionStateChanged(
|
| + mojom::ActionHandlerAction action) {
|
| + for (auto& observer : observers_)
|
| + observer.OnActionStateChanged(action, GetState(action));
|
| +}
|
| +
|
| +} // namespace ash
|
|
|