Home | History | Annotate | Download | only in webui
      1 // Copyright (c) 2011 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 #pragma once
      8 
      9 #include <vector>
     10 
     11 #include "base/memory/scoped_callback_factory.h"
     12 #include "chrome/browser/download/download_item.h"
     13 #include "chrome/browser/download/download_manager.h"
     14 #include "content/browser/webui/web_ui.h"
     15 
     16 class ListValue;
     17 
     18 // The handler for Javascript messages related to the "downloads" view,
     19 // also observes changes to the download manager.
     20 class DownloadsDOMHandler : public WebUIMessageHandler,
     21                             public DownloadManager::Observer,
     22                             public DownloadItem::Observer {
     23  public:
     24   explicit DownloadsDOMHandler(DownloadManager* dlm);
     25   virtual ~DownloadsDOMHandler();
     26 
     27   void Init();
     28 
     29   // WebUIMessageHandler implementation.
     30   virtual void RegisterMessages();
     31 
     32   // DownloadItem::Observer interface
     33   virtual void OnDownloadUpdated(DownloadItem* download);
     34   virtual void OnDownloadOpened(DownloadItem* download) { }
     35 
     36   // DownloadManager::Observer interface
     37   virtual void ModelChanged();
     38 
     39   // Callback for the "getDownloads" message.
     40   void HandleGetDownloads(const ListValue* args);
     41 
     42   // Callback for the "openFile" message - opens the file in the shell.
     43   void HandleOpenFile(const ListValue* args);
     44 
     45   // Callback for the "drag" message - initiates a file object drag.
     46   void HandleDrag(const ListValue* args);
     47 
     48   // Callback for the "saveDangerous" message - specifies that the user
     49   // wishes to save a dangerous file.
     50   void HandleSaveDangerous(const ListValue* args);
     51 
     52   // Callback for the "discardDangerous" message - specifies that the user
     53   // wishes to discard (remove) a dangerous file.
     54   void HandleDiscardDangerous(const ListValue* args);
     55 
     56   // Callback for the "show" message - shows the file in explorer.
     57   void HandleShow(const ListValue* args);
     58 
     59   // Callback for the "pause" message - pauses the file download.
     60   void HandlePause(const ListValue* args);
     61 
     62   // Callback for the "remove" message - removes the file download from shelf
     63   // and list.
     64   void HandleRemove(const ListValue* args);
     65 
     66   // Callback for the "cancel" message - cancels the download.
     67   void HandleCancel(const ListValue* args);
     68 
     69   // Callback for the "clearAll" message - clears all the downloads.
     70   void HandleClearAll(const ListValue* args);
     71 
     72  private:
     73   // Send the current list of downloads to the page.
     74   void SendCurrentDownloads();
     75 
     76   // Clear all download items and their observers.
     77   void ClearDownloadItems();
     78 
     79   // Return the download that corresponds to a given id.
     80   DownloadItem* GetDownloadById(int id);
     81 
     82   // Return the download that is referred to in a given value.
     83   DownloadItem* GetDownloadByValue(const ListValue* args);
     84 
     85   // Current search text.
     86   std::wstring search_text_;
     87 
     88   // Our model
     89   DownloadManager* download_manager_;
     90 
     91   // The current set of visible DownloadItems for this view received from the
     92   // DownloadManager. DownloadManager owns the DownloadItems. The vector is
     93   // kept in order, sorted by ascending start time.
     94   typedef std::vector<DownloadItem*> OrderedDownloads;
     95   OrderedDownloads download_items_;
     96 
     97   base::ScopedCallbackFactory<DownloadsDOMHandler> callback_factory_;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler);
    100 };
    101 
    102 #endif  // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_
    103