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/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"
38 struct GlobalRequestID
;
42 class DownloadControllerAndroidImpl
: public DownloadControllerAndroid
,
43 public DownloadItem::Observer
{
45 static DownloadControllerAndroidImpl
* GetInstance();
47 static bool RegisterDownloadController(JNIEnv
* env
);
49 // Called when DownloadController Java object is instantiated.
50 void Init(JNIEnv
* env
, jobject obj
);
52 // Used to store all the information about an Android download.
53 struct DownloadInfoAndroid
{
54 explicit DownloadInfoAndroid(net::URLRequest
* request
);
55 ~DownloadInfoAndroid();
57 // The URL from which we are downloading. This is the final URL after any
58 // redirection by the server for |original_url_|.
60 // The original URL before any redirection by the server for this URL.
63 std::string content_disposition
;
64 std::string original_mime_type
;
65 std::string user_agent
;
68 bool has_user_gesture
;
70 WebContents
* web_contents
;
71 // Default copy constructor is used for passing this struct by value.
74 friend struct DefaultSingletonTraits
<DownloadControllerAndroidImpl
>;
75 DownloadControllerAndroidImpl();
76 virtual ~DownloadControllerAndroidImpl();
78 // DownloadControllerAndroid implementation.
79 virtual void CreateGETDownload(int render_process_id
, int render_view_id
,
80 int request_id
) OVERRIDE
;
81 virtual void OnDownloadStarted(DownloadItem
* download_item
) OVERRIDE
;
82 virtual void StartContextMenuDownload(
83 const ContextMenuParams
& params
, WebContents
* web_contents
,
84 bool is_link
) OVERRIDE
;
85 virtual void DangerousDownloadValidated(
86 WebContents
* web_contents
, int download_id
, bool accept
) OVERRIDE
;
88 // DownloadItem::Observer interface.
89 virtual void OnDownloadUpdated(DownloadItem
* item
) OVERRIDE
;
91 typedef base::Callback
<void(const DownloadInfoAndroid
&)>
93 void PrepareDownloadInfo(const GlobalRequestID
& global_id
,
94 const GetDownloadInfoCB
& callback
);
95 void CheckPolicyAndLoadCookies(const DownloadInfoAndroid
& info
,
96 const GetDownloadInfoCB
& callback
,
97 const GlobalRequestID
& global_id
,
98 const net::CookieList
& cookie_list
);
99 void DoLoadCookies(const DownloadInfoAndroid
& info
,
100 const GetDownloadInfoCB
& callback
,
101 const GlobalRequestID
& global_id
);
102 void OnCookieResponse(DownloadInfoAndroid info
,
103 const GetDownloadInfoCB
& callback
,
104 const std::string
& cookie
);
105 void StartDownloadOnUIThread(const GetDownloadInfoCB
& callback
,
106 const DownloadInfoAndroid
& info
);
107 void StartAndroidDownload(int render_process_id
,
109 const DownloadInfoAndroid
& info
);
111 // The download item contains dangerous file types.
112 void OnDangerousDownload(DownloadItem
*item
);
114 base::android::ScopedJavaLocalRef
<jobject
> GetContentViewCoreFromWebContents(
115 WebContents
* web_contents
);
117 base::android::ScopedJavaLocalRef
<jobject
> GetContentView(
118 int render_process_id
, int render_view_id
);
120 // Creates Java object if it is not created already and returns it.
121 JavaObject
* GetJavaObject();
123 JavaObject
* java_object_
;
125 DISALLOW_COPY_AND_ASSIGN(DownloadControllerAndroidImpl
);
128 } // namespace content
130 #endif // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_