electron_api_download_item.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_DOWNLOAD_ITEM_H_
  5. #define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_DOWNLOAD_ITEM_H_
  6. #include <string>
  7. #include "base/files/file_path.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "components/download/public/common/download_item.h"
  11. #include "gin/wrappable.h"
  12. #include "shell/browser/event_emitter_mixin.h"
  13. #include "shell/browser/ui/file_dialog.h"
  14. #include "shell/common/gin_helper/pinnable.h"
  15. class GURL;
  16. namespace gin {
  17. template <typename T>
  18. class Handle;
  19. } // namespace gin
  20. namespace electron::api {
  21. class DownloadItem final : public gin::Wrappable<DownloadItem>,
  22. public gin_helper::Pinnable<DownloadItem>,
  23. public gin_helper::EventEmitterMixin<DownloadItem>,
  24. private download::DownloadItem::Observer {
  25. public:
  26. static gin::Handle<DownloadItem> FromOrCreate(v8::Isolate* isolate,
  27. download::DownloadItem* item);
  28. static DownloadItem* FromDownloadItem(download::DownloadItem* item);
  29. // gin::Wrappable
  30. static gin::WrapperInfo kWrapperInfo;
  31. gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
  32. v8::Isolate* isolate) override;
  33. const char* GetTypeName() override;
  34. // JS API, but also C++ calls it sometimes
  35. void SetSavePath(const base::FilePath& path);
  36. base::FilePath GetSavePath() const;
  37. file_dialog::DialogSettings GetSaveDialogOptions() const;
  38. // disable copy
  39. DownloadItem(const DownloadItem&) = delete;
  40. DownloadItem& operator=(const DownloadItem&) = delete;
  41. private:
  42. DownloadItem(v8::Isolate* isolate, download::DownloadItem* item);
  43. ~DownloadItem() override;
  44. bool CheckAlive() const;
  45. // download::DownloadItem::Observer
  46. void OnDownloadUpdated(download::DownloadItem* item) override;
  47. void OnDownloadDestroyed(download::DownloadItem* item) override;
  48. // JS API
  49. void Pause();
  50. bool IsPaused() const;
  51. void Resume();
  52. bool CanResume() const;
  53. void Cancel();
  54. int64_t GetCurrentBytesPerSecond() const;
  55. int64_t GetReceivedBytes() const;
  56. int64_t GetTotalBytes() const;
  57. int GetPercentComplete() const;
  58. std::string GetMimeType() const;
  59. bool HasUserGesture() const;
  60. std::string GetFilename() const;
  61. std::string GetContentDisposition() const;
  62. const GURL& GetURL() const;
  63. v8::Local<v8::Value> GetURLChain() const;
  64. download::DownloadItem::DownloadState GetState() const;
  65. bool IsDone() const;
  66. void SetSaveDialogOptions(const file_dialog::DialogSettings& options);
  67. std::string GetLastModifiedTime() const;
  68. std::string GetETag() const;
  69. double GetStartTime() const;
  70. double GetEndTime() const;
  71. base::FilePath save_path_;
  72. file_dialog::DialogSettings dialog_options_;
  73. raw_ptr<download::DownloadItem> download_item_;
  74. raw_ptr<v8::Isolate> isolate_;
  75. base::WeakPtrFactory<DownloadItem> weak_factory_{this};
  76. };
  77. } // namespace electron::api
  78. #endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_DOWNLOAD_ITEM_H_