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 <set> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "base/memory/weak_ptr.h" 13 #include "chrome/browser/download/all_download_item_notifier.h" 14 #include "chrome/browser/download/download_danger_prompt.h" 15 #include "content/public/browser/download_item.h" 16 #include "content/public/browser/download_manager.h" 17 #include "content/public/browser/web_ui_message_handler.h" 18 19 namespace base { 20 class ListValue; 21 } 22 23 namespace content { 24 class WebContents; 25 } 26 27 // The handler for Javascript messages related to the "downloads" view, 28 // also observes changes to the download manager. 29 class DownloadsDOMHandler : public content::WebUIMessageHandler, 30 public AllDownloadItemNotifier::Observer { 31 public: 32 explicit DownloadsDOMHandler(content::DownloadManager* dlm); 33 virtual ~DownloadsDOMHandler(); 34 35 void Init(); 36 37 // WebUIMessageHandler implementation. 38 virtual void RegisterMessages() OVERRIDE; 39 40 // AllDownloadItemNotifier::Observer interface 41 virtual void OnDownloadCreated( 42 content::DownloadManager* manager, 43 content::DownloadItem* download_item) OVERRIDE; 44 virtual void OnDownloadUpdated( 45 content::DownloadManager* manager, 46 content::DownloadItem* download_item) OVERRIDE; 47 virtual void OnDownloadRemoved( 48 content::DownloadManager* manager, 49 content::DownloadItem* download_item) OVERRIDE; 50 51 // Callback for the "onPageLoaded" message. 52 void OnPageLoaded(const base::ListValue* args); 53 54 // Callback for the "getDownloads" message. 55 void HandleGetDownloads(const base::ListValue* args); 56 57 // Callback for the "openFile" message - opens the file in the shell. 58 void HandleOpenFile(const base::ListValue* args); 59 60 // Callback for the "drag" message - initiates a file object drag. 61 void HandleDrag(const base::ListValue* args); 62 63 // Callback for the "saveDangerous" message - specifies that the user 64 // wishes to save a dangerous file. 65 void HandleSaveDangerous(const base::ListValue* args); 66 67 // Callback for the "discardDangerous" message - specifies that the user 68 // wishes to discard (remove) a dangerous file. 69 void HandleDiscardDangerous(const base::ListValue* args); 70 71 // Callback for the "show" message - shows the file in explorer. 72 void HandleShow(const base::ListValue* args); 73 74 // Callback for the "pause" message - pauses the file download. 75 void HandlePause(const base::ListValue* args); 76 77 // Callback for the "resume" message - resumes the file download. 78 void HandleResume(const base::ListValue* args); 79 80 // Callback for the "remove" message - removes the file download from shelf 81 // and list. 82 void HandleRemove(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 CallDownloadsList(const base::ListValue& downloads); 100 virtual void CallDownloadUpdated(const base::ListValue& download); 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 private: 107 // Shorthand for |observing_items_|, which tracks all items that this is 108 // observing so that RemoveObserver will be called for all of them. 109 typedef std::set<content::DownloadItem*> DownloadSet; 110 111 // Sends the current list of downloads to the page. 112 void SendCurrentDownloads(); 113 114 // Displays a native prompt asking the user for confirmation after accepting 115 // the dangerous download specified by |dangerous|. The function returns 116 // immediately, and will invoke DangerPromptAccepted() asynchronously if the 117 // user accepts the dangerous download. The native prompt will observe 118 // |dangerous| until either the dialog is dismissed or |dangerous| is no 119 // longer an in-progress dangerous download. 120 void ShowDangerPrompt(content::DownloadItem* dangerous); 121 122 // Conveys danger acceptance from the DownloadDangerPrompt to the 123 // DownloadItem. 124 void DangerPromptDone(int download_id, DownloadDangerPrompt::Action action); 125 126 // Returns true if the records of any downloaded items are allowed (and able) 127 // to be deleted. 128 bool IsDeletingHistoryAllowed(); 129 130 // Returns the download that is referred to in a given value. 131 content::DownloadItem* GetDownloadByValue(const base::ListValue* args); 132 133 // Current search terms. 134 scoped_ptr<base::ListValue> search_terms_; 135 136 // Notifies OnDownload*() and provides safe access to the DownloadManager. 137 AllDownloadItemNotifier main_notifier_; 138 139 // If |main_notifier_| observes an incognito profile, then this observes the 140 // DownloadManager for the original profile; otherwise, this is NULL. 141 scoped_ptr<AllDownloadItemNotifier> original_notifier_; 142 143 // Whether a call to SendCurrentDownloads() is currently scheduled. 144 bool update_scheduled_; 145 146 base::WeakPtrFactory<DownloadsDOMHandler> weak_ptr_factory_; 147 148 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); 149 }; 150 151 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ 152