зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1710668 - Refactor handlePromptEvent into PromptController. r=owlish
Differential Revision: https://phabricator.services.mozilla.com/D122503
This commit is contained in:
Родитель
f6d8c81788
Коммит
063b1134e6
|
@ -1130,11 +1130,14 @@ public class GeckoSession {
|
|||
if ("GeckoView:PinOnScreen".equals(event)) {
|
||||
GeckoSession.this.setShouldPinOnScreen(message.getBoolean("pinned"));
|
||||
} else if ("GeckoView:Prompt".equals(event)) {
|
||||
handlePromptEvent(GeckoSession.this, message, callback);
|
||||
mPromptController.handleEvent(
|
||||
GeckoSession.this, message, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final PromptController mPromptController;
|
||||
|
||||
protected @Nullable Window mWindow;
|
||||
private GeckoSessionSettings mSettings;
|
||||
|
||||
|
@ -1149,6 +1152,7 @@ public class GeckoSession {
|
|||
mListener.registerListeners();
|
||||
|
||||
mWebExtensionController = new WebExtension.SessionController(this);
|
||||
mPromptController = new PromptController();
|
||||
|
||||
mAutofillSupport = new Autofill.Support(this);
|
||||
mAutofillSupport.registerListeners();
|
||||
|
@ -2589,291 +2593,6 @@ public class GeckoSession {
|
|||
return mSelectionActionDelegate.getDelegate();
|
||||
}
|
||||
|
||||
/* package */ static void handlePromptEvent(final GeckoSession session,
|
||||
final GeckoBundle message,
|
||||
final EventCallback callback) {
|
||||
final PromptDelegate delegate = session.getPromptDelegate();
|
||||
if (delegate == null) {
|
||||
// Default behavior is same as calling dismiss() on callback.
|
||||
callback.sendSuccess(null);
|
||||
return;
|
||||
}
|
||||
|
||||
final String type = message.getString("type");
|
||||
final String mode = message.getString("mode");
|
||||
final String title = message.getString("title");
|
||||
final String msg = message.getString("msg");
|
||||
GeckoResult<PromptDelegate.PromptResponse> res = null;
|
||||
|
||||
switch (type) {
|
||||
case "alert": {
|
||||
final PromptDelegate.AlertPrompt prompt =
|
||||
new PromptDelegate.AlertPrompt(title, msg);
|
||||
res = delegate.onAlertPrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "beforeUnload": {
|
||||
final PromptDelegate.BeforeUnloadPrompt prompt =
|
||||
new PromptDelegate.BeforeUnloadPrompt();
|
||||
res = delegate.onBeforeUnloadPrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "repost": {
|
||||
final PromptDelegate.RepostConfirmPrompt prompt =
|
||||
new PromptDelegate.RepostConfirmPrompt();
|
||||
res = delegate.onRepostConfirmPrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "button": {
|
||||
final PromptDelegate.ButtonPrompt prompt =
|
||||
new PromptDelegate.ButtonPrompt(title, msg);
|
||||
res = delegate.onButtonPrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "text": {
|
||||
final String defaultValue = message.getString("value");
|
||||
final PromptDelegate.TextPrompt prompt =
|
||||
new PromptDelegate.TextPrompt(title, msg, defaultValue);
|
||||
res = delegate.onTextPrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "auth": {
|
||||
final PromptDelegate.AuthPrompt.AuthOptions authOptions =
|
||||
new PromptDelegate.AuthPrompt.AuthOptions(message.getBundle("options"));
|
||||
final PromptDelegate.AuthPrompt prompt =
|
||||
new PromptDelegate.AuthPrompt(title, msg, authOptions);
|
||||
res = delegate.onAuthPrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "choice": {
|
||||
final int intMode;
|
||||
if ("menu".equals(mode)) {
|
||||
intMode = PromptDelegate.ChoicePrompt.Type.MENU;
|
||||
} else if ("single".equals(mode)) {
|
||||
intMode = PromptDelegate.ChoicePrompt.Type.SINGLE;
|
||||
} else if ("multiple".equals(mode)) {
|
||||
intMode = PromptDelegate.ChoicePrompt.Type.MULTIPLE;
|
||||
} else {
|
||||
callback.sendError("Invalid mode");
|
||||
return;
|
||||
}
|
||||
|
||||
final GeckoBundle[] choiceBundles = message.getBundleArray("choices");
|
||||
final PromptDelegate.ChoicePrompt.Choice[] choices;
|
||||
if (choiceBundles == null || choiceBundles.length == 0) {
|
||||
choices = new PromptDelegate.ChoicePrompt.Choice[0];
|
||||
} else {
|
||||
choices = new PromptDelegate.ChoicePrompt.Choice[choiceBundles.length];
|
||||
for (int i = 0; i < choiceBundles.length; i++) {
|
||||
choices[i] = new PromptDelegate.ChoicePrompt.Choice(choiceBundles[i]);
|
||||
}
|
||||
}
|
||||
|
||||
final PromptDelegate.ChoicePrompt prompt =
|
||||
new PromptDelegate.ChoicePrompt(title, msg, intMode, choices);
|
||||
res = delegate.onChoicePrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "color": {
|
||||
final String defaultValue = message.getString("value");
|
||||
final PromptDelegate.ColorPrompt prompt =
|
||||
new PromptDelegate.ColorPrompt(title, defaultValue);
|
||||
res = delegate.onColorPrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "datetime": {
|
||||
final int intMode;
|
||||
if ("date".equals(mode)) {
|
||||
intMode = PromptDelegate.DateTimePrompt.Type.DATE;
|
||||
} else if ("month".equals(mode)) {
|
||||
intMode = PromptDelegate.DateTimePrompt.Type.MONTH;
|
||||
} else if ("week".equals(mode)) {
|
||||
intMode = PromptDelegate.DateTimePrompt.Type.WEEK;
|
||||
} else if ("time".equals(mode)) {
|
||||
intMode = PromptDelegate.DateTimePrompt.Type.TIME;
|
||||
} else if ("datetime-local".equals(mode)) {
|
||||
intMode = PromptDelegate.DateTimePrompt.Type.DATETIME_LOCAL;
|
||||
} else {
|
||||
callback.sendError("Invalid mode");
|
||||
return;
|
||||
}
|
||||
|
||||
final String defaultValue = message.getString("value");
|
||||
final String minValue = message.getString("min");
|
||||
final String maxValue = message.getString("max");
|
||||
final PromptDelegate.DateTimePrompt prompt =
|
||||
new PromptDelegate.DateTimePrompt(title, intMode, defaultValue, minValue, maxValue);
|
||||
res = delegate.onDateTimePrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "file": {
|
||||
final int intMode;
|
||||
if ("single".equals(mode)) {
|
||||
intMode = PromptDelegate.FilePrompt.Type.SINGLE;
|
||||
} else if ("multiple".equals(mode)) {
|
||||
intMode = PromptDelegate.FilePrompt.Type.MULTIPLE;
|
||||
} else {
|
||||
callback.sendError("Invalid mode");
|
||||
return;
|
||||
}
|
||||
|
||||
final String[] mimeTypes = message.getStringArray("mimeTypes");
|
||||
final int capture = message.getInt("capture");
|
||||
final PromptDelegate.FilePrompt prompt =
|
||||
new PromptDelegate.FilePrompt(title, intMode, capture, mimeTypes);
|
||||
res = delegate.onFilePrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "popup": {
|
||||
final String targetUri = message.getString("targetUri");
|
||||
final PromptDelegate.PopupPrompt prompt =
|
||||
new PromptDelegate.PopupPrompt(targetUri);
|
||||
res = delegate.onPopupPrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "share": {
|
||||
final String text = message.getString("text");
|
||||
final String uri = message.getString("uri");
|
||||
final PromptDelegate.SharePrompt prompt =
|
||||
new PromptDelegate.SharePrompt(title, text, uri);
|
||||
res = delegate.onSharePrompt(session, prompt);
|
||||
break;
|
||||
}
|
||||
case "Autocomplete:Save:Login": {
|
||||
final int hint = message.getInt("hint");
|
||||
final GeckoBundle[] loginBundles =
|
||||
message.getBundleArray("logins");
|
||||
|
||||
if (loginBundles == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
final Autocomplete.LoginSaveOption[] options =
|
||||
new Autocomplete.LoginSaveOption[loginBundles.length];
|
||||
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = new Autocomplete.LoginSaveOption(
|
||||
new Autocomplete.LoginEntry(loginBundles[i]),
|
||||
hint);
|
||||
}
|
||||
|
||||
final PromptDelegate.AutocompleteRequest
|
||||
<Autocomplete.LoginSaveOption> request =
|
||||
new PromptDelegate.AutocompleteRequest<>(options);
|
||||
|
||||
res = delegate.onLoginSave(session, request);
|
||||
break;
|
||||
}
|
||||
case "Autocomplete:Save:Address": {
|
||||
final int hint = message.getInt("hint");
|
||||
final GeckoBundle[] addressBundles =
|
||||
message.getBundleArray("addresses");
|
||||
|
||||
if (addressBundles == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
final Autocomplete.AddressSaveOption[] options =
|
||||
new Autocomplete.AddressSaveOption[addressBundles.length];
|
||||
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = new Autocomplete.AddressSaveOption(
|
||||
new Autocomplete.Address(addressBundles[i]),
|
||||
hint);
|
||||
}
|
||||
|
||||
final PromptDelegate.AutocompleteRequest
|
||||
<Autocomplete.AddressSaveOption> request =
|
||||
new PromptDelegate.AutocompleteRequest<>(options);
|
||||
|
||||
res = delegate.onAddressSave(session, request);
|
||||
break;
|
||||
}
|
||||
case "Autocomplete:Select:Login": {
|
||||
final GeckoBundle[] optionBundles =
|
||||
message.getBundleArray("options");
|
||||
|
||||
if (optionBundles == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
final Autocomplete.LoginSelectOption[] options =
|
||||
new Autocomplete.LoginSelectOption[optionBundles.length];
|
||||
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = Autocomplete.LoginSelectOption.fromBundle(
|
||||
optionBundles[i]);
|
||||
}
|
||||
|
||||
final PromptDelegate.AutocompleteRequest
|
||||
<Autocomplete.LoginSelectOption> request =
|
||||
new PromptDelegate.AutocompleteRequest<>(options);
|
||||
|
||||
res = delegate.onLoginSelect(session, request);
|
||||
break;
|
||||
}
|
||||
case "Autocomplete:Select:CreditCard": {
|
||||
final GeckoBundle[] optionBundles =
|
||||
message.getBundleArray("options");
|
||||
|
||||
if (optionBundles == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
final Autocomplete.CreditCardSelectOption[] options =
|
||||
new Autocomplete.CreditCardSelectOption[optionBundles.length];
|
||||
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = Autocomplete.CreditCardSelectOption.fromBundle(
|
||||
optionBundles[i]);
|
||||
}
|
||||
|
||||
final PromptDelegate.AutocompleteRequest
|
||||
<Autocomplete.CreditCardSelectOption> request =
|
||||
new PromptDelegate.AutocompleteRequest<>(options);
|
||||
|
||||
res = delegate.onCreditCardSelect(session, request);
|
||||
break;
|
||||
}
|
||||
case "Autocomplete:Select:Address": {
|
||||
final GeckoBundle[] optionBundles =
|
||||
message.getBundleArray("options");
|
||||
|
||||
if (optionBundles == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
final Autocomplete.AddressSelectOption[] options =
|
||||
new Autocomplete.AddressSelectOption[optionBundles.length];
|
||||
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = Autocomplete.AddressSelectOption.fromBundle(
|
||||
optionBundles[i]);
|
||||
}
|
||||
|
||||
final PromptDelegate.AutocompleteRequest
|
||||
<Autocomplete.AddressSelectOption> request =
|
||||
new PromptDelegate.AutocompleteRequest<>(options);
|
||||
|
||||
res = delegate.onAddressSelect(session, request);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
callback.sendError("Invalid type");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (res == null) {
|
||||
// Adhere to default behavior if the delegate returns null.
|
||||
callback.sendSuccess(null);
|
||||
} else {
|
||||
res.accept(value -> {
|
||||
value.dispatch(callback);
|
||||
}, exception -> callback.sendError("Failed to get prompt response."));
|
||||
}
|
||||
}
|
||||
|
||||
@UiThread
|
||||
protected void setShouldPinOnScreen(final boolean pinned) {
|
||||
if (DEBUG) {
|
||||
|
|
|
@ -0,0 +1,549 @@
|
|||
package org.mozilla.geckoview;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.mozilla.gecko.util.EventCallback;
|
||||
import org.mozilla.gecko.util.GeckoBundle;
|
||||
|
||||
import org.mozilla.geckoview.Autocomplete.AddressSaveOption;
|
||||
import org.mozilla.geckoview.Autocomplete.AddressSelectOption;
|
||||
import org.mozilla.geckoview.Autocomplete.CreditCardSelectOption;
|
||||
import org.mozilla.geckoview.Autocomplete.LoginSaveOption;
|
||||
import org.mozilla.geckoview.Autocomplete.LoginSelectOption;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.AutocompleteRequest;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.AuthPrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.AlertPrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.BeforeUnloadPrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.ButtonPrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.BasePrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.ChoicePrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.ColorPrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.DateTimePrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.FilePrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.PopupPrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.PromptResponse;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.RepostConfirmPrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.SharePrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.TextPrompt;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate;
|
||||
import org.mozilla.geckoview.GeckoSession.PromptDelegate.AuthPrompt.AuthOptions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/* package */ class PromptController {
|
||||
private static final String LOGTAG = "Prompts";
|
||||
|
||||
public void handleEvent(final GeckoSession session,
|
||||
final GeckoBundle message,
|
||||
final EventCallback callback) {
|
||||
final PromptDelegate delegate = session.getPromptDelegate();
|
||||
if (delegate == null) {
|
||||
// Default behavior is same as calling dismiss() on callback.
|
||||
callback.sendSuccess(null);
|
||||
return;
|
||||
}
|
||||
|
||||
final String type = message.getString("type");
|
||||
final PromptHandler<?> handler = sPromptHandlers.handlerFor(type);
|
||||
if (handler == null) {
|
||||
callback.sendError("Invalid type: " + type);
|
||||
return;
|
||||
}
|
||||
final GeckoResult<PromptResponse> res =
|
||||
getResponse(message, session, delegate, handler);
|
||||
|
||||
if (res == null) {
|
||||
// Adhere to default behavior if the delegate returns null.
|
||||
callback.sendSuccess(null);
|
||||
} else {
|
||||
res.accept(
|
||||
value -> value.dispatch(callback),
|
||||
exception -> callback.sendError("Failed to get prompt response."));
|
||||
}
|
||||
}
|
||||
|
||||
private <PromptType extends BasePrompt> GeckoResult<PromptResponse> getResponse(
|
||||
final GeckoBundle message,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate,
|
||||
final PromptHandler<PromptType> handler) {
|
||||
final PromptType prompt = handler.newPrompt(message);
|
||||
if (prompt == null) {
|
||||
try {
|
||||
Log.e(LOGTAG, "Invalid prompt: " + message.toJSONObject().toString());
|
||||
} catch (final JSONException ex) {
|
||||
Log.e(LOGTAG, "Invalid prompt, invalid data", ex);
|
||||
}
|
||||
|
||||
return GeckoResult.fromException(
|
||||
new IllegalArgumentException("Invalid prompt data."));
|
||||
}
|
||||
return handler.callDelegate(prompt, session, delegate);
|
||||
}
|
||||
|
||||
private interface PromptHandler<PromptType extends BasePrompt> {
|
||||
PromptType newPrompt(GeckoBundle info);
|
||||
GeckoResult<PromptResponse> callDelegate(
|
||||
PromptType prompt, GeckoSession session, PromptDelegate delegate);
|
||||
}
|
||||
|
||||
private static final class AlertHandler implements PromptHandler<AlertPrompt> {
|
||||
@Override
|
||||
public AlertPrompt newPrompt(GeckoBundle info) {
|
||||
return new AlertPrompt(
|
||||
info.getString("title"),
|
||||
info.getString("msg"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final AlertPrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onAlertPrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class BeforeUnloadHandler implements PromptHandler<BeforeUnloadPrompt> {
|
||||
@Override
|
||||
public BeforeUnloadPrompt newPrompt(GeckoBundle info) {
|
||||
return new BeforeUnloadPrompt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final BeforeUnloadPrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onBeforeUnloadPrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class ButtonHandler implements PromptHandler<ButtonPrompt> {
|
||||
@Override
|
||||
public ButtonPrompt newPrompt(GeckoBundle info) {
|
||||
return new ButtonPrompt(
|
||||
info.getString("title"),
|
||||
info.getString("msg"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final ButtonPrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onButtonPrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class TextHandler implements PromptHandler<TextPrompt> {
|
||||
@Override
|
||||
public TextPrompt newPrompt(GeckoBundle info) {
|
||||
return new TextPrompt(
|
||||
info.getString("title"),
|
||||
info.getString("msg"),
|
||||
info.getString("value"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final TextPrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onTextPrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AuthHandler implements PromptHandler<AuthPrompt> {
|
||||
@Override
|
||||
public AuthPrompt newPrompt(GeckoBundle info) {
|
||||
return new AuthPrompt(
|
||||
info.getString("title"),
|
||||
info.getString("msg"),
|
||||
new AuthOptions(info.getBundle("options")));
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final AuthPrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onAuthPrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class ChoiceHandler implements PromptHandler<ChoicePrompt> {
|
||||
@Override
|
||||
public ChoicePrompt newPrompt(GeckoBundle info) {
|
||||
final int intMode;
|
||||
final String mode = info.getString("mode");
|
||||
if ("menu".equals(mode)) {
|
||||
intMode = ChoicePrompt.Type.MENU;
|
||||
} else if ("single".equals(mode)) {
|
||||
intMode = ChoicePrompt.Type.SINGLE;
|
||||
} else if ("multiple".equals(mode)) {
|
||||
intMode = ChoicePrompt.Type.MULTIPLE;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
final GeckoBundle[] choiceBundles = info.getBundleArray("choices");
|
||||
final ChoicePrompt.Choice[] choices;
|
||||
if (choiceBundles == null || choiceBundles.length == 0) {
|
||||
choices = new ChoicePrompt.Choice[0];
|
||||
} else {
|
||||
choices = new ChoicePrompt.Choice[choiceBundles.length];
|
||||
for (int i = 0; i < choiceBundles.length; i++) {
|
||||
choices[i] = new ChoicePrompt.Choice(choiceBundles[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return new ChoicePrompt(
|
||||
info.getString("title"),
|
||||
info.getString("msg"),
|
||||
intMode, choices);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final ChoicePrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onChoicePrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class ColorHandler implements PromptHandler<ColorPrompt> {
|
||||
@Override
|
||||
public ColorPrompt newPrompt(GeckoBundle info) {
|
||||
return new ColorPrompt(
|
||||
info.getString("title"),
|
||||
info.getString("value"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final ColorPrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onColorPrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class DateTimeHandler implements PromptHandler<DateTimePrompt> {
|
||||
@Override
|
||||
public DateTimePrompt newPrompt(GeckoBundle info) {
|
||||
final String mode = info.getString("mode");
|
||||
final int intMode;
|
||||
if ("date".equals(mode)) {
|
||||
intMode = DateTimePrompt.Type.DATE;
|
||||
} else if ("month".equals(mode)) {
|
||||
intMode = DateTimePrompt.Type.MONTH;
|
||||
} else if ("week".equals(mode)) {
|
||||
intMode = DateTimePrompt.Type.WEEK;
|
||||
} else if ("time".equals(mode)) {
|
||||
intMode = DateTimePrompt.Type.TIME;
|
||||
} else if ("datetime-local".equals(mode)) {
|
||||
intMode = DateTimePrompt.Type.DATETIME_LOCAL;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
final String defaultValue = info.getString("value");
|
||||
final String minValue = info.getString("min");
|
||||
final String maxValue = info.getString("max");
|
||||
return new DateTimePrompt(
|
||||
info.getString("title"),
|
||||
intMode,
|
||||
defaultValue,
|
||||
minValue,
|
||||
maxValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final DateTimePrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onDateTimePrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private final static class FileHandler implements PromptHandler<FilePrompt> {
|
||||
@Override
|
||||
public FilePrompt newPrompt(GeckoBundle info) {
|
||||
final String mode = info.getString("mode");
|
||||
final int intMode;
|
||||
if ("single".equals(mode)) {
|
||||
intMode = FilePrompt.Type.SINGLE;
|
||||
} else if ("multiple".equals(mode)) {
|
||||
intMode = FilePrompt.Type.MULTIPLE;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
final String[] mimeTypes = info.getStringArray("mimeTypes");
|
||||
final int capture = info.getInt("capture");
|
||||
return new FilePrompt(
|
||||
info.getString("title"),
|
||||
intMode,
|
||||
capture,
|
||||
mimeTypes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final FilePrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onFilePrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class PopupHandler implements PromptHandler<PopupPrompt> {
|
||||
@Override
|
||||
public PopupPrompt newPrompt(GeckoBundle info) {
|
||||
return new PromptDelegate.PopupPrompt(info.getString("targetUri"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final PopupPrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onPopupPrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RepostHandler implements PromptHandler<RepostConfirmPrompt> {
|
||||
@Override
|
||||
public RepostConfirmPrompt newPrompt(final GeckoBundle info) {
|
||||
return new RepostConfirmPrompt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final RepostConfirmPrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onRepostConfirmPrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class ShareHandler implements PromptHandler<SharePrompt> {
|
||||
@Override
|
||||
public SharePrompt newPrompt(GeckoBundle info) {
|
||||
return new PromptDelegate.SharePrompt(
|
||||
info.getString("title"),
|
||||
info.getString("text"),
|
||||
info.getString("uri"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final SharePrompt prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onSharePrompt(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static final class LoginSaveHandler
|
||||
implements PromptHandler<AutocompleteRequest<LoginSaveOption>> {
|
||||
@Override
|
||||
public AutocompleteRequest<LoginSaveOption> newPrompt(GeckoBundle info) {
|
||||
final int hint = info.getInt("hint");
|
||||
final GeckoBundle[] loginBundles =
|
||||
info.getBundleArray("logins");
|
||||
|
||||
if (loginBundles == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Autocomplete.LoginSaveOption[] options =
|
||||
new Autocomplete.LoginSaveOption[loginBundles.length];
|
||||
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = new Autocomplete.LoginSaveOption(
|
||||
new Autocomplete.LoginEntry(loginBundles[i]),
|
||||
hint);
|
||||
}
|
||||
|
||||
return new AutocompleteRequest<>(options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final AutocompleteRequest<LoginSaveOption> prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onLoginSave(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AddressSaveHandler
|
||||
implements PromptHandler<AutocompleteRequest<AddressSaveOption>> {
|
||||
@Override
|
||||
public AutocompleteRequest<AddressSaveOption> newPrompt(GeckoBundle info) {
|
||||
final GeckoBundle[] addressBundles =
|
||||
info.getBundleArray("addresses");
|
||||
|
||||
if (addressBundles == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Autocomplete.AddressSaveOption[] options =
|
||||
new Autocomplete.AddressSaveOption[addressBundles.length];
|
||||
|
||||
final int hint = info.getInt("hint");
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = new Autocomplete.AddressSaveOption(
|
||||
new Autocomplete.Address(addressBundles[i]),
|
||||
hint);
|
||||
}
|
||||
|
||||
return new AutocompleteRequest<>(options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final AutocompleteRequest<AddressSaveOption> prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onAddressSave(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class LoginSelectHandler
|
||||
implements PromptHandler<AutocompleteRequest<LoginSelectOption>> {
|
||||
@Override
|
||||
public AutocompleteRequest<LoginSelectOption> newPrompt(GeckoBundle info) {
|
||||
final GeckoBundle[] optionBundles =
|
||||
info.getBundleArray("options");
|
||||
|
||||
if (optionBundles == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Autocomplete.LoginSelectOption[] options =
|
||||
new Autocomplete.LoginSelectOption[optionBundles.length];
|
||||
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = Autocomplete.LoginSelectOption.fromBundle(
|
||||
optionBundles[i]);
|
||||
}
|
||||
|
||||
return new AutocompleteRequest<>(options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final AutocompleteRequest<LoginSelectOption> prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onLoginSelect(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CreditCardSelectHandler
|
||||
implements PromptHandler<AutocompleteRequest<CreditCardSelectOption>> {
|
||||
@Override
|
||||
public AutocompleteRequest<CreditCardSelectOption> newPrompt(GeckoBundle info) {
|
||||
final GeckoBundle[] optionBundles =
|
||||
info.getBundleArray("options");
|
||||
|
||||
if (optionBundles == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Autocomplete.CreditCardSelectOption[] options =
|
||||
new Autocomplete.CreditCardSelectOption[optionBundles.length];
|
||||
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = Autocomplete.CreditCardSelectOption.fromBundle(
|
||||
optionBundles[i]);
|
||||
}
|
||||
|
||||
return new AutocompleteRequest<>(options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final AutocompleteRequest<CreditCardSelectOption> prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onCreditCardSelect(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AddressSelectHandler
|
||||
implements PromptHandler<AutocompleteRequest<AddressSelectOption>> {
|
||||
@Override
|
||||
public AutocompleteRequest<AddressSelectOption> newPrompt(GeckoBundle info) {
|
||||
final GeckoBundle[] optionBundles =
|
||||
info.getBundleArray("options");
|
||||
|
||||
if (optionBundles == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Autocomplete.AddressSelectOption[] options =
|
||||
new Autocomplete.AddressSelectOption[optionBundles.length];
|
||||
|
||||
for (int i = 0; i < options.length; ++i) {
|
||||
options[i] = Autocomplete.AddressSelectOption.fromBundle(
|
||||
optionBundles[i]);
|
||||
}
|
||||
|
||||
return new AutocompleteRequest<>(options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<PromptResponse> callDelegate(
|
||||
final AutocompleteRequest<AddressSelectOption> prompt,
|
||||
final GeckoSession session,
|
||||
final PromptDelegate delegate) {
|
||||
return delegate.onAddressSelect(session, prompt);
|
||||
}
|
||||
}
|
||||
|
||||
private static class PromptHandlers {
|
||||
final Map<String, PromptHandler<?>> mPromptHandlers = new HashMap<>();
|
||||
|
||||
public void register(final PromptHandler<?> handler, final String type) {
|
||||
mPromptHandlers.put(type, handler);
|
||||
}
|
||||
|
||||
public PromptHandler<?> handlerFor(final String type) {
|
||||
return mPromptHandlers.get(type);
|
||||
}
|
||||
}
|
||||
|
||||
private static final PromptHandlers sPromptHandlers = new PromptHandlers();
|
||||
static {
|
||||
sPromptHandlers.register(new AlertHandler(), "alert");
|
||||
sPromptHandlers.register(new BeforeUnloadHandler(), "beforeUnload");
|
||||
sPromptHandlers.register(new ButtonHandler(), "button");
|
||||
sPromptHandlers.register(new TextHandler(), "text");
|
||||
sPromptHandlers.register(new AuthHandler(), "auth");
|
||||
sPromptHandlers.register(new ChoiceHandler(), "choice");
|
||||
sPromptHandlers.register(new ColorHandler(), "color");
|
||||
sPromptHandlers.register(new DateTimeHandler(), "datetime");
|
||||
sPromptHandlers.register(new FileHandler(), "file");
|
||||
sPromptHandlers.register(new PopupHandler(), "popup");
|
||||
sPromptHandlers.register(new RepostHandler(), "repost");
|
||||
sPromptHandlers.register(new ShareHandler(), "share");
|
||||
sPromptHandlers.register(new LoginSaveHandler(), "Autocomplete:Save:Login");
|
||||
sPromptHandlers.register(new AddressSaveHandler(),
|
||||
"Autocomplete:Save:Address");
|
||||
sPromptHandlers.register(new LoginSelectHandler(),
|
||||
"Autocomplete:Select:Login");
|
||||
sPromptHandlers.register(new CreditCardSelectHandler(),
|
||||
"Autocomplete:Select:CreditCard");
|
||||
sPromptHandlers.register(new AddressSelectHandler(),
|
||||
"Autocomplete:Select:Address");
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче