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
);
58 // Used to store all the information about an Android download.
59 struct DownloadInfoAndroid
{
60 explicit DownloadInfoAndroid(net::URLRequest
* request
);
61 ~DownloadInfoAndroid();
63 // The URL from which we are downloading. This is the final URL after any
64 // redirection by the server for |original_url_|.
66 // The original URL before any redirection by the server for this URL.
69 std::string content_disposition
;
70 std::string original_mime_type
;
71 std::string user_agent
;
74 bool has_user_gesture
;
76 WebContents
* web_contents
;
77 // Default copy constructor is used for passing this struct by value.
80 friend struct DefaultSingletonTraits
<DownloadControllerAndroidImpl
>;
81 DownloadControllerAndroidImpl();
82 ~DownloadControllerAndroidImpl() override
;
84 // DownloadControllerAndroid implementation.
85 void CreateGETDownload(int render_process_id
,
87 int request_id
) override
;
88 void OnDownloadStarted(DownloadItem
* download_item
) override
;
89 void StartContextMenuDownload(const ContextMenuParams
& params
,
90 WebContents
* web_contents
,
91 bool is_link
) override
;
92 void DangerousDownloadValidated(WebContents
* web_contents
,
94 bool accept
) override
;
96 // DownloadItem::Observer interface.
97 void OnDownloadUpdated(DownloadItem
* item
) override
;
99 typedef base::Callback
<void(const DownloadInfoAndroid
&)>
101 void PrepareDownloadInfo(const GlobalRequestID
& global_id
,
102 const GetDownloadInfoCB
& callback
);
103 void CheckPolicyAndLoadCookies(const DownloadInfoAndroid
& info
,
104 const GetDownloadInfoCB
& callback
,
105 const GlobalRequestID
& global_id
,
106 const net::CookieList
& cookie_list
);
107 void DoLoadCookies(const DownloadInfoAndroid
& info
,
108 const GetDownloadInfoCB
& callback
,
109 const GlobalRequestID
& global_id
);
110 void OnCookieResponse(DownloadInfoAndroid info
,
111 const GetDownloadInfoCB
& callback
,
112 const std::string
& cookie
);
113 void StartDownloadOnUIThread(const GetDownloadInfoCB
& callback
,
114 const DownloadInfoAndroid
& info
);
115 void StartAndroidDownload(int render_process_id
,
117 const DownloadInfoAndroid
& info
);
119 // The download item contains dangerous file types.
120 void OnDangerousDownload(DownloadItem
*item
);
122 base::android::ScopedJavaLocalRef
<jobject
> GetContentViewCoreFromWebContents(
123 WebContents
* web_contents
);
125 WebContents
* GetWebContents(int render_process_id
, int render_view_id
);
127 // Creates Java object if it is not created already and returns it.
128 JavaObject
* GetJavaObject();
130 JavaObject
* java_object_
;
132 ScopedVector
<DeferredDownloadObserver
> deferred_downloads_
;
134 DISALLOW_COPY_AND_ASSIGN(DownloadControllerAndroidImpl
);
137 } // namespace content
139 #endif // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_