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
* dlm
);
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 // Protected for testing.
107 virtual content::DownloadManager
* GetMainNotifierManager();
109 // Actually remove downloads with an ID in |removals_|. This cannot be undone.
110 void FinalizeRemovals();
113 // Shorthand for |observing_items_|, which tracks all items that this is
114 // observing so that RemoveObserver will be called for all of them.
115 typedef std::set
<content::DownloadItem
*> DownloadSet
;
117 // Sends the current list of downloads to the page.
118 void SendCurrentDownloads();
120 // Displays a native prompt asking the user for confirmation after accepting
121 // the dangerous download specified by |dangerous|. The function returns
122 // immediately, and will invoke DangerPromptAccepted() asynchronously if the
123 // user accepts the dangerous download. The native prompt will observe
124 // |dangerous| until either the dialog is dismissed or |dangerous| is no
125 // longer an in-progress dangerous download.
126 void ShowDangerPrompt(content::DownloadItem
* dangerous
);
128 // Conveys danger acceptance from the DownloadDangerPrompt to the
130 void DangerPromptDone(int download_id
, DownloadDangerPrompt::Action action
);
132 // Returns true if the records of any downloaded items are allowed (and able)
134 bool IsDeletingHistoryAllowed();
136 // Returns the download that is referred to in a given value.
137 content::DownloadItem
* GetDownloadByValue(const base::ListValue
* args
);
139 // Returns the download with |id| or NULL if it doesn't exist.
140 content::DownloadItem
* GetDownloadById(uint32 id
);
142 // Remove all downloads in |to_remove| with the ability to undo removal later.
143 void RemoveDownloads(const std::vector
<content::DownloadItem
*>& to_remove
);
145 // Current search terms.
146 scoped_ptr
<base::ListValue
> search_terms_
;
148 // Notifies OnDownload*() and provides safe access to the DownloadManager.
149 AllDownloadItemNotifier main_notifier_
;
151 // If |main_notifier_| observes an incognito profile, then this observes the
152 // DownloadManager for the original profile; otherwise, this is NULL.
153 scoped_ptr
<AllDownloadItemNotifier
> original_notifier_
;
155 // IDs of downloads to remove when this handler gets deleted.
156 std::vector
<std::set
<uint32
>> removals_
;
158 // Whether a call to SendCurrentDownloads() is currently scheduled.
159 bool update_scheduled_
;
161 // IDs of new downloads that the page doesn't know about yet.
162 std::set
<uint32
> new_downloads_
;
164 base::WeakPtrFactory
<DownloadsDOMHandler
> weak_ptr_factory_
;
166 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler
);
169 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_