| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/action_handler/action_handler_state_controller.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 namespace ash { |
| 12 |
| 13 ActionHandlerStateController::ActionHandlerStateController() {} |
| 14 |
| 15 ActionHandlerStateController::~ActionHandlerStateController() {} |
| 16 |
| 17 void ActionHandlerStateController::AddObserver( |
| 18 ActionHandlerStateObserver* observer) { |
| 19 observers_.AddObserver(observer); |
| 20 } |
| 21 |
| 22 void ActionHandlerStateController::RemoveObserver( |
| 23 ActionHandlerStateObserver* observer) { |
| 24 observers_.RemoveObserver(observer); |
| 25 } |
| 26 |
| 27 void ActionHandlerStateController::BindRequest( |
| 28 mojom::ActionHandlerStateControllerRequest request) { |
| 29 bindings_.AddBinding(this, std::move(request)); |
| 30 } |
| 31 |
| 32 mojom::ActionHandlerState ActionHandlerStateController::GetState( |
| 33 mojom::ActionHandlerAction action) { |
| 34 DCHECK_EQ(mojom::ActionHandlerAction::kNewNote, action); |
| 35 |
| 36 if (!action_handler_) |
| 37 return mojom::ActionHandlerState::kNotSupported; |
| 38 return new_note_state_; |
| 39 } |
| 40 |
| 41 void ActionHandlerStateController::SetActionHandler( |
| 42 mojom::ActionHandlerPtr action_handler) { |
| 43 mojom::ActionHandlerState old_effective_new_note_state = |
| 44 GetState(mojom::ActionHandlerAction::kNewNote); |
| 45 |
| 46 action_handler_ = std::move(action_handler); |
| 47 |
| 48 // Setting action handler value can change effective state - notify observers |
| 49 // if that was the case. |
| 50 if (GetState(mojom::ActionHandlerAction::kNewNote) != |
| 51 old_effective_new_note_state) { |
| 52 NotifyActionStateChanged(mojom::ActionHandlerAction::kNewNote); |
| 53 } |
| 54 } |
| 55 |
| 56 void ActionHandlerStateController::UpdateActionState( |
| 57 mojom::ActionHandlerAction action, |
| 58 mojom::ActionHandlerState state) { |
| 59 if (action != mojom::ActionHandlerAction::kNewNote) { |
| 60 LOG(ERROR) << "Unexpected action " << action; |
| 61 return; |
| 62 } |
| 63 |
| 64 if (state == new_note_state_) |
| 65 return; |
| 66 |
| 67 new_note_state_ = state; |
| 68 |
| 69 if (action_handler_) |
| 70 NotifyActionStateChanged(action); |
| 71 } |
| 72 |
| 73 void ActionHandlerStateController::RequestHandleAction( |
| 74 mojom::ActionHandlerAction action) { |
| 75 if (!action_handler_) |
| 76 return; |
| 77 |
| 78 action_handler_->RequestHandleAction(action); |
| 79 } |
| 80 |
| 81 void ActionHandlerStateController::NotifyActionStateChanged( |
| 82 mojom::ActionHandlerAction action) { |
| 83 for (auto& observer : observers_) |
| 84 observer.OnActionStateChanged(action, GetState(action)); |
| 85 } |
| 86 |
| 87 } // namespace ash |
| OLD | NEW |