OLD | NEW |
| (Empty) |
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. | |
4 | |
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. | |
10 // | |
11 // Call sequence | |
12 // GET downloads: | |
13 // DownloadController::NewGetDownload() => | |
14 // DownloadController.newHttpGetDownload() => | |
15 // DownloadListener.onDownloadStart() / | |
16 // DownloadListener2.requestHttpGetDownload() | |
17 // | |
18 | |
19 #ifndef CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_H_ | |
20 #define CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_H_ | |
21 | |
22 #include <string> | |
23 | |
24 #include "base/android/jni_helper.h" | |
25 #include "base/android/scoped_java_ref.h" | |
26 #include "base/memory/singleton.h" | |
27 #include "content/public/browser/download_item.h" | |
28 #include "googleurl/src/gurl.h" | |
29 #include "net/cookies/cookie_monster.h" | |
30 | |
31 namespace net { | |
32 class URLRequest; | |
33 } | |
34 | |
35 namespace content { | |
36 struct GlobalRequestID; | |
37 class RenderViewHost; | |
38 class WebContents; | |
39 | |
40 class DownloadController : public DownloadItem::Observer { | |
41 public: | |
42 static bool RegisterDownloadController(JNIEnv* env); | |
43 static DownloadController* GetInstance(); | |
44 | |
45 // Called when DownloadController Java object is instantiated. | |
46 void Init(JNIEnv* env, jobject obj); | |
47 | |
48 // Starts a new download request with Android. Should be called on the | |
49 // UI thread. | |
50 void CreateGETDownload(RenderViewHost* source, | |
51 int request_id); | |
52 | |
53 // Should be called when a POST download is started. Notifies the embedding | |
54 // app about the download. Called on the UI thread. | |
55 void OnPostDownloadStarted(WebContents* web_contents, | |
56 DownloadItem* download_item); | |
57 | |
58 // DownloadItem::Observer interface. | |
59 virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE; | |
60 virtual void OnDownloadOpened(DownloadItem* item) OVERRIDE; | |
61 | |
62 private: | |
63 // Used to store all the information about an Android download. | |
64 struct DownloadInfoAndroid { | |
65 explicit DownloadInfoAndroid(net::URLRequest* request); | |
66 ~DownloadInfoAndroid(); | |
67 | |
68 // The URL from which we are downloading. This is the final URL after any | |
69 // redirection by the server for |original_url_|. | |
70 GURL url; | |
71 // The original URL before any redirection by the server for this URL. | |
72 GURL original_url; | |
73 int64 total_bytes; | |
74 std::string content_disposition; | |
75 std::string original_mime_type; | |
76 std::string user_agent; | |
77 std::string cookie; | |
78 std::string referer; | |
79 | |
80 WebContents* web_contents; | |
81 // Default copy constructor is used for passing this struct by value. | |
82 }; | |
83 | |
84 struct JavaObject; | |
85 friend struct DefaultSingletonTraits<DownloadController>; | |
86 DownloadController(); | |
87 virtual ~DownloadController(); | |
88 | |
89 void PrepareDownloadInfo(const GlobalRequestID& global_id, | |
90 int render_process_id, | |
91 int render_view_id); | |
92 | |
93 void CheckPolicyAndLoadCookies(const DownloadInfoAndroid& info, | |
94 int render_process_id, | |
95 int render_view_id, | |
96 const GlobalRequestID& global_id, | |
97 const net::CookieList& cookie_list); | |
98 | |
99 void DoLoadCookies(const DownloadInfoAndroid& info, | |
100 int render_process_id, | |
101 int render_view_id, | |
102 const GlobalRequestID& global_id); | |
103 | |
104 void OnCookieResponse(DownloadInfoAndroid info, | |
105 int render_process_id, | |
106 int render_view_id, | |
107 const std::string& cookie); | |
108 | |
109 void StartAndroidDownload(const DownloadInfoAndroid& info, | |
110 int render_process_id, | |
111 int render_view_id); | |
112 | |
113 base::android::ScopedJavaLocalRef<jobject> GetContentViewCoreFromWebContents( | |
114 WebContents* web_contents); | |
115 | |
116 base::android::ScopedJavaLocalRef<jobject> GetContentView( | |
117 int render_process_id, int render_view_id); | |
118 | |
119 // Creates Java object if it is not created already and returns it. | |
120 JavaObject* GetJavaObject(); | |
121 | |
122 JavaObject* java_object_; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(DownloadController); | |
125 }; | |
126 | |
127 } // namespace content | |
128 | |
129 #endif // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_H_ | |
OLD | NEW |