Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / android / download_controller_android_impl.h
blob112167ddef263198610bc75a73eed98d8f6cee13
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.
8 //
9 // Both classes are Singleton classes. C++ object owns Java object.
11 // Call sequence
12 // GET downloads:
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_
22 #include <string>
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"
32 #include "url/gurl.h"
34 namespace net {
35 class URLRequest;
38 namespace content {
39 struct GlobalRequestID;
40 class DeferredDownloadObserver;
41 class RenderViewHost;
42 class WebContents;
44 class DownloadControllerAndroidImpl : public DownloadControllerAndroid,
45 public DownloadItem::Observer {
46 public:
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,
60 int render_view_id,
61 const AcquireFileAccessPermissionCallback& callback) override;
63 private:
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_|.
71 GURL url;
72 // The original URL before any redirection by the server for this URL.
73 GURL original_url;
74 int64 total_bytes;
75 std::string content_disposition;
76 std::string original_mime_type;
77 std::string user_agent;
78 std::string cookie;
79 std::string referer;
80 bool has_user_gesture;
82 WebContents* web_contents;
83 // Default copy constructor is used for passing this struct by value.
85 struct JavaObject;
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,
96 int render_view_id,
97 int request_id) override;
98 void OnDownloadStarted(DownloadItem* download_item) override;
99 void StartContextMenuDownload(const ContextMenuParams& params,
100 WebContents* web_contents,
101 bool is_link,
102 const std::string& extra_headers) override;
103 void DangerousDownloadValidated(WebContents* web_contents,
104 int download_id,
105 bool accept) override;
107 // DownloadItem::Observer interface.
108 void OnDownloadUpdated(DownloadItem* item) override;
110 typedef base::Callback<void(const DownloadInfoAndroid&)>
111 GetDownloadInfoCB;
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,
127 int render_view_id,
128 const DownloadInfoAndroid& info);
129 void StartAndroidDownloadInternal(int render_process_id,
130 int render_view_id,
131 const DownloadInfoAndroid& info,
132 bool allowed);
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_