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 void DownloadPaymentManifest( | |
66 JNIEnv* env, | |
67 const base::android::JavaParamRef<jclass>& jcaller, | |
68 const base::android::JavaParamRef<jobject>& jweb_contents, | |
69 const base::android::JavaParamRef<jobject>& jmethod_name, | |
xunjieli
2017/03/03 16:00:22
nit: could pass a jstring directly here instead of
please use gerrit instead
2017/03/03 16:02:44
Acknowledged. Let's see what Chris thinks about it
| |
70 const base::android::JavaParamRef<jobject>& jcallback) { | |
71 SelfDeletingDownloadDelegate* delegate = | |
72 new SelfDeletingDownloadDelegate(jcallback); | |
73 | |
74 content::WebContents* web_contents = | |
75 content::WebContents::FromJavaWebContents(jweb_contents); | |
76 if (!web_contents) { | |
77 delegate->OnManifestDownloadFailure(); | |
78 return; | |
79 } | |
80 | |
81 GURL method_name(base::android::ConvertJavaStringToUTF8( | |
82 env, Java_PaymentManifestDownloader_getUriString(env, jmethod_name))); | |
83 DCHECK(method_name.is_valid()); | |
84 DCHECK(method_name.SchemeIs(url::kHttpsScheme)); | |
85 | |
86 std::unique_ptr<PaymentManifestDownloader> downloader = | |
87 base::MakeUnique<PaymentManifestDownloader>( | |
88 content::BrowserContext::GetDefaultStoragePartition( | |
89 web_contents->GetBrowserContext()) | |
90 ->GetURLRequestContext(), | |
91 method_name, delegate); | |
92 delegate->set_downloader(std::move(downloader)); | |
93 delegate->Download(); | |
94 } | |
95 | |
96 bool RegisterPaymentManifestDownloader(JNIEnv* env) { | |
97 return RegisterNativesImpl(env); | |
98 } | |
99 | |
100 } // namespace payments | |
OLD | NEW |