| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 // This class pairs with DownloadController on Java side to forward requests | |
| 6 // for GET downloads to the current DownloadListener. POST downloads are | |
| 7 // handled on the native side. | |
| 8 // | |
| 9 // Both classes are Singleton classes. C++ object owns Java object. | |
| 10 // | |
| 11 // Call sequence | |
| 12 // GET downloads: | |
| 13 // DownloadControllerAndroid::CreateGETDownload() => | |
| 14 // DownloadController.newHttpGetDownload() => | |
| 15 // DownloadListener.onDownloadStart() / | |
| 16 // DownloadListener2.requestHttpGetDownload() | |
| 17 // | |
| 18 | |
| 19 #ifndef CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_ | |
| 20 #define CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_ | |
| 21 | |
| 22 #include <string> | |
| 23 | |
| 24 #include <stdint.h> | |
| 25 | |
| 26 #include "base/android/jni_weak_ref.h" | |
| 27 #include "base/android/scoped_java_ref.h" | |
| 28 #include "base/callback.h" | |
| 29 #include "base/macros.h" | |
| 30 #include "base/memory/scoped_vector.h" | |
| 31 #include "base/memory/singleton.h" | |
| 32 #include "content/public/browser/android/download_controller_android.h" | |
| 33 #include "net/cookies/cookie_monster.h" | |
| 34 #include "url/gurl.h" | |
| 35 | |
| 36 namespace net { | |
| 37 class URLRequest; | |
| 38 } | |
| 39 | |
| 40 namespace content { | |
| 41 struct GlobalRequestID; | |
| 42 class DeferredDownloadObserver; | |
| 43 class RenderViewHost; | |
| 44 class WebContents; | |
| 45 | |
| 46 class DownloadControllerAndroidImpl : public DownloadControllerAndroid { | |
| 47 public: | |
| 48 static DownloadControllerAndroidImpl* GetInstance(); | |
| 49 | |
| 50 static bool RegisterDownloadController(JNIEnv* env); | |
| 51 | |
| 52 // Called when DownloadController Java object is instantiated. | |
| 53 void Init(JNIEnv* env, jobject obj); | |
| 54 | |
| 55 // Removes a deferred download from |deferred_downloads_|. | |
| 56 void CancelDeferredDownload(DeferredDownloadObserver* observer); | |
| 57 | |
| 58 // DownloadControllerAndroid implementation. | |
| 59 void AcquireFileAccessPermission( | |
| 60 WebContents* web_contents, | |
| 61 const AcquireFileAccessPermissionCallback& callback) override; | |
| 62 void SetDefaultDownloadFileName(const std::string& file_name) override; | |
| 63 | |
| 64 private: | |
| 65 // Used to store all the information about an Android download. | |
| 66 struct DownloadInfoAndroid { | |
| 67 explicit DownloadInfoAndroid(net::URLRequest* request); | |
| 68 DownloadInfoAndroid(const DownloadInfoAndroid& other); | |
| 69 ~DownloadInfoAndroid(); | |
| 70 | |
| 71 // The URL from which we are downloading. This is the final URL after any | |
| 72 // redirection by the server for |original_url_|. | |
| 73 GURL url; | |
| 74 // The original URL before any redirection by the server for this URL. | |
| 75 GURL original_url; | |
| 76 int64_t total_bytes; | |
| 77 std::string content_disposition; | |
| 78 std::string original_mime_type; | |
| 79 std::string user_agent; | |
| 80 std::string cookie; | |
| 81 std::string referer; | |
| 82 bool has_user_gesture; | |
| 83 | |
| 84 WebContents* web_contents; | |
| 85 // Default copy constructor is used for passing this struct by value. | |
| 86 }; | |
| 87 struct JavaObject; | |
| 88 friend struct base::DefaultSingletonTraits<DownloadControllerAndroidImpl>; | |
| 89 DownloadControllerAndroidImpl(); | |
| 90 ~DownloadControllerAndroidImpl() override; | |
| 91 | |
| 92 // Helper method for implementing AcquireFileAccessPermission(). | |
| 93 bool HasFileAccessPermission( | |
| 94 base::android::ScopedJavaLocalRef<jobject> j_content_view_core); | |
| 95 | |
| 96 // DownloadControllerAndroid implementation. | |
| 97 void CreateGETDownload(int render_process_id, | |
| 98 int render_view_id, | |
| 99 int request_id, | |
| 100 bool must_download) override; | |
| 101 void OnDownloadStarted(DownloadItem* download_item) override; | |
| 102 void StartContextMenuDownload(const ContextMenuParams& params, | |
| 103 WebContents* web_contents, | |
| 104 bool is_link, | |
| 105 const std::string& extra_headers) override; | |
| 106 void DangerousDownloadValidated(WebContents* web_contents, | |
| 107 const std::string& download_guid, | |
| 108 bool accept) override; | |
| 109 | |
| 110 // DownloadItem::Observer interface. | |
| 111 void OnDownloadUpdated(DownloadItem* item) override; | |
| 112 | |
| 113 typedef base::Callback<void(const DownloadInfoAndroid&)> | |
| 114 GetDownloadInfoCB; | |
| 115 void PrepareDownloadInfo(const GlobalRequestID& global_id, | |
| 116 const GetDownloadInfoCB& callback); | |
| 117 void CheckPolicyAndLoadCookies(const DownloadInfoAndroid& info, | |
| 118 const GetDownloadInfoCB& callback, | |
| 119 const GlobalRequestID& global_id, | |
| 120 const net::CookieList& cookie_list); | |
| 121 void DoLoadCookies(const DownloadInfoAndroid& info, | |
| 122 const GetDownloadInfoCB& callback, | |
| 123 const GlobalRequestID& global_id); | |
| 124 void OnCookieResponse(DownloadInfoAndroid info, | |
| 125 const GetDownloadInfoCB& callback, | |
| 126 const std::string& cookie); | |
| 127 void StartDownloadOnUIThread(const GetDownloadInfoCB& callback, | |
| 128 const DownloadInfoAndroid& info); | |
| 129 void StartAndroidDownload(int render_process_id, | |
| 130 int render_view_id, | |
| 131 bool must_download, | |
| 132 const DownloadInfoAndroid& info); | |
| 133 void StartAndroidDownloadInternal(int render_process_id, | |
| 134 int render_view_id, | |
| 135 bool must_download, | |
| 136 const DownloadInfoAndroid& info, | |
| 137 bool allowed); | |
| 138 | |
| 139 // The download item contains dangerous file types. | |
| 140 void OnDangerousDownload(DownloadItem *item); | |
| 141 | |
| 142 base::android::ScopedJavaLocalRef<jobject> GetContentViewCoreFromWebContents( | |
| 143 WebContents* web_contents); | |
| 144 | |
| 145 // Creates Java object if it is not created already and returns it. | |
| 146 JavaObject* GetJavaObject(); | |
| 147 | |
| 148 JavaObject* java_object_; | |
| 149 | |
| 150 std::string default_file_name_; | |
| 151 | |
| 152 ScopedVector<DeferredDownloadObserver> deferred_downloads_; | |
| 153 | |
| 154 DISALLOW_COPY_AND_ASSIGN(DownloadControllerAndroidImpl); | |
| 155 }; | |
| 156 | |
| 157 } // namespace content | |
| 158 | |
| 159 #endif // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_ | |
| OLD | NEW |