| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "content/public/browser/download_item.h" | 12 #include "content/public/browser/download_item.h" |
| 13 #include "content/public/browser/download_manager.h" | 13 #include "content/public/browser/download_manager.h" |
| 14 #include "content/public/browser/web_ui_message_handler.h" | 14 #include "content/public/browser/web_ui_message_handler.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 class ListValue; | 17 class ListValue; |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace content { |
| 21 class WebContents; |
| 22 } |
| 23 |
| 20 // The handler for Javascript messages related to the "downloads" view, | 24 // The handler for Javascript messages related to the "downloads" view, |
| 21 // also observes changes to the download manager. | 25 // also observes changes to the download manager. |
| 22 class DownloadsDOMHandler : public content::WebUIMessageHandler, | 26 class DownloadsDOMHandler : public content::WebUIMessageHandler, |
| 23 public content::DownloadManager::Observer, | 27 public content::DownloadManager::Observer, |
| 24 public content::DownloadItem::Observer { | 28 public content::DownloadItem::Observer { |
| 25 public: | 29 public: |
| 26 explicit DownloadsDOMHandler(content::DownloadManager* dlm); | 30 explicit DownloadsDOMHandler(content::DownloadManager* dlm); |
| 27 virtual ~DownloadsDOMHandler(); | 31 virtual ~DownloadsDOMHandler(); |
| 28 | 32 |
| 29 void Init(); | 33 void Init(); |
| 30 | 34 |
| 31 // WebUIMessageHandler implementation. | 35 // WebUIMessageHandler implementation. |
| 32 virtual void RegisterMessages() OVERRIDE; | 36 virtual void RegisterMessages() OVERRIDE; |
| 33 | 37 |
| 34 // content::DownloadItem::Observer interface | 38 // content::DownloadItem::Observer interface |
| 35 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | 39 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; |
| 36 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; | 40 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; |
| 37 | 41 |
| 38 // content::DownloadManager::Observer interface | 42 // content::DownloadManager::Observer interface |
| 39 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; | 43 virtual void OnDownloadCreated(content::DownloadManager* manager, |
| 44 content::DownloadItem* download) OVERRIDE; |
| 40 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; | 45 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; |
| 41 | 46 |
| 42 // Callback for the "onPageLoaded" message. | 47 // Callback for the "onPageLoaded" message. |
| 43 void OnPageLoaded(const base::ListValue* args); | 48 void OnPageLoaded(const base::ListValue* args); |
| 44 | 49 |
| 45 // Callback for the "getDownloads" message. | 50 // Callback for the "getDownloads" message. |
| 46 void HandleGetDownloads(const base::ListValue* args); | 51 void HandleGetDownloads(const base::ListValue* args); |
| 47 | 52 |
| 48 // Callback for the "openFile" message - opens the file in the shell. | 53 // Callback for the "openFile" message - opens the file in the shell. |
| 49 void HandleOpenFile(const base::ListValue* args); | 54 void HandleOpenFile(const base::ListValue* args); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 72 // Callback for the "cancel" message - cancels the download. | 77 // Callback for the "cancel" message - cancels the download. |
| 73 void HandleCancel(const base::ListValue* args); | 78 void HandleCancel(const base::ListValue* args); |
| 74 | 79 |
| 75 // Callback for the "clearAll" message - clears all the downloads. | 80 // Callback for the "clearAll" message - clears all the downloads. |
| 76 void HandleClearAll(const base::ListValue* args); | 81 void HandleClearAll(const base::ListValue* args); |
| 77 | 82 |
| 78 // Callback for the "openDownloadsFolder" message - opens the downloads | 83 // Callback for the "openDownloadsFolder" message - opens the downloads |
| 79 // folder. | 84 // folder. |
| 80 void HandleOpenDownloadsFolder(const base::ListValue* args); | 85 void HandleOpenDownloadsFolder(const base::ListValue* args); |
| 81 | 86 |
| 87 protected: |
| 88 // These methods are for mocking so that most of this class does not actually |
| 89 // depend on WebUI. The other methods that depend on WebUI are |
| 90 // RegisterMessages() and HandleDrag(). |
| 91 virtual content::WebContents* GetWebUIWebContents(); |
| 92 virtual void CallDownloadsList(const base::ListValue& downloads); |
| 93 virtual void CallDownloadUpdated(const base::ListValue& download); |
| 94 |
| 82 private: | 95 private: |
| 83 class OriginalDownloadManagerObserver; | 96 class OriginalDownloadManagerObserver; |
| 84 | 97 |
| 85 // Send the current list of downloads to the page. | 98 // Send the current list of downloads to the page. |
| 86 void SendCurrentDownloads(); | 99 void SendCurrentDownloads(); |
| 87 | 100 |
| 101 // Returns all the items for both DownloadManagers matching |search_text_|. |
| 102 void SearchDownloads(content::DownloadManager::DownloadVector* downloads); |
| 103 |
| 88 // Clear all download items and their observers. | 104 // Clear all download items and their observers. |
| 89 void ClearDownloadItems(); | 105 void ClearDownloadItems(); |
| 90 | 106 |
| 91 // Display a native prompt asking the user for confirmation after accepting | 107 // Display a native prompt asking the user for confirmation after accepting |
| 92 // the dangerous download specified by |dangerous|. The function returns | 108 // the dangerous download specified by |dangerous|. The function returns |
| 93 // immediately, and will invoke DangerPromptAccepted() asynchronously if the | 109 // immediately, and will invoke DangerPromptAccepted() asynchronously if the |
| 94 // user accepts the dangerous download. The native prompt will observe | 110 // user accepts the dangerous download. The native prompt will observe |
| 95 // |dangerous| until either the dialog is dismissed or |dangerous| is no | 111 // |dangerous| until either the dialog is dismissed or |dangerous| is no |
| 96 // longer an in-progress dangerous download. | 112 // longer an in-progress dangerous download. |
| 97 void ShowDangerPrompt(content::DownloadItem* dangerous); | 113 void ShowDangerPrompt(content::DownloadItem* dangerous); |
| 98 | 114 |
| 99 // Called when the user accepts a dangerous download via the | 115 // Called when the user accepts a dangerous download via the |
| 100 // DownloadDangerPrompt invoked via ShowDangerPrompt(). | 116 // DownloadDangerPrompt invoked via ShowDangerPrompt(). |
| 101 void DangerPromptAccepted(int download_id); | 117 void DangerPromptAccepted(int download_id); |
| 102 | 118 |
| 103 // Return the download that corresponds to a given id. | |
| 104 content::DownloadItem* GetDownloadById(int id); | |
| 105 | |
| 106 // Return the download that is referred to in a given value. | 119 // Return the download that is referred to in a given value. |
| 107 content::DownloadItem* GetDownloadByValue(const base::ListValue* args); | 120 content::DownloadItem* GetDownloadByValue(const base::ListValue* args); |
| 108 | 121 |
| 109 // Current search text. | 122 // Current search text. |
| 110 std::wstring search_text_; | 123 std::wstring search_text_; |
| 111 | 124 |
| 112 // Our model | 125 // Our model |
| 113 content::DownloadManager* download_manager_; | 126 content::DownloadManager* download_manager_; |
| 114 | 127 |
| 115 // If |download_manager_| belongs to an incognito profile than this | 128 // If |download_manager_| belongs to an incognito profile than this |
| 116 // is the DownloadManager for the original profile; otherwise, this is | 129 // is the DownloadManager for the original profile; otherwise, this is |
| 117 // NULL. | 130 // NULL. |
| 118 content::DownloadManager* original_profile_download_manager_; | 131 content::DownloadManager* original_profile_download_manager_; |
| 119 | 132 |
| 120 // True once the page has loaded the first time (it may load multiple times, | 133 bool update_scheduled_; |
| 121 // e.g. on reload). | |
| 122 bool initialized_; | |
| 123 | |
| 124 // The current set of visible DownloadItems for this view received from the | |
| 125 // DownloadManager. DownloadManager owns the DownloadItems. The vector is | |
| 126 // kept in order, sorted by ascending start time. | |
| 127 // Note that when a download item is removed, the entry in the vector becomes | |
| 128 // null. This should only be a transient state, as a ModelChanged() | |
| 129 // notification should follow close on the heels of such a change. | |
| 130 typedef std::vector<content::DownloadItem*> OrderedDownloads; | |
| 131 OrderedDownloads download_items_; | |
| 132 | 134 |
| 133 base::WeakPtrFactory<DownloadsDOMHandler> weak_ptr_factory_; | 135 base::WeakPtrFactory<DownloadsDOMHandler> weak_ptr_factory_; |
| 134 | 136 |
| 135 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); | 137 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); |
| 136 }; | 138 }; |
| 137 | 139 |
| 138 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ | 140 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ |
| OLD | NEW |