summaryrefslogtreecommitdiffstats
path: root/content/browser/android/download_controller_android_impl.h
blob: 26bc2a0c4f92a3ccaa277f627a8ebf302a8f500f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This class pairs with DownloadController on Java side to forward requests
// for GET downloads to the current DownloadListener. POST downloads are
// handled on the native side.
//
// Both classes are Singleton classes. C++ object owns Java object.
//
// Call sequence
// GET downloads:
// DownloadControllerAndroid::CreateGETDownload() =>
// DownloadController.newHttpGetDownload() =>
// DownloadListener.onDownloadStart() /
// DownloadListener2.requestHttpGetDownload()
//

#ifndef CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_
#define CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_

#include <string>

#include "base/android/jni_weak_ref.h"
#include "base/android/scoped_java_ref.h"
#include "base/callback.h"
#include "base/memory/singleton.h"
#include "content/public/browser/android/download_controller_android.h"
#include "content/public/browser/download_item.h"
#include "net/cookies/cookie_monster.h"
#include "url/gurl.h"

namespace net {
class URLRequest;
}

namespace content {
struct GlobalRequestID;
class RenderViewHost;
class WebContents;

class DownloadControllerAndroidImpl : public DownloadControllerAndroid,
                                      public DownloadItem::Observer {
 public:
  static DownloadControllerAndroidImpl* GetInstance();

  static bool RegisterDownloadController(JNIEnv* env);

  // Called when DownloadController Java object is instantiated.
  void Init(JNIEnv* env, jobject obj);
 private:
  // Used to store all the information about an Android download.
  struct DownloadInfoAndroid {
    explicit DownloadInfoAndroid(net::URLRequest* request);
    ~DownloadInfoAndroid();

    // The URL from which we are downloading. This is the final URL after any
    // redirection by the server for |original_url_|.
    GURL url;
    // The original URL before any redirection by the server for this URL.
    GURL original_url;
    int64 total_bytes;
    std::string content_disposition;
    std::string original_mime_type;
    std::string user_agent;
    std::string cookie;
    std::string referer;

    WebContents* web_contents;
    // Default copy constructor is used for passing this struct by value.
  };
  struct JavaObject;
  friend struct DefaultSingletonTraits<DownloadControllerAndroidImpl>;
  DownloadControllerAndroidImpl();
  virtual ~DownloadControllerAndroidImpl();

  // DownloadControllerAndroid implementation.
  virtual void CreateGETDownload(int render_process_id, int render_view_id,
                                 int request_id) OVERRIDE;
  virtual void OnDownloadStarted(DownloadItem* download_item) OVERRIDE;
  virtual void StartContextMenuDownload(
      const ContextMenuParams& params, WebContents* web_contents,
      bool is_link) OVERRIDE;
  virtual void DangerousDownloadValidated(
      WebContents* web_contents, int download_id, bool accept) OVERRIDE;

  // DownloadItem::Observer interface.
  virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE;

  typedef base::Callback<void(const DownloadInfoAndroid&)>
      GetDownloadInfoCB;
  void PrepareDownloadInfo(const GlobalRequestID& global_id,
                           const GetDownloadInfoCB& callback);
  void CheckPolicyAndLoadCookies(const DownloadInfoAndroid& info,
                                 const GetDownloadInfoCB& callback,
                                 const GlobalRequestID& global_id,
                                 const net::CookieList& cookie_list);
  void DoLoadCookies(const DownloadInfoAndroid& info,
                     const GetDownloadInfoCB& callback,
                     const GlobalRequestID& global_id);
  void OnCookieResponse(DownloadInfoAndroid info,
                        const GetDownloadInfoCB& callback,
                        const std::string& cookie);
  void StartDownloadOnUIThread(const GetDownloadInfoCB& callback,
                               const DownloadInfoAndroid& info);
  void StartAndroidDownload(int render_process_id,
                            int render_view_id,
                            const DownloadInfoAndroid& info);

  // The download item contains dangerous file types.
  void OnDangerousDownload(DownloadItem *item);

  base::android::ScopedJavaLocalRef<jobject> GetContentViewCoreFromWebContents(
      WebContents* web_contents);

  base::android::ScopedJavaLocalRef<jobject> GetContentView(
      int render_process_id, int render_view_id);

  // Creates Java object if it is not created already and returns it.
  JavaObject* GetJavaObject();

  JavaObject* java_object_;

  DISALLOW_COPY_AND_ASSIGN(DownloadControllerAndroidImpl);
};

}  // namespace content

#endif  // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_