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 virtual ~DownloadsDOMHandler();
37 // WebUIMessageHandler implementation.
38 virtual void RegisterMessages() OVERRIDE
;
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
;
51 // Callback for the "onPageLoaded" message.
52 void OnPageLoaded(const base::ListValue
* args
);
54 // Callback for the "getDownloads" message.
55 void HandleGetDownloads(const base::ListValue
* args
);
57 // Callback for the "openFile" message - opens the file in the shell.
58 void HandleOpenFile(const base::ListValue
* args
);
60 // Callback for the "drag" message - initiates a file object drag.
61 void HandleDrag(const base::ListValue
* args
);
63 // Callback for the "saveDangerous" message - specifies that the user
64 // wishes to save a dangerous file.
65 void HandleSaveDangerous(const base::ListValue
* args
);
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
);
71 // Callback for the "show" message - shows the file in explorer.
72 void HandleShow(const base::ListValue
* args
);
74 // Callback for the "pause" message - pauses the file download.
75 void HandlePause(const base::ListValue
* args
);
77 // Callback for the "resume" message - resumes the file download.
78 void HandleResume(const base::ListValue
* args
);
80 // Callback for the "remove" message - removes the file download from shelf
82 void HandleRemove(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 CallDownloadsList(const base::ListValue
& downloads
);
100 virtual void CallDownloadUpdated(const base::ListValue
& download
);
102 // Schedules a call to SendCurrentDownloads() in the next message loop
103 // iteration. Protected rather than private for use in tests.
104 void ScheduleSendCurrentDownloads();
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
;
111 // Sends the current list of downloads to the page.
112 void SendCurrentDownloads();
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
);
122 // Conveys danger acceptance from the DownloadDangerPrompt to the
124 void DangerPromptDone(int download_id
, DownloadDangerPrompt::Action action
);
126 // Returns true if the records of any downloaded items are allowed (and able)
128 bool IsDeletingHistoryAllowed();
130 // Returns the download that is referred to in a given value.
131 content::DownloadItem
* GetDownloadByValue(const base::ListValue
* args
);
133 // Current search terms.
134 scoped_ptr
<base::ListValue
> search_terms_
;
136 // Notifies OnDownload*() and provides safe access to the DownloadManager.
137 AllDownloadItemNotifier main_notifier_
;
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_
;
143 // Whether a call to SendCurrentDownloads() is currently scheduled.
144 bool update_scheduled_
;
146 base::WeakPtrFactory
<DownloadsDOMHandler
> weak_ptr_factory_
;
148 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler
);
151 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_