Commit e865a301 authored by Danyao Wang's avatar Danyao Wang Committed by Commit Bot

[Web Payments] Prep changes for JNI PaymentAppService.

This patch contains two preparatory changes for exposing the C++
PaymentAppService via JNI:
1. Replace the output parameter of PaymentAppService::Create() with a
   separate method that callers can use to get the number of payment
   factories. This will make it easier for the upcoming JNI code to
   first query the number of factories, cache it in a delegate, and
   call Create() with the delegate.
2. Added two new utility helpers to deserialize Mojo objects from
   Java ByteBuffers.

Bug: 1063118
Change-Id: Ibb7ff2fcb09e18cc30abe06f410392852bbe7307
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2129066
Commit-Queue: Danyao Wang <danyao@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755458}
parent e9a042e9
...@@ -13,9 +13,10 @@ namespace android { ...@@ -13,9 +13,10 @@ namespace android {
std::vector<uint8_t> JavaByteBufferToNativeByteVector( std::vector<uint8_t> JavaByteBufferToNativeByteVector(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& buffer) { const base::android::JavaRef<jobject>& buffer) {
jbyte* buf_in = static_cast<jbyte*>(env->GetDirectBufferAddress(buffer)); jbyte* buf_in =
jlong buf_size = env->GetDirectBufferCapacity(buffer); static_cast<jbyte*>(env->GetDirectBufferAddress(buffer.obj()));
jlong buf_size = env->GetDirectBufferCapacity(buffer.obj());
std::vector<uint8_t> result(buf_size); std::vector<uint8_t> result(buf_size);
memcpy(&result[0], buf_in, buf_size); memcpy(&result[0], buf_in, buf_size);
return result; return result;
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <vector> #include <vector>
#include "base/android/scoped_java_ref.h" #include "base/android/scoped_java_ref.h"
#include "base/logging.h"
#include "mojo/public/cpp/bindings/struct_ptr.h"
namespace payments { namespace payments {
namespace android { namespace android {
...@@ -22,7 +24,39 @@ namespace android { ...@@ -22,7 +24,39 @@ namespace android {
// &details); // &details);
std::vector<uint8_t> JavaByteBufferToNativeByteVector( std::vector<uint8_t> JavaByteBufferToNativeByteVector(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& buffer); const base::android::JavaRef<jobject>& buffer);
// Deserializes a java.nio.ByteBuffer into a native Mojo object. Returns true if
// deserialization is successful.
template <typename T>
bool DeserializeFromJavaByteBuffer(
JNIEnv* env,
const base::android::JavaRef<jobject>& jbuffer,
mojo::StructPtr<T>* out) {
DCHECK(out);
return T::Deserialize(JavaByteBufferToNativeByteVector(env, jbuffer), out);
}
// Deserializes a java.nio.ByteBuffer[] into a vector of native Mojo objects.
// The content of |out| is replaced. Returns true if all entries are
// deserialized successfully.
template <typename T>
bool DeserializeFromJavaByteBufferArray(
JNIEnv* env,
const base::android::JavaRef<jobjectArray>& jbuffers,
std::vector<mojo::StructPtr<T>>* out) {
DCHECK(out);
out->clear();
for (const auto& jbuffer : jbuffers.ReadElements<jobject>()) {
mojo::StructPtr<T> data;
if (!DeserializeFromJavaByteBuffer(env, jbuffer, &data)) {
out->clear();
return false;
}
out->push_back(std::move(data));
}
return true;
}
} // namespace android } // namespace android
} // namespace payments } // namespace payments
......
...@@ -21,10 +21,12 @@ PaymentAppService::PaymentAppService() { ...@@ -21,10 +21,12 @@ PaymentAppService::PaymentAppService() {
PaymentAppService::~PaymentAppService() = default; PaymentAppService::~PaymentAppService() = default;
size_t PaymentAppService::GetNumberOfFactories() const {
return factories_.size();
}
void PaymentAppService::Create( void PaymentAppService::Create(
base::WeakPtr<PaymentAppFactory::Delegate> delegate, base::WeakPtr<PaymentAppFactory::Delegate> delegate) {
size_t* number_of_payment_app_factories) {
*number_of_payment_app_factories = factories_.size();
for (const auto& factory : factories_) { for (const auto& factory : factories_) {
factory->Create(delegate); factory->Create(delegate);
} }
......
...@@ -22,14 +22,13 @@ class PaymentAppService : public KeyedService { ...@@ -22,14 +22,13 @@ class PaymentAppService : public KeyedService {
PaymentAppService(); PaymentAppService();
~PaymentAppService() override; ~PaymentAppService() override;
// Sets |number_of_payment_app_factories| to the number of payment app // Returns the number of payment app factories, which is the number of times
// factories, which is the number of times that // that |delegate->OnDoneCreatingPaymentApps()| will be called as a result of
// |delegate->OnDoneCreatingPaymentApps()| will be called. The value is passed // Create().
// in as out-param instead of being returned, so it can be set before any size_t GetNumberOfFactories() const;
// factory can call OnDoneCreatingPaymentApps(), which can happen for
// factories that execute synchronously, e.g., AutofillPaymentAppFactory. // Create payment apps for |delegate|.
void Create(base::WeakPtr<PaymentAppFactory::Delegate> delegate, void Create(base::WeakPtr<PaymentAppFactory::Delegate> delegate);
size_t* number_of_payment_app_factories);
private: private:
std::vector<std::unique_ptr<PaymentAppFactory>> factories_; std::vector<std::unique_ptr<PaymentAppFactory>> factories_;
......
...@@ -86,10 +86,10 @@ PaymentRequestState::PaymentRequestState( ...@@ -86,10 +86,10 @@ PaymentRequestState::PaymentRequestState(
PopulateProfileCache(); PopulateProfileCache();
// |web_contents_| is null in unit tests. // |web_contents_| is null in unit tests.
PaymentAppServiceFactory::GetForContext( PaymentAppService* service = PaymentAppServiceFactory::GetForContext(
web_contents_ ? web_contents_->GetBrowserContext() : nullptr) web_contents_ ? web_contents_->GetBrowserContext() : nullptr);
->Create(weak_ptr_factory_.GetWeakPtr(), number_of_payment_app_factories_ = service->GetNumberOfFactories();
&number_of_payment_app_factories_); service->Create(weak_ptr_factory_.GetWeakPtr());
spec_->AddObserver(this); spec_->AddObserver(this);
} }
......
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