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 #ifndef CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOWNLOADS_DOM_HANDLER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOWNLOADS_DOM_HANDLER_H_ 7 8 #include <stdint.h> 9 10 #include <set> 11 #include <vector> 12 13 #include "base/macros.h" 14 #include "base/memory/weak_ptr.h" 15 #include "chrome/browser/download/download_danger_prompt.h" 16 #include "chrome/browser/ui/webui/downloads/downloads.mojom-forward.h" 17 #include "chrome/browser/ui/webui/downloads/downloads_list_tracker.h" 18 #include "content/public/browser/web_contents_observer.h" 19 #include "mojo/public/cpp/bindings/pending_receiver.h" 20 #include "mojo/public/cpp/bindings/pending_remote.h" 21 #include "mojo/public/cpp/bindings/receiver.h" 22 23 namespace content { 24 class DownloadManager; 25 class WebContents; 26 class WebUI; 27 } 28 29 namespace download { 30 class DownloadItem; 31 } 32 33 // The handler for Javascript messages related to the "downloads" view, 34 // also observes changes to the download manager. 35 // TODO(calamity): Remove WebUIMessageHandler. 36 class DownloadsDOMHandler : public content::WebContentsObserver, 37 public downloads::mojom::PageHandler { 38 public: 39 DownloadsDOMHandler( 40 mojo::PendingReceiver<downloads::mojom::PageHandler> receiver, 41 mojo::PendingRemote<downloads::mojom::Page> page, 42 content::DownloadManager* download_manager, 43 content::WebUI* web_ui); 44 ~DownloadsDOMHandler() override; 45 46 // WebContentsObserver implementation. 47 void RenderProcessGone(base::TerminationStatus status) override; 48 49 // downloads::mojom::PageHandler: 50 void GetDownloads(const std::vector<std::string>& search_terms) override; 51 void OpenFileRequiringGesture(const std::string& id) override; 52 void Drag(const std::string& id) override; 53 void SaveDangerousRequiringGesture(const std::string& id) override; 54 void DiscardDangerous(const std::string& id) override; 55 void RetryDownload(const std::string& id) override; 56 void Show(const std::string& id) override; 57 void Pause(const std::string& id) override; 58 void Resume(const std::string& id) override; 59 void Remove(const std::string& id) override; 60 void Undo() override; 61 void Cancel(const std::string& id) override; 62 void ClearAll() override; 63 void OpenDownloadsFolderRequiringGesture() override; 64 void OpenDuringScanningRequiringGesture(const std::string& id) override; 65 66 protected: 67 // These methods are for mocking so that most of this class does not actually 68 // depend on WebUI. The other methods that depend on WebUI are 69 // RegisterMessages() and HandleDrag(). 70 virtual content::WebContents* GetWebUIWebContents(); 71 72 // Actually remove downloads with an ID in |removals_|. This cannot be undone. 73 void FinalizeRemovals(); 74 75 using DownloadVector = std::vector<download::DownloadItem*>; 76 77 // Remove all downloads in |to_remove|. Safe downloads can be revived, 78 // dangerous ones are immediately removed. Protected for testing. 79 void RemoveDownloads(const DownloadVector& to_remove); 80 81 private: 82 using IdSet = std::set<uint32_t>; 83 84 // Convenience method to call |main_notifier_->GetManager()| while 85 // null-checking |main_notifier_|. 86 content::DownloadManager* GetMainNotifierManager() const; 87 88 // Convenience method to call |original_notifier_->GetManager()| while 89 // null-checking |original_notifier_|. 90 content::DownloadManager* GetOriginalNotifierManager() const; 91 92 // Displays a native prompt asking the user for confirmation after accepting 93 // the dangerous download specified by |dangerous|. The function returns 94 // immediately, and will invoke DangerPromptAccepted() asynchronously if the 95 // user accepts the dangerous download. The native prompt will observe 96 // |dangerous| until either the dialog is dismissed or |dangerous| is no 97 // longer an in-progress dangerous download. 98 virtual void ShowDangerPrompt(download::DownloadItem* dangerous); 99 100 // Conveys danger acceptance from the DownloadDangerPrompt to the 101 // DownloadItem. 102 void DangerPromptDone(int download_id, DownloadDangerPrompt::Action action); 103 104 // Returns true if the records of any downloaded items are allowed (and able) 105 // to be deleted. 106 bool IsDeletingHistoryAllowed(); 107 108 // Returns the download that is referred to by a given string |id|. 109 download::DownloadItem* GetDownloadByStringId(const std::string& id); 110 111 // Returns the download with |id| or NULL if it doesn't exist. 112 download::DownloadItem* GetDownloadById(uint32_t id); 113 114 // Removes the download specified by an ID from JavaScript in |args|. 115 void RemoveDownloadInArgs(const std::string& id); 116 117 // Checks whether a download's file was removed from its original location. 118 void CheckForRemovedFiles(); 119 120 DownloadsListTracker list_tracker_; 121 122 // IDs of downloads to remove when this handler gets deleted. 123 std::vector<IdSet> removals_; 124 125 // Whether the render process has gone. 126 bool render_process_gone_ = false; 127 128 content::WebUI* web_ui_; 129 130 mojo::Receiver<downloads::mojom::PageHandler> receiver_; 131 132 base::WeakPtrFactory<DownloadsDOMHandler> weak_ptr_factory_{this}; 133 134 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); 135 }; 136 137 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOWNLOADS_DOM_HANDLER_H_ 138