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_STATE_H_
6 #define COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_STATE_H_
7 
8 #include <ostream>
9 
10 namespace autofill_assistant {
11 
12 // High-level states the Autofill Assistant can be in.
13 //
14 // A typical run, when started from CCT, autostarts a script, then displays a
15 // prompt and continues until a script sends the Stop action:
16 //
17 // INACTIVE -> STARTING -> RUNNING -> PROMPT -> RUNNING -> .. -> STOPPED
18 //
19 // A typical run, when started from a direct action, goes into tracking mode,
20 // execute a script, the goes back to tracking mode:
21 //
22 // INACTIVE -> TRACKING -> RUNNING -> TRACKING -> ... -> STOPPED
23 //
24 // See the individual state for possible state transitions.
25 enum class AutofillAssistantState {
26   // Autofill assistant is not doing or showing anything.
27   //
28   // Initial state.
29   // Next states: STARTING, TRACKING, STOPPED
30   INACTIVE = 0,
31 
32   // Autofill assistant is keeping track of script availability.
33   //
34   // In this mode, no UI is shown and scripts are not autostarted. User
35   // actions might be available.
36   //
37   // Note that it is possible to go from TRACKING to STARTING to trigger
38   // whatever autostartable scripts is defined for a page.
39   //
40   // Next states: STARTING, RUNNING, STOPPED
41   TRACKING,
42 
43   // Autofill assistant is waiting for an autostart script.
44   //
45   // Status message, progress and details are initialized to useful values.
46   //
47   // Next states: RUNNING, AUTOSTART_FALLBACK_PROMPT, STOPPED
48   STARTING,
49 
50   // Autofill assistant is manipulating the website.
51   //
52   // Status message, progress and details kept up-to-date by the running
53   // script.
54   //
55   // Next states: PROMPT, MODAL_DIALOG, TRACKING, STARTING, STOPPED
56   RUNNING,
57 
58   // Autofill assistant is waiting for the user to make a choice.
59   //
60   // Status message is initialized to a useful value. Chips are set and might be
61   // empty. A touchable area must be configured. The user might be filling in
62   // the data for a payment request.
63   //
64   // Next states: RUNNING, TRACKING, STOPPED
65   PROMPT,
66 
67   // Autofill assistant is waiting for the user to make the first choice.
68   //
69   // When autostartable scripts are expected, this is only triggered as a
70   // fallback if there are non-autostartable scripts to choose from instead.
71   //
72   // Next states: RUNNING, STOPPED
73   AUTOSTART_FALLBACK_PROMPT,
74 
75   // Autofill assistant is expecting a modal dialog, such as the one asking for
76   // CVC.
77   //
78   // Next states: RUNNING
79   MODAL_DIALOG,
80 
81   // Autofill assistant is stopped, but the controller is still available.
82   //
83   // This is a final state for the UI, which, when entering this state, detaches
84   // itself from the controller and lets the user read  the message.
85   //
86   // In that scenario, the status message at the time of transition to STOPPED
87   // is supposed to contain the final message.
88   //
89   // Next states: TRACKING
90   STOPPED,
91 
92   // Autofill assistant is waiting for the user to browse the website until one
93   // of a set of specified preconditions match.
94   //
95   // Prompt-like state where a user is expected to browse a website with a
96   // minimal autofill assistant UI. Preconditions are still evaluated and
97   // buttons displayed if they match. There is no touchable
98   // area set for this state.
99   //
100   // In praticular, navigation to subdomains and user gestures like 'go back'
101   // are not a reason to shutdown autofill assistant. Navigating away from the
102   // original domain will however shut down autofill assistant.
103   //
104   // Next states: RUNNING, TRACKING, STOPPED
105   BROWSE,
106 };
107 
108 inline std::ostream& operator<<(std::ostream& out,
109                                 const AutofillAssistantState& state) {
110 #ifdef NDEBUG
111   // Non-debugging builds write the enum number.
112   out << static_cast<int>(state);
113   return out;
114 #else
115   // Debugging builds write a string representation of |state|.
116   switch (state) {
117     case AutofillAssistantState::INACTIVE:
118       out << "INACTIVE";
119       break;
120     case AutofillAssistantState::TRACKING:
121       out << "TRACKING";
122       break;
123     case AutofillAssistantState::STARTING:
124       out << "STARTING";
125       break;
126     case AutofillAssistantState::RUNNING:
127       out << "RUNNING";
128       break;
129     case AutofillAssistantState::PROMPT:
130       out << "PROMPT";
131       break;
132     case AutofillAssistantState::AUTOSTART_FALLBACK_PROMPT:
133       out << "AUTOSTART_FALLBACK_PROMPT";
134       break;
135     case AutofillAssistantState::MODAL_DIALOG:
136       out << "MODAL_DIALOG";
137       break;
138     case AutofillAssistantState::STOPPED:
139       out << "STOPPED";
140       break;
141     case AutofillAssistantState::BROWSE:
142       out << "BROWSE";
143       break;
144       // Intentionally no default case to make compilation fail if a new value
145       // was added to the enum but not to this list.
146   }
147   return out;
148 #endif  // NDEBUG
149 }
150 
151 }  // namespace autofill_assistant
152 
153 #endif  // COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_STATE_H_
154