| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/payments/content/android/payment_manifest_downloader_androi
d.h" | 5 #include "components/payments/content/android/payment_manifest_downloader_androi
d.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "url/url_constants.h" | 21 #include "url/url_constants.h" |
| 22 | 22 |
| 23 namespace payments { | 23 namespace payments { |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 class SelfDeletingDownloadDelegate | 26 class SelfDeletingDownloadDelegate |
| 27 : public PaymentManifestDownloader::Delegate { | 27 : public PaymentManifestDownloader::Delegate { |
| 28 public: | 28 public: |
| 29 explicit SelfDeletingDownloadDelegate( | 29 explicit SelfDeletingDownloadDelegate( |
| 30 const base::android::JavaParamRef<jobject>& jcallback) | 30 const base::android::JavaParamRef<jobject>& jcallback) |
| 31 : jcallback_(jcallback) {} | 31 : jcallback_(jcallback), is_downloading_payment_method_manifest_(true) {} |
| 32 | 32 |
| 33 void set_downloader(std::unique_ptr<PaymentManifestDownloader> downloader) { | 33 void set_downloader(std::unique_ptr<PaymentManifestDownloader> downloader) { |
| 34 downloader_ = std::move(downloader); | 34 downloader_ = std::move(downloader); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void Download() { downloader_->Download(); } | 37 void DownloadPaymentMethodManifest() { |
| 38 is_downloading_payment_method_manifest_ = true; |
| 39 downloader_->DownloadPaymentMethodManifest(); |
| 40 } |
| 41 |
| 42 void DownloadWebAppManifest() { |
| 43 is_downloading_payment_method_manifest_ = false; |
| 44 downloader_->DownloadWebAppManifest(); |
| 45 } |
| 38 | 46 |
| 39 // PaymentManifestDownloader::Delegate | 47 // PaymentManifestDownloader::Delegate |
| 40 void OnManifestDownloadSuccess(const std::string& content) override { | 48 void OnManifestDownloadSuccess(const std::string& content) override { |
| 41 JNIEnv* env = base::android::AttachCurrentThread(); | 49 JNIEnv* env = base::android::AttachCurrentThread(); |
| 42 Java_ManifestDownloadCallback_onManifestDownloadSuccess( | 50 if (is_downloading_payment_method_manifest_) { |
| 43 env, jcallback_, base::android::ConvertUTF8ToJavaString(env, content)); | 51 Java_ManifestDownloadCallback_onPaymentMethodManifestDownloadSuccess( |
| 52 env, jcallback_, |
| 53 base::android::ConvertUTF8ToJavaString(env, content)); |
| 54 } else { |
| 55 Java_ManifestDownloadCallback_onWebAppManifestDownloadSuccess( |
| 56 env, jcallback_, |
| 57 base::android::ConvertUTF8ToJavaString(env, content)); |
| 58 } |
| 44 delete this; | 59 delete this; |
| 45 } | 60 } |
| 46 | 61 |
| 47 // PaymentManifestDownloader::Delegate | 62 // PaymentManifestDownloader::Delegate |
| 48 void OnManifestDownloadFailure() override { | 63 void OnManifestDownloadFailure() override { |
| 49 Java_ManifestDownloadCallback_onManifestDownloadFailure( | 64 Java_ManifestDownloadCallback_onManifestDownloadFailure( |
| 50 base::android::AttachCurrentThread(), jcallback_); | 65 base::android::AttachCurrentThread(), jcallback_); |
| 51 delete this; | 66 delete this; |
| 52 } | 67 } |
| 53 | 68 |
| 54 private: | 69 private: |
| 55 ~SelfDeletingDownloadDelegate() override {} | 70 ~SelfDeletingDownloadDelegate() override {} |
| 56 | 71 |
| 57 base::android::ScopedJavaGlobalRef<jobject> jcallback_; | 72 base::android::ScopedJavaGlobalRef<jobject> jcallback_; |
| 73 bool is_downloading_payment_method_manifest_; |
| 58 std::unique_ptr<PaymentManifestDownloader> downloader_; | 74 std::unique_ptr<PaymentManifestDownloader> downloader_; |
| 59 | 75 |
| 60 DISALLOW_COPY_AND_ASSIGN(SelfDeletingDownloadDelegate); | 76 DISALLOW_COPY_AND_ASSIGN(SelfDeletingDownloadDelegate); |
| 61 }; | 77 }; |
| 62 | 78 |
| 63 } // namespace | 79 SelfDeletingDownloadDelegate* BuildSelfDeletingDownloadDelegate( |
| 64 | |
| 65 bool RegisterPaymentManifestDownloader(JNIEnv* env) { | |
| 66 return RegisterNativesImpl(env); | |
| 67 } | |
| 68 | |
| 69 void DownloadPaymentManifest( | |
| 70 JNIEnv* env, | 80 JNIEnv* env, |
| 71 const base::android::JavaParamRef<jclass>& jcaller, | |
| 72 const base::android::JavaParamRef<jobject>& jweb_contents, | 81 const base::android::JavaParamRef<jobject>& jweb_contents, |
| 73 const base::android::JavaParamRef<jobject>& jmethod_name, | 82 const base::android::JavaParamRef<jobject>& juri, |
| 74 const base::android::JavaParamRef<jobject>& jcallback) { | 83 const base::android::JavaParamRef<jobject>& jcallback) { |
| 75 SelfDeletingDownloadDelegate* delegate = | 84 SelfDeletingDownloadDelegate* delegate = |
| 76 new SelfDeletingDownloadDelegate(jcallback); | 85 new SelfDeletingDownloadDelegate(jcallback); |
| 77 | 86 |
| 78 content::WebContents* web_contents = | 87 content::WebContents* web_contents = |
| 79 content::WebContents::FromJavaWebContents(jweb_contents); | 88 content::WebContents::FromJavaWebContents(jweb_contents); |
| 80 if (!web_contents) { | 89 if (!web_contents) { |
| 81 delegate->OnManifestDownloadFailure(); | 90 delegate->OnManifestDownloadFailure(); |
| 82 return; | 91 return nullptr; |
| 83 } | 92 } |
| 84 | 93 |
| 85 GURL method_name(base::android::ConvertJavaStringToUTF8( | 94 GURL url(base::android::ConvertJavaStringToUTF8( |
| 86 env, Java_PaymentManifestDownloader_getUriString(env, jmethod_name))); | 95 env, Java_PaymentManifestDownloader_getUriString(env, juri))); |
| 87 DCHECK(method_name.is_valid()); | 96 DCHECK(url.is_valid()); |
| 88 DCHECK(method_name.SchemeIs(url::kHttpsScheme)); | 97 DCHECK(url.SchemeIs(url::kHttpsScheme)); |
| 89 | 98 |
| 90 std::unique_ptr<PaymentManifestDownloader> downloader = | 99 std::unique_ptr<PaymentManifestDownloader> downloader = |
| 91 base::MakeUnique<PaymentManifestDownloader>( | 100 base::MakeUnique<PaymentManifestDownloader>( |
| 92 content::BrowserContext::GetDefaultStoragePartition( | 101 content::BrowserContext::GetDefaultStoragePartition( |
| 93 web_contents->GetBrowserContext()) | 102 web_contents->GetBrowserContext()) |
| 94 ->GetURLRequestContext(), | 103 ->GetURLRequestContext(), |
| 95 method_name, delegate); | 104 url, delegate); |
| 96 delegate->set_downloader(std::move(downloader)); | 105 delegate->set_downloader(std::move(downloader)); |
| 97 delegate->Download(); | 106 |
| 107 return delegate; |
| 108 } |
| 109 |
| 110 } // namespace |
| 111 |
| 112 bool RegisterPaymentManifestDownloader(JNIEnv* env) { |
| 113 return RegisterNativesImpl(env); |
| 114 } |
| 115 |
| 116 void DownloadPaymentMethodManifest( |
| 117 JNIEnv* env, |
| 118 const base::android::JavaParamRef<jclass>& jcaller, |
| 119 const base::android::JavaParamRef<jobject>& jweb_contents, |
| 120 const base::android::JavaParamRef<jobject>& jmethod_name, |
| 121 const base::android::JavaParamRef<jobject>& jcallback) { |
| 122 SelfDeletingDownloadDelegate* delegate = BuildSelfDeletingDownloadDelegate( |
| 123 env, jweb_contents, jmethod_name, jcallback); |
| 124 if (delegate) |
| 125 delegate->DownloadPaymentMethodManifest(); |
| 126 } |
| 127 |
| 128 void DownloadWebAppManifest( |
| 129 JNIEnv* env, |
| 130 const base::android::JavaParamRef<jclass>& jcaller, |
| 131 const base::android::JavaParamRef<jobject>& jweb_contents, |
| 132 const base::android::JavaParamRef<jobject>& jweb_app_manifest_uri, |
| 133 const base::android::JavaParamRef<jobject>& jcallback) { |
| 134 SelfDeletingDownloadDelegate* delegate = BuildSelfDeletingDownloadDelegate( |
| 135 env, jweb_contents, jweb_app_manifest_uri, jcallback); |
| 136 if (delegate) |
| 137 delegate->DownloadWebAppManifest(); |
| 98 } | 138 } |
| 99 | 139 |
| 100 } // namespace payments | 140 } // namespace payments |
| OLD | NEW |