diff options
Diffstat (limited to 'chrome/browser/dom_ui/downloads_ui.cc')
-rw-r--r-- | chrome/browser/dom_ui/downloads_ui.cc | 134 |
1 files changed, 109 insertions, 25 deletions
diff --git a/chrome/browser/dom_ui/downloads_ui.cc b/chrome/browser/dom_ui/downloads_ui.cc index f8d9515..5ee67bd 100644 --- a/chrome/browser/dom_ui/downloads_ui.cc +++ b/chrome/browser/dom_ui/downloads_ui.cc @@ -9,7 +9,9 @@ #include "base/thread.h" #include "base/time_format.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/dom_ui/chrome_url_data_manager.h" #include "chrome/browser/dom_ui/fileicon_source.h" +#include "chrome/browser/download/download_manager.h" #if defined(OS_WIN) // TODO(port): re-enable when download_util is ported #include "chrome/browser/download/download_util.h" @@ -19,11 +21,11 @@ #include "chrome/common/jstemplate_builder.h" #include "chrome/common/l10n_util.h" #include "chrome/common/time_format.h" +#include "chrome/common/url_constants.h" #include "grit/browser_resources.h" #include "grit/generated_resources.h" -// DownloadsUI is accessible from chrome-ui://downloads. -static const char kDownloadsHost[] = "downloads"; +namespace { // Maximum number of downloads to show. TODO(glen): Remove this and instead // stuff the downloads down the pipe slowly. @@ -35,8 +37,23 @@ static const int kMaxDownloads = 150; // /////////////////////////////////////////////////////////////////////////////// +class DownloadsUIHTMLSource : public ChromeURLDataManager::DataSource { + public: + DownloadsUIHTMLSource(); + + // Called when the network layer has requested a resource underneath + // the path we registered. + virtual void StartDataRequest(const std::string& path, int request_id); + virtual std::string GetMimeType(const std::string&) const { + return "text/html"; + } + + private: + DISALLOW_COPY_AND_ASSIGN(DownloadsUIHTMLSource); +}; + DownloadsUIHTMLSource::DownloadsUIHTMLSource() - : DataSource(kDownloadsHost, MessageLoop::current()) { + : DataSource(chrome::kChromeUIDownloadsHost, MessageLoop::current()) { } void DownloadsUIHTMLSource::StartDataRequest(const std::string& path, @@ -100,6 +117,82 @@ void DownloadsUIHTMLSource::StartDataRequest(const std::string& path, // /////////////////////////////////////////////////////////////////////////////// +// The handler for Javascript messages related to the "downloads" view, +// also observes changes to the download manager. +class DownloadsDOMHandler : public DOMMessageHandler, + public DownloadManager::Observer, + public DownloadItem::Observer { + public: + explicit DownloadsDOMHandler(DOMUI* dom_ui, DownloadManager* dlm); + virtual ~DownloadsDOMHandler(); + + void Init(); + + // DownloadItem::Observer interface + virtual void OnDownloadUpdated(DownloadItem* download); + + // DownloadManager::Observer interface + virtual void ModelChanged(); + virtual void SetDownloads(std::vector<DownloadItem*>& downloads); + + // Callback for the "getDownloads" message. + void HandleGetDownloads(const Value* value); + + // Callback for the "openFile" message - opens the file in the shell. + void HandleOpenFile(const Value* value); + + // Callback for the "drag" message - initiates a file object drag. + void HandleDrag(const Value* value); + + // Callback for the "saveDangerous" message - specifies that the user + // wishes to save a dangerous file. + void HandleSaveDangerous(const Value* value); + + // Callback for the "discardDangerous" message - specifies that the user + // wishes to discard (remove) a dangerous file. + void HandleDiscardDangerous(const Value* value); + + // Callback for the "show" message - shows the file in explorer. + void HandleShow(const Value* value); + + // Callback for the "pause" message - pauses the file download. + void HandlePause(const Value* value); + + // Callback for the "cancel" message - cancels the download. + void HandleCancel(const Value* value); + + private: + // Send the current list of downloads to the page. + void SendCurrentDownloads(); + + // Creates a representation of a download in a format that the downloads + // HTML page can understand. + DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id); + + // Clear all download items and their observers. + void ClearDownloadItems(); + + // Return the download that corresponds to a given id. + DownloadItem* GetDownloadById(int id); + + // Return the download that is referred to in a given value. + DownloadItem* GetDownloadByValue(const Value* value); + + // Current search text. + std::wstring search_text_; + + // Our model + DownloadManager* download_manager_; + + // The current set of visible DownloadItems for this view received from the + // DownloadManager. DownloadManager owns the DownloadItems. The vector is + // kept in order, sorted by ascending start time. + typedef std::vector<DownloadItem*> OrderedDownloads; + OrderedDownloads download_items_; + + DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); +}; + // Sort DownloadItems into descending order by their start time. class DownloadItemSorter : public std::binary_function<DownloadItem*, DownloadItem*, @@ -120,20 +213,20 @@ DownloadsDOMHandler::DownloadsDOMHandler(DOMUI* dom_ui, DownloadManager* dlm) NewCallback(this, &DownloadsDOMHandler::HandleOpenFile)); dom_ui_->RegisterMessageCallback("drag", - NewCallback(this, &DownloadsDOMHandler::HandleDrag)); + NewCallback(this, &DownloadsDOMHandler::HandleDrag)); dom_ui_->RegisterMessageCallback("saveDangerous", - NewCallback(this, &DownloadsDOMHandler::HandleSaveDangerous)); + NewCallback(this, &DownloadsDOMHandler::HandleSaveDangerous)); dom_ui_->RegisterMessageCallback("discardDangerous", - NewCallback(this, &DownloadsDOMHandler::HandleDiscardDangerous)); + NewCallback(this, &DownloadsDOMHandler::HandleDiscardDangerous)); dom_ui_->RegisterMessageCallback("show", - NewCallback(this, &DownloadsDOMHandler::HandleShow)); + NewCallback(this, &DownloadsDOMHandler::HandleShow)); dom_ui_->RegisterMessageCallback("togglepause", - NewCallback(this, &DownloadsDOMHandler::HandlePause)); + NewCallback(this, &DownloadsDOMHandler::HandlePause)); dom_ui_->RegisterMessageCallback("resume", - NewCallback(this, &DownloadsDOMHandler::HandlePause)); + NewCallback(this, &DownloadsDOMHandler::HandlePause)); dom_ui_->RegisterMessageCallback("cancel", - NewCallback(this, &DownloadsDOMHandler::HandleCancel)); + NewCallback(this, &DownloadsDOMHandler::HandleCancel)); // Create our fileicon data source. g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, @@ -159,8 +252,8 @@ void DownloadsDOMHandler::OnDownloadUpdated(DownloadItem* download) { // errors between the UI and the download_items_ list (we may wish to use // something other than 'id'). OrderedDownloads::iterator it = find(download_items_.begin(), - download_items_.end(), - download); + download_items_.end(), + download); if (it == download_items_.end()) return; const int id = static_cast<int>(it - download_items_.begin()); @@ -346,17 +439,16 @@ DownloadItem* DownloadsDOMHandler::GetDownloadByValue(const Value* value) { return NULL; } +} // namespace + /////////////////////////////////////////////////////////////////////////////// // // DownloadsUI // /////////////////////////////////////////////////////////////////////////////// -DownloadsUI::DownloadsUI(DOMUIContents* contents) : DOMUI(contents) { -} - -void DownloadsUI::Init() { - DownloadManager* dlm = get_profile()->GetDownloadManager(); +DownloadsUI::DownloadsUI(WebContents* contents) : DOMUI(contents) { + DownloadManager* dlm = GetProfile()->GetDownloadManager(); DownloadsDOMHandler* handler = new DownloadsDOMHandler(this, dlm); AddMessageHandler(handler); @@ -370,11 +462,3 @@ void DownloadsUI::Init() { &ChromeURLDataManager::AddDataSource, html_source)); } - -// static -GURL DownloadsUI::GetBaseURL() { - std::string url = DOMUIContents::GetScheme(); - url += "://"; - url += kDownloadsHost; - return GURL(url); -} |