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/android/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) {} |
| 32 |
| 33 void set_downloader(std::unique_ptr<PaymentManifestDownloader> downloader) { |
| 34 downloader_ = std::move(downloader); |
| 35 } |
| 36 |
| 37 void Download() { downloader_->Download(); } |
| 38 |
| 39 // PaymentManifestDownloader::Delegate |
| 40 void OnManifestDownloadSuccess(const std::string& content) override { |
| 41 JNIEnv* env = base::android::AttachCurrentThread(); |
| 42 Java_ManifestDownloadCallback_onManifestDownloadSuccess( |
| 43 env, jcallback_, base::android::ConvertUTF8ToJavaString(env, content)); |
| 44 delete this; |
| 45 } |
| 46 |
| 47 // PaymentManifestDownloader::Delegate |
| 48 void OnManifestDownloadFailure() override { |
| 49 Java_ManifestDownloadCallback_onManifestDownloadFailure( |
| 50 base::android::AttachCurrentThread(), jcallback_); |
| 51 delete this; |
| 52 } |
| 53 |
| 54 private: |
| 55 ~SelfDeletingDownloadDelegate() override {} |
| 56 |
| 57 base::android::ScopedJavaGlobalRef<jobject> jcallback_; |
| 58 std::unique_ptr<PaymentManifestDownloader> downloader_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(SelfDeletingDownloadDelegate); |
| 61 }; |
| 62 |
| 63 } // namespace |
| 64 |
| 65 bool RegisterPaymentManifestDownloader(JNIEnv* env) { |
| 66 return RegisterNativesImpl(env); |
| 67 } |
| 68 |
| 69 void DownloadPaymentManifest( |
| 70 JNIEnv* env, |
| 71 const base::android::JavaParamRef<jclass>& jcaller, |
| 72 const base::android::JavaParamRef<jobject>& jweb_contents, |
| 73 const base::android::JavaParamRef<jobject>& jmethod_name, |
| 74 const base::android::JavaParamRef<jobject>& jcallback) { |
| 75 SelfDeletingDownloadDelegate* delegate = |
| 76 new SelfDeletingDownloadDelegate(jcallback); |
| 77 |
| 78 content::WebContents* web_contents = |
| 79 content::WebContents::FromJavaWebContents(jweb_contents); |
| 80 if (!web_contents) { |
| 81 delegate->OnManifestDownloadFailure(); |
| 82 return; |
| 83 } |
| 84 |
| 85 GURL method_name(base::android::ConvertJavaStringToUTF8( |
| 86 env, Java_PaymentManifestDownloader_getUriString(env, jmethod_name))); |
| 87 DCHECK(method_name.is_valid()); |
| 88 DCHECK(method_name.SchemeIs(url::kHttpsScheme)); |
| 89 |
| 90 std::unique_ptr<PaymentManifestDownloader> downloader = |
| 91 base::MakeUnique<PaymentManifestDownloader>( |
| 92 content::BrowserContext::GetDefaultStoragePartition( |
| 93 web_contents->GetBrowserContext()) |
| 94 ->GetURLRequestContext(), |
| 95 method_name, delegate); |
| 96 delegate->set_downloader(std::move(downloader)); |
| 97 delegate->Download(); |
| 98 } |
| 99 |
| 100 } // namespace payments |
OLD | NEW |