| OLD | NEW |
| (Empty) |
| 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_DOM_HANDLER_H_ | |
| 6 #define CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <set> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/compiler_specific.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "chrome/browser/download/all_download_item_notifier.h" | |
| 17 #include "chrome/browser/download/download_danger_prompt.h" | |
| 18 #include "content/public/browser/download_item.h" | |
| 19 #include "content/public/browser/download_manager.h" | |
| 20 #include "content/public/browser/web_ui_message_handler.h" | |
| 21 | |
| 22 namespace base { | |
| 23 class ListValue; | |
| 24 } | |
| 25 | |
| 26 namespace content { | |
| 27 class WebContents; | |
| 28 } | |
| 29 | |
| 30 // The handler for Javascript messages related to the "downloads" view, | |
| 31 // also observes changes to the download manager. | |
| 32 class DownloadsDOMHandler : public content::WebUIMessageHandler, | |
| 33 public AllDownloadItemNotifier::Observer { | |
| 34 public: | |
| 35 explicit DownloadsDOMHandler(content::DownloadManager* download_manager); | |
| 36 ~DownloadsDOMHandler() override; | |
| 37 | |
| 38 void Init(); | |
| 39 | |
| 40 // WebUIMessageHandler implementation. | |
| 41 void RegisterMessages() override; | |
| 42 | |
| 43 // AllDownloadItemNotifier::Observer interface | |
| 44 void OnDownloadCreated(content::DownloadManager* manager, | |
| 45 content::DownloadItem* download_item) override; | |
| 46 void OnDownloadUpdated(content::DownloadManager* manager, | |
| 47 content::DownloadItem* download_item) override; | |
| 48 void OnDownloadRemoved(content::DownloadManager* manager, | |
| 49 content::DownloadItem* download_item) override; | |
| 50 | |
| 51 // Callback for the "getDownloads" message. | |
| 52 void HandleGetDownloads(const base::ListValue* args); | |
| 53 | |
| 54 // Callback for the "openFile" message - opens the file in the shell. | |
| 55 void HandleOpenFile(const base::ListValue* args); | |
| 56 | |
| 57 // Callback for the "drag" message - initiates a file object drag. | |
| 58 void HandleDrag(const base::ListValue* args); | |
| 59 | |
| 60 // Callback for the "saveDangerous" message - specifies that the user | |
| 61 // wishes to save a dangerous file. | |
| 62 void HandleSaveDangerous(const base::ListValue* args); | |
| 63 | |
| 64 // Callback for the "discardDangerous" message - specifies that the user | |
| 65 // wishes to discard (remove) a dangerous file. | |
| 66 void HandleDiscardDangerous(const base::ListValue* args); | |
| 67 | |
| 68 // Callback for the "show" message - shows the file in explorer. | |
| 69 void HandleShow(const base::ListValue* args); | |
| 70 | |
| 71 // Callback for the "pause" message - pauses the file download. | |
| 72 void HandlePause(const base::ListValue* args); | |
| 73 | |
| 74 // Callback for the "resume" message - resumes the file download. | |
| 75 void HandleResume(const base::ListValue* args); | |
| 76 | |
| 77 // Callback for the "remove" message - removes the file download from shelf | |
| 78 // and list. | |
| 79 void HandleRemove(const base::ListValue* args); | |
| 80 | |
| 81 // Callback for the "undo" message. Currently only undoes removals. | |
| 82 void HandleUndo(const base::ListValue* args); | |
| 83 | |
| 84 // Callback for the "cancel" message - cancels the download. | |
| 85 void HandleCancel(const base::ListValue* args); | |
| 86 | |
| 87 // Callback for the "clearAll" message - clears all the downloads. | |
| 88 void HandleClearAll(const base::ListValue* args); | |
| 89 | |
| 90 // Callback for the "openDownloadsFolder" message - opens the downloads | |
| 91 // folder. | |
| 92 void HandleOpenDownloadsFolder(const base::ListValue* args); | |
| 93 | |
| 94 protected: | |
| 95 // These methods are for mocking so that most of this class does not actually | |
| 96 // depend on WebUI. The other methods that depend on WebUI are | |
| 97 // RegisterMessages() and HandleDrag(). | |
| 98 virtual content::WebContents* GetWebUIWebContents(); | |
| 99 virtual void CallUpdateAll(const base::ListValue& list); | |
| 100 virtual void CallUpdateItem(const base::DictionaryValue& item); | |
| 101 | |
| 102 // Schedules a call to SendCurrentDownloads() in the next message loop | |
| 103 // iteration. Protected rather than private for use in tests. | |
| 104 void ScheduleSendCurrentDownloads(); | |
| 105 | |
| 106 // Actually remove downloads with an ID in |removals_|. This cannot be undone. | |
| 107 void FinalizeRemovals(); | |
| 108 | |
| 109 private: | |
| 110 // Shorthand for |observing_items_|, which tracks all items that this is | |
| 111 // observing so that RemoveObserver will be called for all of them. | |
| 112 typedef std::set<content::DownloadItem*> DownloadSet; | |
| 113 | |
| 114 // Convenience method to call |main_notifier_->GetManager()| while | |
| 115 // null-checking |main_notifier_|. | |
| 116 content::DownloadManager* GetMainNotifierManager() const; | |
| 117 | |
| 118 // Convenience method to call |original_notifier_->GetManager()| while | |
| 119 // null-checking |original_notifier_|. | |
| 120 content::DownloadManager* GetOriginalNotifierManager() const; | |
| 121 | |
| 122 // Sends the current list of downloads to the page. | |
| 123 void SendCurrentDownloads(); | |
| 124 | |
| 125 // Displays a native prompt asking the user for confirmation after accepting | |
| 126 // the dangerous download specified by |dangerous|. The function returns | |
| 127 // immediately, and will invoke DangerPromptAccepted() asynchronously if the | |
| 128 // user accepts the dangerous download. The native prompt will observe | |
| 129 // |dangerous| until either the dialog is dismissed or |dangerous| is no | |
| 130 // longer an in-progress dangerous download. | |
| 131 void ShowDangerPrompt(content::DownloadItem* dangerous); | |
| 132 | |
| 133 // Conveys danger acceptance from the DownloadDangerPrompt to the | |
| 134 // DownloadItem. | |
| 135 void DangerPromptDone(int download_id, DownloadDangerPrompt::Action action); | |
| 136 | |
| 137 // Returns true if the records of any downloaded items are allowed (and able) | |
| 138 // to be deleted. | |
| 139 bool IsDeletingHistoryAllowed(); | |
| 140 | |
| 141 // Returns the download that is referred to in a given value. | |
| 142 content::DownloadItem* GetDownloadByValue(const base::ListValue* args); | |
| 143 | |
| 144 // Returns the download with |id| or NULL if it doesn't exist. | |
| 145 content::DownloadItem* GetDownloadById(uint32_t id); | |
| 146 | |
| 147 // Remove all downloads in |to_remove| with the ability to undo removal later. | |
| 148 void RemoveDownloads(const std::vector<content::DownloadItem*>& to_remove); | |
| 149 | |
| 150 // Weak reference to the DownloadManager this class was constructed with. You | |
| 151 // should probably be using use Get{Main,Original}NotifierManager() instead. | |
| 152 content::DownloadManager* download_manager_; | |
| 153 | |
| 154 // Current search terms. | |
| 155 scoped_ptr<base::ListValue> search_terms_; | |
| 156 | |
| 157 // Notifies OnDownload*() and provides safe access to the DownloadManager. | |
| 158 scoped_ptr<AllDownloadItemNotifier> main_notifier_; | |
| 159 | |
| 160 // If |main_notifier_| observes an incognito profile, then this observes the | |
| 161 // DownloadManager for the original profile; otherwise, this is NULL. | |
| 162 scoped_ptr<AllDownloadItemNotifier> original_notifier_; | |
| 163 | |
| 164 // IDs of downloads to remove when this handler gets deleted. | |
| 165 std::vector<std::set<uint32_t>> removals_; | |
| 166 | |
| 167 // Whether a call to SendCurrentDownloads() is currently scheduled. | |
| 168 bool update_scheduled_; | |
| 169 | |
| 170 // IDs of new downloads that the page doesn't know about yet. | |
| 171 std::set<uint32_t> new_downloads_; | |
| 172 | |
| 173 base::WeakPtrFactory<DownloadsDOMHandler> weak_ptr_factory_; | |
| 174 | |
| 175 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); | |
| 176 }; | |
| 177 | |
| 178 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ | |
| OLD | NEW |