| // Copyright 2018 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. |
| |
| #ifndef COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_SCRIPT_EXECUTOR_H_ |
| #define COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_SCRIPT_EXECUTOR_H_ |
| |
| #include <deque> |
| #include <memory> |
| #include <string> |
| #include <vector> |
| |
| #include "base/callback_forward.h" |
| #include "base/memory/weak_ptr.h" |
| #include "components/autofill_assistant/browser/actions/action.h" |
| #include "components/autofill_assistant/browser/actions/action_delegate.h" |
| #include "components/autofill_assistant/browser/script.h" |
| #include "components/autofill_assistant/browser/script_executor_delegate.h" |
| #include "components/autofill_assistant/browser/service.pb.h" |
| |
| namespace autofill_assistant { |
| // Class to execute an assistant script. |
| class ScriptExecutor : public ActionDelegate { |
| public: |
| // |delegate| should outlive this object and should not be nullptr. |
| ScriptExecutor(const std::string& script_path, |
| ScriptExecutorDelegate* delegate); |
| ~ScriptExecutor() override; |
| |
| // What should happen after the script has run. |
| enum AtEnd { |
| // Continue normally. |
| CONTINUE = 0, |
| |
| // Shut down Autofill Assistant. |
| SHUTDOWN, |
| |
| // Reset all state and restart. |
| RESTART |
| }; |
| |
| // Contains the result of the Run operation. |
| struct Result { |
| bool success = false; |
| AtEnd at_end = AtEnd::CONTINUE; |
| }; |
| |
| using RunScriptCallback = base::OnceCallback<void(Result)>; |
| void Run(RunScriptCallback callback); |
| // Stop the execution of the current script and notify caller with 'at_end = |
| // |CONTINUE|' as the result, so it can continue. It's different from the |
| // interfaces 'ShutDown' and 'Restart' below, which notify caller with 'at_end |
| // = SHUTDOWN' and 'at_end = RESTART' respectively. |
| void Stop(); |
| |
| // Override ActionDelegate: |
| std::unique_ptr<BatchElementChecker> CreateBatchElementChecker() override; |
| void WaitForElement(const std::vector<std::string>& selectors, |
| base::OnceCallback<void(bool)> callback) override; |
| void ShowStatusMessage(const std::string& message) override; |
| void ClickElement(const std::vector<std::string>& selectors, |
| base::OnceCallback<void(bool)> callback) override; |
| void GetPaymentInformation( |
| payments::mojom::PaymentOptionsPtr payment_options, |
| base::OnceCallback<void(std::unique_ptr<PaymentInformation>)> callback) |
| override; |
| void ChooseAddress( |
| base::OnceCallback<void(const std::string&)> callback) override; |
| void FillAddressForm(const std::string& guid, |
| const std::vector<std::string>& selectors, |
| base::OnceCallback<void(bool)> callback) override; |
| void ChooseCard( |
| base::OnceCallback<void(const std::string&)> callback) override; |
| void FillCardForm(std::unique_ptr<autofill::CreditCard> card, |
| const base::string16& cvc, |
| const std::vector<std::string>& selectors, |
| base::OnceCallback<void(bool)> callback) override; |
| void SelectOption(const std::vector<std::string>& selectors, |
| const std::string& selected_option, |
| base::OnceCallback<void(bool)> callback) override; |
| void HighlightElement(const std::vector<std::string>& selectors, |
| base::OnceCallback<void(bool)> callback) override; |
| void FocusElement(const std::vector<std::string>& selectors, |
| base::OnceCallback<void(bool)> callback) override; |
| void SetFieldValue(const std::vector<std::string>& selectors, |
| const std::string& value, |
| base::OnceCallback<void(bool)> callback) override; |
| void GetOuterHtml( |
| const std::vector<std::string>& selectors, |
| base::OnceCallback<void(bool, const std::string&)> callback) override; |
| void LoadURL(const GURL& url) override; |
| void Shutdown() override; |
| void Restart() override; |
| ClientMemory* GetClientMemory() override; |
| autofill::PersonalDataManager* GetPersonalDataManager() override; |
| content::WebContents* GetWebContents() override; |
| void StopCurrentScript(const std::string& message) override; |
| void HideDetails() override; |
| void ShowDetails(const DetailsProto& details) override; |
| void ShowProgressBar(int progress, const std::string& message) override; |
| void HideProgressBar() override; |
| |
| private: |
| void OnGetActions(bool result, const std::string& response); |
| void RunCallback(bool success); |
| void ProcessNextAction(); |
| void ProcessAction(Action* action); |
| void GetNextActions(); |
| void OnProcessedAction(std::unique_ptr<ProcessedActionProto> action); |
| |
| std::string script_path_; |
| ScriptExecutorDelegate* delegate_; |
| RunScriptCallback callback_; |
| |
| std::vector<std::unique_ptr<Action>> actions_; |
| std::vector<ProcessedActionProto> processed_actions_; |
| std::string last_server_payload_; |
| AtEnd at_end_; |
| bool should_stop_script_; |
| bool should_clean_contextual_ui_on_finish_; |
| |
| base::WeakPtrFactory<ScriptExecutor> weak_ptr_factory_; |
| DISALLOW_COPY_AND_ASSIGN(ScriptExecutor); |
| }; |
| } // namespace autofill_assistant |
| |
| #endif // COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_SCRIPT_EXECUTOR_H_ |