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.
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.
9 // Both classes are Singleton classes. C++ object owns Java object.
13 // DownloadControllerAndroid::CreateGETDownload() =>
14 // DownloadController.newHttpGetDownload() =>
15 // DownloadListener.onDownloadStart() /
16 // DownloadListener2.requestHttpGetDownload()
19 #ifndef CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_
20 #define CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_
24 #include "base/android/jni_weak_ref.h"
25 #include "base/android/scoped_java_ref.h"
26 #include "base/callback.h"
27 #include "base/memory/scoped_vector.h"
28 #include "base/memory/singleton.h"
29 #include "content/public/browser/android/download_controller_android.h"
30 #include "content/public/browser/download_item.h"
31 #include "net/cookies/cookie_monster.h"
39 struct GlobalRequestID
;
40 class DeferredDownloadObserver
;
44 class DownloadControllerAndroidImpl
: public DownloadControllerAndroid
,
45 public DownloadItem::Observer
{
47 static DownloadControllerAndroidImpl
* GetInstance();
49 static bool RegisterDownloadController(JNIEnv
* env
);
51 // Called when DownloadController Java object is instantiated.
52 void Init(JNIEnv
* env
, jobject obj
);
54 // Removes a deferred download from |deferred_downloads_|.
55 void CancelDeferredDownload(DeferredDownloadObserver
* observer
);
57 // DownloadControllerAndroid implementation.
58 void AcquireFileAccessPermission(
59 int render_process_id
,
61 const AcquireFileAccessPermissionCallback
& callback
) override
;
64 // Used to store all the information about an Android download.
65 struct DownloadInfoAndroid
{
66 explicit DownloadInfoAndroid(net::URLRequest
* request
);
67 ~DownloadInfoAndroid();
69 // The URL from which we are downloading. This is the final URL after any
70 // redirection by the server for |original_url_|.
72 // The original URL before any redirection by the server for this URL.
75 std::string content_disposition
;
76 std::string original_mime_type
;
77 std::string user_agent
;
80 bool has_user_gesture
;
82 WebContents
* web_contents
;
83 // Default copy constructor is used for passing this struct by value.
86 friend struct base::DefaultSingletonTraits
<DownloadControllerAndroidImpl
>;
87 DownloadControllerAndroidImpl();
88 ~DownloadControllerAndroidImpl() override
;
90 // Helper method for implementing AcquireFileAccessPermission().
91 bool HasFileAccessPermission(
92 base::android::ScopedJavaLocalRef
<jobject
> j_content_view_core
);
94 // DownloadControllerAndroid implementation.
95 void CreateGETDownload(int render_process_id
,
97 int request_id
) override
;
98 void OnDownloadStarted(DownloadItem
* download_item
) override
;
99 void StartContextMenuDownload(const ContextMenuParams
& params
,
100 WebContents
* web_contents
,
102 const std::string
& extra_headers
) override
;
103 void DangerousDownloadValidated(WebContents
* web_contents
,
105 bool accept
) override
;
107 // DownloadItem::Observer interface.
108 void OnDownloadUpdated(DownloadItem
* item
) override
;
110 typedef base::Callback
<void(const DownloadInfoAndroid
&)>
112 void PrepareDownloadInfo(const GlobalRequestID
& global_id
,
113 const GetDownloadInfoCB
& callback
);
114 void CheckPolicyAndLoadCookies(const DownloadInfoAndroid
& info
,
115 const GetDownloadInfoCB
& callback
,
116 const GlobalRequestID
& global_id
,
117 const net::CookieList
& cookie_list
);
118 void DoLoadCookies(const DownloadInfoAndroid
& info
,
119 const GetDownloadInfoCB
& callback
,
120 const GlobalRequestID
& global_id
);
121 void OnCookieResponse(DownloadInfoAndroid info
,
122 const GetDownloadInfoCB
& callback
,
123 const std::string
& cookie
);
124 void StartDownloadOnUIThread(const GetDownloadInfoCB
& callback
,
125 const DownloadInfoAndroid
& info
);
126 void StartAndroidDownload(int render_process_id
,
128 const DownloadInfoAndroid
& info
);
129 void StartAndroidDownloadInternal(int render_process_id
,
131 const DownloadInfoAndroid
& info
,
134 // The download item contains dangerous file types.
135 void OnDangerousDownload(DownloadItem
*item
);
137 base::android::ScopedJavaLocalRef
<jobject
> GetContentViewCoreFromWebContents(
138 WebContents
* web_contents
);
140 // Creates Java object if it is not created already and returns it.
141 JavaObject
* GetJavaObject();
143 JavaObject
* java_object_
;
145 ScopedVector
<DeferredDownloadObserver
> deferred_downloads_
;
147 DISALLOW_COPY_AND_ASSIGN(DownloadControllerAndroidImpl
);
150 } // namespace content
152 #endif // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_