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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_
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"
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
{
32 explicit DownloadsDOMHandler(content::DownloadManager
* download_manager
);
33 ~DownloadsDOMHandler() override
;
37 // WebUIMessageHandler implementation.
38 void RegisterMessages() override
;
40 // AllDownloadItemNotifier::Observer interface
41 void OnDownloadCreated(content::DownloadManager
* manager
,
42 content::DownloadItem
* download_item
) override
;
43 void OnDownloadUpdated(content::DownloadManager
* manager
,
44 content::DownloadItem
* download_item
) override
;
45 void OnDownloadRemoved(content::DownloadManager
* manager
,
46 content::DownloadItem
* download_item
) override
;
48 // Callback for the "onPageLoaded" message.
49 void OnPageLoaded(const base::ListValue
* args
);
51 // Callback for the "getDownloads" message.
52 void HandleGetDownloads(const base::ListValue
* args
);
54 // Callback for the "openFile" message - opens the file in the shell.
55 void HandleOpenFile(const base::ListValue
* args
);
57 // Callback for the "drag" message - initiates a file object drag.
58 void HandleDrag(const base::ListValue
* args
);
60 // Callback for the "saveDangerous" message - specifies that the user
61 // wishes to save a dangerous file.
62 void HandleSaveDangerous(const base::ListValue
* args
);
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
);
68 // Callback for the "show" message - shows the file in explorer.
69 void HandleShow(const base::ListValue
* args
);
71 // Callback for the "pause" message - pauses the file download.
72 void HandlePause(const base::ListValue
* args
);
74 // Callback for the "resume" message - resumes the file download.
75 void HandleResume(const base::ListValue
* args
);
77 // Callback for the "remove" message - removes the file download from shelf
79 void HandleRemove(const base::ListValue
* args
);
81 // Callback for the "undo" message. Currently only undoes removals.
82 void HandleUndo(const base::ListValue
* args
);
84 // Callback for the "cancel" message - cancels the download.
85 void HandleCancel(const base::ListValue
* args
);
87 // Callback for the "clearAll" message - clears all the downloads.
88 void HandleClearAll(const base::ListValue
* args
);
90 // Callback for the "openDownloadsFolder" message - opens the downloads
92 void HandleOpenDownloadsFolder(const base::ListValue
* args
);
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
);
102 // Schedules a call to SendCurrentDownloads() in the next message loop
103 // iteration. Protected rather than private for use in tests.
104 void ScheduleSendCurrentDownloads();
106 // Actually remove downloads with an ID in |removals_|. This cannot be undone.
107 void FinalizeRemovals();
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
;
114 // Convenience method to call |main_notifier_->GetManager()| while
115 // null-checking |main_notifier_|.
116 content::DownloadManager
* GetMainNotifierManager() const;
118 // Convenience method to call |original_notifier_->GetManager()| while
119 // null-checking |original_notifier_|.
120 content::DownloadManager
* GetOriginalNotifierManager() const;
122 // Sends the current list of downloads to the page.
123 void SendCurrentDownloads();
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
);
133 // Conveys danger acceptance from the DownloadDangerPrompt to the
135 void DangerPromptDone(int download_id
, DownloadDangerPrompt::Action action
);
137 // Returns true if the records of any downloaded items are allowed (and able)
139 bool IsDeletingHistoryAllowed();
141 // Returns the download that is referred to in a given value.
142 content::DownloadItem
* GetDownloadByValue(const base::ListValue
* args
);
144 // Returns the download with |id| or NULL if it doesn't exist.
145 content::DownloadItem
* GetDownloadById(uint32 id
);
147 // Remove all downloads in |to_remove| with the ability to undo removal later.
148 void RemoveDownloads(const std::vector
<content::DownloadItem
*>& to_remove
);
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_
;
154 // Current search terms.
155 scoped_ptr
<base::ListValue
> search_terms_
;
157 // Notifies OnDownload*() and provides safe access to the DownloadManager.
158 scoped_ptr
<AllDownloadItemNotifier
> main_notifier_
;
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_
;
164 // IDs of downloads to remove when this handler gets deleted.
165 std::vector
<std::set
<uint32
>> removals_
;
167 // Whether a call to SendCurrentDownloads() is currently scheduled.
168 bool update_scheduled_
;
170 // IDs of new downloads that the page doesn't know about yet.
171 std::set
<uint32
> new_downloads_
;
173 base::WeakPtrFactory
<DownloadsDOMHandler
> weak_ptr_factory_
;
175 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler
);
178 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_