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/android/payment_manifest_downloader_android.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <unordered_map> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/android/jni_string.h" |
| 12 #include "base/android/scoped_java_ref.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/memory/ptr_util.h" |
| 15 #include "components/payments/payment_manifest_downloader.h" |
| 16 #include "content/public/browser/browser_context.h" |
| 17 #include "content/public/browser/storage_partition.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "jni/PaymentManifestDownloader_jni.h" |
| 20 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "url/gurl.h" |
| 22 #include "url/url_constants.h" |
| 23 |
| 24 namespace payments { |
| 25 namespace { |
| 26 |
| 27 class SelfDeletingDownloadDelegate |
| 28 : public payments::PaymentManifestDownloader::Delegate { |
| 29 public: |
| 30 explicit SelfDeletingDownloadDelegate( |
| 31 const base::android::JavaParamRef<jobject>& jcallback) |
| 32 : jcallback_(jcallback) {} |
| 33 |
| 34 void set_downloader( |
| 35 std::unique_ptr<payments::PaymentManifestDownloader> downloader) { |
| 36 downloader_ = std::move(downloader); |
| 37 } |
| 38 |
| 39 void Download() { downloader_->Download(); } |
| 40 |
| 41 // payments::PaymentManifestDownloader::Delegate |
| 42 void OnManifestDownloadSuccess(const std::string& content) override { |
| 43 JNIEnv* env = base::android::AttachCurrentThread(); |
| 44 payments::Java_ManifestDownloadCallback_onManifestDownloadSuccess( |
| 45 env, jcallback_, base::android::ConvertUTF8ToJavaString(env, content)); |
| 46 delete this; |
| 47 } |
| 48 |
| 49 // payments::PaymentManifestDownloader::Delegate |
| 50 void OnManifestDownloadFailure() override { |
| 51 payments::Java_ManifestDownloadCallback_onManifestDownloadFailure( |
| 52 base::android::AttachCurrentThread(), jcallback_); |
| 53 delete this; |
| 54 } |
| 55 |
| 56 private: |
| 57 ~SelfDeletingDownloadDelegate() override {} |
| 58 |
| 59 base::android::ScopedJavaGlobalRef<jobject> jcallback_; |
| 60 std::unique_ptr<payments::PaymentManifestDownloader> downloader_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(SelfDeletingDownloadDelegate); |
| 63 }; |
| 64 |
| 65 } // namespace |
| 66 |
| 67 void DownloadPaymentManifest( |
| 68 JNIEnv* env, |
| 69 const base::android::JavaParamRef<jclass>& jcaller, |
| 70 const base::android::JavaParamRef<jobject>& jweb_contents, |
| 71 const base::android::JavaParamRef<jobject>& jmethod_name, |
| 72 const base::android::JavaParamRef<jobject>& jcallback) { |
| 73 SelfDeletingDownloadDelegate* delegate = |
| 74 new SelfDeletingDownloadDelegate(jcallback); |
| 75 |
| 76 content::WebContents* web_contents = |
| 77 content::WebContents::FromJavaWebContents(jweb_contents); |
| 78 if (!web_contents) { |
| 79 delegate->OnManifestDownloadFailure(); |
| 80 return; |
| 81 } |
| 82 |
| 83 GURL method_name(base::android::ConvertJavaStringToUTF8( |
| 84 env, Java_PaymentManifestDownloader_getUrlString(env, jmethod_name))); |
| 85 DCHECK(method_name.is_valid()); |
| 86 DCHECK(method_name.SchemeIs(url::kHttpsScheme)); |
| 87 |
| 88 std::unique_ptr<payments::PaymentManifestDownloader> downloader = |
| 89 base::MakeUnique<payments::PaymentManifestDownloader>( |
| 90 content::BrowserContext::GetDefaultStoragePartition( |
| 91 web_contents->GetBrowserContext()) |
| 92 ->GetURLRequestContext(), |
| 93 method_name, delegate); |
| 94 delegate->set_downloader(std::move(downloader)); |
| 95 delegate->Download(); |
| 96 } |
| 97 |
| 98 } // namespace payments |
| 99 |
| 100 bool RegisterPaymentManifestDownloader(JNIEnv* env) { |
| 101 return payments::RegisterNativesImpl(env); |
| 102 } |
OLD | NEW |