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 "base/android/jni_helper.h" 25 #include "base/android/scoped_java_ref.h" 26 #include "base/callback.h" 27 #include "base/memory/singleton.h" 28 #include "content/public/browser/android/download_controller_android.h" 29 #include "content/public/browser/download_item.h" 30 #include "net/cookies/cookie_monster.h" 31 #include "url/gurl.h" 32 33 namespace net { 34 class URLRequest; 35 } 36 37 namespace content { 38 struct GlobalRequestID; 39 class RenderViewHost; 40 class WebContents; 41 42 class DownloadControllerAndroidImpl : public DownloadControllerAndroid, 43 public DownloadItem::Observer { 44 public: 45 static DownloadControllerAndroidImpl* GetInstance(); 46 47 static bool RegisterDownloadController(JNIEnv* env); 48 49 // Called when DownloadController Java object is instantiated. 50 void Init(JNIEnv* env, jobject obj); 51 private: 52 // Used to store all the information about an Android download. 53 struct DownloadInfoAndroid { 54 explicit DownloadInfoAndroid(net::URLRequest* request); 55 ~DownloadInfoAndroid(); 56 57 // The URL from which we are downloading. This is the final URL after any 58 // redirection by the server for |original_url_|. 59 GURL url; 60 // The original URL before any redirection by the server for this URL. 61 GURL original_url; 62 int64 total_bytes; 63 std::string content_disposition; 64 std::string original_mime_type; 65 std::string user_agent; 66 std::string cookie; 67 std::string referer; 68 69 WebContents* web_contents; 70 // Default copy constructor is used for passing this struct by value. 71 }; 72 struct JavaObject; 73 friend struct DefaultSingletonTraits<DownloadControllerAndroidImpl>; 74 DownloadControllerAndroidImpl(); 75 virtual ~DownloadControllerAndroidImpl(); 76 77 // DownloadControllerAndroid implementation. 78 virtual void CreateGETDownload(int render_process_id, int render_view_id, 79 int request_id) OVERRIDE; 80 virtual void OnDownloadStarted(DownloadItem* download_item) OVERRIDE; 81 virtual void StartContextMenuDownload( 82 const ContextMenuParams& params, WebContents* web_contents, 83 bool is_link) OVERRIDE; 84 virtual void DangerousDownloadValidated( 85 WebContents* web_contents, int download_id, bool accept) OVERRIDE; 86 87 // DownloadItem::Observer interface. 88 virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE; 89 90 typedef base::Callback<void(const DownloadInfoAndroid&)> 91 GetDownloadInfoCB; 92 void PrepareDownloadInfo(const GlobalRequestID& global_id, 93 const GetDownloadInfoCB& callback); 94 void CheckPolicyAndLoadCookies(const DownloadInfoAndroid& info, 95 const GetDownloadInfoCB& callback, 96 const GlobalRequestID& global_id, 97 const net::CookieList& cookie_list); 98 void DoLoadCookies(const DownloadInfoAndroid& info, 99 const GetDownloadInfoCB& callback, 100 const GlobalRequestID& global_id); 101 void OnCookieResponse(DownloadInfoAndroid info, 102 const GetDownloadInfoCB& callback, 103 const std::string& cookie); 104 void StartDownloadOnUIThread(const GetDownloadInfoCB& callback, 105 const DownloadInfoAndroid& info); 106 void StartAndroidDownload(int render_process_id, 107 int render_view_id, 108 const DownloadInfoAndroid& info); 109 110 // The download item contains dangerous file types. 111 void OnDangerousDownload(DownloadItem *item); 112 113 base::android::ScopedJavaLocalRef<jobject> GetContentViewCoreFromWebContents( 114 WebContents* web_contents); 115 116 base::android::ScopedJavaLocalRef<jobject> GetContentView( 117 int render_process_id, int render_view_id); 118 119 // Creates Java object if it is not created already and returns it. 120 JavaObject* GetJavaObject(); 121 122 JavaObject* java_object_; 123 124 DISALLOW_COPY_AND_ASSIGN(DownloadControllerAndroidImpl); 125 }; 126 127 } // namespace content 128 129 #endif // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_ 130