| 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 <set> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 12 #include "content/public/browser/download_item.h" | 13 #include "content/public/browser/download_item.h" |
| 13 #include "content/public/browser/download_manager.h" | 14 #include "content/public/browser/download_manager.h" |
| 14 #include "content/public/browser/web_ui_message_handler.h" | 15 #include "content/public/browser/web_ui_message_handler.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 class ListValue; | 18 class ListValue; |
| 18 } | 19 } |
| 19 | 20 |
| 21 namespace content { |
| 22 class WebContents; |
| 23 } |
| 24 |
| 20 // The handler for Javascript messages related to the "downloads" view, | 25 // The handler for Javascript messages related to the "downloads" view, |
| 21 // also observes changes to the download manager. | 26 // also observes changes to the download manager. |
| 22 class DownloadsDOMHandler : public content::WebUIMessageHandler, | 27 class DownloadsDOMHandler : public content::WebUIMessageHandler, |
| 23 public content::DownloadManager::Observer, | 28 public content::DownloadManager::Observer, |
| 24 public content::DownloadItem::Observer { | 29 public content::DownloadItem::Observer { |
| 25 public: | 30 public: |
| 26 explicit DownloadsDOMHandler(content::DownloadManager* dlm); | 31 explicit DownloadsDOMHandler(content::DownloadManager* dlm); |
| 27 virtual ~DownloadsDOMHandler(); | 32 virtual ~DownloadsDOMHandler(); |
| 28 | 33 |
| 29 void Init(); | 34 void Init(); |
| 30 | 35 |
| 31 // WebUIMessageHandler implementation. | 36 // WebUIMessageHandler implementation. |
| 32 virtual void RegisterMessages() OVERRIDE; | 37 virtual void RegisterMessages() OVERRIDE; |
| 33 | 38 |
| 34 // content::DownloadItem::Observer interface | 39 // content::DownloadItem::Observer interface |
| 35 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | 40 virtual void OnDownloadUpdated( |
| 36 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; | 41 content::DownloadItem* download_item) OVERRIDE; |
| 42 virtual void OnDownloadDestroyed( |
| 43 content::DownloadItem* download_item) OVERRIDE; |
| 37 | 44 |
| 38 // content::DownloadManager::Observer interface | 45 // content::DownloadManager::Observer interface |
| 39 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; | 46 virtual void OnDownloadCreated(content::DownloadManager* manager, |
| 47 content::DownloadItem* download_item) OVERRIDE; |
| 40 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; | 48 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; |
| 41 | 49 |
| 42 // Callback for the "onPageLoaded" message. | 50 // Callback for the "onPageLoaded" message. |
| 43 void OnPageLoaded(const base::ListValue* args); | 51 void OnPageLoaded(const base::ListValue* args); |
| 44 | 52 |
| 45 // Callback for the "getDownloads" message. | 53 // Callback for the "getDownloads" message. |
| 46 void HandleGetDownloads(const base::ListValue* args); | 54 void HandleGetDownloads(const base::ListValue* args); |
| 47 | 55 |
| 48 // Callback for the "openFile" message - opens the file in the shell. | 56 // Callback for the "openFile" message - opens the file in the shell. |
| 49 void HandleOpenFile(const base::ListValue* args); | 57 void HandleOpenFile(const base::ListValue* args); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 72 // Callback for the "cancel" message - cancels the download. | 80 // Callback for the "cancel" message - cancels the download. |
| 73 void HandleCancel(const base::ListValue* args); | 81 void HandleCancel(const base::ListValue* args); |
| 74 | 82 |
| 75 // Callback for the "clearAll" message - clears all the downloads. | 83 // Callback for the "clearAll" message - clears all the downloads. |
| 76 void HandleClearAll(const base::ListValue* args); | 84 void HandleClearAll(const base::ListValue* args); |
| 77 | 85 |
| 78 // Callback for the "openDownloadsFolder" message - opens the downloads | 86 // Callback for the "openDownloadsFolder" message - opens the downloads |
| 79 // folder. | 87 // folder. |
| 80 void HandleOpenDownloadsFolder(const base::ListValue* args); | 88 void HandleOpenDownloadsFolder(const base::ListValue* args); |
| 81 | 89 |
| 90 protected: |
| 91 // These methods are for mocking so that most of this class does not actually |
| 92 // depend on WebUI. The other methods that depend on WebUI are |
| 93 // RegisterMessages() and HandleDrag(). |
| 94 virtual content::WebContents* GetWebUIWebContents(); |
| 95 virtual void CallDownloadsList(const base::ListValue& downloads); |
| 96 virtual void CallDownloadUpdated(const base::ListValue& download); |
| 97 |
| 82 private: | 98 private: |
| 83 class OriginalDownloadManagerObserver; | 99 // Shorthand for |observing_items_|, which tracks all items that this is |
| 100 // observing so that RemoveObserver will be called for all of them. |
| 101 typedef std::set<content::DownloadItem*> DownloadSet; |
| 84 | 102 |
| 85 // Send the current list of downloads to the page. | 103 // Schedules a call to SendCurrentDownloads() in the next message loop |
| 104 // iteration. |
| 105 void ScheduleSendCurrentDownloads(); |
| 106 |
| 107 // Sends the current list of downloads to the page. |
| 86 void SendCurrentDownloads(); | 108 void SendCurrentDownloads(); |
| 87 | 109 |
| 88 // Clear all download items and their observers. | 110 // Fills |downloads| with all the items for both DownloadManagers matching |
| 111 // |search_text_|. |
| 112 void SearchDownloads(content::DownloadManager::DownloadVector* downloads); |
| 113 |
| 114 // Clears all download items and their observers. |
| 89 void ClearDownloadItems(); | 115 void ClearDownloadItems(); |
| 90 | 116 |
| 91 // Display a native prompt asking the user for confirmation after accepting | 117 // Displays a native prompt asking the user for confirmation after accepting |
| 92 // the dangerous download specified by |dangerous|. The function returns | 118 // the dangerous download specified by |dangerous|. The function returns |
| 93 // immediately, and will invoke DangerPromptAccepted() asynchronously if the | 119 // immediately, and will invoke DangerPromptAccepted() asynchronously if the |
| 94 // user accepts the dangerous download. The native prompt will observe | 120 // user accepts the dangerous download. The native prompt will observe |
| 95 // |dangerous| until either the dialog is dismissed or |dangerous| is no | 121 // |dangerous| until either the dialog is dismissed or |dangerous| is no |
| 96 // longer an in-progress dangerous download. | 122 // longer an in-progress dangerous download. |
| 97 void ShowDangerPrompt(content::DownloadItem* dangerous); | 123 void ShowDangerPrompt(content::DownloadItem* dangerous); |
| 98 | 124 |
| 99 // Called when the user accepts a dangerous download via the | 125 // Conveys danger acceptance from the DownloadDangerPrompt to the |
| 100 // DownloadDangerPrompt invoked via ShowDangerPrompt(). | 126 // DownloadItem. |
| 101 void DangerPromptAccepted(int download_id); | 127 void DangerPromptAccepted(int download_id); |
| 102 | 128 |
| 103 // Return the download that corresponds to a given id. | 129 // Returns the download that is referred to in a given value. |
| 104 content::DownloadItem* GetDownloadById(int id); | |
| 105 | |
| 106 // Return the download that is referred to in a given value. | |
| 107 content::DownloadItem* GetDownloadByValue(const base::ListValue* args); | 130 content::DownloadItem* GetDownloadByValue(const base::ListValue* args); |
| 108 | 131 |
| 109 // Current search text. | 132 // Current search text. |
| 110 std::wstring search_text_; | 133 string16 search_text_; |
| 134 |
| 135 // Keeps track of all items that this is observing so that RemoveObserver will |
| 136 // be called for all of them. |
| 137 DownloadSet observing_items_; |
| 111 | 138 |
| 112 // Our model | 139 // Our model |
| 113 content::DownloadManager* download_manager_; | 140 content::DownloadManager* download_manager_; |
| 114 | 141 |
| 115 // If |download_manager_| belongs to an incognito profile than this | 142 // If |download_manager_| belongs to an incognito profile then this |
| 116 // is the DownloadManager for the original profile; otherwise, this is | 143 // is the DownloadManager for the original profile; otherwise, this is |
| 117 // NULL. | 144 // NULL. |
| 118 content::DownloadManager* original_profile_download_manager_; | 145 content::DownloadManager* original_profile_download_manager_; |
| 119 | 146 |
| 120 // True once the page has loaded the first time (it may load multiple times, | 147 // Whether a call to SendCurrentDownloads() is currently scheduled. |
| 121 // e.g. on reload). | 148 bool update_scheduled_; |
| 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 | 149 |
| 133 base::WeakPtrFactory<DownloadsDOMHandler> weak_ptr_factory_; | 150 base::WeakPtrFactory<DownloadsDOMHandler> weak_ptr_factory_; |
| 134 | 151 |
| 135 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); | 152 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandler); |
| 136 }; | 153 }; |
| 137 | 154 |
| 138 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ | 155 #endif // CHROME_BROWSER_UI_WEBUI_DOWNLOADS_DOM_HANDLER_H_ |
| OLD | NEW |