Chromium Code Reviews| Index: components/payments/android/payment_manifest_downloader_android.cc |
| diff --git a/components/payments/android/payment_manifest_downloader_android.cc b/components/payments/android/payment_manifest_downloader_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c25ee8db9a6fabb0461feebd7c992be664ac8afc |
| --- /dev/null |
| +++ b/components/payments/android/payment_manifest_downloader_android.cc |
| @@ -0,0 +1,102 @@ |
| +// Copyright 2017 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. |
| + |
| +#include "components/payments/android/payment_manifest_downloader_android.h" |
| + |
| +#include <memory> |
| +#include <unordered_map> |
| +#include <utility> |
| + |
| +#include "base/android/jni_string.h" |
| +#include "base/android/scoped_java_ref.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "components/payments/payment_manifest_downloader.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/storage_partition.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "jni/PaymentManifestDownloader_jni.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| +#include "url/gurl.h" |
| +#include "url/url_constants.h" |
| + |
| +namespace payments { |
| +namespace { |
| + |
| +class SelfDeletingDownloadDelegate |
| + : public payments::PaymentManifestDownloader::Delegate { |
| + public: |
| + explicit SelfDeletingDownloadDelegate( |
| + const base::android::JavaParamRef<jobject>& jcallback) |
| + : jcallback_(jcallback) {} |
| + |
| + void set_downloader( |
| + std::unique_ptr<payments::PaymentManifestDownloader> downloader) { |
| + downloader_ = std::move(downloader); |
| + } |
| + |
| + void Download() { downloader_->Download(); } |
| + |
| + // payments::PaymentManifestDownloader::Delegate |
| + void OnManifestDownloadSuccess(const std::string& content) override { |
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + payments::Java_ManifestDownloadCallback_onManifestDownloadSuccess( |
| + env, jcallback_, base::android::ConvertUTF8ToJavaString(env, content)); |
| + delete this; |
| + } |
| + |
| + // payments::PaymentManifestDownloader::Delegate |
| + void OnManifestDownloadFailure() override { |
| + payments::Java_ManifestDownloadCallback_onManifestDownloadFailure( |
| + base::android::AttachCurrentThread(), jcallback_); |
| + delete this; |
| + } |
| + |
| + private: |
| + ~SelfDeletingDownloadDelegate() override {} |
| + |
| + base::android::ScopedJavaGlobalRef<jobject> jcallback_; |
| + std::unique_ptr<payments::PaymentManifestDownloader> downloader_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SelfDeletingDownloadDelegate); |
| +}; |
| + |
| +} // namespace |
| + |
| +void DownloadPaymentManifest( |
| + JNIEnv* env, |
| + const base::android::JavaParamRef<jclass>& jcaller, |
| + const base::android::JavaParamRef<jobject>& jweb_contents, |
| + const base::android::JavaParamRef<jobject>& jmethod_name, |
|
xunjieli
2017/02/24 18:18:30
Suggest passing the string directly.
java.net.URL
please use gerrit instead
2017/03/03 03:11:17
Using java.net.URI instead.
|
| + const base::android::JavaParamRef<jobject>& jcallback) { |
| + SelfDeletingDownloadDelegate* delegate = |
| + new SelfDeletingDownloadDelegate(jcallback); |
| + |
| + content::WebContents* web_contents = |
| + content::WebContents::FromJavaWebContents(jweb_contents); |
| + if (!web_contents) { |
| + delegate->OnManifestDownloadFailure(); |
| + return; |
| + } |
| + |
| + GURL method_name(base::android::ConvertJavaStringToUTF8( |
| + env, Java_PaymentManifestDownloader_getUrlString(env, jmethod_name))); |
| + DCHECK(method_name.is_valid()); |
| + DCHECK(method_name.SchemeIs(url::kHttpsScheme)); |
| + |
| + std::unique_ptr<payments::PaymentManifestDownloader> downloader = |
| + base::MakeUnique<payments::PaymentManifestDownloader>( |
| + content::BrowserContext::GetDefaultStoragePartition( |
| + web_contents->GetBrowserContext()) |
| + ->GetURLRequestContext(), |
| + method_name, delegate); |
| + delegate->set_downloader(std::move(downloader)); |
| + delegate->Download(); |
| +} |
| + |
| +} // namespace payments |
| + |
| +bool RegisterPaymentManifestDownloader(JNIEnv* env) { |
| + return payments::RegisterNativesImpl(env); |
| +} |