1 // Copyright 2019 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 #ifndef COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_USER_ACTION_H_ 6 #define COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_USER_ACTION_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/callback.h" 12 #include "base/macros.h" 13 #include "components/autofill_assistant/browser/chip.h" 14 #include "components/autofill_assistant/browser/direct_action.h" 15 #include "components/autofill_assistant/browser/service.pb.h" 16 #include "components/autofill_assistant/browser/trigger_context.h" 17 18 namespace autofill_assistant { 19 20 // An action that the user can perform, through the UI or, on Android Q, through 21 // a direct action. 22 class UserAction { 23 public: 24 // Executes a user action with the given additional trigger context. 25 // 26 // The context is relevant only for actions that execute a script. 27 using Callback = base::OnceCallback<void(std::unique_ptr<TriggerContext>)>; 28 29 UserAction(UserAction&&); 30 UserAction(); 31 ~UserAction(); 32 UserAction& operator=(UserAction&&); 33 34 // Initializes user action from proto. 35 UserAction(const UserActionProto& action); 36 UserAction(const ChipProto& chip, 37 const DirectActionProto& direct_action, 38 bool enabled, 39 const std::string& identifier); 40 41 // Returns true if the action has no trigger, that is, there is no chip and no 42 // direct action. has_triggers()43 bool has_triggers() const { 44 return !chip_.empty() || !direct_action_.empty(); 45 } 46 chip()47 const Chip& chip() const { return chip_; } chip()48 Chip& chip() { return chip_; } 49 direct_action()50 const DirectAction& direct_action() const { return direct_action_; } direct_action()51 DirectAction& direct_action() { return direct_action_; } 52 identifier()53 std::string identifier() const { return identifier_; } 54 SetEnabled(bool enabled)55 void SetEnabled(bool enabled) { enabled_ = enabled; } 56 enabled()57 bool enabled() const { return enabled_; } 58 59 // Checks whether a callback is assigned to the action. Actions without 60 // callbacks do nothing. HasCallback()61 bool HasCallback() const { return callback_ ? true : false; } 62 63 // Specifies a callback that accepts no context. 64 void SetCallback(base::OnceCallback<void()> callback); 65 66 // Specifies a callback that accepts a context. SetCallback(base::OnceCallback<void (std::unique_ptr<TriggerContext>)> callback)67 void SetCallback( 68 base::OnceCallback<void(std::unique_ptr<TriggerContext>)> callback) { 69 callback_ = std::move(callback); 70 } 71 72 // Intercept calls to this action. 73 void AddInterceptor( 74 base::OnceCallback<void(UserAction::Callback, 75 std::unique_ptr<TriggerContext>)> interceptor); 76 77 // Call this action within the specific context, if a callback is set. Call(std::unique_ptr<TriggerContext> context)78 void Call(std::unique_ptr<TriggerContext> context) { 79 if (!callback_) 80 return; 81 82 std::move(callback_).Run(std::move(context)); 83 } 84 85 private: 86 // Specifies how the user can perform the action through the UI. Might be 87 // empty. 88 Chip chip_; 89 90 // Specifies how the user can perform the action as a direct action. Might be 91 // empty. 92 DirectAction direct_action_; 93 94 // Whether the action is enabled. The chip for a disabled action might still 95 // be shown. 96 bool enabled_ = true; 97 98 // Callback triggered to trigger the action. 99 Callback callback_; 100 101 // Optional identifier to uniquely identify this user action. 102 std::string identifier_; 103 104 DISALLOW_COPY_AND_ASSIGN(UserAction); 105 }; 106 107 } // namespace autofill_assistant 108 109 #endif // COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_USER_ACTION_H_ 110