Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / android / download_controller_android_impl.h
blobdd391238cde6b1e4ee1bbab75e54d5373891d423
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 private:
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_|.
65 GURL url;
66 // The original URL before any redirection by the server for this URL.
67 GURL original_url;
68 int64 total_bytes;
69 std::string content_disposition;
70 std::string original_mime_type;
71 std::string user_agent;
72 std::string cookie;
73 std::string referer;
74 bool has_user_gesture;
76 WebContents* web_contents;
77 // Default copy constructor is used for passing this struct by value.
79 struct JavaObject;
80 friend struct DefaultSingletonTraits<DownloadControllerAndroidImpl>;
81 DownloadControllerAndroidImpl();
82 ~DownloadControllerAndroidImpl() override;
84 // DownloadControllerAndroid implementation.
85 void CreateGETDownload(int render_process_id,
86 int render_view_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,
93 int download_id,
94 bool accept) override;
96 // DownloadItem::Observer interface.
97 void OnDownloadUpdated(DownloadItem* item) override;
99 typedef base::Callback<void(const DownloadInfoAndroid&)>
100 GetDownloadInfoCB;
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,
116 int render_view_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_