Commit 1609feee authored by Jordan Demeulenaere's avatar Jordan Demeulenaere Committed by Commit Bot

[Autofill Assistant] Enable overlay ASAP when necessary.

Bug: 806868
Change-Id: I9aa48df17b16259fd9635e226c6bb9ea347e0fda
Reviewed-on: https://chromium-review.googlesource.com/c/1296595
Commit-Queue: Jordan Demeulenaere <jdemeulenaere@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarTheresa <twellington@chromium.org>
Reviewed-by: default avatarStephane Zermatten <szermatt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602418}
parent 664dd09a
......@@ -150,7 +150,8 @@
android:paddingStart="24dp"
android:paddingEnd="24dp"
android:gravity="center_vertical"
android:orientation="horizontal">
android:orientation="horizontal"
android:visibility="gone">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
......
......@@ -4,6 +4,7 @@ jochen@chromium.org
sdefresne@chromium.org
per-file app_modal_strings.grdp=file://components/app_modal/OWNERS
per-file autofill_assistant_strings.grdp=file://components/autofill_assistant/OWNERS
per-file autofill_strings.grdp=file://components/autofill/OWNERS
per-file bookmark_bar_strings.grdp=file://components/bookmarks/OWNERS
per-file browsing_data_strings.grdp=file://components/browsing_data/OWNERS
......
......@@ -6,6 +6,7 @@
#include <utility>
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "components/autofill_assistant/browser/protocol_utils.h"
#include "components/autofill_assistant/browser/ui_controller.h"
......@@ -28,6 +29,13 @@ static constexpr base::TimeDelta kPeriodicScriptCheckInterval =
// Number of script checks to run after a call to StartPeriodicScriptChecks.
static constexpr int kPeriodicScriptCheckCount = 10;
// Maximum number of script checks we should do before failing when trying to
// autostart.
static constexpr int kAutostartCheckCountLimit = 5;
// Caller parameter name.
static const char* const kCallerScriptParameterName = "CALLER";
} // namespace
// static
......@@ -88,10 +96,6 @@ Controller::Controller(
/* listener= */ this)),
parameters_(std::move(parameters)),
memory_(std::make_unique<ClientMemory>()),
allow_autostart_(true),
periodic_script_check_scheduled_(false),
periodic_script_check_count_(false),
clear_web_contents_delegate_(false),
weak_ptr_factory_(this) {
DCHECK(parameters_);
......@@ -109,6 +113,21 @@ Controller::Controller(
if (!web_contents->IsLoading()) {
GetOrCheckScripts(web_contents->GetLastCommittedURL());
}
if (allow_autostart_) {
auto iter = parameters_->find(kCallerScriptParameterName);
// TODO(crbug.com/806868): Put back an explicit AUTOSTART parameter so we
// don't need to know who calls us.
if (iter != parameters_->end() && iter->second == "1") {
should_fail_after_checking_scripts_ = true;
GetUiController()->ShowOverlay();
// TODO(crbug.com/806868): Find out how to add template string and add
// domain in the "Loading..." message.
GetUiController()->ShowStatusMessage(l10n_util::GetStringFUTF8(
IDS_AUTOFILL_ASSISTANT_LOADING,
base::UTF8ToUTF16(web_contents->GetVisibleURL().host())));
}
}
}
Controller::~Controller() {
......@@ -157,6 +176,15 @@ void Controller::OnPeriodicScriptCheck() {
periodic_script_check_scheduled_ = false;
return;
}
if (should_fail_after_checking_scripts_ &&
++total_script_check_count_ >= kAutostartCheckCountLimit) {
should_fail_after_checking_scripts_ = false;
GetUiController()->HideOverlay();
GetUiController()->ShowStatusMessage(
l10n_util::GetStringUTF8(IDS_AUTOFILL_ASSISTANT_DEFAULT_ERROR));
}
periodic_script_check_count_--;
script_tracker_->CheckScripts(kPeriodicScriptCheckInterval);
base::PostDelayedTaskWithTraits(
......@@ -270,6 +298,11 @@ void Controller::OnRunnableScriptsChanged(
if (script_tracker_->running())
return;
if (!runnable_scripts.empty()) {
should_fail_after_checking_scripts_ = false;
GetUiController()->HideOverlay();
}
// Under specific conditions, we can directly run a script without first
// displaying it. This is meant to work only at the very beginning, when no
// scripts have run, there has been no interaction with the webpage and only
......
......@@ -107,17 +107,22 @@ class Controller : public ScriptExecutorDelegate,
// Domain of the last URL the controller requested scripts from.
std::string script_domain_;
std::unique_ptr<ClientMemory> memory_;
bool allow_autostart_;
bool allow_autostart_ = true;
// Whether a task for periodic checks is scheduled.
bool periodic_script_check_scheduled_;
bool periodic_script_check_scheduled_ = false;
// Number of remaining periodic checks.
int periodic_script_check_count_;
int periodic_script_check_count_ = 0;
int total_script_check_count_ = 0;
// Whether to clear the web_contents delegate when the controller is
// destroyed.
bool clear_web_contents_delegate_;
bool clear_web_contents_delegate_ = false;
// Whether we should hide the overlay and show an error message after a first
// unsuccessful round of preconditions checking.
bool should_fail_after_checking_scripts_ = false;
base::WeakPtrFactory<Controller> weak_ptr_factory_;
......
......@@ -4,5 +4,8 @@
<message name="IDS_AUTOFILL_ASSISTANT_DEFAULT_ERROR" desc="Text label that is shown when communication with server fails.">
Service is not available. Try again later.
</message>
<message name="IDS_AUTOFILL_ASSISTANT_LOADING" desc="Text label that is shown during the loading of the first page, right after being triggered.">
Loading <ph name="SITE_NAME">$1<ex>google.com</ex></ph>
</message>
</if>
</grit-part>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment