summaryrefslogtreecommitdiff
path: root/chromium/weblayer/browser/download_impl.h
blob: d00bc2a19606266168af231b176064cd66eb90fe (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
// Copyright 2020 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.

#ifndef WEBLAYER_BROWSER_DOWNLOAD_IMPL_H_
#define WEBLAYER_BROWSER_DOWNLOAD_IMPL_H_

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/supports_user_data.h"
#include "build/build_config.h"
#include "weblayer/public/download.h"

#if defined(OS_ANDROID)
#include "base/android/scoped_java_ref.h"
#endif

namespace download {
class DownloadItem;
}

namespace weblayer {

class DownloadImpl : public Download, public base::SupportsUserData::Data {
 public:
  ~DownloadImpl() override;
  DownloadImpl(const DownloadImpl&) = delete;
  DownloadImpl& operator=(const DownloadImpl&) = delete;

  static void Create(download::DownloadItem* item);
  static DownloadImpl* Get(download::DownloadItem* item);

#if defined(OS_ANDROID)
  void SetJavaDownload(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& java_download);
  int GetState(JNIEnv* env) { return static_cast<int>(GetState()); }
  jlong GetTotalBytes(JNIEnv* env) { return GetTotalBytes(); }
  jlong GetReceivedBytes(JNIEnv* env) { return GetReceivedBytes(); }
  void Pause(JNIEnv* env) { Pause(); }
  void Resume(JNIEnv* env) { Resume(); }
  void Cancel(JNIEnv* env) { Cancel(); }
  base::android::ScopedJavaLocalRef<jstring> GetLocation(JNIEnv* env);
  base::android::ScopedJavaLocalRef<jstring> GetFileNameToReportToUser(
      JNIEnv* env);
  // Add Impl suffix to avoid compiler clash with the C++ interface method.
  base::android::ScopedJavaLocalRef<jstring> GetMimeTypeImpl(JNIEnv* env);
  int GetError(JNIEnv* env) { return static_cast<int>(GetError()); }

  base::android::ScopedJavaGlobalRef<jobject> java_download() {
    return java_download_;
  }
#endif

  // Download implementation:
  DownloadState GetState() override;
  int64_t GetTotalBytes() override;
  int64_t GetReceivedBytes() override;
  void Pause() override;
  void Resume() override;
  void Cancel() override;
  base::FilePath GetLocation() override;
  base::FilePath GetFileNameToReportToUser() override;
  std::string GetMimeType() override;
  DownloadError GetError() override;

  uint32_t GetId();

 private:
  explicit DownloadImpl(download::DownloadItem* item);

  void PauseInternal();
  void ResumeInternal();
  void CancelInternal();

  download::DownloadItem* item_;
  bool pause_pending_ = false;
  bool resume_pending_ = false;
  bool cancel_pending_ = false;

#if defined(OS_ANDROID)
  base::android::ScopedJavaGlobalRef<jobject> java_download_;
#endif

  base::WeakPtrFactory<DownloadImpl> weak_ptr_factory_{this};
};

}  // namespace weblayer

#endif  // WEBLAYER_BROWSER_DOWNLOAD_IMPL_H_