Commit e21a1979 authored by gogerald's avatar gogerald Committed by Commit Bot

[Payments] Persist payment handler's icon for JIT PH

Test video:
https://drive.google.com/file/d/1XJjWyN1hMDKoZsAkLF66Yp-Rn3pyDXv3/view?usp=sharing

Bug: 782270

Change-Id: Ief93be66fb740feb439f92d2a55c41e6c9ed4f44
Reviewed-on: https://chromium-review.googlesource.com/943676Reviewed-by: default avatarPavel Feldman <pfeldman@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: Ganggui Tang <gogerald@chromium.org>
Cr-Commit-Position: refs/heads/master@{#541000}
parent 6fef1dd8
......@@ -4,7 +4,7 @@
package org.chromium.chrome.browser.payments;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.text.TextUtils;
......@@ -114,7 +114,7 @@ public class ServiceWorkerPaymentApp extends PaymentInstrument implements Paymen
*/
public ServiceWorkerPaymentApp(WebContents webContents, long registrationId, URI scope,
@Nullable String name, @Nullable String userHint, String origin,
@Nullable Drawable icon, String[] methodNames, Capabilities[] capabilities,
@Nullable BitmapDrawable icon, String[] methodNames, Capabilities[] capabilities,
String[] preferredRelatedApplicationIds) {
// Do not display duplicate information.
super(scope.toString(), TextUtils.isEmpty(name) ? origin : name, userHint,
......@@ -161,7 +161,8 @@ public class ServiceWorkerPaymentApp extends PaymentInstrument implements Paymen
* @param methodName The supported method name.
*/
public ServiceWorkerPaymentApp(WebContents webContents, @Nullable String name, String origin,
URI swUri, URI scope, boolean useCache, @Nullable Drawable icon, String methodName) {
URI swUri, URI scope, boolean useCache, @Nullable BitmapDrawable icon,
String methodName) {
// Do not display duplicate information.
super(scope.toString(), TextUtils.isEmpty(name) ? origin : name, null,
TextUtils.isEmpty(name) ? null : origin, icon);
......@@ -322,10 +323,12 @@ public class ServiceWorkerPaymentApp extends PaymentInstrument implements Paymen
PaymentItem total, List<PaymentItem> displayItems,
Map<String, PaymentDetailsModifier> modifiers, InstrumentDetailsCallback callback) {
if (mNeedsInstallation) {
BitmapDrawable icon = (BitmapDrawable) getDrawableIcon();
ServiceWorkerPaymentAppBridge.installAndInvokePaymentApp(mWebContents, origin,
iframeOrigin, id, new HashSet<>(methodData.values()), total,
new HashSet<>(modifiers.values()), callback, mAppName, mSwUri, mScope,
mUseCache, mMethodNames);
new HashSet<>(modifiers.values()), callback, mAppName,
icon == null ? null : icon.getBitmap(), mSwUri, mScope, mUseCache,
mMethodNames);
} else {
ServiceWorkerPaymentAppBridge.invokePaymentApp(mWebContents, mRegistrationId, origin,
iframeOrigin, id, new HashSet<>(methodData.values()), total,
......
......@@ -202,6 +202,7 @@ public class ServiceWorkerPaymentAppBridge implements PaymentAppFactory.PaymentA
* @param modifiers Payment method specific modifiers to the payment items and the total.
* @param callback Called after the payment app is finished running.
* @param appName The installable app name.
* @param appIcon The installable app icon.
* @param swUri The URI to get the app's service worker js script.
* @param scope The scope of the service worker that should be registered.
* @param useCache Whether to use cache when registering the service worker.
......@@ -210,13 +211,14 @@ public class ServiceWorkerPaymentAppBridge implements PaymentAppFactory.PaymentA
public static void installAndInvokePaymentApp(WebContents webContents, String origin,
String iframeOrigin, String paymentRequestId, Set<PaymentMethodData> methodData,
PaymentItem total, Set<PaymentDetailsModifier> modifiers,
PaymentInstrument.InstrumentDetailsCallback callback, String appName, URI swUri,
URI scope, boolean useCache, Set<String> methodNames) {
PaymentInstrument.InstrumentDetailsCallback callback, String appName,
@Nullable Bitmap icon, URI swUri, URI scope, boolean useCache,
Set<String> methodNames) {
ThreadUtils.assertOnUiThread();
nativeInstallAndInvokePaymentApp(webContents, origin, iframeOrigin, paymentRequestId,
methodData.toArray(new PaymentMethodData[0]), total,
modifiers.toArray(new PaymentDetailsModifier[0]), callback, appName,
modifiers.toArray(new PaymentDetailsModifier[0]), callback, appName, icon,
swUri.toString(), scope.toString(), useCache, methodNames.toArray(new String[0]));
}
......@@ -422,8 +424,9 @@ public class ServiceWorkerPaymentAppBridge implements PaymentAppFactory.PaymentA
private static native void nativeInstallAndInvokePaymentApp(WebContents webContents,
String topLevelOrigin, String paymentRequestOrigin, String paymentRequestId,
PaymentMethodData[] methodData, PaymentItem total, PaymentDetailsModifier[] modifiers,
PaymentInstrument.InstrumentDetailsCallback callback, String appName, String swUrl,
String scope, boolean useCache, String[] methodNames);
PaymentInstrument.InstrumentDetailsCallback callback, String appName,
@Nullable Bitmap icon, String swUrl, String scope, boolean useCache,
String[] methodNames);
private static native void nativeAbortPaymentApp(
WebContents webContents, long registrationId, PaymentInstrument.AbortCallback callback);
......
......@@ -14,7 +14,9 @@ import static org.chromium.chrome.browser.payments.PaymentRequestTestRule.ENABLE
import static org.chromium.chrome.browser.payments.PaymentRequestTestRule.HAVE_INSTRUMENTS;
import static org.chromium.chrome.browser.payments.PaymentRequestTestRule.IMMEDIATE_RESPONSE;
import android.graphics.drawable.ColorDrawable;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.test.filters.MediumTest;
import org.junit.Before;
......@@ -25,6 +27,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.autofill.AutofillTestHelper;
import org.chromium.chrome.browser.autofill.CardType;
......@@ -322,19 +325,24 @@ public class PaymentRequestPaymentAppAndBasicCardWithModifiersTest {
PaymentAppFactory.getInstance().addAdditionalFactory((webContents, methodNames,
callback) -> {
ChromeActivity activity = ChromeActivity.fromWebContents(webContents);
BitmapDrawable icon = new BitmapDrawable(activity.getResources(),
Bitmap.createBitmap(new int[] {Color.RED}, 1 /* width */, 1 /* height */,
Bitmap.Config.ARGB_8888));
ServiceWorkerPaymentAppBridge.setCanMakePaymentForTesting(true);
callback.onPaymentAppCreated(new ServiceWorkerPaymentApp(webContents,
0 /* registrationId */,
UriUtils.parseUriFromString("https://bobpay.com") /* scope */,
"BobPay" /* label */, "https://bobpay.com" /* sublabel*/,
"https://bobpay.com" /* tertiarylabel */, new ColorDrawable() /* icon */,
"https://bobpay.com" /* tertiarylabel */, icon /* icon */,
bobpayMethodNames /* methodNames */, bobpayCapabilities /* capabilities */,
new String[0] /* preferredRelatedApplicationIds */));
callback.onPaymentAppCreated(new ServiceWorkerPaymentApp(webContents,
0 /* registrationId */,
UriUtils.parseUriFromString("https://alicepay.com") /* scope */,
"AlicePay" /* label */, "https://bobpay.com" /* sublabel*/,
"https://alicepay.com" /* tertiarylabel */, new ColorDrawable() /* icon */,
"https://alicepay.com" /* tertiarylabel */, icon /* icon */,
alicepayMethodNames /* methodNames */, alicepayCapabilities /* capabilities */,
new String[0] /* preferredRelatedApplicationIds */));
callback.onAllPaymentAppsCreated();
......
......@@ -461,6 +461,7 @@ static void JNI_ServiceWorkerPaymentAppBridge_InstallAndInvokePaymentApp(
const JavaParamRef<jobjectArray>& jmodifiers,
const JavaParamRef<jobject>& jcallback,
const JavaParamRef<jstring>& japp_name,
const JavaParamRef<jobject>& jicon,
const JavaParamRef<jstring>& jsw_js_url,
const JavaParamRef<jstring>& jsw_scope,
jboolean juse_cache,
......@@ -468,6 +469,11 @@ static void JNI_ServiceWorkerPaymentAppBridge_InstallAndInvokePaymentApp(
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(jweb_contents);
SkBitmap icon_bitmap;
if (jicon) {
icon_bitmap = gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(jicon));
}
std::vector<std::string> enabled_methods;
base::android::AppendJavaStringArrayToStringVector(env, jmethod_names,
&enabled_methods);
......@@ -476,7 +482,7 @@ static void JNI_ServiceWorkerPaymentAppBridge_InstallAndInvokePaymentApp(
ConvertPaymentRequestEventDataFromJavaToNative(
env, jtop_level_origin, jpayment_request_origin, jpayment_request_id,
jmethod_data, jtotal, jmodifiers),
ConvertJavaStringToUTF8(env, japp_name),
ConvertJavaStringToUTF8(env, japp_name), icon_bitmap,
ConvertJavaStringToUTF8(env, jsw_js_url),
ConvertJavaStringToUTF8(env, jsw_scope), juse_cache, enabled_methods,
base::BindOnce(&OnPaymentAppInvoked,
......
......@@ -196,7 +196,11 @@ void ServiceWorkerPaymentInstrument::InvokePaymentApp(Delegate* delegate) {
if (needs_installation_) {
content::PaymentAppProvider::GetInstance()->InstallAndInvokePaymentApp(
web_contents_, CreatePaymentRequestEventData(),
installable_web_app_info_->name, installable_web_app_info_->sw_js_url,
installable_web_app_info_->name,
installable_web_app_info_->icon == nullptr
? SkBitmap()
: *(installable_web_app_info_->icon),
installable_web_app_info_->sw_js_url,
installable_web_app_info_->sw_scope,
installable_web_app_info_->sw_use_cache,
std::vector<std::string>(1, installable_enabled_method_),
......
......@@ -405,6 +405,7 @@ void PaymentAppDatabase::SetPaymentAppInfoForRegisteredServiceWorker(
int64_t registration_id,
const std::string& instrument_key,
const std::string& name,
const std::string& icon,
const std::vector<std::string>& enabled_methods,
SetPaymentAppInfoCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
......@@ -412,13 +413,14 @@ void PaymentAppDatabase::SetPaymentAppInfoForRegisteredServiceWorker(
service_worker_context_->FindReadyRegistrationForIdOnly(
registration_id,
base::BindOnce(&PaymentAppDatabase::DidFindRegistrationToSetPaymentApp,
weak_ptr_factory_.GetWeakPtr(), instrument_key, name,
weak_ptr_factory_.GetWeakPtr(), instrument_key, name, icon,
enabled_methods, std::move(callback)));
}
void PaymentAppDatabase::DidFindRegistrationToSetPaymentApp(
const std::string& instrument_key,
const std::string& name,
const std::string& icon,
const std::vector<std::string>& enabled_methods,
SetPaymentAppInfoCallback callback,
ServiceWorkerStatusCode status,
......@@ -434,6 +436,7 @@ void PaymentAppDatabase::DidFindRegistrationToSetPaymentApp(
payment_app_proto.set_registration_id(registration->id());
payment_app_proto.set_scope(registration->pattern().spec());
payment_app_proto.set_name(name);
payment_app_proto.set_icon(icon);
std::string serialized_payment_app;
bool success = payment_app_proto.SerializeToString(&serialized_payment_app);
......
......@@ -82,6 +82,7 @@ class CONTENT_EXPORT PaymentAppDatabase {
int64_t registration_id,
const std::string& instrument_key,
const std::string& name,
const std::string& icon,
const std::vector<std::string>& enabled_methods,
SetPaymentAppInfoCallback callback);
......@@ -205,6 +206,7 @@ class CONTENT_EXPORT PaymentAppDatabase {
void DidFindRegistrationToSetPaymentApp(
const std::string& instrument_key,
const std::string& name,
const std::string& icon,
const std::vector<std::string>& enabled_methods,
SetPaymentAppInfoCallback callback,
ServiceWorkerStatusCode status,
......
......@@ -28,12 +28,14 @@ class SelfDeleteInstaller
public:
SelfDeleteInstaller(WebContents* web_contents,
const std::string& app_name,
const std::string& app_icon,
const GURL& sw_url,
const GURL& scope,
bool use_cache,
const std::vector<std::string>& enabled_methods,
PaymentAppInstaller::InstallPaymentAppCallback callback)
: app_name_(app_name),
app_icon_(app_icon),
sw_url_(sw_url),
scope_(scope),
callback_(std::move(callback)) {
......@@ -135,7 +137,7 @@ class SelfDeleteInstaller
BrowserThread::IO, FROM_HERE,
base::BindOnce(&SelfDeleteInstaller::SetPaymentAppInfoOnIO, this,
payment_app_context, registration_id_, scope_.spec(),
app_name_, enabled_methods_));
app_name_, app_icon_, enabled_methods_));
}
void SetPaymentAppInfoOnIO(
......@@ -143,12 +145,13 @@ class SelfDeleteInstaller
int64_t registration_id,
const std::string& instrument_key,
const std::string& name,
const std::string& app_icon,
const std::vector<std::string>& enabled_methods) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
payment_app_context->payment_app_database()
->SetPaymentAppInfoForRegisteredServiceWorker(
registration_id, instrument_key, name, enabled_methods,
registration_id, instrument_key, name, app_icon, enabled_methods,
base::BindOnce(&SelfDeleteInstaller::OnSetPaymentAppInfo, this));
}
......@@ -186,6 +189,7 @@ class SelfDeleteInstaller
}
std::string app_name_;
std::string app_icon_;
GURL sw_url_;
GURL scope_;
std::vector<std::string> enabled_methods_;
......@@ -203,6 +207,7 @@ class SelfDeleteInstaller
void PaymentAppInstaller::Install(
WebContents* web_contents,
const std::string& app_name,
const std::string& app_icon,
const GURL& sw_url,
const GURL& scope,
bool use_cache,
......@@ -210,8 +215,8 @@ void PaymentAppInstaller::Install(
InstallPaymentAppCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
new SelfDeleteInstaller(web_contents, app_name, sw_url, scope, use_cache,
enabled_methods, std::move(callback));
new SelfDeleteInstaller(web_contents, app_name, app_icon, sw_url, scope,
use_cache, enabled_methods, std::move(callback));
}
} // namespace content
\ No newline at end of file
......@@ -27,6 +27,7 @@ class PaymentAppInstaller {
// Installs the payment app.
// |app_name| is the name of the payment app.
// |app_icon| is the icon of the payment app.
// |sw_url| is the url to get the service worker js script.
// |scope| is the registration scope.
// |use_cache| indicates whether to use cache.
......@@ -34,6 +35,7 @@ class PaymentAppInstaller {
// |callback| to send back registeration result.
static void Install(WebContents* web_contents,
const std::string& app_name,
const std::string& app_icon,
const GURL& sw_url,
const GURL& scope,
bool use_cache,
......
......@@ -4,6 +4,7 @@
#include "content/browser/payments/payment_app_provider_impl.h"
#include "base/base64.h"
#include "base/strings/string_util.h"
#include "content/browser/payments/payment_app_context_impl.h"
#include "content/browser/payments/payment_app_installer.h"
......@@ -17,6 +18,8 @@
#include "content/public/browser/web_contents.h"
#include "mojo/common/time.mojom.h"
#include "third_party/WebKit/public/mojom/service_worker/service_worker_provider_type.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
namespace content {
namespace {
......@@ -401,6 +404,7 @@ void PaymentAppProviderImpl::InstallAndInvokePaymentApp(
WebContents* web_contents,
payments::mojom::PaymentRequestEventDataPtr event_data,
const std::string& app_name,
const SkBitmap& app_icon,
const std::string& sw_js_url,
const std::string& sw_scope,
bool sw_use_cache,
......@@ -420,8 +424,19 @@ void PaymentAppProviderImpl::InstallAndInvokePaymentApp(
return;
}
std::string string_encoded_icon;
if (!app_icon.empty()) {
gfx::Image decoded_image = gfx::Image::CreateFrom1xBitmap(app_icon);
scoped_refptr<base::RefCountedMemory> raw_data =
decoded_image.As1xPNGBytes();
base::Base64Encode(
base::StringPiece(raw_data->front_as<char>(), raw_data->size()),
&string_encoded_icon);
}
PaymentAppInstaller::Install(
web_contents, app_name, url, scope, sw_use_cache, enabled_methods,
web_contents, app_name, string_encoded_icon, url, scope, sw_use_cache,
enabled_methods,
base::BindOnce(&OnInstallPaymentApp, std::move(event_data),
std::move(callback)));
}
......
......@@ -28,6 +28,7 @@ class CONTENT_EXPORT PaymentAppProviderImpl : public PaymentAppProvider {
WebContents* web_contents,
payments::mojom::PaymentRequestEventDataPtr event_data,
const std::string& app_name,
const SkBitmap& app_icon,
const std::string& sw_js_url,
const std::string& sw_scope,
bool sw_use_cache,
......
......@@ -15,6 +15,8 @@
#include "content/public/browser/stored_payment_app.h"
#include "third_party/WebKit/public/platform/modules/payments/payment_app.mojom.h"
class SkBitmap;
namespace content {
class BrowserContext;
......@@ -51,6 +53,7 @@ class CONTENT_EXPORT PaymentAppProvider {
WebContents* web_contents,
payments::mojom::PaymentRequestEventDataPtr event_data,
const std::string& app_name,
const SkBitmap& app_icon,
const std::string& sw_js_url,
const std::string& sw_scope,
bool sw_use_cache,
......
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