| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/payments/content/android/payment_manifest_downloader_androi
d.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/android/jni_string.h" | |
| 11 #include "base/android/scoped_java_ref.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "components/payments/content/payment_manifest_downloader.h" | |
| 15 #include "content/public/browser/browser_context.h" | |
| 16 #include "content/public/browser/storage_partition.h" | |
| 17 #include "content/public/browser/web_contents.h" | |
| 18 #include "jni/PaymentManifestDownloader_jni.h" | |
| 19 #include "net/url_request/url_request_context_getter.h" | |
| 20 #include "url/gurl.h" | |
| 21 #include "url/url_constants.h" | |
| 22 | |
| 23 namespace payments { | |
| 24 namespace { | |
| 25 | |
| 26 class SelfDeletingDownloadDelegate | |
| 27 : public PaymentManifestDownloader::Delegate { | |
| 28 public: | |
| 29 explicit SelfDeletingDownloadDelegate( | |
| 30 const base::android::JavaParamRef<jobject>& jcallback) | |
| 31 : jcallback_(jcallback), is_downloading_payment_method_manifest_(true) {} | |
| 32 | |
| 33 void set_downloader(std::unique_ptr<PaymentManifestDownloader> downloader) { | |
| 34 downloader_ = std::move(downloader); | |
| 35 } | |
| 36 | |
| 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 } | |
| 46 | |
| 47 // PaymentManifestDownloader::Delegate | |
| 48 void OnManifestDownloadSuccess(const std::string& content) override { | |
| 49 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 50 if (is_downloading_payment_method_manifest_) { | |
| 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 } | |
| 59 delete this; | |
| 60 } | |
| 61 | |
| 62 // PaymentManifestDownloader::Delegate | |
| 63 void OnManifestDownloadFailure() override { | |
| 64 Java_ManifestDownloadCallback_onManifestDownloadFailure( | |
| 65 base::android::AttachCurrentThread(), jcallback_); | |
| 66 delete this; | |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 ~SelfDeletingDownloadDelegate() override {} | |
| 71 | |
| 72 base::android::ScopedJavaGlobalRef<jobject> jcallback_; | |
| 73 bool is_downloading_payment_method_manifest_; | |
| 74 std::unique_ptr<PaymentManifestDownloader> downloader_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(SelfDeletingDownloadDelegate); | |
| 77 }; | |
| 78 | |
| 79 SelfDeletingDownloadDelegate* BuildSelfDeletingDownloadDelegate( | |
| 80 JNIEnv* env, | |
| 81 const base::android::JavaParamRef<jobject>& jweb_contents, | |
| 82 const base::android::JavaParamRef<jobject>& juri, | |
| 83 const base::android::JavaParamRef<jobject>& jcallback) { | |
| 84 SelfDeletingDownloadDelegate* delegate = | |
| 85 new SelfDeletingDownloadDelegate(jcallback); | |
| 86 | |
| 87 content::WebContents* web_contents = | |
| 88 content::WebContents::FromJavaWebContents(jweb_contents); | |
| 89 if (!web_contents) { | |
| 90 delegate->OnManifestDownloadFailure(); | |
| 91 return nullptr; | |
| 92 } | |
| 93 | |
| 94 GURL url(base::android::ConvertJavaStringToUTF8( | |
| 95 env, Java_PaymentManifestDownloader_getUriString(env, juri))); | |
| 96 DCHECK(url.is_valid()); | |
| 97 DCHECK(url.SchemeIs(url::kHttpsScheme)); | |
| 98 | |
| 99 std::unique_ptr<PaymentManifestDownloader> downloader = | |
| 100 base::MakeUnique<PaymentManifestDownloader>( | |
| 101 content::BrowserContext::GetDefaultStoragePartition( | |
| 102 web_contents->GetBrowserContext()) | |
| 103 ->GetURLRequestContext(), | |
| 104 url, delegate); | |
| 105 delegate->set_downloader(std::move(downloader)); | |
| 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(); | |
| 138 } | |
| 139 | |
| 140 } // namespace payments | |
| OLD | NEW |