download_controller_android_impl.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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// DownloadControllerAndroid::CreateGETDownload() =>
14// DownloadController.newHttpGetDownload() =>
15// DownloadListener.onDownloadStart() /
16// DownloadListener2.requestHttpGetDownload()
17//
18
19#ifndef CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_
20#define CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_
21
22#include <string>
23
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"
31#include "url/gurl.h"
32
33namespace net {
34class URLRequest;
35}
36
37namespace content {
38struct GlobalRequestID;
39class RenderViewHost;
40class WebContents;
41
42class DownloadControllerAndroidImpl : public DownloadControllerAndroid,
43                                      public DownloadItem::Observer {
44 public:
45  static DownloadControllerAndroidImpl* GetInstance();
46
47  static bool RegisterDownloadController(JNIEnv* env);
48
49  // Called when DownloadController Java object is instantiated.
50  void Init(JNIEnv* env, jobject obj);
51 private:
52  // Used to store all the information about an Android download.
53  struct DownloadInfoAndroid {
54    explicit DownloadInfoAndroid(net::URLRequest* request);
55    ~DownloadInfoAndroid();
56
57    // The URL from which we are downloading. This is the final URL after any
58    // redirection by the server for |original_url_|.
59    GURL url;
60    // The original URL before any redirection by the server for this URL.
61    GURL original_url;
62    int64 total_bytes;
63    std::string content_disposition;
64    std::string original_mime_type;
65    std::string user_agent;
66    std::string cookie;
67    std::string referer;
68    bool has_user_gesture;
69
70    WebContents* web_contents;
71    // Default copy constructor is used for passing this struct by value.
72  };
73  struct JavaObject;
74  friend struct DefaultSingletonTraits<DownloadControllerAndroidImpl>;
75  DownloadControllerAndroidImpl();
76  virtual ~DownloadControllerAndroidImpl();
77
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;
87
88  // DownloadItem::Observer interface.
89  virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE;
90
91  typedef base::Callback<void(const DownloadInfoAndroid&)>
92      GetDownloadInfoCB;
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,
108                            int render_view_id,
109                            const DownloadInfoAndroid& info);
110
111  // The download item contains dangerous file types.
112  void OnDangerousDownload(DownloadItem *item);
113
114  base::android::ScopedJavaLocalRef<jobject> GetContentViewCoreFromWebContents(
115      WebContents* web_contents);
116
117  base::android::ScopedJavaLocalRef<jobject> GetContentView(
118      int render_process_id, int render_view_id);
119
120  // Creates Java object if it is not created already and returns it.
121  JavaObject* GetJavaObject();
122
123  JavaObject* java_object_;
124
125  DISALLOW_COPY_AND_ASSIGN(DownloadControllerAndroidImpl);
126};
127
128}  // namespace content
129
130#endif  // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_
131