| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_MD_DOWNLOADS_DOM_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_MD_DOWNLOADS_DOM_HANDLER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_MD_DOWNLOADS_DOM_HANDLER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_MD_DOWNLOADS_DOM_HANDLER_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/browser/download/all_download_item_notifier.h" |
| 13 #include "chrome/browser/download/download_danger_prompt.h" | 15 #include "chrome/browser/download/download_danger_prompt.h" |
| 14 #include "chrome/browser/ui/webui/md_downloads/downloads_list_tracker.h" | 16 #include "content/public/browser/download_item.h" |
| 17 #include "content/public/browser/download_manager.h" |
| 15 #include "content/public/browser/web_ui_message_handler.h" | 18 #include "content/public/browser/web_ui_message_handler.h" |
| 16 | 19 |
| 17 namespace base { | 20 namespace base { |
| 18 class ListValue; | 21 class ListValue; |
| 19 } | 22 } |
| 20 | 23 |
| 21 namespace content { | 24 namespace content { |
| 22 class DownloadItem; | |
| 23 class DownloadManager; | |
| 24 class RenderViewHost; | |
| 25 class WebContents; | 25 class WebContents; |
| 26 class WebUI; | |
| 27 } | 26 } |
| 28 | 27 |
| 29 // The handler for Javascript messages related to the "downloads" view, | 28 // The handler for Javascript messages related to the "downloads" view, |
| 30 // also observes changes to the download manager. | 29 // also observes changes to the download manager. |
| 31 class MdDownloadsDOMHandler : public content::WebUIMessageHandler { | 30 class MdDownloadsDOMHandler : public content::WebUIMessageHandler, |
| 31 public AllDownloadItemNotifier::Observer { |
| 32 public: | 32 public: |
| 33 MdDownloadsDOMHandler(content::DownloadManager* download_manager, | 33 explicit MdDownloadsDOMHandler(content::DownloadManager* download_manager); |
| 34 content::WebUI* web_ui); | |
| 35 ~MdDownloadsDOMHandler() override; | 34 ~MdDownloadsDOMHandler() override; |
| 36 | 35 |
| 36 void Init(); |
| 37 |
| 37 // WebUIMessageHandler implementation. | 38 // WebUIMessageHandler implementation. |
| 38 void RegisterMessages() override; | 39 void RegisterMessages() override; |
| 39 | 40 |
| 40 void RenderViewReused(content::RenderViewHost* render_view_host); | 41 // AllDownloadItemNotifier::Observer interface |
| 42 void OnDownloadCreated(content::DownloadManager* manager, |
| 43 content::DownloadItem* download_item) override; |
| 44 void OnDownloadUpdated(content::DownloadManager* manager, |
| 45 content::DownloadItem* download_item) override; |
| 46 void OnDownloadRemoved(content::DownloadManager* manager, |
| 47 content::DownloadItem* download_item) override; |
| 41 | 48 |
| 42 // Callback for the "getDownloads" message. | 49 // Callback for the "getDownloads" message. |
| 43 void HandleGetDownloads(const base::ListValue* args); | 50 void HandleGetDownloads(const base::ListValue* args); |
| 44 | 51 |
| 45 // Callback for the "openFile" message - opens the file in the shell. | 52 // Callback for the "openFile" message - opens the file in the shell. |
| 46 void HandleOpenFile(const base::ListValue* args); | 53 void HandleOpenFile(const base::ListValue* args); |
| 47 | 54 |
| 48 // Callback for the "drag" message - initiates a file object drag. | 55 // Callback for the "drag" message - initiates a file object drag. |
| 49 void HandleDrag(const base::ListValue* args); | 56 void HandleDrag(const base::ListValue* args); |
| 50 | 57 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 80 | 87 |
| 81 // Callback for the "openDownloadsFolder" message - opens the downloads | 88 // Callback for the "openDownloadsFolder" message - opens the downloads |
| 82 // folder. | 89 // folder. |
| 83 void HandleOpenDownloadsFolder(const base::ListValue* args); | 90 void HandleOpenDownloadsFolder(const base::ListValue* args); |
| 84 | 91 |
| 85 protected: | 92 protected: |
| 86 // These methods are for mocking so that most of this class does not actually | 93 // These methods are for mocking so that most of this class does not actually |
| 87 // depend on WebUI. The other methods that depend on WebUI are | 94 // depend on WebUI. The other methods that depend on WebUI are |
| 88 // RegisterMessages() and HandleDrag(). | 95 // RegisterMessages() and HandleDrag(). |
| 89 virtual content::WebContents* GetWebUIWebContents(); | 96 virtual content::WebContents* GetWebUIWebContents(); |
| 97 virtual void CallUpdateAll(const base::ListValue& list); |
| 98 virtual void CallUpdateItem(const base::DictionaryValue& item); |
| 99 |
| 100 // Schedules a call to SendCurrentDownloads() in the next message loop |
| 101 // iteration. Protected rather than private for use in tests. |
| 102 void ScheduleSendCurrentDownloads(); |
| 90 | 103 |
| 91 // Actually remove downloads with an ID in |removals_|. This cannot be undone. | 104 // Actually remove downloads with an ID in |removals_|. This cannot be undone. |
| 92 void FinalizeRemovals(); | 105 void FinalizeRemovals(); |
| 93 | 106 |
| 94 private: | 107 private: |
| 95 using IdSet = std::set<uint32>; | 108 using IdSet = std::set<uint32>; |
| 96 using DownloadVector = std::vector<content::DownloadItem*>; | 109 using DownloadVector = std::vector<content::DownloadItem*>; |
| 97 | 110 |
| 98 // Convenience method to call |main_notifier_->GetManager()| while | 111 // Convenience method to call |main_notifier_->GetManager()| while |
| 99 // null-checking |main_notifier_|. | 112 // null-checking |main_notifier_|. |
| 100 content::DownloadManager* GetMainNotifierManager() const; | 113 content::DownloadManager* GetMainNotifierManager() const; |
| 101 | 114 |
| 102 // Convenience method to call |original_notifier_->GetManager()| while | 115 // Convenience method to call |original_notifier_->GetManager()| while |
| 103 // null-checking |original_notifier_|. | 116 // null-checking |original_notifier_|. |
| 104 content::DownloadManager* GetOriginalNotifierManager() const; | 117 content::DownloadManager* GetOriginalNotifierManager() const; |
| 105 | 118 |
| 119 // Sends the current list of downloads to the page. |
| 120 void SendCurrentDownloads(); |
| 121 |
| 106 // Displays a native prompt asking the user for confirmation after accepting | 122 // Displays a native prompt asking the user for confirmation after accepting |
| 107 // the dangerous download specified by |dangerous|. The function returns | 123 // the dangerous download specified by |dangerous|. The function returns |
| 108 // immediately, and will invoke DangerPromptAccepted() asynchronously if the | 124 // immediately, and will invoke DangerPromptAccepted() asynchronously if the |
| 109 // user accepts the dangerous download. The native prompt will observe | 125 // user accepts the dangerous download. The native prompt will observe |
| 110 // |dangerous| until either the dialog is dismissed or |dangerous| is no | 126 // |dangerous| until either the dialog is dismissed or |dangerous| is no |
| 111 // longer an in-progress dangerous download. | 127 // longer an in-progress dangerous download. |
| 112 void ShowDangerPrompt(content::DownloadItem* dangerous); | 128 void ShowDangerPrompt(content::DownloadItem* dangerous); |
| 113 | 129 |
| 114 // Conveys danger acceptance from the DownloadDangerPrompt to the | 130 // Conveys danger acceptance from the DownloadDangerPrompt to the |
| 115 // DownloadItem. | 131 // DownloadItem. |
| 116 void DangerPromptDone(int download_id, DownloadDangerPrompt::Action action); | 132 void DangerPromptDone(int download_id, DownloadDangerPrompt::Action action); |
| 117 | 133 |
| 118 // Returns true if the records of any downloaded items are allowed (and able) | 134 // Returns true if the records of any downloaded items are allowed (and able) |
| 119 // to be deleted. | 135 // to be deleted. |
| 120 bool IsDeletingHistoryAllowed(); | 136 bool IsDeletingHistoryAllowed(); |
| 121 | 137 |
| 122 // Returns the download that is referred to in a given value. | 138 // Returns the download that is referred to in a given value. |
| 123 content::DownloadItem* GetDownloadByValue(const base::ListValue* args); | 139 content::DownloadItem* GetDownloadByValue(const base::ListValue* args); |
| 124 | 140 |
| 125 // Returns the download with |id| or NULL if it doesn't exist. | 141 // Returns the download with |id| or NULL if it doesn't exist. |
| 126 content::DownloadItem* GetDownloadById(uint32 id); | 142 content::DownloadItem* GetDownloadById(uint32 id); |
| 127 | 143 |
| 128 // Remove all downloads in |to_remove| with the ability to undo removal later. | 144 // Remove all downloads in |to_remove| with the ability to undo removal later. |
| 129 void RemoveDownloads(const DownloadVector& to_remove); | 145 void RemoveDownloads(const DownloadVector& to_remove); |
| 130 | 146 |
| 131 DownloadsListTracker list_tracker_; | 147 // Weak reference to the DownloadManager this class was constructed with. You |
| 148 // should probably be using use Get{Main,Original}NotifierManager() instead. |
| 149 content::DownloadManager* download_manager_; |
| 150 |
| 151 // Current search terms. |
| 152 scoped_ptr<base::ListValue> search_terms_; |
| 153 |
| 154 // Notifies OnDownload*() and provides safe access to the DownloadManager. |
| 155 scoped_ptr<AllDownloadItemNotifier> main_notifier_; |
| 156 |
| 157 // If |main_notifier_| observes an incognito profile, then this observes the |
| 158 // DownloadManager for the original profile; otherwise, this is NULL. |
| 159 scoped_ptr<AllDownloadItemNotifier> original_notifier_; |
| 132 | 160 |
| 133 // IDs of downloads to remove when this handler gets deleted. | 161 // IDs of downloads to remove when this handler gets deleted. |
| 134 std::vector<IdSet> removals_; | 162 std::vector<IdSet> removals_; |
| 135 | 163 |
| 164 // Whether a call to SendCurrentDownloads() is currently scheduled. |
| 165 bool update_scheduled_; |
| 166 |
| 167 // IDs of new downloads that the page doesn't know about yet. |
| 168 IdSet new_downloads_; |
| 169 |
| 136 base::WeakPtrFactory<MdDownloadsDOMHandler> weak_ptr_factory_; | 170 base::WeakPtrFactory<MdDownloadsDOMHandler> weak_ptr_factory_; |
| 137 | 171 |
| 138 DISALLOW_COPY_AND_ASSIGN(MdDownloadsDOMHandler); | 172 DISALLOW_COPY_AND_ASSIGN(MdDownloadsDOMHandler); |
| 139 }; | 173 }; |
| 140 | 174 |
| 141 #endif // CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_MD_DOWNLOADS_DOM_HANDLER_H_ | 175 #endif // CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_MD_DOWNLOADS_DOM_HANDLER_H_ |
| OLD | NEW |